LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_set_wire_fee.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 37 52 71.2 %
Date: 2021-08-30 06:43:37 Functions: 4 5 80.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 it
       6             :   under the terms of the GNU General Public License as published by
       7             :   the Free Software Foundation; either version 3, or (at your
       8             :   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 GNU
      13             :   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/testing_api_cmd_set_wire_fee.c
      21             :  * @brief command for testing POST to /management/wire-fees
      22             :  * @author Christian Grothoff
      23             :  */
      24             : #include "platform.h"
      25             : #include "taler_json_lib.h"
      26             : #include <gnunet/gnunet_curl_lib.h>
      27             : #include "taler_testing_lib.h"
      28             : #include "taler_signatures.h"
      29             : #include "backoff.h"
      30             : 
      31             : 
      32             : /**
      33             :  * State for a "wire_add" CMD.
      34             :  */
      35             : struct WireFeeState
      36             : {
      37             : 
      38             :   /**
      39             :    * Wire enable handle while operation is running.
      40             :    */
      41             :   struct TALER_EXCHANGE_ManagementSetWireFeeHandle *dh;
      42             : 
      43             :   /**
      44             :    * Our interpreter.
      45             :    */
      46             :   struct TALER_TESTING_Interpreter *is;
      47             : 
      48             :   /**
      49             :    * Wire method to configure fee for.
      50             :    */
      51             :   const char *wire_method;
      52             : 
      53             :   /**
      54             :    * Wire fee amount to use.
      55             :    */
      56             :   const char *wire_fee;
      57             : 
      58             :   /**
      59             :    * Closing fee amount to use.
      60             :    */
      61             :   const char *closing_fee;
      62             : 
      63             :   /**
      64             :    * Expected HTTP response code.
      65             :    */
      66             :   unsigned int expected_response_code;
      67             : 
      68             :   /**
      69             :    * Should we make the request with a bad master_sig signature?
      70             :    */
      71             :   bool bad_sig;
      72             : };
      73             : 
      74             : 
      75             : /**
      76             :  * Callback to analyze the /management/wire response, just used to check
      77             :  * if the response code is acceptable.
      78             :  *
      79             :  * @param cls closure.
      80             :  * @param hr HTTP response details
      81             :  */
      82             : static void
      83           5 : wire_add_cb (void *cls,
      84             :              const struct TALER_EXCHANGE_HttpResponse *hr)
      85             : {
      86           5 :   struct WireFeeState *ds = cls;
      87             : 
      88           5 :   ds->dh = NULL;
      89           5 :   if (ds->expected_response_code != hr->http_status)
      90             :   {
      91           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      92             :                 "Unexpected response code %u to command %s in %s:%u\n",
      93             :                 hr->http_status,
      94             :                 ds->is->commands[ds->is->ip].label,
      95             :                 __FILE__,
      96             :                 __LINE__);
      97           0 :     json_dumpf (hr->reply,
      98             :                 stderr,
      99             :                 0);
     100           0 :     TALER_TESTING_interpreter_fail (ds->is);
     101           0 :     return;
     102             :   }
     103           5 :   TALER_TESTING_interpreter_next (ds->is);
     104             : }
     105             : 
     106             : 
     107             : /**
     108             :  * Run the command.
     109             :  *
     110             :  * @param cls closure.
     111             :  * @param cmd the command to execute.
     112             :  * @param is the interpreter state.
     113             :  */
     114             : static void
     115           5 : wire_add_run (void *cls,
     116             :               const struct TALER_TESTING_Command *cmd,
     117             :               struct TALER_TESTING_Interpreter *is)
     118             : {
     119           5 :   struct WireFeeState *ds = cls;
     120             :   struct TALER_MasterSignatureP master_sig;
     121             :   struct GNUNET_TIME_Absolute now;
     122             :   struct GNUNET_TIME_Absolute start_time;
     123             :   struct GNUNET_TIME_Absolute end_time;
     124             :   struct TALER_Amount wire_fee;
     125             :   struct TALER_Amount closing_fee;
     126             : 
     127             :   (void) cmd;
     128           5 :   ds->is = is;
     129           5 :   now = GNUNET_TIME_absolute_get ();
     130           5 :   (void) GNUNET_TIME_round_abs (&now);
     131           5 :   start_time = GNUNET_TIME_absolute_subtract (now,
     132             :                                               GNUNET_TIME_UNIT_HOURS);
     133           5 :   end_time = GNUNET_TIME_absolute_add (now,
     134             :                                        GNUNET_TIME_UNIT_HOURS);
     135           5 :   if ( (GNUNET_OK !=
     136           5 :         TALER_string_to_amount (ds->closing_fee,
     137           5 :                                 &closing_fee)) ||
     138             :        (GNUNET_OK !=
     139           5 :         TALER_string_to_amount (ds->wire_fee,
     140             :                                 &wire_fee)) )
     141             :   {
     142           0 :     GNUNET_break (0);
     143           0 :     TALER_TESTING_interpreter_fail (is);
     144           0 :     return;
     145             :   }
     146             : 
     147           5 :   if (ds->bad_sig)
     148             :   {
     149           1 :     memset (&master_sig,
     150             :             42,
     151             :             sizeof (master_sig));
     152             :   }
     153             :   else
     154             :   {
     155           4 :     TALER_exchange_offline_wire_fee_sign (ds->wire_method,
     156             :                                           start_time,
     157             :                                           end_time,
     158             :                                           &wire_fee,
     159             :                                           &closing_fee,
     160           4 :                                           &is->master_priv,
     161             :                                           &master_sig);
     162             :   }
     163          10 :   ds->dh = TALER_EXCHANGE_management_set_wire_fees (
     164             :     is->ctx,
     165           5 :     is->exchange_url,
     166             :     ds->wire_method,
     167             :     start_time,
     168             :     end_time,
     169             :     &wire_fee,
     170             :     &closing_fee,
     171             :     &master_sig,
     172             :     &wire_add_cb,
     173             :     ds);
     174           5 :   if (NULL == ds->dh)
     175             :   {
     176           0 :     GNUNET_break (0);
     177           0 :     TALER_TESTING_interpreter_fail (is);
     178           0 :     return;
     179             :   }
     180             : }
     181             : 
     182             : 
     183             : /**
     184             :  * Free the state of a "wire_add" CMD, and possibly cancel a
     185             :  * pending operation thereof.
     186             :  *
     187             :  * @param cls closure, must be a `struct WireFeeState`.
     188             :  * @param cmd the command which is being cleaned up.
     189             :  */
     190             : static void
     191           5 : wire_add_cleanup (void *cls,
     192             :                   const struct TALER_TESTING_Command *cmd)
     193             : {
     194           5 :   struct WireFeeState *ds = cls;
     195             : 
     196           5 :   if (NULL != ds->dh)
     197             :   {
     198           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     199             :                 "Command %u (%s) did not complete\n",
     200             :                 ds->is->ip,
     201             :                 cmd->label);
     202           0 :     TALER_EXCHANGE_management_set_wire_fees_cancel (ds->dh);
     203           0 :     ds->dh = NULL;
     204             :   }
     205           5 :   GNUNET_free (ds);
     206           5 : }
     207             : 
     208             : 
     209             : /**
     210             :  * Offer internal data from a "wire_add" CMD, to other commands.
     211             :  *
     212             :  * @param cls closure.
     213             :  * @param[out] ret result.
     214             :  * @param trait name of the trait.
     215             :  * @param index index number of the object to offer.
     216             :  *
     217             :  * @return #GNUNET_OK on success.
     218             :  */
     219             : static int
     220           0 : wire_add_traits (void *cls,
     221             :                  const void **ret,
     222             :                  const char *trait,
     223             :                  unsigned int index)
     224             : {
     225           0 :   return GNUNET_NO;
     226             : }
     227             : 
     228             : 
     229             : struct TALER_TESTING_Command
     230           5 : TALER_TESTING_cmd_set_wire_fee (const char *label,
     231             :                                 const char *wire_method,
     232             :                                 const char *wire_fee,
     233             :                                 const char *closing_fee,
     234             :                                 unsigned int expected_http_status,
     235             :                                 bool bad_sig)
     236             : {
     237             :   struct WireFeeState *ds;
     238             : 
     239           5 :   ds = GNUNET_new (struct WireFeeState);
     240           5 :   ds->expected_response_code = expected_http_status;
     241           5 :   ds->bad_sig = bad_sig;
     242           5 :   ds->wire_method = wire_method;
     243           5 :   ds->wire_fee = wire_fee;
     244           5 :   ds->closing_fee = closing_fee;
     245             :   {
     246           5 :     struct TALER_TESTING_Command cmd = {
     247             :       .cls = ds,
     248             :       .label = label,
     249             :       .run = &wire_add_run,
     250             :       .cleanup = &wire_add_cleanup,
     251             :       .traits = &wire_add_traits
     252             :     };
     253             : 
     254           5 :     return cmd;
     255             :   }
     256             : }
     257             : 
     258             : 
     259             : /* end of testing_api_cmd_set_wire_fee.c */

Generated by: LCOV version 1.14