LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_set_wire_fee.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 71.2 % 59 42
Test Date: 2026-03-07 14:54:45 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2020, 2022 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 "taler/platform.h"
      25              : #include "taler/taler_json_lib.h"
      26              : #include <gnunet/gnunet_curl_lib.h>
      27              : #include "taler/taler_testing_lib.h"
      28              : #include "taler/taler_signatures.h"
      29              : #include "taler/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_PostManagementWireFeesHandle *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 sfr response details
      81              :  */
      82              : static void
      83           10 : wire_add_cb (void *cls,
      84              :              const struct TALER_EXCHANGE_PostManagementWireFeesResponse *sfr)
      85              : {
      86           10 :   struct WireFeeState *ds = cls;
      87           10 :   const struct TALER_EXCHANGE_HttpResponse *hr = &sfr->hr;
      88              : 
      89           10 :   ds->dh = NULL;
      90           10 :   if (ds->expected_response_code != hr->http_status)
      91              :   {
      92            0 :     TALER_TESTING_unexpected_status (ds->is,
      93              :                                      hr->http_status,
      94              :                                      ds->expected_response_code);
      95            0 :     return;
      96              :   }
      97           10 :   TALER_TESTING_interpreter_next (ds->is);
      98              : }
      99              : 
     100              : 
     101              : /**
     102              :  * Run the command.
     103              :  *
     104              :  * @param cls closure.
     105              :  * @param cmd the command to execute.
     106              :  * @param is the interpreter state.
     107              :  */
     108              : static void
     109           10 : wire_add_run (void *cls,
     110              :               const struct TALER_TESTING_Command *cmd,
     111              :               struct TALER_TESTING_Interpreter *is)
     112              : {
     113           10 :   struct WireFeeState *ds = cls;
     114              :   struct TALER_MasterSignatureP master_sig;
     115              :   struct GNUNET_TIME_Absolute now;
     116              :   struct GNUNET_TIME_Timestamp start_time;
     117              :   struct GNUNET_TIME_Timestamp end_time;
     118              :   struct TALER_WireFeeSet fees;
     119              :   const char *exchange_url;
     120              : 
     121              :   (void) cmd;
     122              :   {
     123              :     const struct TALER_TESTING_Command *exchange_cmd;
     124              : 
     125           10 :     exchange_cmd = TALER_TESTING_interpreter_get_command (is,
     126              :                                                           "exchange");
     127           10 :     if (NULL == exchange_cmd)
     128              :     {
     129            0 :       GNUNET_break (0);
     130            0 :       TALER_TESTING_interpreter_fail (is);
     131            0 :       return;
     132              :     }
     133           10 :     GNUNET_assert (GNUNET_OK ==
     134              :                    TALER_TESTING_get_trait_exchange_url (exchange_cmd,
     135              :                                                          &exchange_url));
     136              :   }
     137           10 :   ds->is = is;
     138           10 :   now = GNUNET_TIME_absolute_get ();
     139           10 :   start_time = GNUNET_TIME_absolute_to_timestamp (
     140              :     GNUNET_TIME_absolute_subtract (now,
     141              :                                    GNUNET_TIME_UNIT_HOURS));
     142           10 :   end_time = GNUNET_TIME_absolute_to_timestamp (
     143              :     GNUNET_TIME_absolute_add (now,
     144              :                               GNUNET_TIME_UNIT_HOURS));
     145           10 :   if ( (GNUNET_OK !=
     146           10 :         TALER_string_to_amount (ds->closing_fee,
     147           10 :                                 &fees.closing)) ||
     148              :        (GNUNET_OK !=
     149           10 :         TALER_string_to_amount (ds->wire_fee,
     150              :                                 &fees.wire)) )
     151              :   {
     152            0 :     GNUNET_break (0);
     153            0 :     TALER_TESTING_interpreter_fail (is);
     154            0 :     return;
     155              :   }
     156              : 
     157           10 :   if (ds->bad_sig)
     158              :   {
     159            2 :     memset (&master_sig,
     160              :             42,
     161              :             sizeof (master_sig));
     162              :   }
     163              :   else
     164              :   {
     165              :     const struct TALER_TESTING_Command *exchange_cmd;
     166              :     const struct TALER_MasterPrivateKeyP *master_priv;
     167              : 
     168            8 :     exchange_cmd = TALER_TESTING_interpreter_get_command (is,
     169              :                                                           "exchange");
     170            8 :     if (NULL == exchange_cmd)
     171              :     {
     172            0 :       GNUNET_break (0);
     173            0 :       TALER_TESTING_interpreter_fail (is);
     174            0 :       return;
     175              :     }
     176            8 :     GNUNET_assert (GNUNET_OK ==
     177              :                    TALER_TESTING_get_trait_master_priv (exchange_cmd,
     178              :                                                         &master_priv));
     179            8 :     TALER_exchange_offline_wire_fee_sign (ds->wire_method,
     180              :                                           start_time,
     181              :                                           end_time,
     182              :                                           &fees,
     183              :                                           master_priv,
     184              :                                           &master_sig);
     185              :   }
     186           10 :   ds->dh = TALER_EXCHANGE_post_management_wire_fees_create (
     187              :     TALER_TESTING_interpreter_get_context (is),
     188              :     exchange_url,
     189              :     ds->wire_method,
     190              :     start_time,
     191              :     end_time,
     192              :     &fees,
     193              :     &master_sig);
     194           10 :   if (NULL == ds->dh)
     195              :   {
     196            0 :     GNUNET_break (0);
     197            0 :     TALER_TESTING_interpreter_fail (is);
     198            0 :     return;
     199              :   }
     200           10 :   TALER_EXCHANGE_post_management_wire_fees_start (ds->dh, &wire_add_cb, ds);
     201              : }
     202              : 
     203              : 
     204              : /**
     205              :  * Free the state of a "wire_add" CMD, and possibly cancel a
     206              :  * pending operation thereof.
     207              :  *
     208              :  * @param cls closure, must be a `struct WireFeeState`.
     209              :  * @param cmd the command which is being cleaned up.
     210              :  */
     211              : static void
     212           10 : wire_add_cleanup (void *cls,
     213              :                   const struct TALER_TESTING_Command *cmd)
     214              : {
     215           10 :   struct WireFeeState *ds = cls;
     216              : 
     217           10 :   if (NULL != ds->dh)
     218              :   {
     219            0 :     TALER_TESTING_command_incomplete (ds->is,
     220              :                                       cmd->label);
     221            0 :     TALER_EXCHANGE_post_management_wire_fees_cancel (ds->dh);
     222            0 :     ds->dh = NULL;
     223              :   }
     224           10 :   GNUNET_free (ds);
     225           10 : }
     226              : 
     227              : 
     228              : struct TALER_TESTING_Command
     229           10 : TALER_TESTING_cmd_set_wire_fee (const char *label,
     230              :                                 const char *wire_method,
     231              :                                 const char *wire_fee,
     232              :                                 const char *closing_fee,
     233              :                                 unsigned int expected_http_status,
     234              :                                 bool bad_sig)
     235              : {
     236              :   struct WireFeeState *ds;
     237              : 
     238           10 :   ds = GNUNET_new (struct WireFeeState);
     239           10 :   ds->expected_response_code = expected_http_status;
     240           10 :   ds->bad_sig = bad_sig;
     241           10 :   ds->wire_method = wire_method;
     242           10 :   ds->wire_fee = wire_fee;
     243           10 :   ds->closing_fee = closing_fee;
     244              :   {
     245           10 :     struct TALER_TESTING_Command cmd = {
     246              :       .cls = ds,
     247              :       .label = label,
     248              :       .run = &wire_add_run,
     249              :       .cleanup = &wire_add_cleanup
     250              :     };
     251              : 
     252           10 :     return cmd;
     253              :   }
     254              : }
     255              : 
     256              : 
     257              : /* end of testing_api_cmd_set_wire_fee.c */
        

Generated by: LCOV version 2.0-1