LCOV - code coverage report
Current view: top level - lib - test_melt.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 29 29
Test Date: 2026-11-18 17:34:04 Functions: 100.0 % 2 2

            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_melt.c
      18              :  * @brief check the client library's handling of melts
      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              : #include "taler/exchange/post-melt.h"
      25              : 
      26              : 
      27              : static void
      28            1 : check_history_arrays (void)
      29              : {
      30            1 :   const struct TALER_EXCHANGE_Keys keys = {0};
      31            1 :   struct TALER_EXCHANGE_DenomPublicKey dk = {0};
      32            1 :   const struct TALER_RefreshCommitmentP rc = {0};
      33            1 :   const struct TALER_PublicRefreshMasterSeedP seed = {0};
      34            1 :   const struct TALER_DenominationHashP denoms[TALER_MAX_COINS + 1] = {0};
      35            1 :   const struct TALER_TransferPublicKeyP pubs[TALER_MAX_COINS + 1] = {0};
      36              :   const struct
      37              :   {
      38              :     size_t num_denoms;
      39              :     int num_pubs; /* -1 to omit the optional field */
      40              :     enum GNUNET_GenericReturnValue expected;
      41            1 :   } cases[] = {
      42              :     { 1, -1, GNUNET_OK },
      43              :     { TALER_MAX_COINS, TALER_MAX_COINS, GNUNET_OK },
      44              :     { 0, -1, GNUNET_SYSERR },
      45              :     { 1, 0, GNUNET_SYSERR },
      46              :     { 1, 2, GNUNET_SYSERR },
      47              :     { 1, TALER_MAX_COINS + 1, GNUNET_SYSERR },
      48              :     { TALER_MAX_COINS + 1, -1, GNUNET_SYSERR }
      49              :   };
      50              :   struct TALER_CoinSpendPrivateKeyP priv;
      51              :   struct TALER_CoinSpendPublicKeyP pub;
      52              :   struct TALER_CoinSpendSignatureP sig;
      53              : 
      54            1 :   GNUNET_CRYPTO_eddsa_key_create (&priv.eddsa_priv);
      55            1 :   GNUNET_CRYPTO_eddsa_key_get_public (&priv.eddsa_priv,
      56              :                                       &pub.eddsa_pub);
      57            1 :   GNUNET_assert (GNUNET_OK ==
      58              :                  TALER_string_to_amount ("TEST:1",
      59              :                                          &dk.value));
      60            1 :   GNUNET_assert (GNUNET_OK ==
      61              :                  TALER_amount_set_zero ("TEST",
      62              :                                         &dk.fees.refresh));
      63            1 :   TALER_wallet_melt_sign (&dk.value,
      64              :                           &dk.fees.refresh,
      65              :                           &rc,
      66              :                           &dk.h_key,
      67              :                           NULL,
      68              :                           &priv,
      69              :                           &sig);
      70            8 :   for (unsigned int i = 0; i < sizeof (cases) / sizeof (cases[0]); i++)
      71              :   {
      72              :     struct TALER_Amount in;
      73              :     struct TALER_Amount out;
      74              :     struct TALER_EXCHANGE_CoinHistoryEntry entry;
      75            7 :     json_t *history = json_array ();
      76            7 :     json_t *tx = GNUNET_JSON_PACK (
      77              :       GNUNET_JSON_pack_string ("type", "MELT"),
      78              :       GNUNET_JSON_pack_uint64 ("history_offset", 0),
      79              :       TALER_JSON_pack_amount ("amount", &dk.value),
      80              :       TALER_JSON_pack_amount ("melt_fee", &dk.fees.refresh),
      81              :       GNUNET_JSON_pack_data_auto ("rc", &rc),
      82              :       GNUNET_JSON_pack_data_auto ("coin_sig", &sig),
      83              :       GNUNET_JSON_pack_data_auto ("refresh_seed", &seed),
      84              :       TALER_JSON_pack_array_of_data_auto ("denoms_h",
      85              :                                           cases[i].num_denoms,
      86              :                                           denoms),
      87              :       GNUNET_JSON_pack_conditional (
      88              :         cases[i].num_pubs >= 0,
      89              :         TALER_JSON_pack_array_of_data_auto ("transfer_pubs",
      90              :                                             cases[i].num_pubs,
      91              :                                             pubs)));
      92              : 
      93            7 :     GNUNET_assert (0 == json_array_append_new (history, tx));
      94            7 :     GNUNET_assert (cases[i].expected ==
      95              :                    TALER_EXCHANGE_parse_coin_history (&keys,
      96              :                                                       &dk,
      97              :                                                       history,
      98              :                                                       &pub,
      99              :                                                       &in,
     100              :                                                       &out,
     101              :                                                       1,
     102              :                                                       &entry));
     103            7 :     if (GNUNET_OK == cases[i].expected)
     104            2 :       TALER_EXCHANGE_free_coin_history (1,
     105              :                                         &entry);
     106            7 :     json_decref (history);
     107              :   }
     108            1 : }
     109              : 
     110              : 
     111              : int
     112            1 : main (void)
     113              : {
     114            1 :   struct TALER_EXCHANGE_Keys keys = { .version = "44:0:0" };
     115              : 
     116            1 :   GNUNET_assert (TALER_REFRESH_COMMITMENT_VERSION_LEGACY_VDOLDPLUS_BROKEN ==
     117              :                  TALER_EXCHANGE_refresh_commitment_version (&keys));
     118            1 :   keys.version = "45:0:0";
     119            1 :   GNUNET_assert (TALER_REFRESH_COMMITMENT_VERSION_CURRENT ==
     120              :                  TALER_EXCHANGE_refresh_commitment_version (&keys));
     121            1 :   check_history_arrays ();
     122            1 :   return 0;
     123              : }
        

Generated by: LCOV version 2.0-1