LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_tip_pickup.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 99 123 80.5 %
Date: 2021-08-30 06:54:17 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2018 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify
       6             :   it under the terms of the GNU General Public License as
       7             :   published by the Free Software Foundation; either version 3, or
       8             :   (at your 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
      13             :   GNU 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             : /**
      21             :  * @file testing_api_cmd_tip_pickup.c
      22             :  * @brief command to test the tipping.
      23             :  * @author Marcello Stanisci
      24             :  */
      25             : 
      26             : #include "platform.h"
      27             : #include <taler/taler_exchange_service.h>
      28             : #include <taler/taler_testing_lib.h>
      29             : #include "taler_merchant_service.h"
      30             : #include "taler_merchant_testing_lib.h"
      31             : 
      32             : /**
      33             :  * State for a /tip-pickup CMD.
      34             :  */
      35             : struct TipPickupState
      36             : {
      37             :   /**
      38             :    * Merchant base URL.
      39             :    */
      40             :   const char *merchant_url;
      41             : 
      42             :   /**
      43             :    * Exchange base URL.
      44             :    */
      45             :   const char *exchange_url;
      46             : 
      47             :   /**
      48             :    * Expected HTTP response code.
      49             :    */
      50             :   unsigned int http_status;
      51             : 
      52             :   /**
      53             :    * Reference to a /tip/authorize CMD.  This will be used to
      54             :    * get the tip id to make the request with.
      55             :    */
      56             :   const char *authorize_reference;
      57             : 
      58             :   /**
      59             :    * If set to non NULL, it references another pickup CMD
      60             :    * that will provide all the data which is needed to issue
      61             :    * the request (like planchet secrets, denomination keys..).
      62             :    */
      63             :   const char *replay_reference;
      64             : 
      65             :   /**
      66             :    * Handle to a on-going /tip/pickup request.
      67             :    */
      68             :   struct TALER_MERCHANT_TipPickupHandle *tpo;
      69             : 
      70             :   /**
      71             :    * The interpreter state.
      72             :    */
      73             :   struct TALER_TESTING_Interpreter *is;
      74             : 
      75             :   /**
      76             :    * An array of string-defined amounts that indicates
      77             :    * which denominations are going to be used to receive
      78             :    * tips.
      79             :    */
      80             :   const char **amounts;
      81             : 
      82             :   /**
      83             :    * The object version of the above @a amounts.
      84             :    */
      85             :   struct TALER_Amount *amounts_obj;
      86             : 
      87             :   /**
      88             :    * The sum of the the amounts above.
      89             :    */
      90             :   struct TALER_Amount total_amount;
      91             : 
      92             :   /**
      93             :    * How many coins are involved in the tipping operation.
      94             :    */
      95             :   uint64_t num_coins;
      96             : 
      97             :   /**
      98             :    * The array of denomination keys, in the same order of @a
      99             :    * amounts.
     100             :    */
     101             :   const struct TALER_EXCHANGE_DenomPublicKey **dks;
     102             : 
     103             :   /**
     104             :    * The array of planchet secrets, in the same order of @a
     105             :    * amounts.
     106             :    */
     107             :   struct TALER_PlanchetSecretsP *psa;
     108             : 
     109             :   /**
     110             :    * Set (by the interpreter) to an array of @a num_coins
     111             :    * signatures created from the (successful) tip operation.
     112             :    */
     113             :   struct TALER_DenominationSignature *sigs;
     114             : 
     115             :   /**
     116             :    * Expected Taler error code (NOTE: this is NOT the HTTP
     117             :    * response code).
     118             :    */
     119             :   enum TALER_ErrorCode expected_ec;
     120             : };
     121             : 
     122             : 
     123             : /**
     124             :  * Callback for a /tip-pickup request, it mainly checks if
     125             :  * values returned from the backend are as expected, and if so
     126             :  * (and if the status was 200 OK) proceede with the withdrawal.
     127             :  *
     128             :  * @param cls closure
     129             :  * @param hr HTTP response
     130             :  * @param num_sigs length of the @a sigs array,
     131             :  *        0 on error
     132             :  * @param sigs array of signatures over the coins, NULL on error
     133             :  */
     134             : static void
     135           5 : pickup_cb (void *cls,
     136             :            const struct TALER_MERCHANT_HttpResponse *hr,
     137             :            unsigned int num_sigs,
     138             :            const struct TALER_DenominationSignature *sigs)
     139             : {
     140           5 :   struct TipPickupState *tps = cls;
     141             : 
     142           5 :   tps->tpo = NULL;
     143           5 :   if (hr->http_status != tps->http_status)
     144             :   {
     145           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     146             :                 "Unexpected response code %u (%d) to command %s\n",
     147             :                 hr->http_status,
     148             :                 (int) hr->ec,
     149             :                 TALER_TESTING_interpreter_get_current_label (tps->is));
     150           0 :     TALER_TESTING_FAIL (tps->is);
     151             :   }
     152             : 
     153           5 :   if (hr->ec != tps->expected_ec)
     154           0 :     TALER_TESTING_FAIL (tps->is);
     155             : 
     156             :   /* Safe to go ahead: http status was expected.  */
     157           5 :   if ( (MHD_HTTP_OK != hr->http_status) ||
     158           3 :        (TALER_EC_NONE != hr->ec) )
     159             :   {
     160           2 :     TALER_TESTING_interpreter_next (tps->is);
     161           2 :     return;
     162             :   }
     163           3 :   if (num_sigs != tps->num_coins)
     164           0 :     TALER_TESTING_FAIL (tps->is);
     165           3 :   tps->sigs = GNUNET_new_array (tps->num_coins,
     166             :                                 struct TALER_DenominationSignature);
     167           6 :   for (unsigned int i = 0; i<num_sigs; i++)
     168           3 :     tps->sigs[i].rsa_signature
     169           3 :       = GNUNET_CRYPTO_rsa_signature_dup (sigs[i].rsa_signature);
     170           3 :   TALER_TESTING_interpreter_next (tps->is);
     171             : }
     172             : 
     173             : 
     174             : /**
     175             :  * Run a /tip-pickup CMD.
     176             :  *
     177             :  * @param cls closure
     178             :  * @param cmd the current /tip-pickup CMD.
     179             :  * @param is interpreter state.
     180             :  */
     181             : static void
     182           5 : tip_pickup_run (void *cls,
     183             :                 const struct TALER_TESTING_Command *cmd,
     184             :                 struct TALER_TESTING_Interpreter *is)
     185             : {
     186           5 :   struct TipPickupState *tps = cls;
     187             :   unsigned int num_planchets;
     188             :   const struct TALER_TESTING_Command *replay_cmd;
     189             :   const struct TALER_TESTING_Command *authorize_cmd;
     190             :   const struct GNUNET_HashCode *tip_id;
     191             : 
     192           5 :   tps->is = is;
     193           5 :   tps->exchange_url = TALER_EXCHANGE_get_base_url (is->exchange);
     194           5 :   if (NULL == tps->replay_reference)
     195             :   {
     196           5 :     replay_cmd = NULL;
     197             : 
     198             :     /* Count planchets. */
     199           5 :     for (num_planchets = 0;
     200          10 :          NULL != tps->amounts[num_planchets];
     201           5 :          num_planchets++)
     202             :       ;
     203             :   }
     204             :   else
     205             :   {
     206             :     const unsigned int *np;
     207             : 
     208           0 :     if (NULL ==  /* looking for "parent" tip-pickup command */
     209             :         (replay_cmd
     210           0 :            = TALER_TESTING_interpreter_lookup_command (is,
     211             :                                                        tps->replay_reference)) )
     212           0 :       TALER_TESTING_FAIL (is);
     213             : 
     214           0 :     if (GNUNET_OK !=
     215           0 :         TALER_TESTING_get_trait_uint (replay_cmd,
     216             :                                       0,
     217             :                                       &np))
     218           0 :       TALER_TESTING_FAIL (is);
     219           0 :     num_planchets = *np;
     220             :   }
     221             : 
     222           5 :   if (NULL ==
     223             :       (authorize_cmd
     224           5 :          = TALER_TESTING_interpreter_lookup_command (is,
     225             :                                                      tps->authorize_reference)) )
     226           0 :     TALER_TESTING_FAIL (is);
     227             : 
     228           5 :   tps->num_coins = num_planchets;
     229           5 :   {
     230           5 :     struct TALER_MERCHANT_PlanchetData planchets[num_planchets];
     231             : 
     232           5 :     tps->psa = GNUNET_new_array (num_planchets,
     233             :                                  struct TALER_PlanchetSecretsP);
     234           5 :     tps->dks = GNUNET_new_array (num_planchets,
     235             :                                  const struct TALER_EXCHANGE_DenomPublicKey *);
     236           5 :     tps->amounts_obj = GNUNET_new_array (num_planchets,
     237             :                                          struct TALER_Amount);
     238          10 :     for (unsigned int i = 0; i<num_planchets; i++)
     239             :     {
     240           5 :       if (NULL == replay_cmd)
     241             :       {
     242           5 :         GNUNET_assert (GNUNET_OK ==
     243             :                        TALER_string_to_amount (tps->amounts[i],
     244             :                                                &tps->amounts_obj[i]));
     245           5 :         if (0 == i)
     246           5 :           GNUNET_assert (GNUNET_OK ==
     247             :                          TALER_amount_set_zero (tps->amounts_obj[i].currency,
     248             :                                                 &tps->total_amount));
     249             : 
     250           5 :         GNUNET_assert (0 <
     251             :                        TALER_amount_add (&tps->total_amount,
     252             :                                          &tps->total_amount,
     253             :                                          &tps->amounts_obj[i]));
     254          10 :         tps->dks[i] = TALER_TESTING_find_pk (is->keys,
     255           5 :                                              &tps->amounts_obj[i]);
     256           5 :         if (NULL == tps->dks[i])
     257           0 :           TALER_TESTING_FAIL (is);
     258           5 :         TALER_planchet_setup_random (&tps->psa[i]);
     259             :       }
     260             :       else
     261             :       {
     262             :         struct TALER_PlanchetSecretsP *ps;
     263             : 
     264           0 :         if (GNUNET_OK !=
     265           0 :             TALER_TESTING_get_trait_denom_pub (replay_cmd,
     266             :                                                i,
     267           0 :                                                &tps->dks[i]))
     268           0 :           TALER_TESTING_FAIL (is);
     269           0 :         if (GNUNET_OK !=
     270           0 :             TALER_TESTING_get_trait_planchet_secrets (replay_cmd,
     271             :                                                       i,
     272             :                                                       &ps))
     273           0 :           TALER_TESTING_FAIL (is);
     274           0 :         tps->psa[i] = *ps;
     275             :       }
     276           5 :       planchets[i].pk = tps->dks[i];
     277           5 :       planchets[i].ps = tps->psa[i];
     278             :     }
     279           5 :     if (GNUNET_OK !=
     280           5 :         TALER_TESTING_get_trait_tip_id (authorize_cmd,
     281             :                                         0,
     282             :                                         &tip_id))
     283           0 :       TALER_TESTING_FAIL (is);
     284             : 
     285           5 :     tps->tpo = TALER_MERCHANT_tip_pickup (is->ctx,
     286             :                                           tps->merchant_url,
     287             :                                           tip_id,
     288             :                                           num_planchets,
     289             :                                           planchets,
     290             :                                           &pickup_cb,
     291             :                                           tps);
     292           5 :     GNUNET_assert (NULL != tps->tpo);
     293             :   }
     294             : }
     295             : 
     296             : 
     297             : /**
     298             :  * Free a /tip-pickup CMD state, and possibly cancel a
     299             :  * pending /tip-pickup request.
     300             :  *
     301             :  * @param cls closure.
     302             :  * @param cmd current CMD to be freed.
     303             :  */
     304             : static void
     305           5 : tip_pickup_cleanup (void *cls,
     306             :                     const struct TALER_TESTING_Command *cmd)
     307             : {
     308           5 :   struct TipPickupState *tps = cls;
     309             : 
     310           5 :   GNUNET_free (tps->amounts_obj);
     311           5 :   GNUNET_free (tps->dks);
     312           5 :   GNUNET_free (tps->psa);
     313           5 :   if (NULL != tps->sigs)
     314             :   {
     315           6 :     for (unsigned int i = 0; i<tps->num_coins; i++)
     316           3 :       if (NULL != tps->sigs[i].rsa_signature)
     317           3 :         GNUNET_CRYPTO_rsa_signature_free (tps->sigs[i].rsa_signature);
     318           3 :     GNUNET_free (tps->sigs);
     319             :   }
     320           5 :   if (NULL != tps->tpo)
     321             :   {
     322           0 :     TALER_LOG_WARNING ("Tip-pickup operation did not complete\n");
     323           0 :     TALER_MERCHANT_tip_pickup_cancel (tps->tpo);
     324             :   }
     325           5 :   GNUNET_free (tps);
     326           5 : }
     327             : 
     328             : 
     329             : static int
     330           4 : tip_pickup_traits (void *cls,
     331             :                    const void **ret,
     332             :                    const char *trait,
     333             :                    unsigned int index)
     334           4 : {
     335           4 :   struct TipPickupState *tps = cls;
     336             :   #define NUM_TRAITS (tps->num_coins * 5) + 4
     337           4 :   struct TALER_TESTING_Trait traits[NUM_TRAITS];
     338             : 
     339           8 :   for (unsigned int i = 0; i<tps->num_coins; i++)
     340             :   {
     341           4 :     traits[i] = TALER_TESTING_make_trait_planchet_secrets (i,
     342           4 :                                                            &tps->psa[i]);
     343           4 :     traits[i + tps->num_coins] =
     344           4 :       TALER_TESTING_make_trait_coin_priv (i, &tps->psa[i].coin_priv);
     345           4 :     traits[i + (tps->num_coins * 2)] =
     346           4 :       TALER_TESTING_make_trait_denom_pub (i, tps->dks[i]);
     347           4 :     traits[i + (tps->num_coins * 3)] =
     348           4 :       TALER_TESTING_make_trait_denom_sig (i, &tps->sigs[i]);
     349           4 :     traits[i + (tps->num_coins * 4)] =
     350           4 :       TALER_TESTING_make_trait_amount_obj (i, &tps->amounts_obj[i]);
     351             :   }
     352           4 :   traits[NUM_TRAITS - 4]
     353           4 :     = TALER_TESTING_make_trait_amount_obj (tps->num_coins,
     354           4 :                                            &tps->total_amount);
     355           4 :   traits[NUM_TRAITS - 3]
     356           4 :     = TALER_TESTING_make_trait_uint64 (0,
     357           4 :                                        &tps->num_coins);
     358           4 :   traits[NUM_TRAITS - 2]
     359           4 :     = TALER_TESTING_make_trait_url (TALER_TESTING_UT_EXCHANGE_BASE_URL,
     360             :                                     tps->exchange_url);
     361           4 :   traits[NUM_TRAITS - 1] = TALER_TESTING_trait_end ();
     362           4 :   return TALER_TESTING_get_trait (traits,
     363             :                                   ret,
     364             :                                   trait,
     365             :                                   index);
     366             : }
     367             : 
     368             : 
     369             : struct TALER_TESTING_Command
     370           5 : TALER_TESTING_cmd_tip_pickup (const char *label,
     371             :                               const char *merchant_url,
     372             :                               unsigned int http_status,
     373             :                               const char *authorize_reference,
     374             :                               const char **amounts)
     375             : {
     376             :   struct TipPickupState *tps;
     377             : 
     378           5 :   tps = GNUNET_new (struct TipPickupState);
     379           5 :   tps->merchant_url = merchant_url;
     380           5 :   tps->authorize_reference = authorize_reference;
     381           5 :   tps->amounts = amounts;
     382           5 :   tps->http_status = http_status;
     383             :   {
     384           5 :     struct TALER_TESTING_Command cmd = {
     385             :       .cls = tps,
     386             :       .label = label,
     387             :       .run = &tip_pickup_run,
     388             :       .cleanup = &tip_pickup_cleanup,
     389             :       .traits = &tip_pickup_traits
     390             :     };
     391             : 
     392           5 :     return cmd;
     393             :   }
     394             : }
     395             : 
     396             : 
     397             : struct TALER_TESTING_Command
     398           2 : TALER_TESTING_cmd_tip_pickup_with_ec (const char *label,
     399             :                                       const char *merchant_url,
     400             :                                       unsigned int http_status,
     401             :                                       const char *authorize_reference,
     402             :                                       const char **amounts,
     403             :                                       enum TALER_ErrorCode ec)
     404             : {
     405             :   struct TALER_TESTING_Command cmd;
     406             :   struct TipPickupState *tps;
     407             : 
     408           2 :   cmd = TALER_TESTING_cmd_tip_pickup (label,
     409             :                                       merchant_url,
     410             :                                       http_status,
     411             :                                       authorize_reference,
     412             :                                       amounts);
     413           2 :   tps = cmd.cls;
     414           2 :   tps->expected_ec = ec;
     415           2 :   return cmd;
     416             : }
     417             : 
     418             : 
     419             : /* end of testing_api_cmd_tip_pickup.c */

Generated by: LCOV version 1.14