LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_auditor_add.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 33 43 76.7 %
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_auditor_add.c
      21             :  * @brief command for testing /auditor_add.
      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 "auditor_add" CMD.
      34             :  */
      35             : struct AuditorAddState
      36             : {
      37             : 
      38             :   /**
      39             :    * Auditor enable handle while operation is running.
      40             :    */
      41             :   struct TALER_EXCHANGE_ManagementAuditorEnableHandle *dh;
      42             : 
      43             :   /**
      44             :    * Our interpreter.
      45             :    */
      46             :   struct TALER_TESTING_Interpreter *is;
      47             : 
      48             :   /**
      49             :    * Expected HTTP response code.
      50             :    */
      51             :   unsigned int expected_response_code;
      52             : 
      53             :   /**
      54             :    * Should we make the request with a bad master_sig signature?
      55             :    */
      56             :   bool bad_sig;
      57             : };
      58             : 
      59             : 
      60             : /**
      61             :  * Callback to analyze the /management/auditors response, just used to check
      62             :  * if the response code is acceptable.
      63             :  *
      64             :  * @param cls closure.
      65             :  * @param hr HTTP response details
      66             :  */
      67             : static void
      68           9 : auditor_add_cb (void *cls,
      69             :                 const struct TALER_EXCHANGE_HttpResponse *hr)
      70             : {
      71           9 :   struct AuditorAddState *ds = cls;
      72             : 
      73           9 :   ds->dh = NULL;
      74           9 :   if (ds->expected_response_code != hr->http_status)
      75             :   {
      76           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      77             :                 "Unexpected response code %u to command %s in %s:%u\n",
      78             :                 hr->http_status,
      79             :                 ds->is->commands[ds->is->ip].label,
      80             :                 __FILE__,
      81             :                 __LINE__);
      82           0 :     json_dumpf (hr->reply,
      83             :                 stderr,
      84             :                 0);
      85           0 :     TALER_TESTING_interpreter_fail (ds->is);
      86           0 :     return;
      87             :   }
      88           9 :   TALER_TESTING_interpreter_next (ds->is);
      89             : }
      90             : 
      91             : 
      92             : /**
      93             :  * Run the command.
      94             :  *
      95             :  * @param cls closure.
      96             :  * @param cmd the command to execute.
      97             :  * @param is the interpreter state.
      98             :  */
      99             : static void
     100           9 : auditor_add_run (void *cls,
     101             :                  const struct TALER_TESTING_Command *cmd,
     102             :                  struct TALER_TESTING_Interpreter *is)
     103             : {
     104           9 :   struct AuditorAddState *ds = cls;
     105             :   struct GNUNET_TIME_Absolute now;
     106             :   struct TALER_MasterSignatureP master_sig;
     107             : 
     108             :   (void) cmd;
     109           9 :   now = GNUNET_TIME_absolute_get ();
     110           9 :   (void) GNUNET_TIME_round_abs (&now);
     111           9 :   ds->is = is;
     112           9 :   if (ds->bad_sig)
     113             :   {
     114           1 :     memset (&master_sig,
     115             :             42,
     116             :             sizeof (master_sig));
     117             :   }
     118             :   else
     119             :   {
     120           8 :     TALER_exchange_offline_auditor_add_sign (&is->auditor_pub,
     121           8 :                                              is->auditor_url,
     122             :                                              now,
     123           8 :                                              &is->master_priv,
     124             :                                              &master_sig);
     125             :   }
     126          18 :   ds->dh = TALER_EXCHANGE_management_enable_auditor (
     127             :     is->ctx,
     128           9 :     is->exchange_url,
     129           9 :     &is->auditor_pub,
     130           9 :     is->auditor_url,
     131             :     "test-case auditor", /* human-readable auditor name */
     132             :     now,
     133             :     &master_sig,
     134             :     &auditor_add_cb,
     135             :     ds);
     136           9 :   if (NULL == ds->dh)
     137             :   {
     138           0 :     GNUNET_break (0);
     139           0 :     TALER_TESTING_interpreter_fail (is);
     140           0 :     return;
     141             :   }
     142             : }
     143             : 
     144             : 
     145             : /**
     146             :  * Free the state of a "auditor_add" CMD, and possibly cancel a
     147             :  * pending operation thereof.
     148             :  *
     149             :  * @param cls closure, must be a `struct AuditorAddState`.
     150             :  * @param cmd the command which is being cleaned up.
     151             :  */
     152             : static void
     153           9 : auditor_add_cleanup (void *cls,
     154             :                      const struct TALER_TESTING_Command *cmd)
     155             : {
     156           9 :   struct AuditorAddState *ds = cls;
     157             : 
     158           9 :   if (NULL != ds->dh)
     159             :   {
     160           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     161             :                 "Command %u (%s) did not complete\n",
     162             :                 ds->is->ip,
     163             :                 cmd->label);
     164           0 :     TALER_EXCHANGE_management_enable_auditor_cancel (ds->dh);
     165           0 :     ds->dh = NULL;
     166             :   }
     167           9 :   GNUNET_free (ds);
     168           9 : }
     169             : 
     170             : 
     171             : /**
     172             :  * Offer internal data from a "auditor_add" CMD, to other commands.
     173             :  *
     174             :  * @param cls closure.
     175             :  * @param[out] ret result.
     176             :  * @param trait name of the trait.
     177             :  * @param index index number of the object to offer.
     178             :  *
     179             :  * @return #GNUNET_OK on success.
     180             :  */
     181             : static int
     182           8 : auditor_add_traits (void *cls,
     183             :                     const void **ret,
     184             :                     const char *trait,
     185             :                     unsigned int index)
     186             : {
     187           8 :   return GNUNET_NO;
     188             : }
     189             : 
     190             : 
     191             : struct TALER_TESTING_Command
     192           9 : TALER_TESTING_cmd_auditor_add (const char *label,
     193             :                                unsigned int expected_http_status,
     194             :                                bool bad_sig)
     195             : {
     196             :   struct AuditorAddState *ds;
     197             : 
     198           9 :   ds = GNUNET_new (struct AuditorAddState);
     199           9 :   ds->expected_response_code = expected_http_status;
     200           9 :   ds->bad_sig = bad_sig;
     201             :   {
     202           9 :     struct TALER_TESTING_Command cmd = {
     203             :       .cls = ds,
     204             :       .label = label,
     205             :       .run = &auditor_add_run,
     206             :       .cleanup = &auditor_add_cleanup,
     207             :       .traits = &auditor_add_traits
     208             :     };
     209             : 
     210           9 :     return cmd;
     211             :   }
     212             : }
     213             : 
     214             : 
     215             : /* end of testing_api_cmd_auditor_add.c */

Generated by: LCOV version 1.14