LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_abort_order.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 101 0.0 %
Date: 2022-06-30 06:15:34 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, 2020 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             :  * @file testing_api_cmd_abort_order.c
      21             :  * @brief command to test the abort feature.
      22             :  * @author Marcello Stanisci
      23             :  */
      24             : #include "platform.h"
      25             : #include <taler/taler_exchange_service.h>
      26             : #include <taler/taler_testing_lib.h>
      27             : #include <taler/taler_signatures.h>
      28             : #include "taler_merchant_service.h"
      29             : #include "taler_merchant_testing_lib.h"
      30             : 
      31             : /**
      32             :  * State for a " abort" CMD.
      33             :  */
      34             : struct AbortState
      35             : {
      36             : 
      37             :   /**
      38             :    * Reference to the "pay" command to abort.
      39             :    */
      40             :   const char *pay_reference;
      41             : 
      42             :   /**
      43             :    * Merchant URL.
      44             :    */
      45             :   const char *merchant_url;
      46             : 
      47             :   /**
      48             :    * Handle to a "abort" operation.
      49             :    */
      50             :   struct TALER_MERCHANT_OrderAbortHandle *oah;
      51             : 
      52             :   /**
      53             :    * Interpreter state.
      54             :    */
      55             :   struct TALER_TESTING_Interpreter *is;
      56             : 
      57             :   /**
      58             :    * The actual abort/refund data.
      59             :    */
      60             :   struct TALER_MERCHANT_AbortedCoin *acs;
      61             : 
      62             :   /**
      63             :    * Expected HTTP response code.
      64             :    */
      65             :   unsigned int http_status;
      66             : 
      67             :   /**
      68             :    * How many refund permissions this CMD got
      69             :    * the right for.  Roughly, there is one refund
      70             :    * permission for one coin.
      71             :    */
      72             :   unsigned int acs_length;
      73             : 
      74             : };
      75             : 
      76             : 
      77             : /**
      78             :  * Parse the @a coins specification and grow the @a ac
      79             :  * array with the coins found, updating @a nac.
      80             :  *
      81             :  * @param[in,out] ac pointer to array of coins found
      82             :  * @param[in,out] nac length of array at @a pc
      83             :  * @param[in] coins string specifying coins to add to @a pc,
      84             :  *            clobbered in the process
      85             :  * @param is interpreter state
      86             :  * @return #GNUNET_OK on success
      87             :  */
      88             : static enum GNUNET_GenericReturnValue
      89           0 : build_coins (struct TALER_MERCHANT_AbortCoin **ac,
      90             :              unsigned int *nac,
      91             :              char *coins,
      92             :              struct TALER_TESTING_Interpreter *is)
      93             : {
      94           0 :   for (char *token = strtok (coins, ";");
      95             :        NULL != token;
      96           0 :        token = strtok (NULL, ";"))
      97             :   {
      98             :     char *ctok;
      99             :     unsigned int ci;
     100             :     struct TALER_MERCHANT_AbortCoin *icoin;
     101             : 
     102             :     /* Token syntax is "LABEL[/NUMBER]" */
     103           0 :     ctok = strchr (token, '/');
     104           0 :     ci = 0;
     105           0 :     if (NULL != ctok)
     106             :     {
     107           0 :       *ctok = '\0';
     108           0 :       ctok++;
     109           0 :       if (1 != sscanf (ctok,
     110             :                        "%u",
     111             :                        &ci))
     112             :       {
     113           0 :         GNUNET_break (0);
     114           0 :         return GNUNET_SYSERR;
     115             :       }
     116             :     }
     117             :     // FIXME: ci not used!?
     118             :     {
     119             :       const struct TALER_TESTING_Command *coin_cmd;
     120             :       const char **exchange_url;
     121             : 
     122           0 :       coin_cmd = TALER_TESTING_interpreter_lookup_command (is,
     123             :                                                            token);
     124           0 :       if (NULL == coin_cmd)
     125             :       {
     126           0 :         GNUNET_break (0);
     127           0 :         return GNUNET_SYSERR;
     128             :       }
     129           0 :       GNUNET_array_grow (*ac,
     130             :                          *nac,
     131             :                          (*nac) + 1);
     132           0 :       icoin = &((*ac)[(*nac) - 1]);
     133             : 
     134             :       {
     135             :         const struct TALER_CoinSpendPrivateKeyP *coin_priv;
     136             : 
     137           0 :         GNUNET_assert (GNUNET_OK ==
     138             :                        TALER_TESTING_get_trait_coin_priv (coin_cmd,
     139             :                                                           0,
     140             :                                                           &coin_priv));
     141           0 :         GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv->eddsa_priv,
     142             :                                             &icoin->coin_pub.eddsa_pub);
     143             :       }
     144           0 :       GNUNET_assert (GNUNET_OK ==
     145             :                      TALER_TESTING_get_trait_exchange_url (coin_cmd,
     146             :                                                            &exchange_url));
     147           0 :       icoin->exchange_url = *exchange_url;
     148             :       {
     149             :         const struct TALER_Amount *denom_value;
     150             : 
     151           0 :         GNUNET_assert (GNUNET_OK ==
     152             :                        TALER_TESTING_get_trait_amount (coin_cmd,
     153             :                                                        &denom_value));
     154           0 :         icoin->amount_with_fee = *denom_value;
     155             :       }
     156             : 
     157             :     }
     158             :   }
     159           0 :   return GNUNET_OK;
     160             : }
     161             : 
     162             : 
     163             : /**
     164             :  * Callback for a "pay abort" operation.  Mainly, check HTTP
     165             :  * response code was as expected and stores refund permissions
     166             :  * in the state.
     167             :  *
     168             :  * @param cls closure.
     169             :  * @param hr HTTP response
     170             :  * @param merchant_pub public key of the merchant refunding the
     171             :  *        contract.
     172             :  * @param num_aborts length of the @a res array
     173             :  * @param res array containing the abort confirmations
     174             :  */
     175             : static void
     176           0 : abort_cb (void *cls,
     177             :           const struct TALER_MERCHANT_HttpResponse *hr,
     178             :           const struct TALER_MerchantPublicKeyP *merchant_pub,
     179             :           unsigned int num_aborts,
     180             :           const struct TALER_MERCHANT_AbortedCoin res[])
     181             : {
     182           0 :   struct AbortState *as = cls;
     183             : 
     184           0 :   as->oah = NULL;
     185           0 :   if (as->http_status != hr->http_status)
     186             :   {
     187           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     188             :                 "Unexpected response code %u (%d) to command `%s' (expected %u)\n",
     189             :                 hr->http_status,
     190             :                 (int) hr->ec,
     191             :                 TALER_TESTING_interpreter_get_current_label (as->is),
     192             :                 as->http_status);
     193           0 :     TALER_TESTING_FAIL (as->is);
     194             :   }
     195           0 :   if ( (MHD_HTTP_OK == hr->http_status) &&
     196           0 :        (TALER_EC_NONE == hr->ec) )
     197             :   {
     198           0 :     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     199             :                 "Received %u refunds\n",
     200             :                 num_aborts);
     201           0 :     as->acs_length = num_aborts;
     202           0 :     as->acs = GNUNET_new_array (num_aborts,
     203             :                                 struct TALER_MERCHANT_AbortedCoin);
     204           0 :     memcpy (as->acs,
     205             :             res,
     206             :             num_aborts * sizeof (struct TALER_MERCHANT_AbortedCoin));
     207             :   }
     208           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     209             :               "Successful pay-abort (HTTP status: %u)\n",
     210             :               hr->http_status);
     211           0 :   TALER_TESTING_interpreter_next (as->is);
     212             : }
     213             : 
     214             : 
     215             : /**
     216             :  * Run an "abort" CMD.
     217             :  *
     218             :  * @param cls closure
     219             :  * @param cmd command being run.
     220             :  * @param is interpreter state
     221             :  */
     222             : static void
     223           0 : abort_run (void *cls,
     224             :            const struct TALER_TESTING_Command *cmd,
     225             :            struct TALER_TESTING_Interpreter *is)
     226             : {
     227           0 :   struct AbortState *as = cls;
     228             :   const struct TALER_TESTING_Command *pay_cmd;
     229             :   const char **proposal_reference;
     230             :   const char **coin_reference;
     231             :   const struct TALER_TESTING_Command *proposal_cmd;
     232             :   const char *order_id;
     233             :   const struct TALER_PrivateContractHashP *h_proposal;
     234             :   struct TALER_MerchantPublicKeyP merchant_pub;
     235             :   struct TALER_Amount total_amount;
     236             :   const char *error_name;
     237             :   unsigned int error_line;
     238             :   struct TALER_MERCHANT_AbortCoin *abort_coins;
     239             :   unsigned int nabort_coins;
     240             :   char *cr;
     241             : 
     242           0 :   as->is = is;
     243           0 :   pay_cmd = TALER_TESTING_interpreter_lookup_command (is,
     244             :                                                       as->pay_reference);
     245           0 :   if (NULL == pay_cmd)
     246           0 :     TALER_TESTING_FAIL (is);
     247           0 :   if (GNUNET_OK !=
     248           0 :       TALER_TESTING_get_trait_proposal_reference (pay_cmd,
     249             :                                                   &proposal_reference))
     250           0 :     TALER_TESTING_FAIL (is);
     251           0 :   if (GNUNET_OK !=
     252           0 :       TALER_TESTING_get_trait_coin_reference (pay_cmd,
     253             :                                               0,
     254             :                                               &coin_reference))
     255           0 :     TALER_TESTING_FAIL (is);
     256           0 :   proposal_cmd = TALER_TESTING_interpreter_lookup_command (is,
     257             :                                                            *proposal_reference);
     258             : 
     259           0 :   if (NULL == proposal_cmd)
     260           0 :     TALER_TESTING_FAIL (is);
     261             : 
     262             :   {
     263             :     const json_t *contract_terms;
     264             : 
     265           0 :     if (GNUNET_OK !=
     266           0 :         TALER_TESTING_get_trait_contract_terms (proposal_cmd,
     267             :                                                 &contract_terms))
     268           0 :       TALER_TESTING_FAIL (is);
     269             :     {
     270             :       /* Get information that needs to be put verbatim in the
     271             :        * deposit permission */
     272             :       struct GNUNET_JSON_Specification spec[] = {
     273           0 :         GNUNET_JSON_spec_string ("order_id",
     274             :                                  &order_id),
     275           0 :         GNUNET_JSON_spec_fixed_auto ("merchant_pub",
     276             :                                      &merchant_pub),
     277           0 :         TALER_JSON_spec_amount_any ("amount",
     278             :                                     &total_amount),
     279           0 :         GNUNET_JSON_spec_end ()
     280             :       };
     281             : 
     282           0 :       if (GNUNET_OK !=
     283           0 :           GNUNET_JSON_parse (contract_terms,
     284             :                              spec,
     285             :                              &error_name,
     286             :                              &error_line))
     287             :       {
     288             :         char *js;
     289             : 
     290           0 :         js = json_dumps (contract_terms,
     291             :                          JSON_INDENT (1));
     292           0 :         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     293             :                     "Parser failed on %s:%u for input `%s'\n",
     294             :                     error_name,
     295             :                     error_line,
     296             :                     js);
     297           0 :         free (js);
     298           0 :         TALER_TESTING_FAIL (is);
     299             :       }
     300             :     }
     301             :   }
     302             : 
     303           0 :   cr = GNUNET_strdup (*coin_reference);
     304           0 :   abort_coins = NULL;
     305           0 :   nabort_coins = 0;
     306           0 :   if (GNUNET_OK !=
     307           0 :       build_coins (&abort_coins,
     308             :                    &nabort_coins,
     309             :                    cr,
     310             :                    is))
     311             :   {
     312           0 :     GNUNET_array_grow (abort_coins,
     313             :                        nabort_coins,
     314             :                        0);
     315           0 :     GNUNET_free (cr);
     316           0 :     TALER_TESTING_FAIL (is);
     317             :   }
     318           0 :   GNUNET_free (cr);
     319             : 
     320           0 :   if (GNUNET_OK !=
     321           0 :       TALER_TESTING_get_trait_h_contract_terms (proposal_cmd,
     322             :                                                 &h_proposal))
     323           0 :     TALER_TESTING_FAIL (is);
     324           0 :   as->oah = TALER_MERCHANT_order_abort (is->ctx,
     325             :                                         as->merchant_url,
     326             :                                         order_id,
     327             :                                         &merchant_pub,
     328             :                                         h_proposal,
     329             :                                         nabort_coins,
     330             :                                         abort_coins,
     331             :                                         &abort_cb,
     332             :                                         as);
     333           0 :   GNUNET_array_grow (abort_coins,
     334             :                      nabort_coins,
     335             :                      0);
     336           0 :   if (NULL == as->oah)
     337           0 :     TALER_TESTING_FAIL (is);
     338             : }
     339             : 
     340             : 
     341             : /**
     342             :  * Free a "pay abort" CMD, and cancel it if need be.
     343             :  *
     344             :  * @param cls closure.
     345             :  * @param cmd command currently being freed.
     346             :  */
     347             : static void
     348           0 : abort_cleanup (void *cls,
     349             :                const struct TALER_TESTING_Command *cmd)
     350             : {
     351           0 :   struct AbortState *as = cls;
     352             : 
     353           0 :   if (NULL != as->oah)
     354             :   {
     355           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     356             :                 "Command `%s' did not complete.\n",
     357             :                 TALER_TESTING_interpreter_get_current_label (
     358             :                   as->is));
     359           0 :     TALER_MERCHANT_order_abort_cancel (as->oah);
     360             :   }
     361           0 :   GNUNET_array_grow (as->acs,
     362             :                      as->acs_length,
     363             :                      0);
     364           0 :   GNUNET_free (as);
     365           0 : }
     366             : 
     367             : 
     368             : /**
     369             :  * Offer internal data useful to other commands.
     370             :  *
     371             :  * @param cls closure
     372             :  * @param[out] ret result (could be anything)
     373             :  * @param trait name of the trait
     374             :  * @param index index number of the object to extract.
     375             :  * @return #GNUNET_OK on success
     376             :  */
     377             : static int
     378           0 : abort_traits (void *cls,
     379             :               const void **ret,
     380             :               const char *trait,
     381             :               unsigned int index)
     382             : {
     383           0 :   struct AbortState *as = cls;
     384             :   struct TALER_TESTING_Trait traits[] = {
     385           0 :     TALER_TESTING_trait_end ()
     386             :   };
     387             : 
     388             :   (void) as;
     389           0 :   return TALER_TESTING_get_trait (traits,
     390             :                                   ret,
     391             :                                   trait,
     392             :                                   index);
     393             : }
     394             : 
     395             : 
     396             : struct TALER_TESTING_Command
     397           0 : TALER_TESTING_cmd_merchant_order_abort (const char *label,
     398             :                                         const char *merchant_url,
     399             :                                         const char *pay_reference,
     400             :                                         unsigned int http_status)
     401             : {
     402             :   struct AbortState *as;
     403             : 
     404           0 :   as = GNUNET_new (struct AbortState);
     405           0 :   as->http_status = http_status;
     406           0 :   as->pay_reference = pay_reference;
     407           0 :   as->merchant_url = merchant_url;
     408             :   {
     409           0 :     struct TALER_TESTING_Command cmd = {
     410             :       .cls = as,
     411             :       .label = label,
     412             :       .run = &abort_run,
     413             :       .cleanup = &abort_cleanup,
     414             :       .traits = &abort_traits
     415             :     };
     416             : 
     417           0 :     return cmd;
     418             :   }
     419             : }
     420             : 
     421             : 
     422             : /* end of testing_api_cmd_abort_order.c */

Generated by: LCOV version 1.14