LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_tip_pickup.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 123 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 6 0.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 lib/testing_api_cmd_tip.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           0 : pickup_cb (void *cls,
     136             :            const struct TALER_MERCHANT_HttpResponse *hr,
     137             :            unsigned int num_sigs,
     138             :            const struct TALER_DenominationSignature *sigs)
     139             : {
     140           0 :   struct TipPickupState *tps = cls;
     141             : 
     142           0 :   tps->tpo = NULL;
     143           0 :   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           0 :   if (hr->ec != tps->expected_ec)
     154           0 :     TALER_TESTING_FAIL (tps->is);
     155             : 
     156             :   /* Safe to go ahead: http status was expected.  */
     157           0 :   if ( (MHD_HTTP_OK != hr->http_status) ||
     158           0 :        (TALER_EC_NONE != hr->ec) )
     159             :   {
     160           0 :     TALER_TESTING_interpreter_next (tps->is);
     161           0 :     return;
     162             :   }
     163           0 :   if (num_sigs != tps->num_coins)
     164           0 :     TALER_TESTING_FAIL (tps->is);
     165           0 :   tps->sigs = GNUNET_new_array (tps->num_coins,
     166             :                                 struct TALER_DenominationSignature);
     167           0 :   for (unsigned int i = 0; i<num_sigs; i++)
     168           0 :     tps->sigs[i].rsa_signature
     169           0 :       = GNUNET_CRYPTO_rsa_signature_dup (sigs[i].rsa_signature);
     170           0 :   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           0 : tip_pickup_run (void *cls,
     183             :                 const struct TALER_TESTING_Command *cmd,
     184             :                 struct TALER_TESTING_Interpreter *is)
     185             : {
     186           0 :   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           0 :   tps->is = is;
     193           0 :   tps->exchange_url = TALER_EXCHANGE_get_base_url (is->exchange);
     194           0 :   if (NULL == tps->replay_reference)
     195             :   {
     196           0 :     replay_cmd = NULL;
     197             : 
     198             :     /* Count planchets. */
     199           0 :     for (num_planchets = 0;
     200           0 :          NULL != tps->amounts[num_planchets];
     201           0 :          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           0 :   if (NULL ==
     223             :       (authorize_cmd
     224           0 :          = TALER_TESTING_interpreter_lookup_command (is,
     225             :                                                      tps->authorize_reference)) )
     226           0 :     TALER_TESTING_FAIL (is);
     227             : 
     228           0 :   tps->num_coins = num_planchets;
     229           0 :   {
     230           0 :     struct TALER_MERCHANT_PlanchetData planchets[num_planchets];
     231             : 
     232           0 :     tps->psa = GNUNET_new_array (num_planchets,
     233             :                                  struct TALER_PlanchetSecretsP);
     234           0 :     tps->dks = GNUNET_new_array (num_planchets,
     235             :                                  const struct TALER_EXCHANGE_DenomPublicKey *);
     236           0 :     tps->amounts_obj = GNUNET_new_array (num_planchets,
     237             :                                          struct TALER_Amount);
     238           0 :     for (unsigned int i = 0; i<num_planchets; i++)
     239             :     {
     240           0 :       if (NULL == replay_cmd)
     241             :       {
     242           0 :         GNUNET_assert (GNUNET_OK ==
     243             :                        TALER_string_to_amount (tps->amounts[i],
     244             :                                                &tps->amounts_obj[i]));
     245           0 :         if (0 == i)
     246           0 :           GNUNET_assert (GNUNET_OK ==
     247             :                          TALER_amount_get_zero (tps->amounts_obj[i].currency,
     248             :                                                 &tps->total_amount));
     249             : 
     250           0 :         GNUNET_assert (0 <
     251             :                        TALER_amount_add (&tps->total_amount,
     252             :                                          &tps->total_amount,
     253             :                                          &tps->amounts_obj[i]));
     254           0 :         tps->dks[i] = TALER_TESTING_find_pk (is->keys,
     255           0 :                                              &tps->amounts_obj[i]);
     256           0 :         if (NULL == tps->dks[i])
     257           0 :           TALER_TESTING_FAIL (is);
     258           0 :         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           0 :       planchets[i].pk = tps->dks[i];
     277           0 :       planchets[i].ps = tps->psa[i];
     278             :     }
     279           0 :     if (GNUNET_OK !=
     280           0 :         TALER_TESTING_get_trait_tip_id (authorize_cmd,
     281             :                                         0,
     282             :                                         &tip_id))
     283           0 :       TALER_TESTING_FAIL (is);
     284             : 
     285           0 :     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           0 :     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           0 : tip_pickup_cleanup (void *cls,
     306             :                     const struct TALER_TESTING_Command *cmd)
     307             : {
     308           0 :   struct TipPickupState *tps = cls;
     309             : 
     310           0 :   GNUNET_free (tps->amounts_obj);
     311           0 :   GNUNET_free (tps->dks);
     312           0 :   GNUNET_free (tps->psa);
     313           0 :   if (NULL != tps->sigs)
     314             :   {
     315           0 :     for (unsigned int i = 0; i<tps->num_coins; i++)
     316           0 :       if (NULL != tps->sigs[i].rsa_signature)
     317           0 :         GNUNET_CRYPTO_rsa_signature_free (tps->sigs[i].rsa_signature);
     318           0 :     GNUNET_free (tps->sigs);
     319             :   }
     320           0 :   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           0 :   GNUNET_free (tps);
     326           0 : }
     327             : 
     328             : 
     329             : /**
     330             :  * Offers information from the /tip-pickup CMD state to other
     331             :  * commands.
     332             :  *
     333             :  * @param cls closure
     334             :  * @param ret[out] result (could be anything)
     335             :  * @param trait name of the trait
     336             :  * @param index index number of the object to extract.
     337             :  * @return #GNUNET_OK on success
     338             :  */
     339             : static int
     340           0 : tip_pickup_traits (void *cls,
     341             :                    const void **ret,
     342             :                    const char *trait,
     343             :                    unsigned int index)
     344           0 : {
     345           0 :   struct TipPickupState *tps = cls;
     346             :   #define NUM_TRAITS (tps->num_coins * 5) + 4
     347           0 :   struct TALER_TESTING_Trait traits[NUM_TRAITS];
     348             : 
     349           0 :   for (unsigned int i = 0; i<tps->num_coins; i++)
     350             :   {
     351           0 :     traits[i] = TALER_TESTING_make_trait_planchet_secrets (i,
     352           0 :                                                            &tps->psa[i]);
     353           0 :     traits[i + tps->num_coins] =
     354           0 :       TALER_TESTING_make_trait_coin_priv (i, &tps->psa[i].coin_priv);
     355           0 :     traits[i + (tps->num_coins * 2)] =
     356           0 :       TALER_TESTING_make_trait_denom_pub (i, tps->dks[i]);
     357           0 :     traits[i + (tps->num_coins * 3)] =
     358           0 :       TALER_TESTING_make_trait_denom_sig (i, &tps->sigs[i]);
     359           0 :     traits[i + (tps->num_coins * 4)] =
     360           0 :       TALER_TESTING_make_trait_amount_obj (i, &tps->amounts_obj[i]);
     361             :   }
     362           0 :   traits[NUM_TRAITS - 4]
     363           0 :     = TALER_TESTING_make_trait_amount_obj (tps->num_coins,
     364           0 :                                            &tps->total_amount);
     365           0 :   traits[NUM_TRAITS - 3]
     366           0 :     = TALER_TESTING_make_trait_uint64 (0,
     367           0 :                                        &tps->num_coins);
     368           0 :   traits[NUM_TRAITS - 2]
     369           0 :     = TALER_TESTING_make_trait_url (TALER_TESTING_UT_EXCHANGE_BASE_URL,
     370             :                                     tps->exchange_url);
     371           0 :   traits[NUM_TRAITS - 1] = TALER_TESTING_trait_end ();
     372           0 :   return TALER_TESTING_get_trait (traits,
     373             :                                   ret,
     374             :                                   trait,
     375             :                                   index);
     376             : }
     377             : 
     378             : 
     379             : /**
     380             :  * Define a /tip-pickup CMD.
     381             :  *
     382             :  * @param label the command label
     383             :  * @param merchant_url base URL of the backend which will serve
     384             :  *        the /tip-pickup request.
     385             :  * @param http_status expected HTTP response code.
     386             :  * @param authorize_reference reference to a /tip-autorize CMD
     387             :  *        that offers a tip id to pick up.
     388             :  * @param amounts array of string-defined amounts that specifies
     389             :  *        which denominations will be accepted for tipping.
     390             :  */
     391             : struct TALER_TESTING_Command
     392           0 : TALER_TESTING_cmd_tip_pickup (const char *label,
     393             :                               const char *merchant_url,
     394             :                               unsigned int http_status,
     395             :                               const char *authorize_reference,
     396             :                               const char **amounts)
     397             : {
     398             :   struct TipPickupState *tps;
     399             : 
     400           0 :   tps = GNUNET_new (struct TipPickupState);
     401           0 :   tps->merchant_url = merchant_url;
     402           0 :   tps->authorize_reference = authorize_reference;
     403           0 :   tps->amounts = amounts;
     404           0 :   tps->http_status = http_status;
     405             :   {
     406           0 :     struct TALER_TESTING_Command cmd = {
     407             :       .cls = tps,
     408             :       .label = label,
     409             :       .run = &tip_pickup_run,
     410             :       .cleanup = &tip_pickup_cleanup,
     411             :       .traits = &tip_pickup_traits
     412             :     };
     413             : 
     414           0 :     return cmd;
     415             :   }
     416             : }
     417             : 
     418             : 
     419             : /**
     420             :  * Define a /tip-pickup CMD, equipped with the expected error
     421             :  * code.
     422             :  *
     423             :  * @param label the command label
     424             :  * @param merchant_url base URL of the backend which will serve
     425             :  *        the /tip-pickup request.
     426             :  * @param http_status expected HTTP response code.
     427             :  * @param authorize_reference reference to a /tip-autorize CMD
     428             :  *        that offers a tip id to pick up.
     429             :  * @param amounts array of string-defined amounts that specifies
     430             :  *        which denominations will be accepted for tipping.
     431             :  * @param exchange connection handle to the exchange that will
     432             :  *        eventually serve the withdraw operation.
     433             :  * @param ec expected Taler error code.
     434             :  */
     435             : struct TALER_TESTING_Command
     436           0 : TALER_TESTING_cmd_tip_pickup_with_ec (const char *label,
     437             :                                       const char *merchant_url,
     438             :                                       unsigned int http_status,
     439             :                                       const char *authorize_reference,
     440             :                                       const char **amounts,
     441             :                                       enum TALER_ErrorCode ec)
     442             : {
     443             :   struct TALER_TESTING_Command cmd;
     444             :   struct TipPickupState *tps;
     445             : 
     446           0 :   cmd = TALER_TESTING_cmd_tip_pickup (label,
     447             :                                       merchant_url,
     448             :                                       http_status,
     449             :                                       authorize_reference,
     450             :                                       amounts);
     451           0 :   tps = cmd.cls;
     452           0 :   tps->expected_ec = ec;
     453           0 :   return cmd;
     454             : }
     455             : 
     456             : 
     457             : /* end of testing_api_cmd_tip_pickup.c */

Generated by: LCOV version 1.14