testing_api_cmd_coin_history.c (20809B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as 7 published by the Free Software Foundation; either version 3, or 8 (at your option) any later version. 9 10 TALER is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public 16 License along with TALER; see the file COPYING. If not, see 17 <http://www.gnu.org/licenses/> 18 */ 19 /** 20 * @file testing/testing_api_cmd_coin_history.c 21 * @brief Implement the /coins/$COIN_PUB/history test command. 22 * @author Christian Grothoff 23 */ 24 #include "taler/taler_json_lib.h" 25 #include <gnunet/gnunet_curl_lib.h> 26 struct HistoryState; 27 #define TALER_EXCHANGE_GET_COINS_HISTORY_RESULT_CLOSURE \ 28 struct HistoryState 29 #include "taler/exchange/get-coins-COIN_PUB-history.h" 30 #include "taler/taler_testing_lib.h" 31 32 33 /** 34 * State for a "history" CMD. 35 */ 36 struct HistoryState 37 { 38 39 /** 40 * Public key of the coin being analyzed. 41 */ 42 struct TALER_CoinSpendPublicKeyP coin_pub; 43 44 /** 45 * Label to the command which created the coin to check, 46 * needed to resort the coin key. 47 */ 48 const char *coin_reference; 49 50 /** 51 * Handle to the "coin history" operation. 52 */ 53 struct TALER_EXCHANGE_GetCoinsHistoryHandle *rsh; 54 55 /** 56 * Expected coin balance. 57 */ 58 const char *expected_balance; 59 60 /** 61 * Private key of the coin being analyzed. 62 */ 63 const struct TALER_CoinSpendPrivateKeyP *coin_priv; 64 65 /** 66 * Interpreter state. 67 */ 68 struct TALER_TESTING_Interpreter *is; 69 70 /** 71 * Expected HTTP response code. 72 */ 73 unsigned int expected_response_code; 74 75 }; 76 77 78 /** 79 * Closure for analysis_cb(). 80 */ 81 struct AnalysisContext 82 { 83 /** 84 * Coin public key we are looking at. 85 */ 86 const struct TALER_CoinSpendPublicKeyP *coin_pub; 87 88 /** 89 * Length of the @e history array. 90 */ 91 unsigned int history_length; 92 93 /** 94 * Array of history items to match. 95 */ 96 const struct TALER_EXCHANGE_CoinHistoryEntry *history; 97 98 /** 99 * Array of @e history_length of matched entries. 100 */ 101 bool *found; 102 103 /** 104 * Set to true if an entry could not be found. 105 */ 106 bool failure; 107 }; 108 109 110 /** 111 * Compare @a h1 and @a h2. 112 * 113 * @param h1 a history entry 114 * @param h2 a history entry 115 * @return 0 if @a h1 and @a h2 are equal 116 */ 117 static int 118 history_entry_cmp ( 119 const struct TALER_EXCHANGE_CoinHistoryEntry *h1, 120 const struct TALER_EXCHANGE_CoinHistoryEntry *h2) 121 { 122 if (h1->type != h2->type) 123 return 1; 124 if (0 != TALER_amount_cmp (&h1->amount, 125 &h2->amount)) 126 { 127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 128 "Amount mismatch (%s)\n", 129 TALER_amount2s (&h1->amount)); 130 return 1; 131 } 132 switch (h1->type) 133 { 134 case TALER_EXCHANGE_CTT_NONE: 135 GNUNET_break (0); 136 break; 137 case TALER_EXCHANGE_CTT_DEPOSIT: 138 if (0 != GNUNET_memcmp (&h1->details.deposit.h_contract_terms, 139 &h2->details.deposit.h_contract_terms)) 140 return 1; 141 if (0 != GNUNET_memcmp (&h1->details.deposit.merchant_pub, 142 &h2->details.deposit.merchant_pub)) 143 return 1; 144 if (0 != GNUNET_memcmp (&h1->details.deposit.h_wire, 145 &h2->details.deposit.h_wire)) 146 return 1; 147 if (0 != GNUNET_memcmp (&h1->details.deposit.sig, 148 &h2->details.deposit.sig)) 149 return 1; 150 return 0; 151 case TALER_EXCHANGE_CTT_MELT: 152 if (0 != GNUNET_memcmp (&h1->details.melt.h_age_commitment, 153 &h2->details.melt.h_age_commitment)) 154 return 1; 155 if (0 != GNUNET_memcmp (&h1->details.melt.rc, 156 &h2->details.melt.rc)) 157 return 1; 158 /* presence of noreveal_index and transfer_pubs is checked, 159 and transfer_pubs are verified, in check_melt_entries() */ 160 if ( (! h2->details.melt.no_noreveal_index) && 161 (h1->details.melt.noreveal_index != h2->details.melt.noreveal_index) ) 162 return 1; 163 /* Note: most other fields are not initialized 164 in the trait as they are hard to extract from 165 the API */ 166 return 0; 167 case TALER_EXCHANGE_CTT_REFUND: 168 if (0 != GNUNET_memcmp (&h1->details.refund.sig, 169 &h2->details.refund.sig)) 170 return 1; 171 return 0; 172 case TALER_EXCHANGE_CTT_RECOUP: 173 if (0 != GNUNET_memcmp (&h1->details.recoup.coin_sig, 174 &h2->details.recoup.coin_sig)) 175 return 1; 176 /* Note: exchange_sig, exchange_pub and timestamp are 177 fundamentally not available in the initiating command */ 178 return 0; 179 case TALER_EXCHANGE_CTT_RECOUP_REFRESH: 180 if (0 != GNUNET_memcmp (&h1->details.recoup_refresh.coin_sig, 181 &h2->details.recoup_refresh.coin_sig)) 182 return 1; 183 /* Note: exchange_sig, exchange_pub and timestamp are 184 fundamentally not available in the initiating command */ 185 return 0; 186 case TALER_EXCHANGE_CTT_OLD_COIN_RECOUP: 187 if (0 != GNUNET_memcmp (&h1->details.old_coin_recoup.new_coin_pub, 188 &h2->details.old_coin_recoup.new_coin_pub)) 189 return 1; 190 /* Note: exchange_sig, exchange_pub and timestamp are 191 fundamentally not available in the initiating command */ 192 return 0; 193 case TALER_EXCHANGE_CTT_PURSE_DEPOSIT: 194 /* coin_sig is not initialized */ 195 if (0 != GNUNET_memcmp (&h1->details.purse_deposit.purse_pub, 196 &h2->details.purse_deposit.purse_pub)) 197 { 198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 199 "Purse public key mismatch\n"); 200 return 1; 201 } 202 if (0 != strcmp (h1->details.purse_deposit.exchange_base_url, 203 h2->details.purse_deposit.exchange_base_url)) 204 { 205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 206 "Exchange base URL mismatch (%s/%s)\n", 207 h1->details.purse_deposit.exchange_base_url, 208 h2->details.purse_deposit.exchange_base_url); 209 GNUNET_break (0); 210 return 1; 211 } 212 return 0; 213 case TALER_EXCHANGE_CTT_PURSE_REFUND: 214 /* NOTE: not supported yet (trait not returned) */ 215 return 0; 216 case TALER_EXCHANGE_CTT_RESERVE_OPEN_DEPOSIT: 217 /* NOTE: not supported yet (trait not returned) */ 218 if (0 != GNUNET_memcmp (&h1->details.reserve_open_deposit.coin_sig, 219 &h2->details.reserve_open_deposit.coin_sig)) 220 return 1; 221 return 0; 222 } 223 GNUNET_assert (0); 224 return -1; 225 } 226 227 228 /** 229 * Check if @a cmd changed the coin, if so, find the 230 * entry in our history and set the respective index in found 231 * to true. If the entry is not found, set failure. 232 * 233 * @param cls our `struct AnalysisContext *` 234 * @param cmd command to analyze for impact on history 235 */ 236 static void 237 analyze_command (void *cls, 238 const struct TALER_TESTING_Command *cmd) 239 { 240 struct AnalysisContext *ac = cls; 241 const struct TALER_CoinSpendPublicKeyP *coin_pub = ac->coin_pub; 242 const struct TALER_EXCHANGE_CoinHistoryEntry *history = ac->history; 243 unsigned int history_length = ac->history_length; 244 bool *found = ac->found; 245 246 if (TALER_TESTING_cmd_is_batch (cmd)) 247 { 248 struct TALER_TESTING_Command *cur; 249 struct TALER_TESTING_Command *bcmd; 250 251 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 252 "Checking `%s' for history of coin `%s'\n", 253 cmd->label, 254 TALER_B2S (coin_pub)); 255 cur = TALER_TESTING_cmd_batch_get_current (cmd); 256 if (GNUNET_OK != 257 TALER_TESTING_get_trait_batch_cmds (cmd, 258 &bcmd)) 259 { 260 GNUNET_break (0); 261 ac->failure = true; 262 return; 263 } 264 for (unsigned int i = 0; NULL != bcmd[i].label; i++) 265 { 266 struct TALER_TESTING_Command *step = &bcmd[i]; 267 268 analyze_command (ac, 269 step); 270 if (ac->failure) 271 { 272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 273 "Entry for batch step `%s' missing in coin history\n", 274 step->label); 275 return; 276 } 277 if (step == cur) 278 break; /* if *we* are in a batch, make sure not to analyze commands past 'now' */ 279 } 280 return; 281 } 282 283 for (unsigned int j = 0; true; j++) 284 { 285 const struct TALER_CoinSpendPublicKeyP *rp; 286 const struct TALER_EXCHANGE_CoinHistoryEntry *he; 287 bool matched = false; 288 289 if (GNUNET_OK != 290 TALER_TESTING_get_trait_coin_pub (cmd, 291 j, 292 &rp)) 293 { 294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 295 "Command `%s#%u' has no public key for a coin\n", 296 cmd->label, 297 j); 298 break; /* command does nothing for coins */ 299 } 300 if (0 != 301 GNUNET_memcmp (rp, 302 coin_pub)) 303 { 304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 305 "Command `%s#%u' is about another coin %s\n", 306 cmd->label, 307 j, 308 TALER_B2S (rp)); 309 continue; /* command affects some _other_ coin */ 310 } 311 if (GNUNET_OK != 312 TALER_TESTING_get_trait_coin_history (cmd, 313 j, 314 &he)) 315 { 316 /* NOTE: only for debugging... */ 317 if (0 == j) 318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 319 "Command `%s' has the coin_pub, but lacks coin history trait\n", 320 cmd->label); 321 return; /* command does nothing for coins */ 322 } 323 if (TALER_EXCHANGE_CTT_NONE == he->type) 324 continue; /* operation was rejected, coin unaffected */ 325 for (unsigned int i = 0; i<history_length; i++) 326 { 327 if (found[i]) 328 continue; /* already found, skip */ 329 if (0 == 330 history_entry_cmp (he, 331 &history[i])) 332 { 333 found[i] = true; 334 matched = true; 335 break; 336 } 337 } 338 if (! matched) 339 { 340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 341 "Command `%s' coin history entry #%u not found\n", 342 cmd->label, 343 j); 344 ac->failure = true; 345 return; 346 } 347 } 348 } 349 350 351 /** 352 * Check the details of the melt entries of a coin history: they must 353 * have a noreveal index and transfer public keys, and the latter must be 354 * those the client derived for the batch at the noreveal index. 355 * 356 * @param ss our state, with the coin's private key 357 * @param hlen length of @a rhist 358 * @param rhist the parsed coin history 359 * @return true if all melt entries are consistent 360 */ 361 static bool 362 check_melt_entries ( 363 const struct HistoryState *ss, 364 unsigned int hlen, 365 const struct TALER_EXCHANGE_CoinHistoryEntry rhist[static hlen]) 366 { 367 const struct TALER_EXCHANGE_Keys *keys = TALER_TESTING_get_keys (ss->is); 368 unsigned int protocol; 369 370 GNUNET_assert (1 == sscanf (keys->version, "%u", &protocol)); 371 for (unsigned int i = 0; i < hlen; i++) 372 { 373 const struct TALER_EXCHANGE_CoinHistoryEntry *rh = &rhist[i]; 374 size_t n = rh->details.melt.num_fresh_coins; 375 376 if (TALER_EXCHANGE_CTT_MELT != rh->type) 377 continue; 378 if ( (protocol < 46) && 379 rh->details.melt.no_noreveal_index && 380 (NULL == rh->details.melt.transfer_pubs) ) 381 continue; 382 if ( (rh->details.melt.no_noreveal_index) || 383 (NULL == rh->details.melt.transfer_pubs) || 384 (0 == n) ) 385 { 386 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 387 "Melt history entry lacks noreveal_index or transfer_pubs\n"); 388 return false; 389 } 390 { 391 struct TALER_KappaPrivateRefreshBatchSeedsP kappa_seeds; 392 struct TALER_PlanchetMasterSecretP ps[n]; 393 struct TALER_TransferPublicKeyP tp[n]; 394 395 TALER_refresh_expand_seed_to_kappa_batch_seeds ( 396 &rh->details.melt.refresh_seed, 397 ss->coin_priv, 398 &kappa_seeds); 399 TALER_refresh_expand_batch_seed_to_transfer_data ( 400 &kappa_seeds.tuple[rh->details.melt.noreveal_index], 401 &ss->coin_pub, 402 n, 403 ps, 404 tp); 405 if (0 != memcmp (tp, 406 rh->details.melt.transfer_pubs, 407 sizeof (tp))) 408 { 409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 410 "Melt history has wrong transfer_pubs\n"); 411 return false; 412 } 413 } 414 } 415 return true; 416 } 417 418 419 /** 420 * Check that the coin balance and HTTP response code are 421 * both acceptable. 422 * 423 * @param ss closure. 424 * @param rs HTTP response details 425 */ 426 static void 427 coin_history_cb (struct HistoryState *ss, 428 const struct TALER_EXCHANGE_GetCoinsHistoryResponse *rs) 429 { 430 struct TALER_TESTING_Interpreter *is = ss->is; 431 struct TALER_Amount eb; 432 unsigned int hlen; 433 434 ss->rsh = NULL; 435 if (ss->expected_response_code != rs->hr.http_status) 436 { 437 TALER_TESTING_unexpected_status (ss->is, 438 rs->hr.http_status, 439 ss->expected_response_code); 440 return; 441 } 442 if (MHD_HTTP_OK != rs->hr.http_status) 443 { 444 TALER_TESTING_interpreter_next (is); 445 return; 446 } 447 GNUNET_assert (GNUNET_OK == 448 TALER_string_to_amount (ss->expected_balance, 449 &eb)); 450 451 if (0 != TALER_amount_cmp (&eb, 452 &rs->details.ok.balance)) 453 { 454 GNUNET_break (0); 455 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 456 "Unexpected balance for coin: %s\n", 457 TALER_amount_to_string (&rs->details.ok.balance)); 458 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 459 "Expected balance of: %s\n", 460 TALER_amount_to_string (&eb)); 461 TALER_TESTING_interpreter_fail (ss->is); 462 return; 463 } 464 hlen = json_array_size (rs->details.ok.history); 465 { 466 bool found[GNUNET_NZL (hlen)]; 467 struct TALER_EXCHANGE_CoinHistoryEntry rhist[hlen + 1]; 468 struct AnalysisContext ac = { 469 .coin_pub = &ss->coin_pub, 470 .history = rhist, 471 .history_length = hlen, 472 .found = found 473 }; 474 const struct TALER_EXCHANGE_DenomPublicKey *dk; 475 struct TALER_Amount total_in; 476 struct TALER_Amount total_out; 477 struct TALER_Amount hbal; 478 bool failed = false; 479 480 dk = TALER_EXCHANGE_get_denomination_key_by_hash ( 481 TALER_TESTING_get_keys (is), 482 &rs->details.ok.h_denom_pub); 483 GNUNET_assert (NULL != dk); 484 memset (found, 485 0, 486 sizeof (found)); 487 memset (rhist, 488 0, 489 sizeof (rhist)); 490 /* Exercise the public parse/free contract with spare capacity. */ 491 rhist[hlen].type = TALER_EXCHANGE_CTT_MELT; 492 if (GNUNET_OK != 493 TALER_EXCHANGE_parse_coin_history ( 494 TALER_TESTING_get_keys (is), 495 dk, 496 rs->details.ok.history, 497 &ss->coin_pub, 498 &total_in, 499 &total_out, 500 hlen + 1, 501 rhist)) 502 { 503 GNUNET_break (0); 504 json_dumpf (rs->hr.reply, 505 stderr, 506 JSON_INDENT (2)); 507 TALER_TESTING_interpreter_fail (ss->is); 508 return; 509 } 510 GNUNET_assert (TALER_EXCHANGE_CTT_NONE == rhist[hlen].type); 511 if (! check_melt_entries (ss, 512 hlen, 513 rhist)) 514 { 515 failed = true; 516 goto cleanup; 517 } 518 if (0 > 519 TALER_amount_subtract (&hbal, 520 &total_in, 521 &total_out)) 522 { 523 GNUNET_break (0); 524 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 525 "Coin credits: %s\n", 526 TALER_amount2s (&total_in)); 527 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 528 "Coin debits: %s\n", 529 TALER_amount2s (&total_out)); 530 failed = true; 531 goto cleanup; 532 } 533 if (0 != TALER_amount_cmp (&hbal, 534 &rs->details.ok.balance)) 535 { 536 GNUNET_break (0); 537 failed = true; 538 goto cleanup; 539 } 540 (void) ac; 541 TALER_TESTING_iterate (is, 542 true, 543 &analyze_command, 544 &ac); 545 if (ac.failure) 546 { 547 json_dumpf (rs->hr.reply, 548 stderr, 549 JSON_INDENT (2)); 550 failed = true; 551 goto cleanup; 552 } 553 #if 1 554 for (unsigned int i = 0; i<hlen; i++) 555 { 556 if (found[i]) 557 continue; 558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 559 "History entry at index %u of type %d not justified by command history\n", 560 i, 561 rhist[i].type); 562 json_dumpf (rs->hr.reply, 563 stderr, 564 JSON_INDENT (2)); 565 failed = true; 566 goto cleanup; 567 } 568 #endif 569 cleanup: 570 TALER_EXCHANGE_free_coin_history (hlen + 1, 571 rhist); 572 if (failed) 573 { 574 TALER_TESTING_interpreter_fail (ss->is); 575 return; 576 } 577 } 578 TALER_TESTING_interpreter_next (is); 579 } 580 581 582 /** 583 * Run the command. 584 * 585 * @param cls closure. 586 * @param cmd the command being executed. 587 * @param is the interpreter state. 588 */ 589 static void 590 history_run (void *cls, 591 const struct TALER_TESTING_Command *cmd, 592 struct TALER_TESTING_Interpreter *is) 593 { 594 struct HistoryState *ss = cls; 595 const struct TALER_TESTING_Command *create_coin; 596 char *cref; 597 unsigned int idx; 598 599 ss->is = is; 600 GNUNET_assert ( 601 GNUNET_OK == 602 TALER_TESTING_parse_coin_reference ( 603 ss->coin_reference, 604 &cref, 605 &idx)); 606 create_coin 607 = TALER_TESTING_interpreter_lookup_command (is, 608 cref); 609 GNUNET_free (cref); 610 if (NULL == create_coin) 611 { 612 GNUNET_break (0); 613 TALER_TESTING_interpreter_fail (is); 614 return; 615 } 616 if (GNUNET_OK != 617 TALER_TESTING_get_trait_coin_priv (create_coin, 618 idx, 619 &ss->coin_priv)) 620 { 621 GNUNET_break (0); 622 TALER_LOG_ERROR ("Failed to find coin_priv for history query\n"); 623 TALER_TESTING_interpreter_fail (is); 624 return; 625 } 626 GNUNET_CRYPTO_eddsa_key_get_public (&ss->coin_priv->eddsa_priv, 627 &ss->coin_pub.eddsa_pub); 628 ss->rsh = TALER_EXCHANGE_get_coins_history_create ( 629 TALER_TESTING_interpreter_get_context (is), 630 TALER_TESTING_get_exchange_url (is), 631 ss->coin_priv); 632 GNUNET_assert (NULL != ss->rsh); 633 GNUNET_assert (TALER_EC_NONE == 634 TALER_EXCHANGE_get_coins_history_start (ss->rsh, 635 &coin_history_cb, 636 ss)); 637 } 638 639 640 /** 641 * Offer internal data from a "history" CMD, to other commands. 642 * 643 * @param cls closure. 644 * @param[out] ret result. 645 * @param trait name of the trait. 646 * @param index index number of the object to offer. 647 * @return #GNUNET_OK on success. 648 */ 649 static enum GNUNET_GenericReturnValue 650 history_traits (void *cls, 651 const void **ret, 652 const char *trait, 653 unsigned int index) 654 { 655 struct HistoryState *hs = cls; 656 struct TALER_TESTING_Trait traits[] = { 657 TALER_TESTING_make_trait_coin_pub (0, 658 &hs->coin_pub), 659 TALER_TESTING_trait_end () 660 }; 661 662 return TALER_TESTING_get_trait (traits, 663 ret, 664 trait, 665 index); 666 } 667 668 669 /** 670 * Cleanup the state from a "coin history" CMD, and possibly 671 * cancel a pending operation thereof. 672 * 673 * @param cls closure. 674 * @param cmd the command which is being cleaned up. 675 */ 676 static void 677 history_cleanup (void *cls, 678 const struct TALER_TESTING_Command *cmd) 679 { 680 struct HistoryState *ss = cls; 681 682 if (NULL != ss->rsh) 683 { 684 TALER_TESTING_command_incomplete (ss->is, 685 cmd->label); 686 TALER_EXCHANGE_get_coins_history_cancel (ss->rsh); 687 ss->rsh = NULL; 688 } 689 GNUNET_free (ss); 690 } 691 692 693 struct TALER_TESTING_Command 694 TALER_TESTING_cmd_coin_history (const char *label, 695 const char *coin_reference, 696 const char *expected_balance, 697 unsigned int expected_response_code) 698 { 699 struct HistoryState *ss; 700 701 GNUNET_assert (NULL != coin_reference); 702 ss = GNUNET_new (struct HistoryState); 703 ss->coin_reference = coin_reference; 704 ss->expected_balance = expected_balance; 705 ss->expected_response_code = expected_response_code; 706 { 707 struct TALER_TESTING_Command cmd = { 708 .cls = ss, 709 .label = label, 710 .run = &history_run, 711 .cleanup = &history_cleanup, 712 .traits = &history_traits 713 }; 714 715 return cmd; 716 } 717 }