LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_auditor_deposit_confirmation.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 56 100 56.0 %
Date: 2021-08-30 06:43:37 Functions: 4 7 57.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2018 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_auditor_deposit_confirmation.c
      21             :  * @brief command for testing /deposit_confirmation.
      22             :  * @author Christian Grothoff
      23             :  */
      24             : #include "platform.h"
      25             : #include "taler_json_lib.h"
      26             : #include <gnunet/gnunet_curl_lib.h>
      27             : #include "taler_auditor_service.h"
      28             : #include "taler_testing_lib.h"
      29             : #include "taler_signatures.h"
      30             : #include "backoff.h"
      31             : 
      32             : /**
      33             :  * How long do we wait AT MOST when retrying?
      34             :  */
      35             : #define MAX_BACKOFF GNUNET_TIME_relative_multiply ( \
      36             :     GNUNET_TIME_UNIT_MILLISECONDS, 100)
      37             : 
      38             : /**
      39             :  * How often do we retry before giving up?
      40             :  */
      41             : #define NUM_RETRIES 5
      42             : 
      43             : 
      44             : /**
      45             :  * State for a "deposit confirmation" CMD.
      46             :  */
      47             : struct DepositConfirmationState
      48             : {
      49             : 
      50             :   /**
      51             :    * Reference to any command that is able to provide a deposit.
      52             :    */
      53             :   const char *deposit_reference;
      54             : 
      55             :   /**
      56             :    * What is the deposited amount without the fee (i.e. the
      57             :    * amount we expect in the deposit confirmation)?
      58             :    */
      59             :   const char *amount_without_fee;
      60             : 
      61             :   /**
      62             :    * Which coin of the @e deposit_reference should we confirm.
      63             :    */
      64             :   unsigned int coin_index;
      65             : 
      66             :   /**
      67             :    * DepositConfirmation handle while operation is running.
      68             :    */
      69             :   struct TALER_AUDITOR_DepositConfirmationHandle *dc;
      70             : 
      71             :   /**
      72             :    * Auditor connection.
      73             :    */
      74             :   struct TALER_AUDITOR_Handle *auditor;
      75             : 
      76             :   /**
      77             :    * Interpreter state.
      78             :    */
      79             :   struct TALER_TESTING_Interpreter *is;
      80             : 
      81             :   /**
      82             :    * Task scheduled to try later.
      83             :    */
      84             :   struct GNUNET_SCHEDULER_Task *retry_task;
      85             : 
      86             :   /**
      87             :    * How long do we wait until we retry?
      88             :    */
      89             :   struct GNUNET_TIME_Relative backoff;
      90             : 
      91             :   /**
      92             :    * Expected HTTP response code.
      93             :    */
      94             :   unsigned int expected_response_code;
      95             : 
      96             :   /**
      97             :    * How often should we retry on (transient) failures?
      98             :    */
      99             :   unsigned int do_retry;
     100             : 
     101             : };
     102             : 
     103             : 
     104             : /**
     105             :  * Run the command.
     106             :  *
     107             :  * @param cls closure.
     108             :  * @param cmd the command to execute.
     109             :  * @param is the interpreter state.
     110             :  */
     111             : static void
     112             : deposit_confirmation_run (void *cls,
     113             :                           const struct TALER_TESTING_Command *cmd,
     114             :                           struct TALER_TESTING_Interpreter *is);
     115             : 
     116             : 
     117             : /**
     118             :  * Task scheduled to re-try #deposit_confirmation_run.
     119             :  *
     120             :  * @param cls a `struct DepositConfirmationState`
     121             :  */
     122             : static void
     123           0 : do_retry (void *cls)
     124             : {
     125           0 :   struct DepositConfirmationState *dcs = cls;
     126             : 
     127           0 :   dcs->retry_task = NULL;
     128           0 :   dcs->is->commands[dcs->is->ip].last_req_time
     129           0 :     = GNUNET_TIME_absolute_get ();
     130           0 :   deposit_confirmation_run (dcs,
     131             :                             NULL,
     132             :                             dcs->is);
     133           0 : }
     134             : 
     135             : 
     136             : /**
     137             :  * Callback to analyze the /deposit-confirmation response, just used
     138             :  * to check if the response code is acceptable.
     139             :  *
     140             :  * @param cls closure.
     141             :  * @param hr HTTP response details
     142             :  */
     143             : static void
     144           1 : deposit_confirmation_cb (void *cls,
     145             :                          const struct TALER_AUDITOR_HttpResponse *hr)
     146             : {
     147           1 :   struct DepositConfirmationState *dcs = cls;
     148             : 
     149           1 :   dcs->dc = NULL;
     150           1 :   if (dcs->expected_response_code != hr->http_status)
     151             :   {
     152           0 :     if (0 != dcs->do_retry)
     153             :     {
     154           0 :       dcs->do_retry--;
     155           0 :       if ( (0 == hr->http_status) ||
     156           0 :            (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec) ||
     157           0 :            (MHD_HTTP_INTERNAL_SERVER_ERROR == hr->http_status) )
     158             :       {
     159           0 :         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     160             :                     "Retrying deposit confirmation failed with %u/%d\n",
     161             :                     hr->http_status,
     162             :                     (int) hr->ec);
     163             :         /* on DB conflicts, do not use backoff */
     164           0 :         if (TALER_EC_GENERIC_DB_SOFT_FAILURE == hr->ec)
     165           0 :           dcs->backoff = GNUNET_TIME_UNIT_ZERO;
     166             :         else
     167           0 :           dcs->backoff = GNUNET_TIME_randomized_backoff (dcs->backoff,
     168             :                                                          MAX_BACKOFF);
     169           0 :         dcs->is->commands[dcs->is->ip].num_tries++;
     170           0 :         dcs->retry_task = GNUNET_SCHEDULER_add_delayed (dcs->backoff,
     171             :                                                         &do_retry,
     172             :                                                         dcs);
     173           0 :         return;
     174             :       }
     175             :     }
     176           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     177             :                 "Unexpected response code %u to command %s in %s:%u\n",
     178             :                 hr->http_status,
     179             :                 dcs->is->commands[dcs->is->ip].label,
     180             :                 __FILE__,
     181             :                 __LINE__);
     182           0 :     json_dumpf (hr->reply, stderr, 0);
     183           0 :     TALER_TESTING_interpreter_fail (dcs->is);
     184           0 :     return;
     185             :   }
     186           1 :   TALER_TESTING_interpreter_next (dcs->is);
     187             : }
     188             : 
     189             : 
     190             : /**
     191             :  * Run the command.
     192             :  *
     193             :  * @param cls closure.
     194             :  * @param cmd the command to execute.
     195             :  * @param is the interpreter state.
     196             :  */
     197             : static void
     198           1 : deposit_confirmation_run (void *cls,
     199             :                           const struct TALER_TESTING_Command *cmd,
     200             :                           struct TALER_TESTING_Interpreter *is)
     201             : {
     202           1 :   struct DepositConfirmationState *dcs = cls;
     203             :   const struct TALER_TESTING_Command *deposit_cmd;
     204             :   struct GNUNET_HashCode h_wire;
     205             :   struct GNUNET_HashCode h_contract_terms;
     206           1 :   const struct GNUNET_TIME_Absolute *exchange_timestamp = NULL;
     207             :   struct GNUNET_TIME_Absolute timestamp;
     208             :   struct GNUNET_TIME_Absolute refund_deadline;
     209             :   struct TALER_Amount amount_without_fee;
     210             :   struct TALER_CoinSpendPublicKeyP coin_pub;
     211             :   const struct TALER_MerchantPrivateKeyP *merchant_priv;
     212             :   struct TALER_MerchantPublicKeyP merchant_pub;
     213             :   const struct TALER_ExchangePublicKeyP *exchange_pub;
     214             :   const struct TALER_ExchangeSignatureP *exchange_sig;
     215             :   const json_t *wire_details;
     216             :   const json_t *contract_terms;
     217             :   const struct TALER_CoinSpendPrivateKeyP *coin_priv;
     218             :   const struct TALER_EXCHANGE_Keys *keys;
     219             :   const struct TALER_EXCHANGE_SigningPublicKey *spk;
     220             : 
     221             :   (void) cmd;
     222           1 :   dcs->is = is;
     223           1 :   GNUNET_assert (NULL != dcs->deposit_reference);
     224             :   deposit_cmd
     225           1 :     = TALER_TESTING_interpreter_lookup_command (is,
     226             :                                                 dcs->deposit_reference);
     227           1 :   if (NULL == deposit_cmd)
     228             :   {
     229           0 :     GNUNET_break (0);
     230           0 :     TALER_TESTING_interpreter_fail (is);
     231           0 :     return;
     232             :   }
     233             : 
     234           1 :   GNUNET_assert (GNUNET_OK ==
     235             :                  TALER_TESTING_get_trait_exchange_pub (deposit_cmd,
     236             :                                                        dcs->coin_index,
     237             :                                                        &exchange_pub));
     238           1 :   GNUNET_assert (GNUNET_OK ==
     239             :                  TALER_TESTING_get_trait_exchange_sig (deposit_cmd,
     240             :                                                        dcs->coin_index,
     241             :                                                        &exchange_sig));
     242           1 :   GNUNET_assert (GNUNET_OK ==
     243             :                  TALER_TESTING_get_trait_absolute_time (deposit_cmd,
     244             :                                                         dcs->coin_index,
     245             :                                                         &exchange_timestamp));
     246           1 :   GNUNET_assert (NULL != exchange_timestamp);
     247           1 :   keys = TALER_EXCHANGE_get_keys (dcs->is->exchange);
     248           1 :   GNUNET_assert (NULL != keys);
     249           1 :   spk = TALER_EXCHANGE_get_signing_key_info (keys,
     250             :                                              exchange_pub);
     251             : 
     252           1 :   GNUNET_assert (GNUNET_OK ==
     253             :                  TALER_TESTING_get_trait_contract_terms (deposit_cmd,
     254             :                                                          dcs->coin_index,
     255             :                                                          &contract_terms));
     256             :   /* Very unlikely to fail */
     257           1 :   GNUNET_assert (NULL != contract_terms);
     258           1 :   GNUNET_assert (GNUNET_OK ==
     259             :                  TALER_JSON_contract_hash (contract_terms,
     260             :                                            &h_contract_terms));
     261           1 :   GNUNET_assert (GNUNET_OK ==
     262             :                  TALER_TESTING_get_trait_wire_details (deposit_cmd,
     263             :                                                        dcs->coin_index,
     264             :                                                        &wire_details));
     265           1 :   GNUNET_assert (GNUNET_OK ==
     266             :                  TALER_JSON_merchant_wire_signature_hash (wire_details,
     267             :                                                           &h_wire));
     268           1 :   GNUNET_assert (GNUNET_OK ==
     269             :                  TALER_TESTING_get_trait_coin_priv (deposit_cmd,
     270             :                                                     dcs->coin_index,
     271             :                                                     &coin_priv));
     272           1 :   GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv->eddsa_priv,
     273             :                                       &coin_pub.eddsa_pub);
     274           1 :   GNUNET_assert (GNUNET_OK ==
     275             :                  TALER_TESTING_get_trait_merchant_priv (deposit_cmd,
     276             :                                                         dcs->coin_index,
     277             :                                                         &merchant_priv));
     278           1 :   GNUNET_CRYPTO_eddsa_key_get_public (&merchant_priv->eddsa_priv,
     279             :                                       &merchant_pub.eddsa_pub);
     280           1 :   GNUNET_assert (GNUNET_OK ==
     281             :                  TALER_string_to_amount (dcs->amount_without_fee,
     282             :                                          &amount_without_fee));
     283             :   /* timestamp is mandatory */
     284             :   {
     285             :     struct GNUNET_JSON_Specification spec[] = {
     286           1 :       TALER_JSON_spec_absolute_time ("timestamp", &timestamp),
     287           1 :       GNUNET_JSON_spec_end ()
     288             :     };
     289             : 
     290           1 :     if (GNUNET_OK !=
     291           1 :         GNUNET_JSON_parse (contract_terms,
     292             :                            spec,
     293             :                            NULL, NULL))
     294             :     {
     295           0 :       GNUNET_break (0);
     296           0 :       TALER_TESTING_interpreter_fail (is);
     297           0 :       return;
     298             :     }
     299             :   }
     300             :   /* refund deadline is optional, defaults to zero */
     301             :   {
     302             :     struct GNUNET_JSON_Specification spec[] = {
     303           1 :       TALER_JSON_spec_absolute_time ("refund_deadline", &refund_deadline),
     304           1 :       GNUNET_JSON_spec_end ()
     305             :     };
     306             : 
     307           1 :     if (GNUNET_OK !=
     308           1 :         GNUNET_JSON_parse (contract_terms,
     309             :                            spec,
     310             :                            NULL, NULL))
     311             :     {
     312           1 :       refund_deadline = timestamp;
     313             :     }
     314             :   }
     315           1 :   dcs->dc = TALER_AUDITOR_deposit_confirmation (dcs->auditor,
     316             :                                                 &h_wire,
     317             :                                                 &h_contract_terms,
     318             :                                                 *exchange_timestamp,
     319             :                                                 refund_deadline,
     320             :                                                 &amount_without_fee,
     321             :                                                 &coin_pub,
     322             :                                                 &merchant_pub,
     323             :                                                 exchange_pub,
     324             :                                                 exchange_sig,
     325             :                                                 &keys->master_pub,
     326             :                                                 spk->valid_from,
     327             :                                                 spk->valid_until,
     328             :                                                 spk->valid_legal,
     329             :                                                 &spk->master_sig,
     330             :                                                 &deposit_confirmation_cb,
     331             :                                                 dcs);
     332             : 
     333           1 :   if (NULL == dcs->dc)
     334             :   {
     335           0 :     GNUNET_break (0);
     336           0 :     TALER_TESTING_interpreter_fail (is);
     337           0 :     return;
     338             :   }
     339           1 :   return;
     340             : }
     341             : 
     342             : 
     343             : /**
     344             :  * Free the state of a "deposit_confirmation" CMD, and possibly cancel a
     345             :  * pending operation thereof.
     346             :  *
     347             :  * @param cls closure, a `struct DepositConfirmationState`
     348             :  * @param cmd the command which is being cleaned up.
     349             :  */
     350             : static void
     351           1 : deposit_confirmation_cleanup (void *cls,
     352             :                               const struct TALER_TESTING_Command *cmd)
     353             : {
     354           1 :   struct DepositConfirmationState *dcs = cls;
     355             : 
     356           1 :   if (NULL != dcs->dc)
     357             :   {
     358           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     359             :                 "Command %u (%s) did not complete\n",
     360             :                 dcs->is->ip,
     361             :                 cmd->label);
     362           0 :     TALER_AUDITOR_deposit_confirmation_cancel (dcs->dc);
     363           0 :     dcs->dc = NULL;
     364             :   }
     365           1 :   if (NULL != dcs->retry_task)
     366             :   {
     367           0 :     GNUNET_SCHEDULER_cancel (dcs->retry_task);
     368           0 :     dcs->retry_task = NULL;
     369             :   }
     370           1 :   GNUNET_free (dcs);
     371           1 : }
     372             : 
     373             : 
     374             : /**
     375             :  * Offer internal data to other commands.
     376             :  *
     377             :  * @param cls closure.
     378             :  * @param[out] ret set to the wanted data.
     379             :  * @param trait name of the trait.
     380             :  * @param index index number of the traits to be returned.
     381             :  *
     382             :  * @return #GNUNET_OK on success
     383             :  */
     384             : static int
     385           0 : deposit_confirmation_traits (void *cls,
     386             :                              const void **ret,
     387             :                              const char *trait,
     388             :                              unsigned int index)
     389             : {
     390             :   (void) cls;
     391             :   (void) ret;
     392             :   (void) trait;
     393             :   (void) index;
     394             :   /* Must define this function because some callbacks
     395             :    * look for certain traits on _all_ the commands. */
     396           0 :   return GNUNET_SYSERR;
     397             : }
     398             : 
     399             : 
     400             : /**
     401             :  * Create a "deposit-confirmation" command.
     402             :  *
     403             :  * @param label command label.
     404             :  * @param auditor auditor connection.
     405             :  * @param deposit_reference reference to any operation that can
     406             :  *        provide a coin.
     407             :  * @param coin_index if @a deposit_reference offers an array of
     408             :  *        coins, this parameter selects which one in that array.
     409             :  *        This value is currently ignored, as only one-coin
     410             :  *        deposits are implemented.
     411             :  * @param amount_without_fee deposited amount without the fee
     412             :  * @param expected_response_code expected HTTP response code.
     413             :  * @return the command.
     414             :  */
     415             : struct TALER_TESTING_Command
     416           1 : TALER_TESTING_cmd_deposit_confirmation (const char *label,
     417             :                                         struct TALER_AUDITOR_Handle *auditor,
     418             :                                         const char *deposit_reference,
     419             :                                         unsigned int coin_index,
     420             :                                         const char *amount_without_fee,
     421             :                                         unsigned int expected_response_code)
     422             : {
     423             :   struct DepositConfirmationState *dcs;
     424             : 
     425           1 :   dcs = GNUNET_new (struct DepositConfirmationState);
     426           1 :   dcs->auditor = auditor;
     427           1 :   dcs->deposit_reference = deposit_reference;
     428           1 :   dcs->coin_index = coin_index;
     429           1 :   dcs->amount_without_fee = amount_without_fee;
     430           1 :   dcs->expected_response_code = expected_response_code;
     431             : 
     432             :   {
     433           1 :     struct TALER_TESTING_Command cmd = {
     434             :       .cls = dcs,
     435             :       .label = label,
     436             :       .run = &deposit_confirmation_run,
     437             :       .cleanup = &deposit_confirmation_cleanup,
     438             :       .traits = &deposit_confirmation_traits
     439             :     };
     440             : 
     441           1 :     return cmd;
     442             :   }
     443             : }
     444             : 
     445             : 
     446             : /**
     447             :  * Modify a deposit confirmation command to enable retries when we get
     448             :  * transient errors from the auditor.
     449             :  *
     450             :  * @param cmd a deposit confirmation command
     451             :  * @return the command with retries enabled
     452             :  */
     453             : struct TALER_TESTING_Command
     454           0 : TALER_TESTING_cmd_deposit_confirmation_with_retry (
     455             :   struct TALER_TESTING_Command cmd)
     456             : {
     457             :   struct DepositConfirmationState *dcs;
     458             : 
     459           0 :   GNUNET_assert (&deposit_confirmation_run == cmd.run);
     460           0 :   dcs = cmd.cls;
     461           0 :   dcs->do_retry = NUM_RETRIES;
     462           0 :   return cmd;
     463             : }
     464             : 
     465             : 
     466             : /* end of testing_auditor_api_cmd_deposit_confirmation.c */

Generated by: LCOV version 1.14