LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_wire_add.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 74.1 % 54 40
Test Date: 2026-09-11 18:55:36 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2020-2024 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_wire_add.c
      21              :  * @brief command for testing POST to /management/wire
      22              :  * @author Christian Grothoff
      23              :  */
      24              : #include "taler/taler_json_lib.h"
      25              : #include <gnunet/gnunet_curl_lib.h>
      26              : struct WireAddState;
      27              : #define TALER_EXCHANGE_POST_MANAGEMENT_WIRE_RESULT_CLOSURE struct WireAddState
      28              : #include "taler/exchange/post-management-wire.h"
      29              : #include "taler/taler_testing_lib.h"
      30              : 
      31              : 
      32              : /**
      33              :  * State for a "wire_add" CMD.
      34              :  */
      35              : struct WireAddState
      36              : {
      37              : 
      38              :   /**
      39              :    * Wire enable handle while operation is running.
      40              :    */
      41              :   struct TALER_EXCHANGE_PostManagementWireHandle *dh;
      42              : 
      43              :   /**
      44              :    * Our interpreter.
      45              :    */
      46              :   struct TALER_TESTING_Interpreter *is;
      47              : 
      48              :   /**
      49              :    * Account to add.
      50              :    */
      51              :   struct TALER_FullPayto payto_uri;
      52              : 
      53              :   /**
      54              :    * Expected HTTP response code.
      55              :    */
      56              :   unsigned int expected_response_code;
      57              : 
      58              :   /**
      59              :    * Should we make the request with a bad master_sig signature?
      60              :    */
      61              :   bool bad_sig;
      62              : };
      63              : 
      64              : 
      65              : /**
      66              :  * Callback to analyze the /management/wire response, just used to check
      67              :  * if the response code is acceptable.
      68              :  *
      69              :  * @param ds closure.
      70              :  * @param wer response details
      71              :  */
      72              : static void
      73            8 : wire_add_cb (struct WireAddState *ds,
      74              :              const struct TALER_EXCHANGE_PostManagementWireResponse *wer)
      75              : {
      76            8 :   const struct TALER_EXCHANGE_HttpResponse *hr = &wer->hr;
      77              : 
      78            8 :   ds->dh = NULL;
      79            8 :   if (ds->expected_response_code != hr->http_status)
      80              :   {
      81            0 :     TALER_TESTING_unexpected_status (ds->is,
      82              :                                      hr->http_status,
      83              :                                      ds->expected_response_code);
      84            0 :     return;
      85              :   }
      86            8 :   TALER_TESTING_interpreter_next (ds->is);
      87              : }
      88              : 
      89              : 
      90              : /**
      91              :  * Run the command.
      92              :  *
      93              :  * @param cls closure.
      94              :  * @param cmd the command to execute.
      95              :  * @param is the interpreter state.
      96              :  */
      97              : static void
      98            8 : wire_add_run (void *cls,
      99              :               const struct TALER_TESTING_Command *cmd,
     100              :               struct TALER_TESTING_Interpreter *is)
     101              : {
     102            8 :   struct WireAddState *ds = cls;
     103              :   struct TALER_MasterSignatureP master_sig1;
     104              :   struct TALER_MasterSignatureP master_sig2;
     105              :   struct GNUNET_TIME_Timestamp now;
     106              :   json_t *credit_rest;
     107              :   json_t *debit_rest;
     108              :   const char *exchange_url;
     109              : 
     110              :   (void) cmd;
     111              :   {
     112              :     const struct TALER_TESTING_Command *exchange_cmd;
     113              : 
     114            8 :     exchange_cmd = TALER_TESTING_interpreter_get_command (is,
     115              :                                                           "exchange");
     116            8 :     if (NULL == exchange_cmd)
     117              :     {
     118            0 :       GNUNET_break (0);
     119            0 :       TALER_TESTING_interpreter_fail (is);
     120            0 :       return;
     121              :     }
     122            8 :     GNUNET_assert (GNUNET_OK ==
     123              :                    TALER_TESTING_get_trait_exchange_url (exchange_cmd,
     124              :                                                          &exchange_url));
     125              :   }
     126            8 :   now = GNUNET_TIME_timestamp_get ();
     127            8 :   ds->is = is;
     128            8 :   debit_rest = json_array ();
     129            8 :   credit_rest = json_array ();
     130            8 :   if (ds->bad_sig)
     131              :   {
     132            2 :     memset (&master_sig1,
     133              :             42,
     134              :             sizeof (master_sig1));
     135            2 :     memset (&master_sig2,
     136              :             42,
     137              :             sizeof (master_sig2));
     138              :   }
     139              :   else
     140              :   {
     141              :     const struct TALER_TESTING_Command *exchange_cmd;
     142              :     const struct TALER_MasterPrivateKeyP *master_priv;
     143              : 
     144            6 :     exchange_cmd = TALER_TESTING_interpreter_get_command (is,
     145              :                                                           "exchange");
     146            6 :     if (NULL == exchange_cmd)
     147              :     {
     148            0 :       GNUNET_break (0);
     149            0 :       TALER_TESTING_interpreter_fail (is);
     150            0 :       return;
     151              :     }
     152            6 :     GNUNET_assert (GNUNET_OK ==
     153              :                    TALER_TESTING_get_trait_master_priv (exchange_cmd,
     154              :                                                         &master_priv));
     155            6 :     TALER_exchange_offline_wire_add_sign (ds->payto_uri,
     156              :                                           NULL,
     157              :                                           NULL,
     158              :                                           NULL,
     159              :                                           debit_rest,
     160              :                                           credit_rest,
     161              :                                           now,
     162              :                                           master_priv,
     163              :                                           &master_sig1);
     164            6 :     TALER_exchange_wire_signature_make (ds->payto_uri,
     165              :                                         NULL,
     166              :                                         NULL,
     167              :                                         NULL,
     168              :                                         debit_rest,
     169              :                                         credit_rest,
     170              :                                         master_priv,
     171              :                                         &master_sig2);
     172              :   }
     173            8 :   ds->dh = TALER_EXCHANGE_post_management_wire_create (
     174              :     TALER_TESTING_interpreter_get_context (is),
     175              :     exchange_url,
     176              :     ds->payto_uri,
     177              :     now,
     178              :     &master_sig1,
     179              :     &master_sig2);
     180            8 :   if (NULL == ds->dh)
     181              :   {
     182            0 :     GNUNET_break (0);
     183            0 :     TALER_TESTING_interpreter_fail (is);
     184            0 :     return;
     185              :   }
     186            8 :   TALER_EXCHANGE_post_management_wire_set_options (
     187              :     ds->dh,
     188              :     TALER_EXCHANGE_post_management_wire_option_bank_label ("test-account"),
     189              :     TALER_EXCHANGE_post_management_wire_option_debit_restrictions (
     190              :       debit_rest),
     191              :     TALER_EXCHANGE_post_management_wire_option_credit_restrictions (
     192              :       credit_rest));
     193            8 :   json_decref (debit_rest);
     194            8 :   json_decref (credit_rest);
     195            8 :   TALER_EXCHANGE_post_management_wire_start (ds->dh, &wire_add_cb, ds);
     196              : }
     197              : 
     198              : 
     199              : /**
     200              :  * Free the state of a "wire_add" CMD, and possibly cancel a
     201              :  * pending operation thereof.
     202              :  *
     203              :  * @param cls closure, must be a `struct WireAddState`.
     204              :  * @param cmd the command which is being cleaned up.
     205              :  */
     206              : static void
     207            8 : wire_add_cleanup (void *cls,
     208              :                   const struct TALER_TESTING_Command *cmd)
     209              : {
     210            8 :   struct WireAddState *ds = cls;
     211              : 
     212            8 :   if (NULL != ds->dh)
     213              :   {
     214            0 :     TALER_TESTING_command_incomplete (ds->is,
     215              :                                       cmd->label);
     216            0 :     TALER_EXCHANGE_post_management_wire_cancel (ds->dh);
     217            0 :     ds->dh = NULL;
     218              :   }
     219            8 :   GNUNET_free (ds);
     220            8 : }
     221              : 
     222              : 
     223              : struct TALER_TESTING_Command
     224            8 : TALER_TESTING_cmd_wire_add (const char *label,
     225              :                             struct TALER_FullPayto payto_uri,
     226              :                             unsigned int expected_http_status,
     227              :                             bool bad_sig)
     228              : {
     229              :   struct WireAddState *ds;
     230              : 
     231            8 :   ds = GNUNET_new (struct WireAddState);
     232            8 :   ds->expected_response_code = expected_http_status;
     233            8 :   ds->bad_sig = bad_sig;
     234            8 :   ds->payto_uri = payto_uri;
     235              :   {
     236            8 :     struct TALER_TESTING_Command cmd = {
     237              :       .cls = ds,
     238              :       .label = label,
     239              :       .run = &wire_add_run,
     240              :       .cleanup = &wire_add_cleanup
     241              :     };
     242              : 
     243            8 :     return cmd;
     244              :   }
     245              : }
     246              : 
     247              : 
     248              : /* end of testing_api_cmd_wire_add.c */
        

Generated by: LCOV version 2.0-1