LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_wire_add.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 34 44 77.3 %
Date: 2021-08-30 06:43:37 Functions: 5 5 100.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_wire_add.c
      21             :  * @brief command for testing POST to /management/wire
      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 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             :   const char *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 hr HTTP response details
      71             :  */
      72             : static void
      73          11 : wire_add_cb (void *cls,
      74             :              const struct TALER_EXCHANGE_HttpResponse *hr)
      75             : {
      76          11 :   struct WireAddState *ds = cls;
      77             : 
      78          11 :   ds->dh = NULL;
      79          11 :   if (ds->expected_response_code != hr->http_status)
      80             :   {
      81           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      82             :                 "Unexpected response code %u to command %s in %s:%u\n",
      83             :                 hr->http_status,
      84             :                 ds->is->commands[ds->is->ip].label,
      85             :                 __FILE__,
      86             :                 __LINE__);
      87           0 :     json_dumpf (hr->reply,
      88             :                 stderr,
      89             :                 0);
      90           0 :     TALER_TESTING_interpreter_fail (ds->is);
      91           0 :     return;
      92             :   }
      93          11 :   TALER_TESTING_interpreter_next (ds->is);
      94             : }
      95             : 
      96             : 
      97             : /**
      98             :  * Run the command.
      99             :  *
     100             :  * @param cls closure.
     101             :  * @param cmd the command to execute.
     102             :  * @param is the interpreter state.
     103             :  */
     104             : static void
     105          11 : wire_add_run (void *cls,
     106             :               const struct TALER_TESTING_Command *cmd,
     107             :               struct TALER_TESTING_Interpreter *is)
     108             : {
     109          11 :   struct WireAddState *ds = cls;
     110             :   struct TALER_MasterSignatureP master_sig1;
     111             :   struct TALER_MasterSignatureP master_sig2;
     112             :   struct GNUNET_TIME_Absolute now;
     113             : 
     114             :   (void) cmd;
     115          11 :   now = GNUNET_TIME_absolute_get ();
     116          11 :   (void) GNUNET_TIME_round_abs (&now);
     117          11 :   ds->is = is;
     118          11 :   if (ds->bad_sig)
     119             :   {
     120           1 :     memset (&master_sig1,
     121             :             42,
     122             :             sizeof (master_sig1));
     123           1 :     memset (&master_sig2,
     124             :             42,
     125             :             sizeof (master_sig2));
     126             :   }
     127             :   else
     128             :   {
     129          10 :     TALER_exchange_offline_wire_add_sign (ds->payto_uri,
     130             :                                           now,
     131          10 :                                           &is->master_priv,
     132             :                                           &master_sig1);
     133          10 :     TALER_exchange_wire_signature_make (ds->payto_uri,
     134          10 :                                         &is->master_priv,
     135             :                                         &master_sig2);
     136             :   }
     137          22 :   ds->dh = TALER_EXCHANGE_management_enable_wire (
     138             :     is->ctx,
     139          11 :     is->exchange_url,
     140             :     ds->payto_uri,
     141             :     now,
     142             :     &master_sig1,
     143             :     &master_sig2,
     144             :     &wire_add_cb,
     145             :     ds);
     146          11 :   if (NULL == ds->dh)
     147             :   {
     148           0 :     GNUNET_break (0);
     149           0 :     TALER_TESTING_interpreter_fail (is);
     150           0 :     return;
     151             :   }
     152             : }
     153             : 
     154             : 
     155             : /**
     156             :  * Free the state of a "wire_add" CMD, and possibly cancel a
     157             :  * pending operation thereof.
     158             :  *
     159             :  * @param cls closure, must be a `struct WireAddState`.
     160             :  * @param cmd the command which is being cleaned up.
     161             :  */
     162             : static void
     163          11 : wire_add_cleanup (void *cls,
     164             :                   const struct TALER_TESTING_Command *cmd)
     165             : {
     166          11 :   struct WireAddState *ds = cls;
     167             : 
     168          11 :   if (NULL != ds->dh)
     169             :   {
     170           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     171             :                 "Command %u (%s) did not complete\n",
     172             :                 ds->is->ip,
     173             :                 cmd->label);
     174           0 :     TALER_EXCHANGE_management_enable_wire_cancel (ds->dh);
     175           0 :     ds->dh = NULL;
     176             :   }
     177          11 :   GNUNET_free (ds);
     178          11 : }
     179             : 
     180             : 
     181             : /**
     182             :  * Offer internal data from a "wire_add" CMD, to other commands.
     183             :  *
     184             :  * @param cls closure.
     185             :  * @param[out] ret result.
     186             :  * @param trait name of the trait.
     187             :  * @param index index number of the object to offer.
     188             :  *
     189             :  * @return #GNUNET_OK on success.
     190             :  */
     191             : static int
     192           8 : wire_add_traits (void *cls,
     193             :                  const void **ret,
     194             :                  const char *trait,
     195             :                  unsigned int index)
     196             : {
     197           8 :   return GNUNET_NO;
     198             : }
     199             : 
     200             : 
     201             : struct TALER_TESTING_Command
     202          11 : TALER_TESTING_cmd_wire_add (const char *label,
     203             :                             const char *payto_uri,
     204             :                             unsigned int expected_http_status,
     205             :                             bool bad_sig)
     206             : {
     207             :   struct WireAddState *ds;
     208             : 
     209          11 :   ds = GNUNET_new (struct WireAddState);
     210          11 :   ds->expected_response_code = expected_http_status;
     211          11 :   ds->bad_sig = bad_sig;
     212          11 :   ds->payto_uri = payto_uri;
     213             :   {
     214          11 :     struct TALER_TESTING_Command cmd = {
     215             :       .cls = ds,
     216             :       .label = label,
     217             :       .run = &wire_add_run,
     218             :       .cleanup = &wire_add_cleanup,
     219             :       .traits = &wire_add_traits
     220             :     };
     221             : 
     222          11 :     return cmd;
     223             :   }
     224             : }
     225             : 
     226             : 
     227             : /* end of testing_api_cmd_wire_add.c */

Generated by: LCOV version 1.14