LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_deposit.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 139 206 67.5 %
Date: 2021-08-30 06:43:37 Functions: 7 9 77.8 %
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_deposit.c
      21             :  * @brief command for testing /deposit.
      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             : /**
      33             :  * How often do we retry before giving up?
      34             :  */
      35             : #define NUM_RETRIES 5
      36             : 
      37             : /**
      38             :  * How long do we wait AT MOST when retrying?
      39             :  */
      40             : #define MAX_BACKOFF GNUNET_TIME_relative_multiply ( \
      41             :     GNUNET_TIME_UNIT_MILLISECONDS, 100)
      42             : 
      43             : 
      44             : /**
      45             :  * State for a "deposit" CMD.
      46             :  */
      47             : struct DepositState
      48             : {
      49             : 
      50             :   /**
      51             :    * Amount to deposit.
      52             :    */
      53             :   struct TALER_Amount amount;
      54             : 
      55             :   /**
      56             :    * Deposit fee.
      57             :    */
      58             :   struct TALER_Amount deposit_fee;
      59             : 
      60             :   /**
      61             :    * Reference to any command that is able to provide a coin.
      62             :    */
      63             :   const char *coin_reference;
      64             : 
      65             :   /**
      66             :    * If @e coin_reference refers to an operation that generated
      67             :    * an array of coins, this value determines which coin to pick.
      68             :    */
      69             :   unsigned int coin_index;
      70             : 
      71             :   /**
      72             :    * Wire details of who is depositing -- this would be merchant
      73             :    * wire details in a normal scenario.
      74             :    */
      75             :   json_t *wire_details;
      76             : 
      77             :   /**
      78             :    * JSON string describing what a proposal is about.
      79             :    */
      80             :   json_t *contract_terms;
      81             : 
      82             :   /**
      83             :    * Refund deadline. Zero for no refunds.
      84             :    */
      85             :   struct GNUNET_TIME_Absolute refund_deadline;
      86             : 
      87             :   /**
      88             :    * Set (by the interpreter) to a fresh private key.  This
      89             :    * key will be used to sign the deposit request.
      90             :    */
      91             :   struct TALER_MerchantPrivateKeyP merchant_priv;
      92             : 
      93             :   /**
      94             :    * Deposit handle while operation is running.
      95             :    */
      96             :   struct TALER_EXCHANGE_DepositHandle *dh;
      97             : 
      98             :   /**
      99             :    * Timestamp of the /deposit operation in the wallet (contract signing time).
     100             :    */
     101             :   struct GNUNET_TIME_Absolute wallet_timestamp;
     102             : 
     103             :   /**
     104             :    * Interpreter state.
     105             :    */
     106             :   struct TALER_TESTING_Interpreter *is;
     107             : 
     108             :   /**
     109             :    * Task scheduled to try later.
     110             :    */
     111             :   struct GNUNET_SCHEDULER_Task *retry_task;
     112             : 
     113             :   /**
     114             :    * How long do we wait until we retry?
     115             :    */
     116             :   struct GNUNET_TIME_Relative backoff;
     117             : 
     118             :   /**
     119             :    * Expected HTTP response code.
     120             :    */
     121             :   unsigned int expected_response_code;
     122             : 
     123             :   /**
     124             :    * How often should we retry on (transient) failures?
     125             :    */
     126             :   unsigned int do_retry;
     127             : 
     128             :   /**
     129             :    * Set to #GNUNET_YES if the /deposit succeeded
     130             :    * and we now can provide the resulting traits.
     131             :    */
     132             :   int deposit_succeeded;
     133             : 
     134             :   /**
     135             :    * When did the exchange receive the deposit?
     136             :    */
     137             :   struct GNUNET_TIME_Absolute exchange_timestamp;
     138             : 
     139             :   /**
     140             :    * Signing key used by the exchange to sign the
     141             :    * deposit confirmation.
     142             :    */
     143             :   struct TALER_ExchangePublicKeyP exchange_pub;
     144             : 
     145             :   /**
     146             :    * Signature from the exchange on the
     147             :    * deposit confirmation.
     148             :    */
     149             :   struct TALER_ExchangeSignatureP exchange_sig;
     150             : 
     151             :   /**
     152             :    * Reference to previous deposit operation.
     153             :    * Only present if we're supposed to replay the previous deposit.
     154             :    */
     155             :   const char *deposit_reference;
     156             : 
     157             :   /**
     158             :    * Did we set the parameters for this deposit command?
     159             :    *
     160             :    * When we're referencing another deposit operation,
     161             :    * this will only be set after the command has been started.
     162             :    */
     163             :   int command_initialized;
     164             : 
     165             :   /**
     166             :    * Reference to fetch the merchant private key from.
     167             :    * If NULL, we generate our own, fresh merchant key.
     168             :    */
     169             :   const char *merchant_priv_reference;
     170             : };
     171             : 
     172             : 
     173             : /**
     174             :  * Run the command.
     175             :  *
     176             :  * @param cls closure.
     177             :  * @param cmd the command to execute.
     178             :  * @param is the interpreter state.
     179             :  */
     180             : static void
     181             : deposit_run (void *cls,
     182             :              const struct TALER_TESTING_Command *cmd,
     183             :              struct TALER_TESTING_Interpreter *is);
     184             : 
     185             : 
     186             : /**
     187             :  * Task scheduled to re-try #deposit_run.
     188             :  *
     189             :  * @param cls a `struct DepositState`
     190             :  */
     191             : static void
     192           0 : do_retry (void *cls)
     193             : {
     194           0 :   struct DepositState *ds = cls;
     195             : 
     196           0 :   ds->retry_task = NULL;
     197           0 :   ds->is->commands[ds->is->ip].last_req_time
     198           0 :     = GNUNET_TIME_absolute_get ();
     199           0 :   deposit_run (ds,
     200             :                NULL,
     201             :                ds->is);
     202           0 : }
     203             : 
     204             : 
     205             : /**
     206             :  * Callback to analyze the /deposit response, just used to
     207             :  * check if the response code is acceptable.
     208             :  *
     209             :  * @param cls closure.
     210             :  * @param hr HTTP response details
     211             :  * @param exchange_timestamp when did the exchange receive the deposit permission
     212             :  * @param exchange_sig signature provided by the exchange
     213             :  *        (NULL on errors)
     214             :  * @param exchange_pub public key of the exchange,
     215             :  *        used for signing the response.
     216             :  */
     217             : static void
     218          41 : deposit_cb (void *cls,
     219             :             const struct TALER_EXCHANGE_HttpResponse *hr,
     220             :             const struct GNUNET_TIME_Absolute exchange_timestamp,
     221             :             const struct TALER_ExchangeSignatureP *exchange_sig,
     222             :             const struct TALER_ExchangePublicKeyP *exchange_pub)
     223             : {
     224          41 :   struct DepositState *ds = cls;
     225             : 
     226          41 :   ds->dh = NULL;
     227          41 :   if (ds->expected_response_code != hr->http_status)
     228             :   {
     229           0 :     if (0 != ds->do_retry)
     230             :     {
     231           0 :       ds->do_retry--;
     232           0 :       if ( (0 == hr->http_status) ||
     233           0 :            (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec) ||
     234           0 :            (MHD_HTTP_INTERNAL_SERVER_ERROR == hr->http_status) )
     235             :       {
     236           0 :         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     237             :                     "Retrying deposit failed with %u/%d\n",
     238             :                     hr->http_status,
     239             :                     (int) hr->ec);
     240             :         /* on DB conflicts, do not use backoff */
     241           0 :         if (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec)
     242           0 :           ds->backoff = GNUNET_TIME_UNIT_ZERO;
     243             :         else
     244           0 :           ds->backoff = GNUNET_TIME_randomized_backoff (ds->backoff,
     245             :                                                         MAX_BACKOFF);
     246           0 :         ds->is->commands[ds->is->ip].num_tries++;
     247             :         ds->retry_task
     248           0 :           = GNUNET_SCHEDULER_add_delayed (ds->backoff,
     249             :                                           &do_retry,
     250             :                                           ds);
     251           0 :         return;
     252             :       }
     253             :     }
     254           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     255             :                 "Unexpected response code %u to command %s in %s:%u\n",
     256             :                 hr->http_status,
     257             :                 ds->is->commands[ds->is->ip].label,
     258             :                 __FILE__,
     259             :                 __LINE__);
     260           0 :     json_dumpf (hr->reply,
     261             :                 stderr,
     262             :                 0);
     263           0 :     TALER_TESTING_interpreter_fail (ds->is);
     264           0 :     return;
     265             :   }
     266          41 :   if (MHD_HTTP_OK == hr->http_status)
     267             :   {
     268          33 :     ds->deposit_succeeded = GNUNET_YES;
     269          33 :     ds->exchange_timestamp = exchange_timestamp;
     270          33 :     ds->exchange_pub = *exchange_pub;
     271          33 :     ds->exchange_sig = *exchange_sig;
     272             :   }
     273          41 :   TALER_TESTING_interpreter_next (ds->is);
     274             : }
     275             : 
     276             : 
     277             : /**
     278             :  * Run the command.
     279             :  *
     280             :  * @param cls closure.
     281             :  * @param cmd the command to execute.
     282             :  * @param is the interpreter state.
     283             :  */
     284             : static void
     285          41 : deposit_run (void *cls,
     286             :              const struct TALER_TESTING_Command *cmd,
     287             :              struct TALER_TESTING_Interpreter *is)
     288             : {
     289          41 :   struct DepositState *ds = cls;
     290             :   const struct TALER_TESTING_Command *coin_cmd;
     291             :   const struct TALER_CoinSpendPrivateKeyP *coin_priv;
     292             :   struct TALER_CoinSpendPublicKeyP coin_pub;
     293             :   const struct TALER_EXCHANGE_DenomPublicKey *denom_pub;
     294             :   const struct TALER_DenominationSignature *denom_pub_sig;
     295             :   struct TALER_CoinSpendSignatureP coin_sig;
     296             :   struct GNUNET_TIME_Absolute wire_deadline;
     297             :   struct TALER_MerchantPublicKeyP merchant_pub;
     298             :   struct GNUNET_HashCode h_contract_terms;
     299             :   enum TALER_ErrorCode ec;
     300             : 
     301             :   (void) cmd;
     302          41 :   ds->is = is;
     303          41 :   if (NULL != ds->deposit_reference)
     304             :   {
     305             :     /* We're copying another deposit operation, initialize here. */
     306             :     const struct TALER_TESTING_Command *cmd;
     307             :     struct DepositState *ods;
     308             : 
     309           1 :     cmd = TALER_TESTING_interpreter_lookup_command (is,
     310             :                                                     ds->deposit_reference);
     311           1 :     if (NULL == cmd)
     312             :     {
     313           0 :       GNUNET_break (0);
     314           0 :       TALER_TESTING_interpreter_fail (is);
     315           0 :       return;
     316             :     }
     317           1 :     ods = cmd->cls;
     318           1 :     ds->coin_reference = ods->coin_reference;
     319           1 :     ds->coin_index = ods->coin_index;
     320           1 :     ds->wire_details = json_incref (ods->wire_details);
     321           1 :     ds->contract_terms = json_incref (ods->contract_terms);
     322           1 :     ds->wallet_timestamp = ods->wallet_timestamp;
     323           1 :     ds->refund_deadline = ods->refund_deadline;
     324           1 :     ds->amount = ods->amount;
     325           1 :     ds->merchant_priv = ods->merchant_priv;
     326           1 :     ds->command_initialized = GNUNET_YES;
     327             :   }
     328          40 :   else if (NULL != ds->merchant_priv_reference)
     329             :   {
     330             :     /* We're copying the merchant key from another deposit operation */
     331             :     const struct TALER_MerchantPrivateKeyP *merchant_priv;
     332             :     const struct TALER_TESTING_Command *cmd;
     333             : 
     334           1 :     cmd = TALER_TESTING_interpreter_lookup_command (is,
     335             :                                                     ds->merchant_priv_reference);
     336           1 :     if (NULL == cmd)
     337             :     {
     338           0 :       GNUNET_break (0);
     339           0 :       TALER_TESTING_interpreter_fail (is);
     340           0 :       return;
     341             :     }
     342           1 :     if ( (GNUNET_OK !=
     343           1 :           TALER_TESTING_get_trait_merchant_priv (cmd,
     344             :                                                  0,
     345             :                                                  &merchant_priv)) )
     346             :     {
     347           0 :       GNUNET_break (0);
     348           0 :       TALER_TESTING_interpreter_fail (is);
     349           0 :       return;
     350             :     }
     351           1 :     ds->merchant_priv = *merchant_priv;
     352             :   }
     353          41 :   GNUNET_assert (ds->coin_reference);
     354          41 :   coin_cmd = TALER_TESTING_interpreter_lookup_command (is,
     355             :                                                        ds->coin_reference);
     356          41 :   if (NULL == coin_cmd)
     357             :   {
     358           0 :     GNUNET_break (0);
     359           0 :     TALER_TESTING_interpreter_fail (is);
     360           0 :     return;
     361             :   }
     362             : 
     363          41 :   if ( (GNUNET_OK !=
     364          41 :         TALER_TESTING_get_trait_coin_priv (coin_cmd,
     365             :                                            ds->coin_index,
     366          41 :                                            &coin_priv)) ||
     367             :        (GNUNET_OK !=
     368          41 :         TALER_TESTING_get_trait_denom_pub (coin_cmd,
     369             :                                            ds->coin_index,
     370          41 :                                            &denom_pub)) ||
     371             :        (GNUNET_OK !=
     372          41 :         TALER_TESTING_get_trait_denom_sig (coin_cmd,
     373             :                                            ds->coin_index,
     374          41 :                                            &denom_pub_sig)) ||
     375             :        (GNUNET_OK !=
     376          41 :         TALER_JSON_contract_hash (ds->contract_terms,
     377             :                                   &h_contract_terms)) )
     378             :   {
     379           0 :     GNUNET_break (0);
     380           0 :     TALER_TESTING_interpreter_fail (is);
     381           0 :     return;
     382             :   }
     383          41 :   ds->deposit_fee = denom_pub->fee_deposit;
     384          41 :   GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv->eddsa_priv,
     385             :                                       &coin_pub.eddsa_pub);
     386             : 
     387          41 :   if (0 != ds->refund_deadline.abs_value_us)
     388             :   {
     389             :     struct GNUNET_TIME_Relative refund_deadline;
     390             : 
     391           8 :     refund_deadline = GNUNET_TIME_absolute_get_remaining (ds->refund_deadline);
     392           8 :     wire_deadline = GNUNET_TIME_relative_to_absolute
     393             :                       (GNUNET_TIME_relative_multiply (refund_deadline, 2));
     394             :   }
     395             :   else
     396             :   {
     397          33 :     ds->refund_deadline = ds->wallet_timestamp;
     398          33 :     wire_deadline = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_ZERO);
     399             :   }
     400          41 :   GNUNET_CRYPTO_eddsa_key_get_public (&ds->merchant_priv.eddsa_priv,
     401             :                                       &merchant_pub.eddsa_pub);
     402          41 :   (void) GNUNET_TIME_round_abs (&wire_deadline);
     403             :   {
     404             :     struct GNUNET_HashCode h_wire;
     405             : 
     406          41 :     GNUNET_assert (GNUNET_OK ==
     407             :                    TALER_JSON_merchant_wire_signature_hash (ds->wire_details,
     408             :                                                             &h_wire));
     409          41 :     TALER_EXCHANGE_deposit_permission_sign (&ds->amount,
     410          41 :                                             &denom_pub->fee_deposit,
     411             :                                             &h_wire,
     412             :                                             &h_contract_terms,
     413          41 :                                             &denom_pub->h_key,
     414             :                                             coin_priv,
     415             :                                             ds->wallet_timestamp,
     416             :                                             &merchant_pub,
     417             :                                             ds->refund_deadline,
     418             :                                             &coin_sig);
     419             :   }
     420          82 :   ds->dh = TALER_EXCHANGE_deposit (is->exchange,
     421          41 :                                    &ds->amount,
     422             :                                    wire_deadline,
     423             :                                    ds->wire_details,
     424             :                                    &h_contract_terms,
     425             :                                    &coin_pub,
     426             :                                    denom_pub_sig,
     427          41 :                                    &denom_pub->key,
     428             :                                    ds->wallet_timestamp,
     429             :                                    &merchant_pub,
     430             :                                    ds->refund_deadline,
     431             :                                    &coin_sig,
     432             :                                    &deposit_cb,
     433             :                                    ds,
     434             :                                    &ec);
     435          41 :   if (NULL == ds->dh)
     436             :   {
     437           0 :     GNUNET_break (0);
     438           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     439             :                 "Could not create deposit with EC %d\n",
     440             :                 (int) ec);
     441           0 :     TALER_TESTING_interpreter_fail (is);
     442           0 :     return;
     443             :   }
     444             : }
     445             : 
     446             : 
     447             : /**
     448             :  * Free the state of a "deposit" CMD, and possibly cancel a
     449             :  * pending operation thereof.
     450             :  *
     451             :  * @param cls closure, must be a `struct DepositState`.
     452             :  * @param cmd the command which is being cleaned up.
     453             :  */
     454             : static void
     455          41 : deposit_cleanup (void *cls,
     456             :                  const struct TALER_TESTING_Command *cmd)
     457             : {
     458          41 :   struct DepositState *ds = cls;
     459             : 
     460          41 :   if (NULL != ds->dh)
     461             :   {
     462           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     463             :                 "Command %u (%s) did not complete\n",
     464             :                 ds->is->ip,
     465             :                 cmd->label);
     466           0 :     TALER_EXCHANGE_deposit_cancel (ds->dh);
     467           0 :     ds->dh = NULL;
     468             :   }
     469          41 :   if (NULL != ds->retry_task)
     470             :   {
     471           0 :     GNUNET_SCHEDULER_cancel (ds->retry_task);
     472           0 :     ds->retry_task = NULL;
     473             :   }
     474          41 :   json_decref (ds->wire_details);
     475          41 :   json_decref (ds->contract_terms);
     476          41 :   GNUNET_free (ds);
     477          41 : }
     478             : 
     479             : 
     480             : /**
     481             :  * Offer internal data from a "deposit" CMD, to other commands.
     482             :  *
     483             :  * @param cls closure.
     484             :  * @param[out] ret result.
     485             :  * @param trait name of the trait.
     486             :  * @param index index number of the object to offer.
     487             :  *
     488             :  * @return #GNUNET_OK on success.
     489             :  */
     490             : static int
     491         160 : deposit_traits (void *cls,
     492             :                 const void **ret,
     493             :                 const char *trait,
     494             :                 unsigned int index)
     495             : {
     496         160 :   struct DepositState *ds = cls;
     497             :   const struct TALER_TESTING_Command *coin_cmd;
     498             :   /* Will point to coin cmd internals. */
     499             :   const struct TALER_CoinSpendPrivateKeyP *coin_spent_priv;
     500             : 
     501         160 :   if (GNUNET_YES != ds->command_initialized)
     502             :   {
     503             :     /* No access to traits yet. */
     504           0 :     GNUNET_break (0);
     505           0 :     return GNUNET_NO;
     506             :   }
     507             : 
     508             :   coin_cmd
     509         160 :     = TALER_TESTING_interpreter_lookup_command (ds->is,
     510             :                                                 ds->coin_reference);
     511         160 :   if (NULL == coin_cmd)
     512             :   {
     513           0 :     GNUNET_break (0);
     514           0 :     TALER_TESTING_interpreter_fail (ds->is);
     515           0 :     return GNUNET_NO;
     516             :   }
     517         160 :   if (GNUNET_OK !=
     518         160 :       TALER_TESTING_get_trait_coin_priv (coin_cmd,
     519             :                                          ds->coin_index,
     520             :                                          &coin_spent_priv))
     521             :   {
     522           0 :     GNUNET_break (0);
     523           0 :     TALER_TESTING_interpreter_fail (ds->is);
     524           0 :     return GNUNET_NO;
     525             :   }
     526             :   {
     527             :     struct TALER_TESTING_Trait traits[] = {
     528             :       /* First two traits are only available if
     529             :          ds->traits is #GNUNET_YES */
     530         160 :       TALER_TESTING_make_trait_exchange_pub (0,
     531         160 :                                              &ds->exchange_pub),
     532         160 :       TALER_TESTING_make_trait_exchange_sig (0,
     533         160 :                                              &ds->exchange_sig),
     534             :       /* These traits are always available */
     535         160 :       TALER_TESTING_make_trait_coin_priv (0,
     536             :                                           coin_spent_priv),
     537         160 :       TALER_TESTING_make_trait_wire_details (0,
     538         160 :                                              ds->wire_details),
     539         160 :       TALER_TESTING_make_trait_contract_terms (0,
     540         160 :                                                ds->contract_terms),
     541         160 :       TALER_TESTING_make_trait_merchant_priv (0,
     542         160 :                                               &ds->merchant_priv),
     543         160 :       TALER_TESTING_make_trait_amount_obj (
     544             :         TALER_TESTING_CMD_DEPOSIT_TRAIT_IDX_DEPOSIT_VALUE,
     545         160 :         &ds->amount),
     546         160 :       TALER_TESTING_make_trait_amount_obj (
     547             :         TALER_TESTING_CMD_DEPOSIT_TRAIT_IDX_DEPOSIT_FEE,
     548         160 :         &ds->deposit_fee),
     549         160 :       TALER_TESTING_make_trait_absolute_time (0,
     550         160 :                                               &ds->exchange_timestamp),
     551         160 :       TALER_TESTING_trait_end ()
     552             :     };
     553             : 
     554         160 :     return TALER_TESTING_get_trait ((ds->deposit_succeeded)
     555             :                                     ? traits
     556             :                                     : &traits[2],
     557             :                                     ret,
     558             :                                     trait,
     559             :                                     index);
     560             :   }
     561             : }
     562             : 
     563             : 
     564             : /**
     565             :  * Create a "deposit" command.
     566             :  *
     567             :  * @param label command label.
     568             :  * @param coin_reference reference to any operation that can
     569             :  *        provide a coin.
     570             :  * @param coin_index if @a withdraw_reference offers an array of
     571             :  *        coins, this parameter selects which one in that array.
     572             :  *        This value is currently ignored, as only one-coin
     573             :  *        withdrawals are implemented.
     574             :  * @param target_account_payto target account for the "deposit"
     575             :  *        request.
     576             :  * @param contract_terms contract terms to be signed over by the
     577             :  *        coin.
     578             :  * @param refund_deadline refund deadline, zero means 'no refunds'.
     579             :  *        Note, if time were absolute, then it would have come
     580             :  *        one day and disrupt tests meaning.
     581             :  * @param amount how much is going to be deposited.
     582             :  * @param expected_response_code expected HTTP response code.
     583             :  *
     584             :  * @return the command.
     585             :  */
     586             : struct TALER_TESTING_Command
     587          39 : TALER_TESTING_cmd_deposit (const char *label,
     588             :                            const char *coin_reference,
     589             :                            unsigned int coin_index,
     590             :                            const char *target_account_payto,
     591             :                            const char *contract_terms,
     592             :                            struct GNUNET_TIME_Relative refund_deadline,
     593             :                            const char *amount,
     594             :                            unsigned int expected_response_code)
     595             : {
     596             :   struct DepositState *ds;
     597             :   json_t *wire_details;
     598             : 
     599          39 :   wire_details = TALER_TESTING_make_wire_details (target_account_payto);
     600          39 :   ds = GNUNET_new (struct DepositState);
     601          39 :   ds->coin_reference = coin_reference;
     602          39 :   ds->coin_index = coin_index;
     603          39 :   ds->wire_details = wire_details;
     604          39 :   ds->contract_terms = json_loads (contract_terms,
     605             :                                    JSON_REJECT_DUPLICATES,
     606             :                                    NULL);
     607          39 :   GNUNET_CRYPTO_eddsa_key_create (&ds->merchant_priv.eddsa_priv);
     608          39 :   if (NULL == ds->contract_terms)
     609             :   {
     610           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     611             :                 "Failed to parse contract terms `%s' for CMD `%s'\n",
     612             :                 contract_terms,
     613             :                 label);
     614           0 :     GNUNET_assert (0);
     615             :   }
     616          39 :   ds->wallet_timestamp = GNUNET_TIME_absolute_get ();
     617          39 :   (void) GNUNET_TIME_round_abs (&ds->wallet_timestamp);
     618             : 
     619          39 :   json_object_set_new (ds->contract_terms,
     620             :                        "timestamp",
     621             :                        GNUNET_JSON_from_time_abs (ds->wallet_timestamp));
     622          39 :   if (0 != refund_deadline.rel_value_us)
     623             :   {
     624           7 :     ds->refund_deadline = GNUNET_TIME_relative_to_absolute (refund_deadline);
     625           7 :     (void) GNUNET_TIME_round_abs (&ds->refund_deadline);
     626           7 :     json_object_set_new (ds->contract_terms,
     627             :                          "refund_deadline",
     628             :                          GNUNET_JSON_from_time_abs (ds->refund_deadline));
     629             :   }
     630          39 :   GNUNET_assert (GNUNET_OK ==
     631             :                  TALER_string_to_amount (amount,
     632             :                                          &ds->amount));
     633          39 :   ds->expected_response_code = expected_response_code;
     634          39 :   ds->command_initialized = GNUNET_YES;
     635             :   {
     636          39 :     struct TALER_TESTING_Command cmd = {
     637             :       .cls = ds,
     638             :       .label = label,
     639             :       .run = &deposit_run,
     640             :       .cleanup = &deposit_cleanup,
     641             :       .traits = &deposit_traits
     642             :     };
     643             : 
     644          39 :     return cmd;
     645             :   }
     646             : }
     647             : 
     648             : 
     649             : /**
     650             :  * Create a "deposit" command that references an existing merchant key.
     651             :  *
     652             :  * @param label command label.
     653             :  * @param coin_reference reference to any operation that can
     654             :  *        provide a coin.
     655             :  * @param coin_index if @a withdraw_reference offers an array of
     656             :  *        coins, this parameter selects which one in that array.
     657             :  *        This value is currently ignored, as only one-coin
     658             :  *        withdrawals are implemented.
     659             :  * @param target_account_payto target account for the "deposit"
     660             :  *        request.
     661             :  * @param contract_terms contract terms to be signed over by the
     662             :  *        coin.
     663             :  * @param refund_deadline refund deadline, zero means 'no refunds'.
     664             :  *        Note, if time were absolute, then it would have come
     665             :  *        one day and disrupt tests meaning.
     666             :  * @param amount how much is going to be deposited.
     667             :  * @param expected_response_code expected HTTP response code.
     668             :  * @param merchant_priv_reference reference to another operation
     669             :  *        that has a merchant private key trait
     670             :  *
     671             :  * @return the command.
     672             :  */
     673             : struct TALER_TESTING_Command
     674           1 : TALER_TESTING_cmd_deposit_with_ref (const char *label,
     675             :                                     const char *coin_reference,
     676             :                                     unsigned int coin_index,
     677             :                                     const char *target_account_payto,
     678             :                                     const char *contract_terms,
     679             :                                     struct GNUNET_TIME_Relative refund_deadline,
     680             :                                     const char *amount,
     681             :                                     unsigned int expected_response_code,
     682             :                                     const char *merchant_priv_reference)
     683             : {
     684             :   struct DepositState *ds;
     685             :   json_t *wire_details;
     686             : 
     687           1 :   wire_details = TALER_TESTING_make_wire_details (target_account_payto);
     688           1 :   ds = GNUNET_new (struct DepositState);
     689           1 :   ds->merchant_priv_reference = merchant_priv_reference;
     690           1 :   ds->coin_reference = coin_reference;
     691           1 :   ds->coin_index = coin_index;
     692           1 :   ds->wire_details = wire_details;
     693           1 :   ds->contract_terms = json_loads (contract_terms,
     694             :                                    JSON_REJECT_DUPLICATES,
     695             :                                    NULL);
     696           1 :   if (NULL == ds->contract_terms)
     697             :   {
     698           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     699             :                 "Failed to parse contract terms `%s' for CMD `%s'\n",
     700             :                 contract_terms,
     701             :                 label);
     702           0 :     GNUNET_assert (0);
     703             :   }
     704           1 :   ds->wallet_timestamp = GNUNET_TIME_absolute_get ();
     705           1 :   (void) GNUNET_TIME_round_abs (&ds->wallet_timestamp);
     706             : 
     707           1 :   json_object_set_new (ds->contract_terms,
     708             :                        "timestamp",
     709             :                        GNUNET_JSON_from_time_abs (ds->wallet_timestamp));
     710           1 :   if (0 != refund_deadline.rel_value_us)
     711             :   {
     712           0 :     ds->refund_deadline = GNUNET_TIME_relative_to_absolute (refund_deadline);
     713           0 :     (void) GNUNET_TIME_round_abs (&ds->refund_deadline);
     714           0 :     json_object_set_new (ds->contract_terms,
     715             :                          "refund_deadline",
     716             :                          GNUNET_JSON_from_time_abs (ds->refund_deadline));
     717             :   }
     718           1 :   GNUNET_assert (GNUNET_OK ==
     719             :                  TALER_string_to_amount (amount,
     720             :                                          &ds->amount));
     721           1 :   ds->expected_response_code = expected_response_code;
     722           1 :   ds->command_initialized = GNUNET_YES;
     723             :   {
     724           1 :     struct TALER_TESTING_Command cmd = {
     725             :       .cls = ds,
     726             :       .label = label,
     727             :       .run = &deposit_run,
     728             :       .cleanup = &deposit_cleanup,
     729             :       .traits = &deposit_traits
     730             :     };
     731             : 
     732           1 :     return cmd;
     733             :   }
     734             : }
     735             : 
     736             : 
     737             : /**
     738             :  * Create a "deposit" command that repeats an existing
     739             :  * deposit command.
     740             :  *
     741             :  * @param label command label.
     742             :  * @param deposit_reference which deposit command should we repeat
     743             :  * @param expected_response_code expected HTTP response code.
     744             :  * @return the command.
     745             :  */
     746             : struct TALER_TESTING_Command
     747           1 : TALER_TESTING_cmd_deposit_replay (const char *label,
     748             :                                   const char *deposit_reference,
     749             :                                   unsigned int expected_response_code)
     750             : {
     751             :   struct DepositState *ds;
     752             : 
     753           1 :   ds = GNUNET_new (struct DepositState);
     754           1 :   ds->deposit_reference = deposit_reference;
     755           1 :   ds->expected_response_code = expected_response_code;
     756             :   {
     757           1 :     struct TALER_TESTING_Command cmd = {
     758             :       .cls = ds,
     759             :       .label = label,
     760             :       .run = &deposit_run,
     761             :       .cleanup = &deposit_cleanup,
     762             :       .traits = &deposit_traits
     763             :     };
     764             : 
     765           1 :     return cmd;
     766             :   }
     767             : }
     768             : 
     769             : 
     770             : /**
     771             :  * Modify a deposit command to enable retries when we get transient
     772             :  * errors from the exchange.
     773             :  *
     774             :  * @param cmd a deposit command
     775             :  * @return the command with retries enabled
     776             :  */
     777             : struct TALER_TESTING_Command
     778           0 : TALER_TESTING_cmd_deposit_with_retry (struct TALER_TESTING_Command cmd)
     779             : {
     780             :   struct DepositState *ds;
     781             : 
     782           0 :   GNUNET_assert (&deposit_run == cmd.run);
     783           0 :   ds = cmd.cls;
     784           0 :   ds->do_retry = NUM_RETRIES;
     785           0 :   return cmd;
     786             : }
     787             : 
     788             : 
     789             : /* end of testing_api_cmd_deposit.c */

Generated by: LCOV version 1.14