LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_wire_add.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 73.6 % 53 39
Test Date: 2025-12-26 23:00:34 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/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 WireAddState
      36              : {
      37              : 
      38              :   /**
      39              :    * Wire enable handle while operation is running.
      40              :    */
      41              :   struct TALER_EXCHANGE_ManagementWireEnableHandle *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 cls closure.
      70              :  * @param wer response details
      71              :  */
      72              : static void
      73            8 : wire_add_cb (void *cls,
      74              :              const struct TALER_EXCHANGE_ManagementWireEnableResponse *wer)
      75              : {
      76            8 :   struct WireAddState *ds = cls;
      77            8 :   const struct TALER_EXCHANGE_HttpResponse *hr = &wer->hr;
      78              : 
      79            8 :   ds->dh = NULL;
      80            8 :   if (ds->expected_response_code != hr->http_status)
      81              :   {
      82            0 :     TALER_TESTING_unexpected_status (ds->is,
      83              :                                      hr->http_status,
      84              :                                      ds->expected_response_code);
      85            0 :     return;
      86              :   }
      87            8 :   TALER_TESTING_interpreter_next (ds->is);
      88              : }
      89              : 
      90              : 
      91              : /**
      92              :  * Run the command.
      93              :  *
      94              :  * @param cls closure.
      95              :  * @param cmd the command to execute.
      96              :  * @param is the interpreter state.
      97              :  */
      98              : static void
      99            8 : wire_add_run (void *cls,
     100              :               const struct TALER_TESTING_Command *cmd,
     101              :               struct TALER_TESTING_Interpreter *is)
     102              : {
     103            8 :   struct WireAddState *ds = cls;
     104              :   struct TALER_MasterSignatureP master_sig1;
     105              :   struct TALER_MasterSignatureP master_sig2;
     106              :   struct GNUNET_TIME_Timestamp now;
     107              :   json_t *credit_rest;
     108              :   json_t *debit_rest;
     109              :   const char *exchange_url;
     110              : 
     111              :   (void) cmd;
     112              :   {
     113              :     const struct TALER_TESTING_Command *exchange_cmd;
     114              : 
     115            8 :     exchange_cmd = TALER_TESTING_interpreter_get_command (is,
     116              :                                                           "exchange");
     117            8 :     if (NULL == exchange_cmd)
     118              :     {
     119            0 :       GNUNET_break (0);
     120            0 :       TALER_TESTING_interpreter_fail (is);
     121            0 :       return;
     122              :     }
     123            8 :     GNUNET_assert (GNUNET_OK ==
     124              :                    TALER_TESTING_get_trait_exchange_url (exchange_cmd,
     125              :                                                          &exchange_url));
     126              :   }
     127            8 :   now = GNUNET_TIME_timestamp_get ();
     128            8 :   ds->is = is;
     129            8 :   debit_rest = json_array ();
     130            8 :   credit_rest = json_array ();
     131            8 :   if (ds->bad_sig)
     132              :   {
     133            2 :     memset (&master_sig1,
     134              :             42,
     135              :             sizeof (master_sig1));
     136            2 :     memset (&master_sig2,
     137              :             42,
     138              :             sizeof (master_sig2));
     139              :   }
     140              :   else
     141              :   {
     142              :     const struct TALER_TESTING_Command *exchange_cmd;
     143              :     const struct TALER_MasterPrivateKeyP *master_priv;
     144              : 
     145            6 :     exchange_cmd = TALER_TESTING_interpreter_get_command (is,
     146              :                                                           "exchange");
     147            6 :     if (NULL == exchange_cmd)
     148              :     {
     149            0 :       GNUNET_break (0);
     150            0 :       TALER_TESTING_interpreter_fail (is);
     151            0 :       return;
     152              :     }
     153            6 :     GNUNET_assert (GNUNET_OK ==
     154              :                    TALER_TESTING_get_trait_master_priv (exchange_cmd,
     155              :                                                         &master_priv));
     156            6 :     TALER_exchange_offline_wire_add_sign (ds->payto_uri,
     157              :                                           NULL,
     158              :                                           debit_rest,
     159              :                                           credit_rest,
     160              :                                           now,
     161              :                                           master_priv,
     162              :                                           &master_sig1);
     163            6 :     TALER_exchange_wire_signature_make (ds->payto_uri,
     164              :                                         NULL,
     165              :                                         debit_rest,
     166              :                                         credit_rest,
     167              :                                         master_priv,
     168              :                                         &master_sig2);
     169              :   }
     170            8 :   ds->dh = TALER_EXCHANGE_management_enable_wire (
     171              :     TALER_TESTING_interpreter_get_context (is),
     172              :     exchange_url,
     173              :     ds->payto_uri,
     174              :     NULL,
     175              :     debit_rest,
     176              :     credit_rest,
     177              :     now,
     178              :     &master_sig1,
     179              :     &master_sig2,
     180              :     NULL,
     181              :     0LL,
     182              :     &wire_add_cb,
     183              :     ds);
     184            8 :   json_decref (debit_rest);
     185            8 :   json_decref (credit_rest);
     186            8 :   if (NULL == ds->dh)
     187              :   {
     188            0 :     GNUNET_break (0);
     189            0 :     TALER_TESTING_interpreter_fail (is);
     190            0 :     return;
     191              :   }
     192              : }
     193              : 
     194              : 
     195              : /**
     196              :  * Free the state of a "wire_add" CMD, and possibly cancel a
     197              :  * pending operation thereof.
     198              :  *
     199              :  * @param cls closure, must be a `struct WireAddState`.
     200              :  * @param cmd the command which is being cleaned up.
     201              :  */
     202              : static void
     203            8 : wire_add_cleanup (void *cls,
     204              :                   const struct TALER_TESTING_Command *cmd)
     205              : {
     206            8 :   struct WireAddState *ds = cls;
     207              : 
     208            8 :   if (NULL != ds->dh)
     209              :   {
     210            0 :     TALER_TESTING_command_incomplete (ds->is,
     211              :                                       cmd->label);
     212            0 :     TALER_EXCHANGE_management_enable_wire_cancel (ds->dh);
     213            0 :     ds->dh = NULL;
     214              :   }
     215            8 :   GNUNET_free (ds);
     216            8 : }
     217              : 
     218              : 
     219              : struct TALER_TESTING_Command
     220            8 : TALER_TESTING_cmd_wire_add (const char *label,
     221              :                             struct TALER_FullPayto payto_uri,
     222              :                             unsigned int expected_http_status,
     223              :                             bool bad_sig)
     224              : {
     225              :   struct WireAddState *ds;
     226              : 
     227            8 :   ds = GNUNET_new (struct WireAddState);
     228            8 :   ds->expected_response_code = expected_http_status;
     229            8 :   ds->bad_sig = bad_sig;
     230            8 :   ds->payto_uri = payto_uri;
     231              :   {
     232            8 :     struct TALER_TESTING_Command cmd = {
     233              :       .cls = ds,
     234              :       .label = label,
     235              :       .run = &wire_add_run,
     236              :       .cleanup = &wire_add_cleanup
     237              :     };
     238              : 
     239            8 :     return cmd;
     240              :   }
     241              : }
     242              : 
     243              : 
     244              : /* end of testing_api_cmd_wire_add.c */
        

Generated by: LCOV version 2.0-1