LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_claim_order.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 71 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 5 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             : /**
      21             :  * @file exchange/testing_api_cmd_claim_order.c
      22             :  * @brief command to claim an order
      23             :  * @author Marcello Stanisci
      24             :  */
      25             : #include "platform.h"
      26             : #include <taler/taler_exchange_service.h>
      27             : #include <taler/taler_testing_lib.h>
      28             : #include "taler_merchant_service.h"
      29             : #include "taler_merchant_testing_lib.h"
      30             : 
      31             : /**
      32             :  * State for a "order claim" CMD.  Not used by
      33             :  * the initial claim operation.
      34             :  */
      35             : struct OrderClaimState
      36             : {
      37             :   /**
      38             :    * The interpreter state.
      39             :    */
      40             :   struct TALER_TESTING_Interpreter *is;
      41             : 
      42             :   /**
      43             :    * URL of the merchant backend.
      44             :    */
      45             :   const char *merchant_url;
      46             : 
      47             :   /**
      48             :    * Contract terms we downloaded. Only set if we got #MHD_HTTP_OK.
      49             :    */
      50             :   json_t *contract_terms;
      51             : 
      52             :   /**
      53             :    * Hash over the contract terms. Only set if we got #MHD_HTTP_OK.
      54             :    */
      55             :   struct GNUNET_HashCode contract_terms_hash;
      56             : 
      57             :   /**
      58             :    * Signature of the merchant. Only set if we got #MHD_HTTP_OK.
      59             :    */
      60             :   struct TALER_MerchantSignatureP merchant_sig;
      61             : 
      62             :   /**
      63             :    * Public key of the merchant. Only set if we got #MHD_HTTP_OK.
      64             :    */
      65             :   struct TALER_MerchantPublicKeyP merchant_pub;
      66             : 
      67             :   /**
      68             :    * Expected status code.
      69             :    */
      70             :   unsigned int http_status;
      71             : 
      72             :   /**
      73             :    * /order/claim operation handle.
      74             :    */
      75             :   struct TALER_MERCHANT_OrderClaimHandle *och;
      76             : 
      77             :   /**
      78             :    * Reference to a order operation.  Will offer the
      79             :    * nonce for the operation.
      80             :    */
      81             :   const char *order_reference;
      82             : 
      83             :   /**
      84             :    * Order id to claim upon.  If null, the @a order_reference
      85             :    * will offer this value.
      86             :    */
      87             :   const char *order_id;
      88             : };
      89             : 
      90             : 
      91             : /**
      92             :  * Free the state of a "order claim" CMD, and possibly
      93             :  * cancel it if it did not complete.
      94             :  *
      95             :  * @param cls closure.
      96             :  * @param cmd command being freed.
      97             :  */
      98             : static void
      99           0 : order_claim_cleanup (void *cls,
     100             :                      const struct TALER_TESTING_Command *cmd)
     101             : {
     102           0 :   struct OrderClaimState *pls = cls;
     103             : 
     104           0 :   if (NULL != pls->och)
     105             :   {
     106           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     107             :                 "Command '%s' did not complete\n",
     108             :                 cmd->label);
     109           0 :     TALER_MERCHANT_order_claim_cancel (pls->och);
     110           0 :     pls->och = NULL;
     111             :   }
     112           0 :   if (NULL != pls->contract_terms)
     113             :   {
     114           0 :     json_decref (pls->contract_terms);
     115           0 :     pls->contract_terms = NULL;
     116             :   }
     117           0 :   GNUNET_free (pls);
     118           0 : }
     119             : 
     120             : 
     121             : /**
     122             :  * Callback for "order claim" operation, to check the
     123             :  * response code is as expected.
     124             :  *
     125             :  * @param cls closure
     126             :  * @param hr HTTP response we got
     127             :  * @param contract_terms the contract terms; they are the
     128             :  *        backend-filled up order minus cryptographic
     129             :  *        information.
     130             :  * @param sig merchant signature over the contract terms.
     131             :  * @param hash hash code of the contract terms.
     132             :  */
     133             : static void
     134           0 : order_claim_cb (void *cls,
     135             :                 const struct TALER_MERCHANT_HttpResponse *hr,
     136             :                 const json_t *contract_terms,
     137             :                 const struct TALER_MerchantSignatureP *sig,
     138             :                 const struct GNUNET_HashCode *hash)
     139             : {
     140           0 :   struct OrderClaimState *pls = cls;
     141             : 
     142           0 :   pls->och = NULL;
     143           0 :   if (pls->http_status != hr->http_status)
     144           0 :     TALER_TESTING_FAIL (pls->is);
     145           0 :   if (MHD_HTTP_OK == hr->http_status)
     146             :   {
     147           0 :     pls->contract_terms = json_object_get (hr->reply,
     148             :                                            "contract_terms");
     149           0 :     if (NULL == pls->contract_terms)
     150           0 :       TALER_TESTING_FAIL (pls->is);
     151           0 :     json_incref (pls->contract_terms);
     152           0 :     pls->contract_terms_hash = *hash;
     153           0 :     pls->merchant_sig = *sig;
     154             :     {
     155             :       const char *error_name;
     156             :       unsigned int error_line;
     157             :       struct GNUNET_JSON_Specification spec[] = {
     158           0 :         GNUNET_JSON_spec_fixed_auto ("merchant_pub",
     159             :                                      &pls->merchant_pub),
     160           0 :         GNUNET_JSON_spec_end ()
     161             :       };
     162             : 
     163           0 :       if (GNUNET_OK !=
     164           0 :           GNUNET_JSON_parse (contract_terms,
     165             :                              spec,
     166             :                              &error_name,
     167             :                              &error_line))
     168           0 :         TALER_TESTING_FAIL (pls->is);
     169             :     }
     170             :   }
     171           0 :   TALER_TESTING_interpreter_next (pls->is);
     172             : }
     173             : 
     174             : 
     175             : /**
     176             :  * Run the "order claim" CMD.
     177             :  *
     178             :  * @param cls closure.
     179             :  * @param cmd command currently being run.
     180             :  * @param is interpreter state.
     181             :  */
     182             : static void
     183           0 : order_claim_run (void *cls,
     184             :                  const struct TALER_TESTING_Command *cmd,
     185             :                  struct TALER_TESTING_Interpreter *is)
     186             : {
     187           0 :   struct OrderClaimState *pls = cls;
     188             :   const char *order_id;
     189             :   const struct GNUNET_CRYPTO_EddsaPublicKey *nonce;
     190             :   /* Only used if we do NOT use the nonce/token from traits.  */
     191             :   struct GNUNET_CRYPTO_EddsaPublicKey dummy_nonce;
     192             :   const struct TALER_ClaimTokenP *claim_token;
     193             : 
     194           0 :   pls->is = is;
     195           0 :   if (NULL != pls->order_id)
     196             :   {
     197           0 :     order_id = pls->order_id;
     198           0 :     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
     199             :                                 &dummy_nonce,
     200             :                                 sizeof (dummy_nonce));
     201           0 :     nonce = &dummy_nonce;
     202             :   }
     203             :   else
     204             :   {
     205             :     const struct TALER_TESTING_Command *order_cmd;
     206             : 
     207             :     order_cmd
     208           0 :       = TALER_TESTING_interpreter_lookup_command (is,
     209             :                                                   pls->order_reference);
     210           0 :     if (NULL == order_cmd)
     211           0 :       TALER_TESTING_FAIL (is);
     212           0 :     if (GNUNET_OK !=
     213           0 :         TALER_TESTING_get_trait_claim_nonce (order_cmd,
     214             :                                              0,
     215             :                                              &nonce))
     216             :     {
     217           0 :       GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
     218             :                                   &dummy_nonce,
     219             :                                   sizeof (dummy_nonce));
     220           0 :       nonce = &dummy_nonce;
     221             :     }
     222           0 :     if (GNUNET_OK !=
     223           0 :         TALER_TESTING_get_trait_claim_token (order_cmd,
     224             :                                              0,
     225             :                                              &claim_token))
     226             :     {
     227           0 :       TALER_TESTING_FAIL (is);
     228             :     }
     229             : 
     230           0 :     if (GNUNET_OK !=
     231           0 :         TALER_TESTING_get_trait_order_id (order_cmd,
     232             :                                           0,
     233             :                                           &order_id))
     234           0 :       TALER_TESTING_FAIL (is);
     235             :   }
     236           0 :   pls->och = TALER_MERCHANT_order_claim (is->ctx,
     237             :                                          pls->merchant_url,
     238             :                                          order_id,
     239             :                                          nonce,
     240             :                                          claim_token,
     241             :                                          &order_claim_cb,
     242             :                                          pls);
     243           0 :   GNUNET_assert (NULL != pls->och);
     244             : }
     245             : 
     246             : 
     247             : /**
     248             :  * Offer internal data to other commands.
     249             :  *
     250             :  * @param cls closure
     251             :  * @param ret[out] result (could be anything)
     252             :  * @param trait name of the trait
     253             :  * @param index index number of the object to extract.
     254             :  * @return #GNUNET_OK on success
     255             :  */
     256             : static int
     257           0 : order_claim_traits (void *cls,
     258             :                     const void **ret,
     259             :                     const char *trait,
     260             :                     unsigned int index)
     261             : {
     262           0 :   struct OrderClaimState *pls = cls;
     263             :   struct TALER_TESTING_Trait traits[] = {
     264           0 :     TALER_TESTING_make_trait_contract_terms (0,
     265           0 :                                              pls->contract_terms),
     266           0 :     TALER_TESTING_make_trait_h_contract_terms (0,
     267           0 :                                                &pls->contract_terms_hash),
     268           0 :     TALER_TESTING_make_trait_merchant_sig (0,
     269           0 :                                            &pls->merchant_sig),
     270           0 :     TALER_TESTING_make_trait_merchant_pub (0,
     271           0 :                                            &pls->merchant_pub),
     272           0 :     TALER_TESTING_trait_end ()
     273             :   };
     274             : 
     275           0 :   return TALER_TESTING_get_trait (traits,
     276             :                                   ret,
     277             :                                   trait,
     278             :                                   index);
     279             : }
     280             : 
     281             : 
     282             : /**
     283             :  * Make a "order claim" command.
     284             :  *
     285             :  * @param label command label.
     286             :  * @param merchant_url base URL of the merchant backend
     287             :  *        serving the order claim request.
     288             :  * @param http_status expected HTTP response code.
     289             :  * @param order_reference reference to a POST order CMD, can be NULL if @a order_id given
     290             :  * @param order_id order id to lookup, can be NULL (then we use @a order_reference)
     291             :  * @return the command.
     292             :  */
     293             : struct TALER_TESTING_Command
     294           0 : TALER_TESTING_cmd_merchant_claim_order (
     295             :   const char *label,
     296             :   const char *merchant_url,
     297             :   unsigned int http_status,
     298             :   const char *order_reference,
     299             :   const char *order_id)
     300             : {
     301             :   struct OrderClaimState *pls;
     302             : 
     303           0 :   pls = GNUNET_new (struct OrderClaimState);
     304           0 :   pls->http_status = http_status;
     305           0 :   pls->order_reference = order_reference;
     306           0 :   pls->merchant_url = merchant_url;
     307           0 :   pls->order_id = order_id;
     308             :   {
     309           0 :     struct TALER_TESTING_Command cmd = {
     310             :       .cls = pls,
     311             :       .label = label,
     312             :       .run = &order_claim_run,
     313             :       .cleanup = &order_claim_cleanup,
     314             :       .traits = &order_claim_traits
     315             :     };
     316             : 
     317           0 :     return cmd;
     318             :   }
     319             : }

Generated by: LCOV version 1.14