LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_patch_instance.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 72 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) 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_patch_instance.c
      21             :  * @brief command to test PATCH /instance
      22             :  * @author Christian Grothoff
      23             :  */
      24             : #include "platform.h"
      25             : #include <taler/taler_exchange_service.h>
      26             : #include <taler/taler_testing_lib.h>
      27             : #include "taler_merchant_service.h"
      28             : #include "taler_merchant_testing_lib.h"
      29             : 
      30             : 
      31             : /**
      32             :  * State of a "PATCH /instance" CMD.
      33             :  */
      34             : struct PatchInstanceState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "PATCH /instance/$ID" request.
      39             :    */
      40             :   struct TALER_MERCHANT_InstancePatchHandle *iph;
      41             : 
      42             :   /**
      43             :    * The interpreter state.
      44             :    */
      45             :   struct TALER_TESTING_Interpreter *is;
      46             : 
      47             :   /**
      48             :    * Base URL of the merchant serving the request.
      49             :    */
      50             :   const char *merchant_url;
      51             : 
      52             :   /**
      53             :    * ID of the instance to run PATCH for.
      54             :    */
      55             :   const char *instance_id;
      56             : 
      57             :   /**
      58             :    * Length of the @payto_uris array
      59             :    */
      60             :   unsigned int payto_uris_length;
      61             : 
      62             :   /**
      63             :    * Array of payto URIs.
      64             :    */
      65             :   const char **payto_uris;
      66             : 
      67             :   /**
      68             :    * Name of the instance.
      69             :    */
      70             :   const char *name;
      71             : 
      72             :   /**
      73             :    * Address to use.
      74             :    */
      75             :   json_t *address;
      76             : 
      77             :   /**
      78             :    * Jurisdiction to use.
      79             :    */
      80             :   json_t *jurisdiction;
      81             : 
      82             :   /**
      83             :    * Wire fee to use.
      84             :    */
      85             :   struct TALER_Amount default_max_wire_fee;
      86             : 
      87             :   /**
      88             :    * Amortization to use.
      89             :    */
      90             :   uint32_t default_wire_fee_amortization;
      91             : 
      92             :   /**
      93             :    * Deposit fee ceiling to use.
      94             :    */
      95             :   struct TALER_Amount default_max_deposit_fee;
      96             : 
      97             :   /**
      98             :    * Wire transfer delay to use.
      99             :    */
     100             :   struct GNUNET_TIME_Relative default_wire_transfer_delay;
     101             : 
     102             :   /**
     103             :    * Order validity default duration to use.
     104             :    */
     105             :   struct GNUNET_TIME_Relative default_pay_delay;
     106             : 
     107             :   /**
     108             :    * Expected HTTP response code.
     109             :    */
     110             :   unsigned int http_status;
     111             : 
     112             : };
     113             : 
     114             : 
     115             : /**
     116             :  * Callback for a PATCH /instances/$ID operation.
     117             :  *
     118             :  * @param cls closure for this function
     119             :  */
     120             : static void
     121           0 : patch_instance_cb (void *cls,
     122             :                    const struct TALER_MERCHANT_HttpResponse *hr)
     123             : {
     124           0 :   struct PatchInstanceState *pis = cls;
     125             : 
     126           0 :   pis->iph = NULL;
     127           0 :   if (pis->http_status != hr->http_status)
     128             :   {
     129           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     130             :                 "Unexpected response code %u (%d) to command %s\n",
     131             :                 hr->http_status,
     132             :                 (int) hr->ec,
     133             :                 TALER_TESTING_interpreter_get_current_label (pis->is));
     134           0 :     TALER_TESTING_interpreter_fail (pis->is);
     135           0 :     return;
     136             :   }
     137           0 :   switch (hr->http_status)
     138             :   {
     139           0 :   case MHD_HTTP_OK:
     140           0 :     break;
     141             :   // FIXME: add other legitimate states here...
     142           0 :   default:
     143           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     144             :                 "Unhandled HTTP status.\n");
     145             :   }
     146           0 :   TALER_TESTING_interpreter_next (pis->is);
     147             : }
     148             : 
     149             : 
     150             : /**
     151             :  * Run the "PATCH /instances/$ID" CMD.
     152             :  *
     153             :  *
     154             :  * @param cls closure.
     155             :  * @param cmd command being run now.
     156             :  * @param is interpreter state.
     157             :  */
     158             : static void
     159           0 : patch_instance_run (void *cls,
     160             :                     const struct TALER_TESTING_Command *cmd,
     161             :                     struct TALER_TESTING_Interpreter *is)
     162             : {
     163           0 :   struct PatchInstanceState *pis = cls;
     164             : 
     165           0 :   pis->is = is;
     166           0 :   pis->iph = TALER_MERCHANT_instance_patch (is->ctx,
     167             :                                             pis->merchant_url,
     168             :                                             pis->instance_id,
     169             :                                             pis->payto_uris_length,
     170             :                                             pis->payto_uris,
     171             :                                             pis->name,
     172           0 :                                             pis->address,
     173           0 :                                             pis->jurisdiction,
     174           0 :                                             &pis->default_max_wire_fee,
     175             :                                             pis->default_wire_fee_amortization,
     176           0 :                                             &pis->default_max_deposit_fee,
     177             :                                             pis->default_wire_transfer_delay,
     178             :                                             pis->default_pay_delay,
     179             :                                             &patch_instance_cb,
     180             :                                             pis);
     181           0 :   GNUNET_assert (NULL != pis->iph);
     182           0 : }
     183             : 
     184             : 
     185             : /**
     186             :  * Offers information from the PATCH /instances CMD state to other
     187             :  * commands.
     188             :  *
     189             :  * @param cls closure
     190             :  * @param ret[out] result (could be anything)
     191             :  * @param trait name of the trait
     192             :  * @param index index number of the object to extract.
     193             :  * @return #GNUNET_OK on success
     194             :  */
     195             : static int
     196           0 : patch_instance_traits (void *cls,
     197             :                        const void **ret,
     198             :                        const char *trait,
     199             :                        unsigned int index)
     200           0 : {
     201           0 :   struct PatchInstanceState *pis = cls;
     202             :   #define NUM_TRAITS (pis->payto_uris_length) + 11
     203           0 :   struct TALER_TESTING_Trait traits[NUM_TRAITS];
     204             :   traits[0] =
     205           0 :     TALER_TESTING_make_trait_string (0, pis->name);
     206             :   traits[1] =
     207           0 :     TALER_TESTING_make_trait_string (1, pis->instance_id);
     208             :   traits[2] =
     209           0 :     TALER_TESTING_make_trait_json (0, pis->address);
     210             :   traits[3] =
     211           0 :     TALER_TESTING_make_trait_json (1, pis->jurisdiction);
     212             :   traits[4] =
     213           0 :     TALER_TESTING_make_trait_amount_obj (0, &pis->default_max_wire_fee);
     214             :   traits[5] =
     215           0 :     TALER_TESTING_make_trait_uint32 (0, &pis->default_wire_fee_amortization);
     216             :   traits[6] =
     217           0 :     TALER_TESTING_make_trait_amount_obj (1, &pis->default_max_deposit_fee);
     218             :   traits[7] =
     219           0 :     TALER_TESTING_make_trait_relative_time (0,
     220           0 :                                             &pis->default_wire_transfer_delay);
     221             :   traits[8] =
     222           0 :     TALER_TESTING_make_trait_relative_time (1, &pis->default_pay_delay);
     223             :   traits[9] =
     224           0 :     TALER_TESTING_make_trait_uint32 (1, &pis->payto_uris_length);
     225           0 :   traits[NUM_TRAITS - 1] =
     226           0 :     TALER_TESTING_trait_end ();
     227           0 :   for (unsigned int i = 0; i < pis->payto_uris_length; ++i)
     228             :   {
     229           0 :     traits[10 + i] =
     230           0 :       TALER_TESTING_make_trait_string (2 + i, pis->payto_uris[i]);
     231             :   }
     232             : 
     233           0 :   return TALER_TESTING_get_trait (traits,
     234             :                                   ret,
     235             :                                   trait,
     236             :                                   index);
     237             : }
     238             : 
     239             : 
     240             : /**
     241             :  * Free the state of a "PATCH /instances/$ID" CMD, and possibly
     242             :  * cancel a pending operation thereof.
     243             :  *
     244             :  * @param cls closure.
     245             :  * @param cmd command being run.
     246             :  */
     247             : static void
     248           0 : patch_instance_cleanup (void *cls,
     249             :                         const struct TALER_TESTING_Command *cmd)
     250             : {
     251           0 :   struct PatchInstanceState *pis = cls;
     252             : 
     253           0 :   if (NULL != pis->iph)
     254             :   {
     255           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     256             :                 "PATCH /instance/$ID operation did not complete\n");
     257           0 :     TALER_MERCHANT_instance_patch_cancel (pis->iph);
     258             :   }
     259           0 :   json_decref (pis->address);
     260           0 :   json_decref (pis->jurisdiction);
     261           0 :   GNUNET_free (pis->payto_uris);
     262           0 :   GNUNET_free (pis);
     263           0 : }
     264             : 
     265             : 
     266             : /**
     267             :  * Define a "PATCH /instances/$ID" CMD.
     268             :  *
     269             :  * @param label command label.
     270             :  * @param merchant_url base URL of the merchant serving the
     271             :  *        PATCH /instance request.
     272             :  * @param instance_id the ID of the instance to query
     273             :  * @param payto_uris_length length of the @a accounts array
     274             :  * @param payto_uris URIs of the bank accounts of the merchant instance
     275             :  * @param name name of the merchant instance
     276             :  * @param address physical address of the merchant instance
     277             :  * @param jurisdiction jurisdiction of the merchant instance
     278             :  * @param default_max_wire_fee default maximum wire fee merchant is willing to fully pay
     279             :  * @param default_wire_fee_amortization default amortization factor for excess wire fees
     280             :  * @param default_max_deposit_fee default maximum deposit fee merchant is willing to pay
     281             :  * @param default_wire_transfer_delay default wire transfer delay merchant will ask for
     282             :  * @param default_pay_delay default validity period for offers merchant makes
     283             :  * @param http_status expected HTTP response code.
     284             :  * @return the command.
     285             :  */
     286             : struct TALER_TESTING_Command
     287           0 : TALER_TESTING_cmd_merchant_patch_instance (
     288             :   const char *label,
     289             :   const char *merchant_url,
     290             :   const char *instance_id,
     291             :   unsigned int payto_uris_length,
     292             :   const char *payto_uris[],
     293             :   const char *name,
     294             :   json_t *address,
     295             :   json_t *jurisdiction,
     296             :   const char *default_max_wire_fee,
     297             :   uint32_t default_wire_fee_amortization,
     298             :   const char *default_max_deposit_fee,
     299             :   struct GNUNET_TIME_Relative default_wire_transfer_delay,
     300             :   struct GNUNET_TIME_Relative default_pay_delay,
     301             :   unsigned int http_status)
     302             : {
     303             :   struct PatchInstanceState *pis;
     304             : 
     305           0 :   pis = GNUNET_new (struct PatchInstanceState);
     306           0 :   pis->merchant_url = merchant_url;
     307           0 :   pis->instance_id = instance_id;
     308           0 :   pis->http_status = http_status;
     309           0 :   pis->payto_uris_length = payto_uris_length;
     310           0 :   pis->payto_uris = GNUNET_new_array (payto_uris_length,
     311             :                                       const char *);
     312           0 :   memcpy (pis->payto_uris,
     313             :           payto_uris,
     314             :           sizeof (const char *) * payto_uris_length);
     315           0 :   pis->name = name;
     316           0 :   pis->address = address; /* ownership transfer! */
     317           0 :   pis->jurisdiction = jurisdiction; /* ownership transfer! */
     318           0 :   GNUNET_assert (GNUNET_OK ==
     319             :                  TALER_string_to_amount (default_max_wire_fee,
     320             :                                          &pis->default_max_wire_fee));
     321           0 :   pis->default_wire_fee_amortization = default_wire_fee_amortization;
     322           0 :   GNUNET_assert (GNUNET_OK ==
     323             :                  TALER_string_to_amount (default_max_deposit_fee,
     324             :                                          &pis->default_max_deposit_fee));
     325           0 :   pis->default_wire_transfer_delay = default_wire_transfer_delay;
     326           0 :   pis->default_pay_delay = default_pay_delay;
     327             :   {
     328           0 :     struct TALER_TESTING_Command cmd = {
     329             :       .cls = pis,
     330             :       .label = label,
     331             :       .run = &patch_instance_run,
     332             :       .cleanup = &patch_instance_cleanup,
     333             :       .traits = &patch_instance_traits
     334             :     };
     335             : 
     336           0 :     return cmd;
     337             :   }
     338             : }
     339             : 
     340             : 
     341             : /* end of testing_api_cmd_patch_instance.c */

Generated by: LCOV version 1.14