LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_refresh.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 239 422 56.6 %
Date: 2021-08-30 06:43:37 Functions: 16 22 72.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2018-2020 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it
       6             :   under the terms of the GNU General Public License as published by
       7             :   the Free Software Foundation; either version 3, or (at your
       8             :   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 GNU
      13             :   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_refresh.c
      21             :  * @brief commands for testing all "refresh" features.
      22             :  * @author Marcello Stanisci
      23             :  */
      24             : #include "platform.h"
      25             : #include "taler_json_lib.h"
      26             : #include <gnunet/gnunet_curl_lib.h>
      27             : #include "taler_testing_lib.h"
      28             : #include "taler_signatures.h"
      29             : #include "backoff.h"
      30             : 
      31             : /**
      32             :  * How long do we wait AT MOST when retrying?
      33             :  */
      34             : #define MAX_BACKOFF GNUNET_TIME_relative_multiply ( \
      35             :     GNUNET_TIME_UNIT_MILLISECONDS, 100)
      36             : 
      37             : /**
      38             :  * How often do we retry before giving up?
      39             :  */
      40             : #define NUM_RETRIES 5
      41             : 
      42             : /**
      43             :  * How long do we wait AT MOST when retrying?
      44             :  */
      45             : #define MAX_BACKOFF GNUNET_TIME_relative_multiply ( \
      46             :     GNUNET_TIME_UNIT_MILLISECONDS, 100)
      47             : 
      48             : /**
      49             :  * Information about a fresh coin generated by the refresh
      50             :  * operation.
      51             :  */
      52             : struct TALER_TESTING_FreshCoinData
      53             : {
      54             : 
      55             :   /**
      56             :    * If @e amount is NULL, this specifies the denomination key to
      57             :    * use.  Otherwise, this will be set (by the interpreter) to the
      58             :    * denomination PK matching @e amount.
      59             :    */
      60             :   const struct TALER_EXCHANGE_DenomPublicKey *pk;
      61             : 
      62             :   /**
      63             :    * Set (by the interpreter) to the exchange's signature over the
      64             :    * coin's public key.
      65             :    */
      66             :   struct TALER_DenominationSignature sig;
      67             : 
      68             :   /**
      69             :    * Set (by the interpreter) to the coin's private key.
      70             :    */
      71             :   struct TALER_CoinSpendPrivateKeyP coin_priv;
      72             : 
      73             :   /**
      74             :    * The blinding key (needed for recoup operations).
      75             :    */
      76             :   struct TALER_DenominationBlindingKeyP blinding_key;
      77             : 
      78             : };
      79             : 
      80             : 
      81             : /**
      82             :  * State for a "refresh melt" command.
      83             :  */
      84             : struct RefreshMeltState
      85             : {
      86             : 
      87             :   /**
      88             :    * Reference to reserve_withdraw operations for coin to
      89             :    * be used for the /refresh/melt operation.
      90             :    */
      91             :   const char *coin_reference;
      92             : 
      93             :   /**
      94             :    * "Crypto data" used in the refresh operation.
      95             :    */
      96             :   char *refresh_data;
      97             : 
      98             :   /**
      99             :    * Reference to a previous melt command.
     100             :    */
     101             :   const char *melt_reference;
     102             : 
     103             :   /**
     104             :    * Melt handle while operation is running.
     105             :    */
     106             :   struct TALER_EXCHANGE_MeltHandle *rmh;
     107             : 
     108             :   /**
     109             :    * Interpreter state.
     110             :    */
     111             :   struct TALER_TESTING_Interpreter *is;
     112             : 
     113             :   /**
     114             :    * Array of the denomination public keys
     115             :    * corresponding to the @e num_fresh_coins;
     116             :    */
     117             :   struct TALER_EXCHANGE_DenomPublicKey *fresh_pks;
     118             : 
     119             :   /**
     120             :    * Private key of the dirty coin being melted.
     121             :    */
     122             :   const struct TALER_CoinSpendPrivateKeyP *melt_priv;
     123             : 
     124             :   /**
     125             :    * Task scheduled to try later.
     126             :    */
     127             :   struct GNUNET_SCHEDULER_Task *retry_task;
     128             : 
     129             :   /**
     130             :    * How long do we wait until we retry?
     131             :    */
     132             :   struct GNUNET_TIME_Relative backoff;
     133             : 
     134             :   /**
     135             :    * How long did we wait in total for retries?
     136             :    */
     137             :   struct GNUNET_TIME_Relative total_backoff;
     138             : 
     139             :   /**
     140             :    * Number of bytes in @e refresh_data.
     141             :    */
     142             :   size_t refresh_data_length;
     143             : 
     144             :   /**
     145             :    * Amounts to be generated during melt.
     146             :    */
     147             :   const char **melt_fresh_amounts;
     148             : 
     149             :   /**
     150             :    * Number of fresh coins generated by the melt.
     151             :    */
     152             :   unsigned int num_fresh_coins;
     153             : 
     154             :   /**
     155             :    * Expected HTTP response code.
     156             :    */
     157             :   unsigned int expected_response_code;
     158             : 
     159             :   /**
     160             :    * if set to #GNUNET_YES, then two /refresh/melt operations
     161             :    * will be performed.  This is needed to trigger the logic
     162             :    * that manages those already-made requests.  Note: it
     163             :    * is not possible to just copy-and-paste a test refresh melt
     164             :    * CMD to have the same effect, because every data preparation
     165             :    * generates new planchets that (in turn) make the whole "hash"
     166             :    * different from any previous one, therefore NOT allowing the
     167             :    * exchange to pick any previous /rerfesh/melt operation from
     168             :    * the database.
     169             :    */
     170             :   unsigned int double_melt;
     171             : 
     172             :   /**
     173             :    * How often should we retry on (transient) failures?
     174             :    */
     175             :   unsigned int do_retry;
     176             : 
     177             :   /**
     178             :    * Set by the melt callback as it comes from the exchange.
     179             :    */
     180             :   uint16_t noreveal_index;
     181             : };
     182             : 
     183             : 
     184             : /**
     185             :  * State for a "refresh reveal" CMD.
     186             :  */
     187             : struct RefreshRevealState
     188             : {
     189             :   /**
     190             :    * Link to a "refresh melt" command.
     191             :    */
     192             :   const char *melt_reference;
     193             : 
     194             :   /**
     195             :    * Reveal handle while operation is running.
     196             :    */
     197             :   struct TALER_EXCHANGE_RefreshesRevealHandle *rrh;
     198             : 
     199             :   /**
     200             :    * Convenience struct to keep in one place all the
     201             :    * data related to one fresh coin, set by the reveal callback
     202             :    * as it comes from the exchange.
     203             :    */
     204             :   struct TALER_TESTING_FreshCoinData *fresh_coins;
     205             : 
     206             :   /**
     207             :    * Interpreter state.
     208             :    */
     209             :   struct TALER_TESTING_Interpreter *is;
     210             : 
     211             :   /**
     212             :    * Task scheduled to try later.
     213             :    */
     214             :   struct GNUNET_SCHEDULER_Task *retry_task;
     215             : 
     216             :   /**
     217             :    * How long do we wait until we retry?
     218             :    */
     219             :   struct GNUNET_TIME_Relative backoff;
     220             : 
     221             :   /**
     222             :    * How long did we wait in total for retries?
     223             :    */
     224             :   struct GNUNET_TIME_Relative total_backoff;
     225             : 
     226             :   /**
     227             :    * Number of fresh coins withdrawn, set by the
     228             :    * reveal callback as it comes from the exchange,
     229             :    * it is the length of the @e fresh_coins array.
     230             :    */
     231             :   unsigned int num_fresh_coins;
     232             : 
     233             :   /**
     234             :    * Expected HTTP response code.
     235             :    */
     236             :   unsigned int expected_response_code;
     237             : 
     238             :   /**
     239             :    * How often should we retry on (transient) failures?
     240             :    */
     241             :   unsigned int do_retry;
     242             : 
     243             : };
     244             : 
     245             : 
     246             : /**
     247             :  * State for a "refresh link" CMD.
     248             :  */
     249             : struct RefreshLinkState
     250             : {
     251             :   /**
     252             :    * Link to a "refresh reveal" command.
     253             :    */
     254             :   const char *reveal_reference;
     255             : 
     256             :   /**
     257             :    * Handle to the ongoing operation.
     258             :    */
     259             :   struct TALER_EXCHANGE_LinkHandle *rlh;
     260             : 
     261             :   /**
     262             :    * Interpreter state.
     263             :    */
     264             :   struct TALER_TESTING_Interpreter *is;
     265             : 
     266             :   /**
     267             :    * Task scheduled to try later.
     268             :    */
     269             :   struct GNUNET_SCHEDULER_Task *retry_task;
     270             : 
     271             :   /**
     272             :    * How long do we wait until we retry?
     273             :    */
     274             :   struct GNUNET_TIME_Relative backoff;
     275             : 
     276             :   /**
     277             :    * How long did we wait in total for retries?
     278             :    */
     279             :   struct GNUNET_TIME_Relative total_backoff;
     280             : 
     281             :   /**
     282             :    * Expected HTTP response code.
     283             :    */
     284             :   unsigned int expected_response_code;
     285             : 
     286             :   /**
     287             :    * How often should we retry on (transient) failures?
     288             :    */
     289             :   unsigned int do_retry;
     290             : 
     291             : };
     292             : 
     293             : 
     294             : /**
     295             :  * Run the command.
     296             :  *
     297             :  * @param cls closure.
     298             :  * @param cmd the command to execute.
     299             :  * @param is the interpreter state.
     300             :  */
     301             : static void
     302             : refresh_reveal_run (void *cls,
     303             :                     const struct TALER_TESTING_Command *cmd,
     304             :                     struct TALER_TESTING_Interpreter *is);
     305             : 
     306             : 
     307             : /**
     308             :  * Task scheduled to re-try #refresh_reveal_run.
     309             :  *
     310             :  * @param cls a `struct RefreshRevealState`
     311             :  */
     312             : static void
     313           0 : do_reveal_retry (void *cls)
     314             : {
     315           0 :   struct RefreshRevealState *rrs = cls;
     316             : 
     317           0 :   rrs->retry_task = NULL;
     318           0 :   rrs->is->commands[rrs->is->ip].last_req_time
     319           0 :     = GNUNET_TIME_absolute_get ();
     320           0 :   refresh_reveal_run (rrs,
     321             :                       NULL,
     322             :                       rrs->is);
     323           0 : }
     324             : 
     325             : 
     326             : /**
     327             :  * "refresh reveal" request callback; it checks that the response
     328             :  * code is expected and copies into its command's state the data
     329             :  * coming from the exchange, namely the fresh coins.
     330             :  *
     331             :  * @param cls closure, a `struct RefreshRevealState`
     332             :  * @param hr HTTP response details
     333             :  * @param num_coins number of fresh coins created, length of the
     334             :  *        @a sigs and @a coin_privs arrays, 0 if the operation
     335             :  *        failed.
     336             :  * @param coin_privs array of @a num_coins private keys for the
     337             :  *        coins that were created, NULL on error.
     338             :  * @param sigs array of signature over @a num_coins coins,
     339             :  *        NULL on error.
     340             :  */
     341             : static void
     342           8 : reveal_cb (void *cls,
     343             :            const struct TALER_EXCHANGE_HttpResponse *hr,
     344             :            unsigned int num_coins,
     345             :            const struct TALER_PlanchetSecretsP *coin_privs,
     346             :            const struct TALER_DenominationSignature *sigs)
     347             : {
     348           8 :   struct RefreshRevealState *rrs = cls;
     349             :   const struct TALER_TESTING_Command *melt_cmd;
     350             : 
     351           8 :   rrs->rrh = NULL;
     352           8 :   if (rrs->expected_response_code != hr->http_status)
     353             :   {
     354           0 :     if (0 != rrs->do_retry)
     355             :     {
     356           0 :       rrs->do_retry--;
     357           0 :       if ( (0 == hr->http_status) ||
     358           0 :            (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec) ||
     359           0 :            (MHD_HTTP_INTERNAL_SERVER_ERROR == hr->http_status) )
     360             :       {
     361           0 :         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     362             :                     "Retrying refresh reveal failed with %u/%d\n",
     363             :                     hr->http_status,
     364             :                     (int) hr->ec);
     365             :         /* on DB conflicts, do not use backoff */
     366           0 :         if (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec)
     367           0 :           rrs->backoff = GNUNET_TIME_UNIT_ZERO;
     368             :         else
     369           0 :           rrs->backoff = GNUNET_TIME_randomized_backoff (rrs->backoff,
     370             :                                                          MAX_BACKOFF);
     371           0 :         rrs->total_backoff = GNUNET_TIME_relative_add (rrs->total_backoff,
     372             :                                                        rrs->backoff);
     373           0 :         rrs->is->commands[rrs->is->ip].num_tries++;
     374           0 :         rrs->retry_task = GNUNET_SCHEDULER_add_delayed (rrs->backoff,
     375             :                                                         &do_reveal_retry,
     376             :                                                         rrs);
     377           0 :         return;
     378             :       }
     379             :     }
     380           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     381             :                 "Unexpected response code %u/%d to command %s in %s:%u\n",
     382             :                 hr->http_status,
     383             :                 (int) hr->ec,
     384             :                 rrs->is->commands[rrs->is->ip].label,
     385             :                 __FILE__,
     386             :                 __LINE__);
     387           0 :     json_dumpf (hr->reply,
     388             :                 stderr,
     389             :                 0);
     390           0 :     TALER_TESTING_interpreter_fail (rrs->is);
     391           0 :     return;
     392             :   }
     393           8 :   melt_cmd = TALER_TESTING_interpreter_lookup_command (rrs->is,
     394             :                                                        rrs->melt_reference);
     395           8 :   if (NULL == melt_cmd)
     396             :   {
     397           0 :     GNUNET_break (0);
     398           0 :     TALER_TESTING_interpreter_fail (rrs->is);
     399           0 :     return;
     400             :   }
     401           8 :   rrs->num_fresh_coins = num_coins;
     402           8 :   switch (hr->http_status)
     403             :   {
     404           7 :   case MHD_HTTP_OK:
     405           7 :     rrs->fresh_coins = GNUNET_new_array (num_coins,
     406             :                                          struct TALER_TESTING_FreshCoinData);
     407          26 :     for (unsigned int i = 0; i<num_coins; i++)
     408             :     {
     409          19 :       struct TALER_TESTING_FreshCoinData *fc = &rrs->fresh_coins[i];
     410             : 
     411          19 :       if (GNUNET_OK !=
     412          19 :           TALER_TESTING_get_trait_denom_pub (melt_cmd,
     413             :                                              i,
     414             :                                              &fc->pk))
     415             :       {
     416           0 :         GNUNET_break (0);
     417           0 :         TALER_TESTING_interpreter_fail (rrs->is);
     418           0 :         return;
     419             :       }
     420          19 :       fc->coin_priv = coin_privs[i].coin_priv;
     421          19 :       fc->blinding_key = coin_privs[i].blinding_key;
     422          19 :       fc->sig.rsa_signature = GNUNET_CRYPTO_rsa_signature_dup
     423          19 :                                 (sigs[i].rsa_signature);
     424             :     }
     425           7 :     if (0 != rrs->total_backoff.rel_value_us)
     426             :     {
     427           0 :       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     428             :                   "Total reveal backoff for %s was %s\n",
     429             :                   rrs->is->commands[rrs->is->ip].label,
     430             :                   GNUNET_STRINGS_relative_time_to_string (rrs->total_backoff,
     431             :                                                           GNUNET_YES));
     432             :     }
     433           7 :     break;
     434           1 :   default:
     435           1 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     436             :                 "Unknown HTTP status %u/%d\n",
     437             :                 hr->http_status,
     438             :                 (int) hr->ec);
     439             :   }
     440           8 :   TALER_TESTING_interpreter_next (rrs->is);
     441             : }
     442             : 
     443             : 
     444             : /**
     445             :  * Run the command.
     446             :  *
     447             :  * @param cls closure.
     448             :  * @param cmd the command to execute.
     449             :  * @param is the interpreter state.
     450             :  */
     451             : static void
     452           8 : refresh_reveal_run (void *cls,
     453             :                     const struct TALER_TESTING_Command *cmd,
     454             :                     struct TALER_TESTING_Interpreter *is)
     455             : {
     456           8 :   struct RefreshRevealState *rrs = cls;
     457             :   struct RefreshMeltState *rms;
     458             :   const struct TALER_TESTING_Command *melt_cmd;
     459             : 
     460           8 :   rrs->is = is;
     461           8 :   melt_cmd = TALER_TESTING_interpreter_lookup_command (is,
     462             :                                                        rrs->melt_reference);
     463           8 :   if (NULL == melt_cmd)
     464             :   {
     465           0 :     GNUNET_break (0);
     466           0 :     TALER_TESTING_interpreter_fail (rrs->is);
     467           0 :     return;
     468             :   }
     469           8 :   rms = melt_cmd->cls;
     470          16 :   rrs->rrh = TALER_EXCHANGE_refreshes_reveal (is->exchange,
     471             :                                               rms->refresh_data_length,
     472           8 :                                               rms->refresh_data,
     473           8 :                                               rms->noreveal_index,
     474             :                                               &reveal_cb,
     475             :                                               rrs);
     476             : 
     477           8 :   if (NULL == rrs->rrh)
     478             :   {
     479           0 :     GNUNET_break (0);
     480           0 :     TALER_TESTING_interpreter_fail (is);
     481           0 :     return;
     482             :   }
     483             : }
     484             : 
     485             : 
     486             : /**
     487             :  * Free the state from a "refresh reveal" CMD, and possibly
     488             :  * cancel a pending operation thereof.
     489             :  *
     490             :  * @param cls closure.
     491             :  * @param cmd the command which is being cleaned up.
     492             :  */
     493             : static void
     494           8 : refresh_reveal_cleanup (void *cls,
     495             :                         const struct TALER_TESTING_Command *cmd)
     496             : {
     497           8 :   struct RefreshRevealState *rrs = cls;
     498             : 
     499           8 :   if (NULL != rrs->rrh)
     500             :   {
     501           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     502             :                 "Command %u (%s) did not complete\n",
     503             :                 rrs->is->ip,
     504             :                 cmd->label);
     505           0 :     TALER_EXCHANGE_refreshes_reveal_cancel (rrs->rrh);
     506           0 :     rrs->rrh = NULL;
     507             :   }
     508           8 :   if (NULL != rrs->retry_task)
     509             :   {
     510           0 :     GNUNET_SCHEDULER_cancel (rrs->retry_task);
     511           0 :     rrs->retry_task = NULL;
     512             :   }
     513             : 
     514          27 :   for (unsigned int j = 0; j < rrs->num_fresh_coins; j++)
     515          19 :     GNUNET_CRYPTO_rsa_signature_free (rrs->fresh_coins[j].sig.rsa_signature);
     516             : 
     517           8 :   GNUNET_free (rrs->fresh_coins);
     518           8 :   rrs->fresh_coins = NULL;
     519           8 :   rrs->num_fresh_coins = 0;
     520           8 :   GNUNET_free (rrs);
     521           8 : }
     522             : 
     523             : 
     524             : /**
     525             :  * Run the command.
     526             :  *
     527             :  * @param cls closure.
     528             :  * @param cmd the command to execute.
     529             :  * @param is the interpreter state.
     530             :  */
     531             : static void
     532             : refresh_link_run (void *cls,
     533             :                   const struct TALER_TESTING_Command *cmd,
     534             :                   struct TALER_TESTING_Interpreter *is);
     535             : 
     536             : 
     537             : /**
     538             :  * Task scheduled to re-try #refresh_link_run.
     539             :  *
     540             :  * @param cls a `struct RefreshLinkState`
     541             :  */
     542             : static void
     543           0 : do_link_retry (void *cls)
     544             : {
     545           0 :   struct RefreshLinkState *rls = cls;
     546             : 
     547           0 :   rls->retry_task = NULL;
     548           0 :   rls->is->commands[rls->is->ip].last_req_time
     549           0 :     = GNUNET_TIME_absolute_get ();
     550           0 :   refresh_link_run (rls,
     551             :                     NULL,
     552             :                     rls->is);
     553           0 : }
     554             : 
     555             : 
     556             : /**
     557             :  * "refresh link" operation callback, checks that HTTP response
     558             :  * code is expected _and_ that all the linked coins were actually
     559             :  * withdrawn by the "refresh reveal" CMD.
     560             :  *
     561             :  * @param cls closure.
     562             :  * @param hr HTTP response details
     563             :  * @param num_coins number of fresh coins created, length of the
     564             :  *        @a sigs and @a coin_privs arrays, 0 if the operation
     565             :  *        failed.
     566             :  * @param coin_privs array of @a num_coins private keys for the
     567             :  *        coins that were created, NULL on error.
     568             :  * @param sigs array of signature over @a num_coins coins, NULL on
     569             :  *        error.
     570             :  * @param pubs array of public keys for the @a sigs,
     571             :  *        NULL on error.
     572             :  */
     573             : static void
     574           1 : link_cb (void *cls,
     575             :          const struct TALER_EXCHANGE_HttpResponse *hr,
     576             :          unsigned int num_coins,
     577             :          const struct TALER_CoinSpendPrivateKeyP *coin_privs,
     578             :          const struct TALER_DenominationSignature *sigs,
     579             :          const struct TALER_DenominationPublicKey *pubs)
     580             : {
     581             : 
     582           1 :   struct RefreshLinkState *rls = cls;
     583             :   const struct TALER_TESTING_Command *reveal_cmd;
     584           1 :   struct TALER_TESTING_Command *link_cmd = &rls->is->commands[rls->is->ip];
     585             :   unsigned int found;
     586             :   const unsigned int *num_fresh_coins;
     587             : 
     588           1 :   rls->rlh = NULL;
     589           1 :   if (rls->expected_response_code != hr->http_status)
     590             :   {
     591           0 :     if (0 != rls->do_retry)
     592             :     {
     593           0 :       rls->do_retry--;
     594           0 :       if ( (0 == hr->http_status) ||
     595           0 :            (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec) ||
     596           0 :            (MHD_HTTP_INTERNAL_SERVER_ERROR == hr->http_status) )
     597             :       {
     598           0 :         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     599             :                     "Retrying refresh link failed with %u/%d\n",
     600             :                     hr->http_status,
     601             :                     (int) hr->ec);
     602             :         /* on DB conflicts, do not use backoff */
     603           0 :         if (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec)
     604           0 :           rls->backoff = GNUNET_TIME_UNIT_ZERO;
     605             :         else
     606           0 :           rls->backoff = GNUNET_TIME_randomized_backoff (rls->backoff,
     607             :                                                          MAX_BACKOFF);
     608           0 :         rls->total_backoff = GNUNET_TIME_relative_add (rls->total_backoff,
     609             :                                                        rls->backoff);
     610           0 :         rls->is->commands[rls->is->ip].num_tries++;
     611           0 :         rls->retry_task = GNUNET_SCHEDULER_add_delayed (rls->backoff,
     612             :                                                         &do_link_retry,
     613             :                                                         rls);
     614           0 :         return;
     615             :       }
     616             :     }
     617           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     618             :                 "Unexpected response code %u/%d to command %s in %s:%u\n",
     619             :                 hr->http_status,
     620             :                 (int) hr->ec,
     621             :                 link_cmd->label,
     622             :                 __FILE__,
     623             :                 __LINE__);
     624           0 :     json_dumpf (hr->reply,
     625             :                 stderr,
     626             :                 0);
     627           0 :     TALER_TESTING_interpreter_fail (rls->is);
     628           0 :     return;
     629             :   }
     630           1 :   reveal_cmd = TALER_TESTING_interpreter_lookup_command (rls->is,
     631             :                                                          rls->reveal_reference);
     632           1 :   if (NULL == reveal_cmd)
     633             :   {
     634           0 :     GNUNET_break (0);
     635           0 :     TALER_TESTING_interpreter_fail (rls->is);
     636           0 :     return;
     637             :   }
     638             : 
     639           1 :   switch (hr->http_status)
     640             :   {
     641           1 :   case MHD_HTTP_OK:
     642             :     /* check that number of coins returned matches */
     643           1 :     if (GNUNET_OK !=
     644           1 :         TALER_TESTING_get_trait_uint (reveal_cmd,
     645             :                                       0,
     646             :                                       &num_fresh_coins))
     647             :     {
     648           0 :       GNUNET_break (0);
     649           0 :       TALER_TESTING_interpreter_fail (rls->is);
     650           0 :       return;
     651             :     }
     652           1 :     if (num_coins != *num_fresh_coins)
     653             :     {
     654           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     655             :                   "Unexpected number of fresh coins: %d vs %d in %s:%u\n",
     656             :                   num_coins,
     657             :                   *num_fresh_coins,
     658             :                   __FILE__,
     659             :                   __LINE__);
     660           0 :       TALER_TESTING_interpreter_fail (rls->is);
     661           0 :       return;
     662             :     }
     663             :     /* check that the coins match */
     664           5 :     for (unsigned int i = 0; i<num_coins; i++)
     665          10 :       for (unsigned int j = i + 1; j<num_coins; j++)
     666           6 :         if (0 ==
     667           6 :             GNUNET_memcmp (&coin_privs[i],
     668             :                            &coin_privs[j]))
     669           0 :           GNUNET_break (0);
     670             :     /* Note: coins might be legitimately permutated in here... */
     671           1 :     found = 0;
     672             : 
     673             :     /* Will point to the pointer inside the cmd state. */
     674             :     {
     675           1 :       const struct TALER_TESTING_FreshCoinData *fc = NULL;
     676             : 
     677           1 :       if (GNUNET_OK !=
     678           1 :           TALER_TESTING_get_trait_fresh_coins (reveal_cmd,
     679             :                                                0,
     680             :                                                &fc))
     681             :       {
     682           0 :         GNUNET_break (0);
     683           0 :         TALER_TESTING_interpreter_fail (rls->is);
     684           0 :         return;
     685             :       }
     686             : 
     687           5 :       for (unsigned int i = 0; i<num_coins; i++)
     688          10 :         for (unsigned int j = 0; j<num_coins; j++)
     689             :         {
     690          10 :           if ( (0 ==
     691          10 :                 GNUNET_memcmp (&coin_privs[i],
     692           4 :                                &fc[j].coin_priv)) &&
     693             :                (0 ==
     694           4 :                 GNUNET_CRYPTO_rsa_signature_cmp (fc[i].sig.rsa_signature,
     695           8 :                                                  sigs[j].rsa_signature)) &&
     696             :                (0 ==
     697           4 :                 GNUNET_CRYPTO_rsa_public_key_cmp (fc[i].pk->key.rsa_public_key,
     698           4 :                                                   pubs[j].rsa_public_key)) )
     699             :           {
     700           4 :             found++;
     701           4 :             break;
     702             :           }
     703             :         }
     704             :     }
     705           1 :     if (found != num_coins)
     706             :     {
     707           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     708             :                   "Only %u/%u coins match expectations\n",
     709             :                   found, num_coins);
     710           0 :       GNUNET_break (0);
     711           0 :       TALER_TESTING_interpreter_fail (rls->is);
     712           0 :       return;
     713             :     }
     714           1 :     if (0 != rls->total_backoff.rel_value_us)
     715             :     {
     716           0 :       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     717             :                   "Total link backoff for %s was %s\n",
     718             :                   rls->is->commands[rls->is->ip].label,
     719             :                   GNUNET_STRINGS_relative_time_to_string (rls->total_backoff,
     720             :                                                           GNUNET_YES));
     721             :     }
     722           1 :     break;
     723           0 :   default:
     724           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     725             :                 "Unknown HTTP response code %u/%d.\n",
     726             :                 hr->http_status,
     727             :                 hr->ec);
     728             :   }
     729           1 :   TALER_TESTING_interpreter_next (rls->is);
     730             : }
     731             : 
     732             : 
     733             : /**
     734             :  * Run the command.
     735             :  *
     736             :  * @param cls closure.
     737             :  * @param cmd the command to execute.
     738             :  * @param is the interpreter state.
     739             :  */
     740             : static void
     741           1 : refresh_link_run (void *cls,
     742             :                   const struct TALER_TESTING_Command *cmd,
     743             :                   struct TALER_TESTING_Interpreter *is)
     744             : {
     745           1 :   struct RefreshLinkState *rls = cls;
     746             :   struct RefreshRevealState *rrs;
     747             :   struct RefreshMeltState *rms;
     748             :   const struct TALER_TESTING_Command *reveal_cmd;
     749             :   const struct TALER_TESTING_Command *melt_cmd;
     750             :   const struct TALER_TESTING_Command *coin_cmd;
     751             : 
     752           1 :   rls->is = is;
     753           1 :   reveal_cmd = TALER_TESTING_interpreter_lookup_command (rls->is,
     754             :                                                          rls->reveal_reference);
     755           1 :   if (NULL == reveal_cmd)
     756             :   {
     757           0 :     GNUNET_break (0);
     758           0 :     TALER_TESTING_interpreter_fail (rls->is);
     759           0 :     return;
     760             :   }
     761           1 :   rrs = reveal_cmd->cls;
     762           1 :   melt_cmd = TALER_TESTING_interpreter_lookup_command (rls->is,
     763             :                                                        rrs->melt_reference);
     764           1 :   if (NULL == melt_cmd)
     765             :   {
     766           0 :     GNUNET_break (0);
     767           0 :     TALER_TESTING_interpreter_fail (rls->is);
     768           0 :     return;
     769             :   }
     770             : 
     771             :   /* find reserve_withdraw command */
     772             :   {
     773           1 :     rms = melt_cmd->cls;
     774           1 :     coin_cmd = TALER_TESTING_interpreter_lookup_command (rls->is,
     775             :                                                          rms->coin_reference);
     776           1 :     if (NULL == coin_cmd)
     777             :     {
     778           0 :       GNUNET_break (0);
     779           0 :       TALER_TESTING_interpreter_fail (rls->is);
     780           0 :       return;
     781             :     }
     782             :   }
     783             : 
     784             :   const struct TALER_CoinSpendPrivateKeyP *coin_priv;
     785           1 :   if (GNUNET_OK != TALER_TESTING_get_trait_coin_priv
     786             :         (coin_cmd, 0, &coin_priv))
     787             :   {
     788           0 :     GNUNET_break (0);
     789           0 :     TALER_TESTING_interpreter_fail (rls->is);
     790           0 :     return;
     791             :   }
     792             : 
     793             :   /* finally, use private key from withdraw sign command */
     794           1 :   rls->rlh = TALER_EXCHANGE_link (is->exchange,
     795             :                                   coin_priv,
     796             :                                   &link_cb,
     797             :                                   rls);
     798             : 
     799           1 :   if (NULL == rls->rlh)
     800             :   {
     801           0 :     GNUNET_break (0);
     802           0 :     TALER_TESTING_interpreter_fail (rls->is);
     803           0 :     return;
     804             :   }
     805             : }
     806             : 
     807             : 
     808             : /**
     809             :  * Free the state of the "refresh link" CMD, and possibly
     810             :  * cancel a operation thereof.
     811             :  *
     812             :  * @param cls closure
     813             :  * @param cmd the command which is being cleaned up.
     814             :  */
     815             : static void
     816           1 : refresh_link_cleanup (void *cls,
     817             :                       const struct TALER_TESTING_Command *cmd)
     818             : {
     819           1 :   struct RefreshLinkState *rls = cls;
     820             : 
     821           1 :   if (NULL != rls->rlh)
     822             :   {
     823             : 
     824           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     825             :                 "Command %u (%s) did not complete\n",
     826             :                 rls->is->ip,
     827             :                 cmd->label);
     828           0 :     TALER_EXCHANGE_link_cancel (rls->rlh);
     829           0 :     rls->rlh = NULL;
     830             :   }
     831           1 :   if (NULL != rls->retry_task)
     832             :   {
     833           0 :     GNUNET_SCHEDULER_cancel (rls->retry_task);
     834           0 :     rls->retry_task = NULL;
     835             :   }
     836           1 :   GNUNET_free (rls);
     837           1 : }
     838             : 
     839             : 
     840             : /**
     841             :  * Run the command.
     842             :  *
     843             :  * @param cls closure.
     844             :  * @param cmd the command to execute.
     845             :  * @param is the interpreter state.
     846             :  */
     847             : static void
     848             : melt_run (void *cls,
     849             :           const struct TALER_TESTING_Command *cmd,
     850             :           struct TALER_TESTING_Interpreter *is);
     851             : 
     852             : 
     853             : /**
     854             :  * Task scheduled to re-try #melt_run.
     855             :  *
     856             :  * @param cls a `struct RefreshMeltState`
     857             :  */
     858             : static void
     859           0 : do_melt_retry (void *cls)
     860             : {
     861           0 :   struct RefreshMeltState *rms = cls;
     862             : 
     863           0 :   rms->retry_task = NULL;
     864           0 :   rms->is->commands[rms->is->ip].last_req_time
     865           0 :     = GNUNET_TIME_absolute_get ();
     866           0 :   melt_run (rms,
     867             :             NULL,
     868             :             rms->is);
     869           0 : }
     870             : 
     871             : 
     872             : /**
     873             :  * Callback for a "refresh melt" operation; checks if the HTTP
     874             :  * response code is okay and re-run the melt operation if the
     875             :  * CMD was set to do so.
     876             :  *
     877             :  * @param cls closure.
     878             :  * @param hr HTTP response details
     879             :  * @param noreveal_index choice by the exchange in the
     880             :  *        cut-and-choose protocol, UINT16_MAX on error.
     881             :  * @param exchange_pub public key the exchange used for signing.
     882             :  */
     883             : static void
     884          21 : melt_cb (void *cls,
     885             :          const struct TALER_EXCHANGE_HttpResponse *hr,
     886             :          uint32_t noreveal_index,
     887             :          const struct TALER_ExchangePublicKeyP *exchange_pub)
     888             : {
     889          21 :   struct RefreshMeltState *rms = cls;
     890             : 
     891          21 :   rms->rmh = NULL;
     892          21 :   if (rms->expected_response_code != hr->http_status)
     893             :   {
     894           0 :     if (0 != rms->do_retry)
     895             :     {
     896           0 :       rms->do_retry--;
     897           0 :       if ( (0 == hr->http_status) ||
     898           0 :            (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec) ||
     899           0 :            (MHD_HTTP_INTERNAL_SERVER_ERROR == hr->http_status) )
     900             :       {
     901           0 :         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     902             :                     "Retrying refresh melt failed with %u/%d\n",
     903             :                     hr->http_status,
     904             :                     (int) hr->ec);
     905             :         /* on DB conflicts, do not use backoff */
     906           0 :         if (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec)
     907           0 :           rms->backoff = GNUNET_TIME_UNIT_ZERO;
     908             :         else
     909           0 :           rms->backoff = GNUNET_TIME_randomized_backoff (rms->backoff,
     910             :                                                          MAX_BACKOFF);
     911           0 :         rms->total_backoff = GNUNET_TIME_relative_add (rms->total_backoff,
     912             :                                                        rms->backoff);
     913           0 :         rms->is->commands[rms->is->ip].num_tries++;
     914           0 :         rms->retry_task = GNUNET_SCHEDULER_add_delayed (rms->backoff,
     915             :                                                         &do_melt_retry,
     916             :                                                         rms);
     917           0 :         return;
     918             :       }
     919             :     }
     920           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     921             :                 "Unexpected response code %u/%d to command %s in %s:%u\n",
     922             :                 hr->http_status,
     923             :                 (int) hr->ec,
     924             :                 rms->is->commands[rms->is->ip].label,
     925             :                 __FILE__,
     926             :                 __LINE__);
     927           0 :     json_dumpf (hr->reply,
     928             :                 stderr,
     929             :                 0);
     930           0 :     TALER_TESTING_interpreter_fail (rms->is);
     931           0 :     return;
     932             :   }
     933          21 :   rms->noreveal_index = noreveal_index;
     934          21 :   if (0 != rms->total_backoff.rel_value_us)
     935             :   {
     936           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     937             :                 "Total melt backoff for %s was %s\n",
     938             :                 rms->is->commands[rms->is->ip].label,
     939             :                 GNUNET_STRINGS_relative_time_to_string (rms->total_backoff,
     940             :                                                         GNUNET_YES));
     941             :   }
     942          21 :   if (GNUNET_YES == rms->double_melt)
     943             :   {
     944           2 :     TALER_LOG_DEBUG ("Doubling the melt (%s)\n",
     945             :                      rms->is->commands[rms->is->ip].label);
     946           4 :     rms->rmh = TALER_EXCHANGE_melt (rms->is->exchange,
     947             :                                     rms->refresh_data_length,
     948           2 :                                     rms->refresh_data,
     949             :                                     &melt_cb,
     950             :                                     rms);
     951           2 :     rms->double_melt = GNUNET_NO;
     952           2 :     return;
     953             :   }
     954          19 :   TALER_TESTING_interpreter_next (rms->is);
     955             : }
     956             : 
     957             : 
     958             : /**
     959             :  * Run the command.
     960             :  *
     961             :  * @param cls closure.
     962             :  * @param cmd the command to execute.
     963             :  * @param is the interpreter state.
     964             :  */
     965             : static void
     966          19 : melt_run (void *cls,
     967             :           const struct TALER_TESTING_Command *cmd,
     968             :           struct TALER_TESTING_Interpreter *is)
     969             : {
     970          19 :   struct RefreshMeltState *rms = cls;
     971             :   unsigned int num_fresh_coins;
     972          19 :   const char *default_melt_fresh_amounts[] = {
     973             :     "EUR:1", "EUR:1", "EUR:1", "EUR:0.1",
     974             :     NULL
     975             :   };
     976             :   const char **melt_fresh_amounts;
     977             : 
     978          19 :   if (NULL == (melt_fresh_amounts = rms->melt_fresh_amounts))
     979           7 :     melt_fresh_amounts = default_melt_fresh_amounts;
     980          19 :   rms->is = is;
     981          19 :   rms->noreveal_index = UINT16_MAX;
     982          19 :   for (num_fresh_coins = 0;
     983          59 :        NULL != melt_fresh_amounts[num_fresh_coins];
     984          40 :        num_fresh_coins++)
     985             :     ;
     986          19 :   rms->num_fresh_coins = num_fresh_coins;
     987          19 :   rms->fresh_pks = GNUNET_new_array
     988             :                      (num_fresh_coins,
     989             :                      struct TALER_EXCHANGE_DenomPublicKey);
     990             :   {
     991             :     struct TALER_Amount melt_amount;
     992             :     struct TALER_Amount fresh_amount;
     993             :     const struct TALER_DenominationSignature *melt_sig;
     994             :     const struct TALER_EXCHANGE_DenomPublicKey *melt_denom_pub;
     995             :     const struct TALER_TESTING_Command *coin_command;
     996             : 
     997          19 :     if (NULL == (coin_command
     998          19 :                    = TALER_TESTING_interpreter_lookup_command
     999             :                        (is, rms->coin_reference)))
    1000             :     {
    1001           0 :       GNUNET_break (0);
    1002           0 :       TALER_TESTING_interpreter_fail (rms->is);
    1003           0 :       return;
    1004             :     }
    1005             : 
    1006          19 :     if (GNUNET_OK != TALER_TESTING_get_trait_coin_priv
    1007             :           (coin_command, 0, &rms->melt_priv))
    1008             :     {
    1009           0 :       GNUNET_break (0);
    1010           0 :       TALER_TESTING_interpreter_fail (rms->is);
    1011           0 :       return;
    1012             :     }
    1013             : 
    1014          19 :     if (GNUNET_OK !=
    1015          19 :         TALER_TESTING_get_trait_denom_sig (coin_command,
    1016             :                                            0,
    1017             :                                            &melt_sig))
    1018             :     {
    1019           0 :       GNUNET_break (0);
    1020           0 :       TALER_TESTING_interpreter_fail (rms->is);
    1021           0 :       return;
    1022             :     }
    1023          19 :     if (GNUNET_OK != TALER_TESTING_get_trait_denom_pub
    1024             :           (coin_command, 0, &melt_denom_pub))
    1025             :     {
    1026           0 :       GNUNET_break (0);
    1027           0 :       TALER_TESTING_interpreter_fail (rms->is);
    1028           0 :       return;
    1029             :     }
    1030             :     /* Melt amount starts with the melt fee of the old coin; we'll add the
    1031             :        values and withdraw fees of the fresh coins next */
    1032          19 :     melt_amount = melt_denom_pub->fee_refresh;
    1033          59 :     for (unsigned int i = 0; i<num_fresh_coins; i++)
    1034             :     {
    1035             :       const struct TALER_EXCHANGE_DenomPublicKey *fresh_pk;
    1036             : 
    1037          40 :       if (GNUNET_OK != TALER_string_to_amount
    1038          40 :             (melt_fresh_amounts[i], &fresh_amount))
    1039             :       {
    1040           0 :         GNUNET_break (0);
    1041           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    1042             :                     "Failed to parse amount `%s' at index %u\n",
    1043             :                     melt_fresh_amounts[i], i);
    1044           0 :         TALER_TESTING_interpreter_fail (rms->is);
    1045           0 :         return;
    1046             :       }
    1047          40 :       fresh_pk = TALER_TESTING_find_pk
    1048             :                    (TALER_EXCHANGE_get_keys (is->exchange), &fresh_amount);
    1049          40 :       if (NULL == fresh_pk)
    1050             :       {
    1051           0 :         GNUNET_break (0);
    1052             :         /* Subroutine logs specific error */
    1053           0 :         TALER_TESTING_interpreter_fail (rms->is);
    1054           0 :         return;
    1055             :       }
    1056          40 :       GNUNET_assert (0 <=
    1057             :                      TALER_amount_add (&melt_amount,
    1058             :                                        &melt_amount,
    1059             :                                        &fresh_amount));
    1060          40 :       GNUNET_assert (0 <=
    1061             :                      TALER_amount_add (&melt_amount,
    1062             :                                        &melt_amount,
    1063             :                                        &fresh_pk->fee_withdraw));
    1064          40 :       rms->fresh_pks[i] = *fresh_pk;
    1065             :       /* Make a deep copy of the RSA key */
    1066          40 :       rms->fresh_pks[i].key.rsa_public_key
    1067          40 :         = GNUNET_CRYPTO_rsa_public_key_dup (fresh_pk->key.rsa_public_key);
    1068             :     }
    1069             :     rms->refresh_data
    1070          38 :       = TALER_EXCHANGE_refresh_prepare (rms->melt_priv,
    1071             :                                         &melt_amount,
    1072             :                                         melt_sig,
    1073             :                                         melt_denom_pub,
    1074             :                                         num_fresh_coins,
    1075          19 :                                         rms->fresh_pks,
    1076             :                                         &rms->refresh_data_length);
    1077             : 
    1078          19 :     if (NULL == rms->refresh_data)
    1079             :     {
    1080           0 :       GNUNET_break (0);
    1081           0 :       TALER_TESTING_interpreter_fail (rms->is);
    1082           0 :       return;
    1083             :     }
    1084          38 :     rms->rmh = TALER_EXCHANGE_melt (is->exchange,
    1085             :                                     rms->refresh_data_length,
    1086          19 :                                     rms->refresh_data,
    1087             :                                     &melt_cb,
    1088             :                                     rms);
    1089             : 
    1090          19 :     if (NULL == rms->rmh)
    1091             :     {
    1092           0 :       GNUNET_break (0);
    1093           0 :       TALER_TESTING_interpreter_fail (rms->is);
    1094           0 :       return;
    1095             :     }
    1096             :   }
    1097             : }
    1098             : 
    1099             : 
    1100             : /**
    1101             :  * Free the "refresh melt" CMD state, and possibly cancel a
    1102             :  * pending operation thereof.
    1103             :  *
    1104             :  * @param cls closure, must be a `struct RefreshMeltState`.
    1105             :  * @param cmd the command which is being cleaned up.
    1106             :  */
    1107             : static void
    1108          19 : melt_cleanup (void *cls,
    1109             :               const struct TALER_TESTING_Command *cmd)
    1110             : {
    1111          19 :   struct RefreshMeltState *rms = cls;
    1112             : 
    1113          19 :   if (NULL != rms->rmh)
    1114             :   {
    1115           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    1116             :                 "Command %u (%s) did not complete\n",
    1117             :                 rms->is->ip, rms->is->commands[rms->is->ip].label);
    1118           0 :     TALER_EXCHANGE_melt_cancel (rms->rmh);
    1119           0 :     rms->rmh = NULL;
    1120             :   }
    1121          19 :   if (NULL != rms->retry_task)
    1122             :   {
    1123           0 :     GNUNET_SCHEDULER_cancel (rms->retry_task);
    1124           0 :     rms->retry_task = NULL;
    1125             :   }
    1126          19 :   if (NULL != rms->fresh_pks)
    1127             :   {
    1128          59 :     for (unsigned int i = 0; i < rms->num_fresh_coins; i++)
    1129          40 :       GNUNET_CRYPTO_rsa_public_key_free (rms->fresh_pks[i].key.rsa_public_key);
    1130             :   }
    1131          19 :   GNUNET_free (rms->fresh_pks);
    1132          19 :   rms->fresh_pks = NULL;
    1133          19 :   GNUNET_free (rms->refresh_data);
    1134          19 :   rms->refresh_data = NULL;
    1135          19 :   rms->refresh_data_length = 0;
    1136          19 :   GNUNET_free (rms->melt_fresh_amounts);
    1137          19 :   GNUNET_free (rms);
    1138          19 : }
    1139             : 
    1140             : 
    1141             : /**
    1142             :  * Offer internal data to the "refresh melt" CMD.
    1143             :  *
    1144             :  * @param cls closure.
    1145             :  * @param[out] ret result (could be anything).
    1146             :  * @param trait name of the trait.
    1147             :  * @param index index number of the object to offer.
    1148             :  * @return #GNUNET_OK on success.
    1149             :  */
    1150             : static int
    1151         111 : melt_traits (void *cls,
    1152             :              const void **ret,
    1153             :              const char *trait,
    1154             :              unsigned int index)
    1155             : {
    1156         111 :   struct RefreshMeltState *rms = cls;
    1157             : 
    1158         111 :   if (index >= rms->num_fresh_coins)
    1159             :   {
    1160           0 :     GNUNET_break (0);
    1161           0 :     return GNUNET_SYSERR;
    1162             :   }
    1163             :   {
    1164             :     struct TALER_TESTING_Trait traits[] = {
    1165         111 :       TALER_TESTING_make_trait_denom_pub (index, &rms->fresh_pks[index]),
    1166         111 :       TALER_TESTING_make_trait_coin_priv (0, rms->melt_priv),
    1167         111 :       TALER_TESTING_trait_end ()
    1168             :     };
    1169             : 
    1170         111 :     return TALER_TESTING_get_trait (traits,
    1171             :                                     ret,
    1172             :                                     trait,
    1173             :                                     index);
    1174             :   }
    1175             : }
    1176             : 
    1177             : 
    1178             : /**
    1179             :  * Parse list of amounts for melt operation.
    1180             :  *
    1181             :  * @param[in,out] rms where to store the list
    1182             :  * @param ap NULL-termianted list of amounts to be melted (one per fresh coin)
    1183             :  * @return #GNUNET_OK on success
    1184             :  */
    1185             : static int
    1186          19 : parse_amounts (struct RefreshMeltState *rms,
    1187             :                va_list ap)
    1188             : {
    1189             :   unsigned int len;
    1190             :   unsigned int off;
    1191             :   const char *amount;
    1192             : 
    1193          19 :   len = 0;
    1194          19 :   off = 0;
    1195          31 :   while (NULL != (amount = va_arg (ap, const char *)))
    1196             :   {
    1197          12 :     if (len == off)
    1198             :     {
    1199             :       struct TALER_Amount a;
    1200             : 
    1201          12 :       GNUNET_array_grow (rms->melt_fresh_amounts,
    1202             :                          len,
    1203             :                          off + 16);
    1204          12 :       if (GNUNET_OK !=
    1205          12 :           TALER_string_to_amount (amount, &a))
    1206             :       {
    1207           0 :         GNUNET_break (0);
    1208           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    1209             :                     "Failed to parse amount `%s' at index %u\n",
    1210             :                     amount, off);
    1211           0 :         GNUNET_free (rms->melt_fresh_amounts);
    1212           0 :         rms->melt_fresh_amounts = NULL;
    1213           0 :         return GNUNET_SYSERR;
    1214             :       }
    1215          12 :       rms->melt_fresh_amounts[off++] = amount;
    1216             :     }
    1217             :   }
    1218          19 :   if (0 == off)
    1219           7 :     return GNUNET_OK; /* no amounts given == use defaults! */
    1220             :   /* ensure NULL-termination */
    1221          12 :   GNUNET_array_grow (rms->melt_fresh_amounts,
    1222             :                      len,
    1223             :                      off + 1);
    1224          12 :   return GNUNET_OK;
    1225             : }
    1226             : 
    1227             : 
    1228             : /**
    1229             :  * Create a "refresh melt" command.
    1230             :  *
    1231             :  * @param label command label.
    1232             :  * @param coin_reference reference to a command
    1233             :  *        that will provide a coin to refresh.
    1234             :  * @param expected_response_code expected HTTP code.
    1235             :  * @param ... NULL-terminated list of amounts to be melted
    1236             :  * @return the command.
    1237             :  */
    1238             : struct TALER_TESTING_Command
    1239          17 : TALER_TESTING_cmd_melt (const char *label,
    1240             :                         const char *coin_reference,
    1241             :                         unsigned int expected_response_code,
    1242             :                         ...)
    1243             : {
    1244             :   struct RefreshMeltState *rms;
    1245             :   va_list ap;
    1246             : 
    1247          17 :   rms = GNUNET_new (struct RefreshMeltState);
    1248          17 :   rms->coin_reference = coin_reference;
    1249          17 :   rms->expected_response_code = expected_response_code;
    1250          17 :   va_start (ap, expected_response_code);
    1251          17 :   GNUNET_assert (GNUNET_OK ==
    1252             :                  parse_amounts (rms, ap));
    1253          17 :   va_end (ap);
    1254             :   {
    1255          17 :     struct TALER_TESTING_Command cmd = {
    1256             :       .label = label,
    1257             :       .cls = rms,
    1258             :       .run = &melt_run,
    1259             :       .cleanup = &melt_cleanup,
    1260             :       .traits = &melt_traits
    1261             :     };
    1262             : 
    1263          17 :     return cmd;
    1264             :   }
    1265             : }
    1266             : 
    1267             : 
    1268             : /**
    1269             :  * Create a "refresh melt" CMD that does TWO /refresh/melt
    1270             :  * requests.  This was needed to test the replay of a valid melt
    1271             :  * request, see #5312.
    1272             :  *
    1273             :  * @param label command label
    1274             :  * @param coin_reference reference to a command that will provide
    1275             :  *        a coin to refresh
    1276             :  * @param expected_response_code expected HTTP code
    1277             :  * @param ... NULL-terminated list of amounts to be melted
    1278             :  * @return the command.
    1279             :  */
    1280             : struct TALER_TESTING_Command
    1281           2 : TALER_TESTING_cmd_melt_double (const char *label,
    1282             :                                const char *coin_reference,
    1283             :                                unsigned int expected_response_code,
    1284             :                                ...)
    1285             : {
    1286             :   struct RefreshMeltState *rms;
    1287             :   va_list ap;
    1288             : 
    1289           2 :   rms = GNUNET_new (struct RefreshMeltState);
    1290           2 :   rms->coin_reference = coin_reference;
    1291           2 :   rms->expected_response_code = expected_response_code;
    1292           2 :   rms->double_melt = GNUNET_YES;
    1293           2 :   va_start (ap, expected_response_code);
    1294           2 :   GNUNET_assert (GNUNET_OK ==
    1295             :                  parse_amounts (rms, ap));
    1296           2 :   va_end (ap);
    1297             :   {
    1298           2 :     struct TALER_TESTING_Command cmd = {
    1299             :       .label = label,
    1300             :       .cls = rms,
    1301             :       .run = &melt_run,
    1302             :       .cleanup = &melt_cleanup,
    1303             :       .traits = &melt_traits
    1304             :     };
    1305             : 
    1306           2 :     return cmd;
    1307             :   }
    1308             : }
    1309             : 
    1310             : 
    1311             : /**
    1312             :  * Modify a "refresh melt" command to enable retries.
    1313             :  *
    1314             :  * @param cmd command
    1315             :  * @return modified command.
    1316             :  */
    1317             : struct TALER_TESTING_Command
    1318           0 : TALER_TESTING_cmd_melt_with_retry (struct TALER_TESTING_Command cmd)
    1319             : {
    1320             :   struct RefreshMeltState *rms;
    1321             : 
    1322           0 :   GNUNET_assert (&melt_run == cmd.run);
    1323           0 :   rms = cmd.cls;
    1324           0 :   rms->do_retry = NUM_RETRIES;
    1325           0 :   return cmd;
    1326             : }
    1327             : 
    1328             : 
    1329             : /**
    1330             :  * Offer internal data from a "refresh reveal" CMD.
    1331             :  *
    1332             :  * @param cls closure.
    1333             :  * @param[out] ret result (could be anything).
    1334             :  * @param trait name of the trait.
    1335             :  * @param index index number of the object to offer.
    1336             :  * @return #GNUNET_OK on success.
    1337             :  */
    1338             : static int
    1339          92 : refresh_reveal_traits (void *cls,
    1340             :                        const void **ret,
    1341             :                        const char *trait,
    1342             :                        unsigned int index)
    1343          92 : {
    1344          92 :   struct RefreshRevealState *rrs = cls;
    1345          92 :   unsigned int num_coins = rrs->num_fresh_coins;
    1346             : #define NUM_TRAITS ((num_coins * 4) + 3)
    1347          92 :   struct TALER_TESTING_Trait traits[NUM_TRAITS];
    1348             : 
    1349             :   /* Making coin privs traits */
    1350         397 :   for (unsigned int i = 0; i<num_coins; i++)
    1351         305 :     traits[i] = TALER_TESTING_make_trait_coin_priv
    1352         305 :                   (i, &rrs->fresh_coins[i].coin_priv);
    1353             : 
    1354             :   /* Making denom pubs traits */
    1355         397 :   for (unsigned int i = 0; i<num_coins; i++)
    1356         305 :     traits[num_coins + i]
    1357         305 :       = TALER_TESTING_make_trait_denom_pub
    1358         305 :           (i, rrs->fresh_coins[i].pk);
    1359             : 
    1360             :   /* Making denom sigs traits */
    1361         397 :   for (unsigned int i = 0; i<num_coins; i++)
    1362         305 :     traits[(num_coins * 2) + i]
    1363         305 :       = TALER_TESTING_make_trait_denom_sig
    1364         305 :           (i, &rrs->fresh_coins[i].sig);
    1365             :   /* blinding key traits */
    1366         397 :   for (unsigned int i = 0; i<num_coins; i++)
    1367         305 :     traits[(num_coins * 3) + i]
    1368         305 :       = TALER_TESTING_make_trait_blinding_key (i,
    1369         305 :                                                &rrs->fresh_coins[i].blinding_key),
    1370             : 
    1371             :     /* number of fresh coins */
    1372         305 :     traits[(num_coins * 4)] = TALER_TESTING_make_trait_uint
    1373         305 :                                 (0, &rrs->num_fresh_coins);
    1374             : 
    1375             :   /* whole array of fresh coins */
    1376          92 :   traits[(num_coins * 4) + 1]
    1377          92 :     = TALER_TESTING_make_trait_fresh_coins (0, rrs->fresh_coins),
    1378             : 
    1379             :   /* end of traits */
    1380          92 :   traits[(num_coins * 4) + 2] = TALER_TESTING_trait_end ();
    1381             : 
    1382          92 :   return TALER_TESTING_get_trait (traits,
    1383             :                                   ret,
    1384             :                                   trait,
    1385             :                                   index);
    1386             : }
    1387             : 
    1388             : 
    1389             : /**
    1390             :  * Create a "refresh reveal" command.
    1391             :  *
    1392             :  * @param label command label.
    1393             :  * @param melt_reference reference to a "refresh melt" command.
    1394             :  * @param expected_response_code expected HTTP response code.
    1395             :  * @return the command.
    1396             :  */
    1397             : struct TALER_TESTING_Command
    1398           8 : TALER_TESTING_cmd_refresh_reveal (const char *label,
    1399             :                                   const char *melt_reference,
    1400             :                                   unsigned int expected_response_code)
    1401             : {
    1402             :   struct RefreshRevealState *rrs;
    1403             : 
    1404           8 :   rrs = GNUNET_new (struct RefreshRevealState);
    1405           8 :   rrs->melt_reference = melt_reference;
    1406           8 :   rrs->expected_response_code = expected_response_code;
    1407             :   {
    1408           8 :     struct TALER_TESTING_Command cmd = {
    1409             :       .cls = rrs,
    1410             :       .label = label,
    1411             :       .run = &refresh_reveal_run,
    1412             :       .cleanup = &refresh_reveal_cleanup,
    1413             :       .traits = &refresh_reveal_traits
    1414             :     };
    1415             : 
    1416           8 :     return cmd;
    1417             :   }
    1418             : }
    1419             : 
    1420             : 
    1421             : /**
    1422             :  * Modify a "refresh reveal" command to enable retries.
    1423             :  *
    1424             :  * @param cmd command
    1425             :  * @return modified command.
    1426             :  */
    1427             : struct TALER_TESTING_Command
    1428           0 : TALER_TESTING_cmd_refresh_reveal_with_retry (struct TALER_TESTING_Command cmd)
    1429             : {
    1430             :   struct RefreshRevealState *rrs;
    1431             : 
    1432           0 :   GNUNET_assert (&refresh_reveal_run == cmd.run);
    1433           0 :   rrs = cmd.cls;
    1434           0 :   rrs->do_retry = NUM_RETRIES;
    1435           0 :   return cmd;
    1436             : }
    1437             : 
    1438             : 
    1439             : /**
    1440             :  * Create a "refresh link" command.
    1441             :  *
    1442             :  * @param label command label.
    1443             :  * @param reveal_reference reference to a "refresh reveal" CMD.
    1444             :  * @param expected_response_code expected HTTP response code
    1445             :  * @return the "refresh link" command
    1446             :  */
    1447             : struct TALER_TESTING_Command
    1448           1 : TALER_TESTING_cmd_refresh_link (const char *label,
    1449             :                                 const char *reveal_reference,
    1450             :                                 unsigned int expected_response_code)
    1451             : {
    1452             :   struct RefreshLinkState *rrs;
    1453             : 
    1454           1 :   rrs = GNUNET_new (struct RefreshLinkState);
    1455           1 :   rrs->reveal_reference = reveal_reference;
    1456           1 :   rrs->expected_response_code = expected_response_code;
    1457             :   {
    1458           1 :     struct TALER_TESTING_Command cmd = {
    1459             :       .cls = rrs,
    1460             :       .label = label,
    1461             :       .run = &refresh_link_run,
    1462             :       .cleanup = &refresh_link_cleanup
    1463             :     };
    1464             : 
    1465           1 :     return cmd;
    1466             :   }
    1467             : }
    1468             : 
    1469             : 
    1470             : /**
    1471             :  * Modify a "refresh link" command to enable retries.
    1472             :  *
    1473             :  * @param cmd command
    1474             :  * @return modified command.
    1475             :  */
    1476             : struct TALER_TESTING_Command
    1477           0 : TALER_TESTING_cmd_refresh_link_with_retry (struct TALER_TESTING_Command cmd)
    1478             : {
    1479             :   struct RefreshLinkState *rls;
    1480             : 
    1481           0 :   GNUNET_assert (&refresh_link_run == cmd.run);
    1482           0 :   rls = cmd.cls;
    1483           0 :   rls->do_retry = NUM_RETRIES;
    1484           0 :   return cmd;
    1485             : }

Generated by: LCOV version 1.14