testing_api_cmd_truth_solve.c (9761B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2020, 2022 Anastasis SARL 4 5 Anastasis is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file testing/testing_api_cmd_truth_solve.c 18 * @brief Testing of Implementation of the /truth GET 19 * @author Christian Grothoff 20 * @author Dennis Neufeld 21 * @author Dominik Meister 22 */ 23 24 #include "platform.h" 25 #include "anastasis_testing_lib.h" 26 #include <taler/taler_util.h> 27 #include <taler/taler_testing_lib.h> 28 #include <taler/taler_merchant_service.h> 29 30 31 /** 32 * State for a "keyshare lookup" CMD. 33 */ 34 struct TruthSolveState 35 { 36 /** 37 * The interpreter state. 38 */ 39 struct TALER_TESTING_Interpreter *is; 40 41 /** 42 * URL of the anastasis backend. 43 */ 44 const char *anastasis_url; 45 46 /** 47 * Expected status code. 48 */ 49 unsigned int expected_http_status; 50 51 /** 52 * Resulting encrypted key share. 53 * Note: currently not used. 54 */ 55 struct ANASTASIS_CRYPTO_EncryptedKeyShareP eks; 56 57 /** 58 * The /truth GET operation handle. 59 */ 60 struct ANASTASIS_TruthSolveOperation *tso; 61 62 /** 63 * answer to a challenge 64 */ 65 const char *answer; 66 67 /** 68 * Reference to upload command we expect to lookup. 69 */ 70 const char *upload_reference; 71 72 /** 73 * Reference to upload command we expect to lookup. 74 */ 75 const char *payment_reference; 76 77 /** 78 * Payment secret requested by the service, if any. 79 */ 80 struct ANASTASIS_PaymentSecretP payment_secret_response; 81 82 /** 83 * Taler-URI with payment request, if any. 84 */ 85 char *pay_uri; 86 87 /** 88 * Order ID for payment request, if any. 89 */ 90 char *order_id; 91 92 /** 93 * Redirect-URI for challenge, if any. 94 */ 95 char *redirect_uri; 96 97 /** 98 * "code" returned by service, if any. 99 */ 100 char *code; 101 102 /** 103 * "instructions" for how to solve the challenge as returned by service, if any. 104 */ 105 char *instructions; 106 107 /** 108 * Name of the file where the service will write the challenge, if method is "file". 109 * Otherwise NULL. 110 */ 111 char *filename; 112 113 /** 114 * Mode for the lookup(0 = question, 1 = code based) 115 */ 116 int lookup_mode; 117 118 }; 119 120 121 static void 122 truth_solve_cb (void *cls, 123 const struct ANASTASIS_TruthSolveReply *tsr) 124 { 125 struct TruthSolveState *ksls = cls; 126 127 ksls->tso = NULL; 128 if (tsr->http_status != ksls->expected_http_status) 129 { 130 TALER_TESTING_unexpected_status (ksls->is, 131 tsr->http_status, 132 ksls->expected_http_status); 133 return; 134 } 135 switch (tsr->http_status) 136 { 137 case MHD_HTTP_OK: 138 ksls->eks = tsr->details.success.eks; 139 break; 140 case MHD_HTTP_PAYMENT_REQUIRED: 141 ksls->pay_uri = GNUNET_strdup ( 142 tsr->details.payment_required.payment_request); 143 ksls->payment_secret_response = tsr->details.payment_required.ps; 144 ksls->order_id = GNUNET_strdup (tsr->details.payment_required.pd->order_id); 145 break; 146 default: 147 break; 148 } 149 TALER_TESTING_interpreter_next (ksls->is); 150 } 151 152 153 static void 154 truth_solve_run (void *cls, 155 const struct TALER_TESTING_Command *cmd, 156 struct TALER_TESTING_Interpreter *is) 157 { 158 struct TruthSolveState *ksls = cls; 159 const struct ANASTASIS_CRYPTO_TruthKeyP *truth_key; 160 const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid; 161 const struct ANASTASIS_PaymentSecretP *payment_secret; 162 const char *answer; 163 164 ksls->is = is; 165 if (NULL == ksls->upload_reference) 166 { 167 GNUNET_break (0); 168 TALER_TESTING_interpreter_fail (ksls->is); 169 return; 170 } 171 { 172 const struct TALER_TESTING_Command *upload_cmd; 173 174 upload_cmd = TALER_TESTING_interpreter_lookup_command ( 175 is, 176 ksls->upload_reference); 177 if (NULL == upload_cmd) 178 { 179 GNUNET_break (0); 180 TALER_TESTING_interpreter_fail (ksls->is); 181 return; 182 } 183 { 184 const char *fn; 185 186 if (GNUNET_OK != 187 ANASTASIS_TESTING_get_trait_filename (upload_cmd, 188 &fn)) 189 { 190 GNUNET_break (0); 191 TALER_TESTING_interpreter_fail (ksls->is); 192 return; 193 } 194 if (NULL != fn) 195 ksls->filename = GNUNET_strdup (fn); 196 } 197 if (GNUNET_OK != 198 ANASTASIS_TESTING_get_trait_truth_uuid (upload_cmd, 199 &truth_uuid)) 200 { 201 GNUNET_break (0); 202 TALER_TESTING_interpreter_fail (ksls->is); 203 return; 204 } 205 if (NULL == truth_uuid) 206 { 207 GNUNET_break (0); 208 TALER_TESTING_interpreter_fail (ksls->is); 209 return; 210 } 211 if (GNUNET_OK != 212 ANASTASIS_TESTING_get_trait_truth_key (upload_cmd, 213 &truth_key)) 214 { 215 GNUNET_break (0); 216 TALER_TESTING_interpreter_fail (ksls->is); 217 return; 218 } 219 if (NULL == truth_key) 220 { 221 GNUNET_break (0); 222 TALER_TESTING_interpreter_fail (ksls->is); 223 return; 224 } 225 } 226 227 if (ksls->lookup_mode == 1) 228 { 229 const struct TALER_TESTING_Command *download_cmd; 230 231 download_cmd = TALER_TESTING_interpreter_lookup_command (is, 232 ksls->answer); 233 if (NULL == download_cmd) 234 { 235 GNUNET_break (0); 236 TALER_TESTING_interpreter_fail (ksls->is); 237 return; 238 } 239 if (GNUNET_OK != 240 ANASTASIS_TESTING_get_trait_code (download_cmd, 241 &answer)) 242 { 243 GNUNET_break (0); 244 TALER_TESTING_interpreter_fail (ksls->is); 245 return; 246 } 247 if (NULL == answer) 248 { 249 GNUNET_break (0); 250 TALER_TESTING_interpreter_fail (ksls->is); 251 return; 252 } 253 } 254 else 255 { 256 /* answer is the answer */ 257 answer = ksls->answer; 258 } 259 if (NULL == answer) 260 { 261 GNUNET_break (0); 262 TALER_TESTING_interpreter_fail (ksls->is); 263 return; 264 } 265 266 if (NULL != ksls->payment_reference) 267 { 268 const struct TALER_TESTING_Command *payment_cmd; 269 270 payment_cmd = TALER_TESTING_interpreter_lookup_command ( 271 is, 272 ksls->payment_reference); 273 if (GNUNET_OK != 274 ANASTASIS_TESTING_get_trait_payment_secret (payment_cmd, 275 &payment_secret)) 276 { 277 GNUNET_break (0); 278 TALER_TESTING_interpreter_fail (ksls->is); 279 return; 280 } 281 } 282 else 283 { 284 payment_secret = NULL; 285 } 286 287 { 288 struct GNUNET_HashCode h_answer; 289 290 GNUNET_CRYPTO_hash (answer, 291 strlen (answer), 292 &h_answer); 293 ksls->tso = ANASTASIS_truth_solve ( 294 TALER_TESTING_interpreter_get_context (is), 295 ksls->anastasis_url, 296 truth_uuid, 297 truth_key, 298 payment_secret, 299 GNUNET_TIME_UNIT_ZERO, 300 &h_answer, 301 &truth_solve_cb, 302 ksls); 303 } 304 if (NULL == ksls->tso) 305 { 306 GNUNET_break (0); 307 TALER_TESTING_interpreter_fail (ksls->is); 308 return; 309 } 310 } 311 312 313 static void 314 truth_solve_cleanup (void *cls, 315 const struct TALER_TESTING_Command *cmd) 316 { 317 struct TruthSolveState *ksls = cls; 318 319 if (NULL != ksls->tso) 320 { 321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 322 "Command '%s' did not complete (keyshare lookup)\n", 323 cmd->label); 324 ANASTASIS_truth_solve_cancel (ksls->tso); 325 ksls->tso = NULL; 326 } 327 GNUNET_free (ksls->pay_uri); 328 GNUNET_free (ksls->order_id); 329 GNUNET_free (ksls->code); 330 GNUNET_free (ksls->filename); 331 GNUNET_free (ksls->instructions); 332 GNUNET_free (ksls->redirect_uri); 333 GNUNET_free (ksls); 334 } 335 336 337 /** 338 * Offer internal data to other commands. 339 * 340 * @param cls closure 341 * @param[out] ret result (could be anything) 342 * @param[out] trait name of the trait 343 * @param index index number of the object to extract. 344 * @return #GNUNET_OK on success 345 */ 346 static enum GNUNET_GenericReturnValue 347 truth_solve_traits (void *cls, 348 const void **ret, 349 const char *trait, 350 unsigned int index) 351 { 352 struct TruthSolveState *ksls = cls; 353 struct TALER_TESTING_Trait traits[] = { 354 ANASTASIS_TESTING_make_trait_payment_secret ( 355 &ksls->payment_secret_response), 356 TALER_TESTING_make_trait_taler_uri (ksls->pay_uri), 357 TALER_TESTING_make_trait_order_id (ksls->order_id), 358 ANASTASIS_TESTING_make_trait_code (ksls->code), 359 TALER_TESTING_trait_end () 360 }; 361 362 return TALER_TESTING_get_trait (traits, 363 ret, 364 trait, 365 index); 366 } 367 368 369 struct TALER_TESTING_Command 370 ANASTASIS_TESTING_cmd_truth_solve ( 371 const char *label, 372 const char *anastasis_url, 373 const char *answer, 374 const char *payment_ref, 375 const char *upload_ref, 376 int lookup_mode, 377 unsigned int http_status) 378 { 379 struct TruthSolveState *ksls; 380 381 GNUNET_assert (NULL != upload_ref); 382 ksls = GNUNET_new (struct TruthSolveState); 383 ksls->expected_http_status = http_status; 384 ksls->anastasis_url = anastasis_url; 385 ksls->upload_reference = upload_ref; 386 ksls->payment_reference = payment_ref; 387 ksls->answer = answer; 388 ksls->lookup_mode = lookup_mode; 389 { 390 struct TALER_TESTING_Command cmd = { 391 .cls = ksls, 392 .label = label, 393 .run = &truth_solve_run, 394 .cleanup = &truth_solve_cleanup, 395 .traits = &truth_solve_traits 396 }; 397 398 return cmd; 399 } 400 } 401 402 403 /* end of testing_api_cmd_truth_solve.c */