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

Generated by: LCOV version 1.14