LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_pay_order.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 122 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, 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 lib/testing_api_cmd_pay_order.c
      21             :  * @brief command to test the /orders/ID/pay feature.
      22             :  * @author Marcello Stanisci
      23             :  * @author Christian Grothoff
      24             :  */
      25             : #include "platform.h"
      26             : #include <taler/taler_exchange_service.h>
      27             : #include <taler/taler_testing_lib.h>
      28             : #include <taler/taler_signatures.h>
      29             : #include "taler_merchant_service.h"
      30             : #include "taler_merchant_testing_lib.h"
      31             : 
      32             : 
      33             : /**
      34             :  * State for a /pay CMD.
      35             :  */
      36             : struct PayState
      37             : {
      38             :   /**
      39             :    * Contract terms hash code.
      40             :    */
      41             :   struct GNUNET_HashCode h_contract_terms;
      42             : 
      43             :   /**
      44             :    * The interpreter state.
      45             :    */
      46             :   struct TALER_TESTING_Interpreter *is;
      47             : 
      48             :   /**
      49             :    * Expected HTTP response status code.
      50             :    */
      51             :   unsigned int http_status;
      52             : 
      53             :   /**
      54             :    * Reference to a command that can provide a order id,
      55             :    * typically a /proposal test command.
      56             :    */
      57             :   const char *proposal_reference;
      58             : 
      59             :   /**
      60             :    * Reference to a command that can provide a coin, so
      61             :    * we can pay here.
      62             :    */
      63             :   const char *coin_reference;
      64             : 
      65             :   /**
      66             :    * The merchant base URL.
      67             :    */
      68             :   const char *merchant_url;
      69             : 
      70             :   /**
      71             :    * Amount to be paid, plus the deposit fee.
      72             :    */
      73             :   const char *amount_with_fee;
      74             : 
      75             :   /**
      76             :    * Amount to be paid, including NO fees.
      77             :    */
      78             :   const char *amount_without_fee;
      79             : 
      80             :   /**
      81             :    * Handle to the pay operation.
      82             :    */
      83             :   struct TALER_MERCHANT_OrderPayHandle *oph;
      84             : 
      85             :   /**
      86             :    * Signature from the merchant, set on success.
      87             :    */
      88             :   struct TALER_MerchantSignatureP merchant_sig;
      89             : 
      90             :   /**
      91             :    * The session for which the payment is made.
      92             :    */
      93             :   const char *session_id;
      94             : };
      95             : 
      96             : 
      97             : /**
      98             :  * Parse the @a coins specification and grow the @a pc
      99             :  * array with the coins found, updating @a npc.
     100             :  *
     101             :  * @param[in,out] pc pointer to array of coins found
     102             :  * @param[in,out] npc length of array at @a pc
     103             :  * @param[in] coins string specifying coins to add to @a pc,
     104             :  *            clobbered in the process
     105             :  * @param is interpreter state
     106             :  * @param amount_with_fee total amount to be paid for a contract.
     107             :  * @param amount_without_fee to be removed, there is no
     108             :  *        per-contract fee, only per-coin exists.
     109             :  * @return #GNUNET_OK on success
     110             :  */
     111             : static int
     112           0 : build_coins (struct TALER_MERCHANT_PayCoin **pc,
     113             :              unsigned int *npc,
     114             :              char *coins,
     115             :              struct TALER_TESTING_Interpreter *is,
     116             :              const char *amount_with_fee,
     117             :              const char *amount_without_fee)
     118             : {
     119             :   char *token;
     120             : 
     121           0 :   for (token = strtok (coins, ";");
     122             :        NULL != token;
     123           0 :        token = strtok (NULL, ";"))
     124             :   {
     125             :     const struct TALER_TESTING_Command *coin_cmd;
     126             :     char *ctok;
     127             :     unsigned int ci;
     128             :     struct TALER_MERCHANT_PayCoin *icoin;
     129             :     const struct TALER_EXCHANGE_DenomPublicKey *dpk;
     130             : 
     131             :     /* Token syntax is "LABEL[/NUMBER]" */
     132           0 :     ctok = strchr (token, '/');
     133           0 :     ci = 0;
     134           0 :     if (NULL != ctok)
     135             :     {
     136           0 :       *ctok = '\0';
     137           0 :       ctok++;
     138           0 :       if (1 != sscanf (ctok,
     139             :                        "%u",
     140             :                        &ci))
     141             :       {
     142           0 :         GNUNET_break (0);
     143           0 :         return GNUNET_SYSERR;
     144             :       }
     145             :     }
     146             : 
     147           0 :     coin_cmd = TALER_TESTING_interpreter_lookup_command
     148             :                  (is, token);
     149             : 
     150           0 :     if (NULL == coin_cmd)
     151             :     {
     152           0 :       GNUNET_break (0);
     153           0 :       return GNUNET_SYSERR;
     154             :     }
     155             : 
     156           0 :     GNUNET_array_grow (*pc,
     157             :                        *npc,
     158             :                        (*npc) + 1);
     159             : 
     160           0 :     icoin = &((*pc)[(*npc) - 1]);
     161             : 
     162             :     {
     163             :       const struct TALER_CoinSpendPrivateKeyP *coin_priv;
     164             :       const struct TALER_DenominationSignature *denom_sig;
     165             :       const struct TALER_Amount *denom_value;
     166             :       const struct TALER_EXCHANGE_DenomPublicKey *denom_pub;
     167             : 
     168           0 :       GNUNET_assert
     169             :         (GNUNET_OK == TALER_TESTING_get_trait_coin_priv
     170             :           (coin_cmd, 0, &coin_priv));
     171             : 
     172           0 :       GNUNET_assert
     173             :         (GNUNET_OK == TALER_TESTING_get_trait_denom_pub
     174             :           (coin_cmd, 0, &denom_pub));
     175             : 
     176           0 :       GNUNET_assert
     177             :         (GNUNET_OK == TALER_TESTING_get_trait_denom_sig
     178             :           (coin_cmd, 0, &denom_sig));
     179             : 
     180           0 :       GNUNET_assert
     181             :         (GNUNET_OK == TALER_TESTING_get_trait_amount_obj
     182             :           (coin_cmd, 0, &denom_value));
     183             : 
     184           0 :       icoin->coin_priv = *coin_priv;
     185           0 :       icoin->denom_pub = denom_pub->key;
     186           0 :       icoin->denom_sig = *denom_sig;
     187           0 :       icoin->denom_value = *denom_value;
     188           0 :       icoin->amount_with_fee = *denom_value;
     189             :     }
     190           0 :     GNUNET_assert (NULL != (dpk =
     191             :                               TALER_TESTING_find_pk (is->keys,
     192             :                                                      &icoin->denom_value)));
     193             : 
     194           0 :     GNUNET_assert (0 <=
     195             :                    TALER_amount_subtract (&icoin->amount_without_fee,
     196             :                                           &icoin->denom_value,
     197             :                                           &dpk->fee_deposit));
     198           0 :     GNUNET_assert (GNUNET_OK ==
     199             :                    TALER_TESTING_get_trait_url (coin_cmd,
     200             :                                                 TALER_TESTING_UT_EXCHANGE_BASE_URL,
     201             :                                                 &icoin->exchange_url));
     202             :   }
     203             : 
     204           0 :   return GNUNET_OK;
     205             : }
     206             : 
     207             : 
     208             : /**
     209             :  * Function called with the result of a /pay operation.
     210             :  * Checks whether the merchant signature is valid and the
     211             :  * HTTP response code matches our expectation.
     212             :  *
     213             :  * @param cls closure with the interpreter state
     214             :  * @param hr HTTP response
     215             :  * @param merchant_sig signature affirming payment,
     216             :  *        NULL on errors
     217             :  */
     218             : static void
     219           0 : pay_cb (void *cls,
     220             :         const struct TALER_MERCHANT_HttpResponse *hr,
     221             :         const struct TALER_MerchantSignatureP *merchant_sig)
     222             : {
     223           0 :   struct PayState *ps = cls;
     224             : 
     225           0 :   ps->oph = NULL;
     226           0 :   if (ps->http_status != hr->http_status)
     227             :   {
     228           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     229             :                 "Unexpected response code %u (%d) to command %s\n",
     230             :                 hr->http_status,
     231             :                 (int) hr->ec,
     232             :                 TALER_TESTING_interpreter_get_current_label (ps->is));
     233           0 :     TALER_TESTING_FAIL (ps->is);
     234             :   }
     235           0 :   if (MHD_HTTP_OK == hr->http_status)
     236             :   {
     237           0 :     ps->merchant_sig = *merchant_sig;
     238             :   }
     239           0 :   TALER_TESTING_interpreter_next (ps->is);
     240             : }
     241             : 
     242             : 
     243             : /**
     244             :  * Run a "pay" CMD.
     245             :  *
     246             :  * @param cls closure.
     247             :  * @param cmd current CMD being run.
     248             :  * @param is interpreter state.
     249             :  */
     250             : static void
     251           0 : pay_run (void *cls,
     252             :          const struct TALER_TESTING_Command *cmd,
     253             :          struct TALER_TESTING_Interpreter *is)
     254             : {
     255           0 :   struct PayState *ps = cls;
     256             :   const struct TALER_TESTING_Command *proposal_cmd;
     257             :   const json_t *contract_terms;
     258             :   const char *order_id;
     259             :   struct GNUNET_TIME_Absolute refund_deadline;
     260             :   struct GNUNET_TIME_Absolute pay_deadline;
     261             :   struct GNUNET_TIME_Absolute timestamp;
     262             :   struct TALER_MerchantPublicKeyP merchant_pub;
     263             :   struct GNUNET_HashCode h_wire;
     264             :   const struct GNUNET_HashCode *h_proposal;
     265             :   struct TALER_Amount total_amount;
     266             :   struct TALER_Amount max_fee;
     267             :   const char *error_name;
     268             :   unsigned int error_line;
     269             :   struct TALER_MERCHANT_PayCoin *pay_coins;
     270             :   unsigned int npay_coins;
     271             :   struct TALER_MerchantSignatureP *merchant_sig;
     272             : 
     273           0 :   ps->is = is;
     274           0 :   proposal_cmd = TALER_TESTING_interpreter_lookup_command (
     275             :     is,
     276             :     ps->proposal_reference);
     277             : 
     278           0 :   if (NULL == proposal_cmd)
     279           0 :     TALER_TESTING_FAIL (is);
     280             : 
     281           0 :   if (GNUNET_OK !=
     282           0 :       TALER_TESTING_get_trait_contract_terms (proposal_cmd,
     283             :                                               0,
     284             :                                               &contract_terms))
     285           0 :     TALER_TESTING_FAIL (is);
     286             :   {
     287             :     /* Get information that needs to be put verbatim in the
     288             :      * deposit permission */
     289             :     struct GNUNET_JSON_Specification spec[] = {
     290           0 :       GNUNET_JSON_spec_string ("order_id",
     291             :                                &order_id),
     292           0 :       TALER_JSON_spec_absolute_time ("refund_deadline",
     293             :                                      &refund_deadline),
     294           0 :       TALER_JSON_spec_absolute_time ("pay_deadline",
     295             :                                      &pay_deadline),
     296           0 :       TALER_JSON_spec_absolute_time ("timestamp",
     297             :                                      &timestamp),
     298           0 :       GNUNET_JSON_spec_fixed_auto ("merchant_pub",
     299             :                                    &merchant_pub),
     300           0 :       GNUNET_JSON_spec_fixed_auto ("h_wire",
     301             :                                    &h_wire),
     302           0 :       TALER_JSON_spec_amount ("amount",
     303             :                               &total_amount),
     304           0 :       TALER_JSON_spec_amount ("max_fee",
     305             :                               &max_fee),
     306           0 :       GNUNET_JSON_spec_end ()
     307             :     };
     308             : 
     309           0 :     if (GNUNET_OK !=
     310           0 :         GNUNET_JSON_parse (contract_terms,
     311             :                            spec,
     312             :                            &error_name,
     313             :                            &error_line))
     314             :     {
     315             :       char *js;
     316             : 
     317           0 :       js = json_dumps (contract_terms,
     318             :                        JSON_INDENT (1));
     319           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     320             :                   "Parser failed on %s:%u for input `%s'\n",
     321             :                   error_name,
     322             :                   error_line,
     323             :                   js);
     324           0 :       free (js);
     325           0 :       TALER_TESTING_FAIL (is);
     326             :     }
     327             :   }
     328             : 
     329             :   {
     330             :     char *cr;
     331             : 
     332           0 :     cr = GNUNET_strdup (ps->coin_reference);
     333           0 :     pay_coins = NULL;
     334           0 :     npay_coins = 0;
     335           0 :     if (GNUNET_OK !=
     336           0 :         build_coins (&pay_coins,
     337             :                      &npay_coins,
     338             :                      cr,
     339             :                      is,
     340             :                      ps->amount_with_fee,
     341             :                      ps->amount_without_fee))
     342             :     {
     343           0 :       GNUNET_array_grow (pay_coins,
     344             :                          npay_coins,
     345             :                          0);
     346           0 :       GNUNET_free (cr);
     347           0 :       TALER_TESTING_FAIL (is);
     348             :     }
     349           0 :     GNUNET_free (cr);
     350             :   }
     351           0 :   if (GNUNET_OK !=
     352           0 :       TALER_TESTING_get_trait_merchant_sig (proposal_cmd,
     353             :                                             0,
     354             :                                             &merchant_sig))
     355           0 :     TALER_TESTING_FAIL (is);
     356             : 
     357           0 :   if (GNUNET_OK !=
     358           0 :       TALER_TESTING_get_trait_h_contract_terms (proposal_cmd,
     359             :                                                 0,
     360             :                                                 &h_proposal))
     361           0 :     TALER_TESTING_FAIL (is);
     362           0 :   ps->h_contract_terms = *h_proposal;
     363           0 :   ps->oph = TALER_MERCHANT_order_pay (is->ctx,
     364             :                                       ps->merchant_url,
     365             :                                       ps->session_id,
     366             :                                       h_proposal,
     367             :                                       &total_amount,
     368             :                                       &max_fee,
     369             :                                       &merchant_pub,
     370             :                                       merchant_sig,
     371             :                                       timestamp,
     372             :                                       refund_deadline,
     373             :                                       pay_deadline,
     374             :                                       &h_wire,
     375             :                                       order_id,
     376             :                                       npay_coins,
     377             :                                       pay_coins,
     378             :                                       &pay_cb,
     379             :                                       ps);
     380           0 :   GNUNET_array_grow (pay_coins,
     381             :                      npay_coins,
     382             :                      0);
     383           0 :   if (NULL == ps->oph)
     384           0 :     TALER_TESTING_FAIL (is);
     385             : }
     386             : 
     387             : 
     388             : /**
     389             :  * Free a "pay" CMD, and cancel it if need be.
     390             :  *
     391             :  * @param cls closure.
     392             :  * @param cmd command currently being freed.
     393             :  */
     394             : static void
     395           0 : pay_cleanup (void *cls,
     396             :              const struct TALER_TESTING_Command *cmd)
     397             : {
     398           0 :   struct PayState *ps = cls;
     399             : 
     400           0 :   if (NULL != ps->oph)
     401             :   {
     402           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     403             :                 "Command `%s' did not complete.\n",
     404             :                 TALER_TESTING_interpreter_get_current_label (
     405             :                   ps->is));
     406           0 :     TALER_MERCHANT_order_pay_cancel (ps->oph);
     407             :   }
     408             : 
     409           0 :   GNUNET_free (ps);
     410           0 : }
     411             : 
     412             : 
     413             : /**
     414             :  * Offer internal data useful to other commands.
     415             :  *
     416             :  * @param cls closure
     417             :  * @param ret[out] result
     418             :  * @param trait name of the trait
     419             :  * @param index index number of the object to extract.
     420             :  * @return #GNUNET_OK on success
     421             :  */
     422             : static int
     423           0 : pay_traits (void *cls,
     424             :             const void **ret,
     425             :             const char *trait,
     426             :             unsigned int index)
     427             : {
     428             : 
     429           0 :   struct PayState *ps = cls;
     430             :   const char *order_id;
     431             :   const struct TALER_TESTING_Command *proposal_cmd;
     432             :   const struct TALER_MerchantPublicKeyP *merchant_pub;
     433             : 
     434           0 :   if (NULL ==
     435           0 :       (proposal_cmd = TALER_TESTING_interpreter_lookup_command
     436             :                         (ps->is, ps->proposal_reference)))
     437             :   {
     438           0 :     GNUNET_break (0);
     439           0 :     return GNUNET_SYSERR;
     440             :   }
     441             : 
     442           0 :   if (GNUNET_OK != TALER_TESTING_get_trait_order_id
     443             :         (proposal_cmd, 0, &order_id))
     444             :   {
     445           0 :     GNUNET_break (0);
     446           0 :     return GNUNET_SYSERR;
     447             :   }
     448             : 
     449           0 :   if (GNUNET_OK != TALER_TESTING_get_trait_merchant_pub
     450             :         (proposal_cmd,
     451             :         0,
     452             :         &merchant_pub))
     453             :   {
     454           0 :     GNUNET_break (0);
     455           0 :     return GNUNET_SYSERR;
     456             :   }
     457             :   {
     458             :     struct TALER_TESTING_Trait traits[] = {
     459           0 :       TALER_TESTING_make_trait_proposal_reference (0,
     460             :                                                    ps->proposal_reference),
     461           0 :       TALER_TESTING_make_trait_coin_reference (0,
     462             :                                                ps->coin_reference),
     463           0 :       TALER_TESTING_make_trait_order_id (0,
     464             :                                          order_id),
     465           0 :       TALER_TESTING_make_trait_merchant_pub (0,
     466             :                                              merchant_pub),
     467           0 :       TALER_TESTING_make_trait_merchant_sig (0,
     468           0 :                                              &ps->merchant_sig),
     469           0 :       TALER_TESTING_make_trait_string (0,
     470             :                                        ps->amount_with_fee),
     471           0 :       TALER_TESTING_trait_end ()
     472             :     };
     473             : 
     474           0 :     return TALER_TESTING_get_trait (traits,
     475             :                                     ret,
     476             :                                     trait,
     477             :                                     index);
     478             :   }
     479             : }
     480             : 
     481             : 
     482             : /**
     483             :  * Make a "pay" test command.
     484             :  *
     485             :  * @param label command label.
     486             :  * @param merchant_url merchant base url
     487             :  * @param http_status expected HTTP response code.
     488             :  * @param proposal_reference the proposal whose payment status
     489             :  *        is going to be checked.
     490             :  * @param coin_reference reference to any command which is able
     491             :  *        to provide coins to use for paying.
     492             :  * @param amount_with_fee amount to pay, including the deposit
     493             :  *        fee
     494             :  * @param amount_without_fee amount to pay, no fees included.
     495             :  * @param session_id the session id to use for the payment (can be NULL).
     496             :  * @return the command
     497             :  */
     498             : struct TALER_TESTING_Command
     499           0 : TALER_TESTING_cmd_merchant_pay_order (const char *label,
     500             :                                       const char *merchant_url,
     501             :                                       unsigned int http_status,
     502             :                                       const char *proposal_reference,
     503             :                                       const char *coin_reference,
     504             :                                       const char *amount_with_fee,
     505             :                                       const char *amount_without_fee,
     506             :                                       const char *session_id)
     507             : {
     508             :   struct PayState *ps;
     509             : 
     510           0 :   ps = GNUNET_new (struct PayState);
     511           0 :   ps->http_status = http_status;
     512           0 :   ps->proposal_reference = proposal_reference;
     513           0 :   ps->coin_reference = coin_reference;
     514           0 :   ps->merchant_url = merchant_url;
     515           0 :   ps->amount_with_fee = amount_with_fee;
     516           0 :   ps->amount_without_fee = amount_without_fee;
     517           0 :   ps->session_id = session_id;
     518             :   {
     519           0 :     struct TALER_TESTING_Command cmd = {
     520             :       .cls = ps,
     521             :       .label = label,
     522             :       .run = &pay_run,
     523             :       .cleanup = &pay_cleanup,
     524             :       .traits = &pay_traits
     525             :     };
     526             : 
     527           0 :     return cmd;
     528             :   }
     529             : }
     530             : 
     531             : 
     532             : /* end of testing_api_cmd_pay_order.c */

Generated by: LCOV version 1.14