LCOV - code coverage report
Current view: top level - lib - test_purse_refund_history.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 54 54
Test Date: 2026-11-22 15:54:18 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2026 Taler Systems SA
       4              : 
       5              :   TALER 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              :   TALER 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              :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15              : */
      16              : /**
      17              :  * @file lib/test_purse_refund_history.c
      18              :  * @brief Verify current and legacy signed purse refunds.
      19              :  */
      20              : #include "platform.h"
      21              : #include "taler/taler_json_lib.h"
      22              : #include "taler/exchange/get-coins-COIN_PUB-history.h"
      23              : #include "taler/exchange/get-keys.h"
      24              : 
      25              : static struct TALER_ExchangePrivateKeyP exchange_priv;
      26              : 
      27              : 
      28              : static enum TALER_ErrorCode
      29            8 : sign_confirmation (const struct GNUNET_CRYPTO_SignaturePurpose *purpose,
      30              :                    struct TALER_ExchangePublicKeyP *pub,
      31              :                    struct TALER_ExchangeSignatureP *sig)
      32              : {
      33            8 :   GNUNET_CRYPTO_eddsa_key_get_public (&exchange_priv.eddsa_priv,
      34              :                                       &pub->eddsa_pub);
      35            8 :   GNUNET_assert (GNUNET_OK ==
      36              :                  GNUNET_CRYPTO_eddsa_sign_ (&exchange_priv.eddsa_priv,
      37              :                                             purpose,
      38              :                                             &sig->eddsa_signature));
      39            8 :   return TALER_EC_NONE;
      40              : }
      41              : 
      42              : 
      43              : static json_t *
      44            8 : refund_entry (const struct TALER_CoinSpendPublicKeyP *coin_pub,
      45              :               const struct TALER_PurseContractPublicKeyP *purse,
      46              :               const struct TALER_Amount *amount,
      47              :               const struct TALER_Amount *fee,
      48              :               uint64_t offset)
      49              : {
      50              :   struct TALER_ExchangePublicKeyP pub;
      51              :   struct TALER_ExchangeSignatureP sig;
      52              : 
      53            8 :   GNUNET_assert (TALER_EC_NONE ==
      54              :                  TALER_exchange_online_purse_refund_sign (&sign_confirmation,
      55              :                                                           amount, fee,
      56              :                                                           coin_pub, purse,
      57              :                                                           &pub, &sig));
      58            8 :   return GNUNET_JSON_PACK (
      59              :     GNUNET_JSON_pack_string ("type", "PURSE-REFUND"),
      60              :     GNUNET_JSON_pack_uint64 ("history_offset", offset),
      61              :     GNUNET_JSON_pack_data_auto ("purse_pub", purse),
      62              :     GNUNET_JSON_pack_data_auto ("exchange_pub", &pub),
      63              :     GNUNET_JSON_pack_data_auto ("exchange_sig", &sig),
      64              :     TALER_JSON_pack_amount ("amount", amount),
      65              :     TALER_JSON_pack_amount ("refund_fee", fee));
      66              : }
      67              : 
      68              : 
      69              : static enum GNUNET_GenericReturnValue
      70            8 : parse_history (const struct TALER_EXCHANGE_Keys *keys,
      71              :                const struct TALER_EXCHANGE_DenomPublicKey *dk,
      72              :                const struct TALER_CoinSpendPublicKeyP *coin_pub,
      73              :                const json_t *history)
      74            8 : {
      75            8 :   const unsigned int len = json_array_size (history);
      76            8 :   struct TALER_EXCHANGE_CoinHistoryEntry entries[len];
      77              :   struct TALER_Amount in;
      78              :   struct TALER_Amount out;
      79              :   enum GNUNET_GenericReturnValue ret;
      80              : 
      81            8 :   ret = TALER_EXCHANGE_parse_coin_history (keys, dk, history, coin_pub,
      82              :                                            &in, &out, len, entries);
      83            8 :   if (GNUNET_OK == ret)
      84              :   {
      85              :     struct TALER_Amount balance;
      86              : 
      87            3 :     GNUNET_assert (0 <= TALER_amount_subtract (&balance, &in, &out));
      88            3 :     GNUNET_assert (0 == TALER_amount_cmp (&dk->value, &balance));
      89            3 :     TALER_EXCHANGE_free_coin_history (len, entries);
      90              :   }
      91            8 :   return ret;
      92              : }
      93              : 
      94              : 
      95              : int
      96            1 : main (void)
      97              : {
      98            1 :   struct TALER_EXCHANGE_SigningPublicKey sign_key = {
      99              :     .valid_until = GNUNET_TIME_UNIT_FOREVER_TS
     100              :   };
     101            1 :   const struct TALER_EXCHANGE_Keys keys = {
     102              :     .sign_keys = &sign_key,
     103              :     .num_sign_keys = 1
     104              :   };
     105            1 :   struct TALER_EXCHANGE_DenomPublicKey dk = {0};
     106              :   struct TALER_CoinSpendPrivateKeyP coin_priv;
     107              :   struct TALER_CoinSpendPublicKeyP coin_pub;
     108              :   struct TALER_PurseContractPublicKeyP purses[2];
     109            1 :   const char *url = "https://exchange.example/";
     110              :   const struct {
     111              :     const char *amount;
     112              :     const char *fee;
     113              :     enum GNUNET_GenericReturnValue expected;
     114            1 :   } cases[] = {
     115              :     { "TEST:3", "TEST:0", GNUNET_OK },
     116              :     { "TEST:2.98", "TEST:0.02", GNUNET_OK },
     117              :     { "TEST:2.98", "TEST:0", GNUNET_SYSERR },
     118              :     { "TEST:2.99", "TEST:0.01", GNUNET_SYSERR },
     119              :     { "TEST:3", "EUR:0", GNUNET_SYSERR },
     120              :   };
     121              :   struct TALER_Amount deposit;
     122              :   struct TALER_Amount zero_fee;
     123              :   json_t *deposits[2];
     124              :   json_t *history;
     125              : 
     126            1 :   GNUNET_CRYPTO_eddsa_key_create (&exchange_priv.eddsa_priv);
     127            1 :   GNUNET_CRYPTO_eddsa_key_get_public (&exchange_priv.eddsa_priv,
     128              :                                       &sign_key.key.eddsa_pub);
     129            1 :   GNUNET_CRYPTO_eddsa_key_create (&coin_priv.eddsa_priv);
     130            1 :   GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv.eddsa_priv,
     131              :                                       &coin_pub.eddsa_pub);
     132            1 :   GNUNET_assert (GNUNET_OK == TALER_string_to_amount ("TEST:8", &dk.value));
     133            1 :   GNUNET_assert (GNUNET_OK == TALER_string_to_amount ("TEST:3", &deposit));
     134            1 :   GNUNET_assert (GNUNET_OK ==
     135              :                  TALER_string_to_amount ("TEST:0.1", &dk.fees.deposit));
     136            1 :   GNUNET_assert (GNUNET_OK ==
     137              :                  TALER_string_to_amount ("TEST:0.02", &dk.fees.refund));
     138            1 :   GNUNET_assert (GNUNET_OK == TALER_amount_set_zero ("TEST", &zero_fee));
     139            3 :   for (unsigned int i = 0; i < 2; i++)
     140              :   {
     141              :     struct TALER_PurseContractPrivateKeyP priv;
     142              :     struct TALER_CoinSpendSignatureP coin_sig;
     143              : 
     144            2 :     GNUNET_CRYPTO_eddsa_key_create (&priv.eddsa_priv);
     145            2 :     GNUNET_CRYPTO_eddsa_key_get_public (&priv.eddsa_priv,
     146              :                                         &purses[i].eddsa_pub);
     147            2 :     TALER_wallet_purse_deposit_sign (url, &purses[i], &deposit, &dk.h_key,
     148              :                                      NULL, &coin_priv, &coin_sig);
     149            2 :     deposits[i] = GNUNET_JSON_PACK (
     150              :       GNUNET_JSON_pack_string ("type", "PURSE-DEPOSIT"),
     151              :       GNUNET_JSON_pack_uint64 ("history_offset", i + 1),
     152              :       GNUNET_JSON_pack_string ("exchange_base_url", url),
     153              :       GNUNET_JSON_pack_data_auto ("purse_pub", &purses[i]),
     154              :       GNUNET_JSON_pack_data_auto ("coin_sig", &coin_sig),
     155              :       GNUNET_JSON_pack_bool ("refunded", true),
     156              :       TALER_JSON_pack_amount ("amount", &deposit));
     157              :   }
     158            6 :   for (unsigned int i = 0; i < sizeof (cases) / sizeof (cases[0]); i++)
     159              :   {
     160              :     struct TALER_Amount amount;
     161              :     struct TALER_Amount fee;
     162              : 
     163            5 :     GNUNET_assert (GNUNET_OK == TALER_string_to_amount (cases[i].amount, &amount));
     164            5 :     GNUNET_assert (GNUNET_OK == TALER_string_to_amount (cases[i].fee, &fee));
     165            5 :     history = json_pack ("[O,o]", deposits[0],
     166              :                          refund_entry (&coin_pub, &purses[0], &amount, &fee, 2));
     167            5 :     GNUNET_assert (cases[i].expected ==
     168              :                    parse_history (&keys, &dk, &coin_pub, history));
     169            5 :     json_decref (history);
     170              :   }
     171              : 
     172              :   /* Only the first purse received a deposit. The second has a signed refund. */
     173            1 :   history = json_pack ("[O,o]", deposits[0],
     174              :                        refund_entry (&coin_pub, &purses[1],
     175              :                                      &deposit, &zero_fee, 3));
     176            1 :   GNUNET_assert (GNUNET_SYSERR ==
     177              :                  parse_history (&keys, &dk, &coin_pub, history));
     178              : 
     179              :   /* Two equal deposits, each refunded once. */
     180            1 :   GNUNET_assert (0 == json_array_insert (history, 1, deposits[1]));
     181            1 :   GNUNET_assert (0 == json_array_append_new (
     182              :                    history,
     183              :                    refund_entry (&coin_pub, &purses[0], &deposit, &zero_fee, 4)));
     184            1 :   GNUNET_assert (GNUNET_OK ==
     185              :                  parse_history (&keys, &dk, &coin_pub, history));
     186              : 
     187              :   /* Repeating the second purse's refund at a fresh offset preserves the
     188              :      entry count and total value. Each deposit still needs its own refund. */
     189            1 :   GNUNET_assert (0 == json_array_set_new (
     190              :                    history, 3,
     191              :                    refund_entry (&coin_pub, &purses[1], &deposit, &zero_fee, 4)));
     192            1 :   GNUNET_assert (GNUNET_SYSERR ==
     193              :                  parse_history (&keys, &dk, &coin_pub, history));
     194              : 
     195            1 :   json_decref (history);
     196            1 :   json_decref (deposits[0]);
     197            1 :   json_decref (deposits[1]);
     198            1 :   return 0;
     199              : }
        

Generated by: LCOV version 2.0-1