LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_auditor_add.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 70.8 % 48 34
Test Date: 2025-12-26 20:22:50 Functions: 100.0 % 4 4

            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 "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 "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 r response details
      66              :  */
      67              : static void
      68            6 : auditor_add_cb (
      69              :   void *cls,
      70              :   const struct TALER_EXCHANGE_ManagementAuditorEnableResponse *r)
      71              : {
      72            6 :   struct AuditorAddState *ds = cls;
      73            6 :   const struct TALER_EXCHANGE_HttpResponse *hr = &r->hr;
      74              : 
      75            6 :   ds->dh = NULL;
      76            6 :   if (ds->expected_response_code != hr->http_status)
      77              :   {
      78            0 :     TALER_TESTING_unexpected_status (ds->is,
      79              :                                      hr->http_status,
      80              :                                      ds->expected_response_code);
      81            0 :     return;
      82              :   }
      83            6 :   TALER_TESTING_interpreter_next (ds->is);
      84              : }
      85              : 
      86              : 
      87              : /**
      88              :  * Run the command.
      89              :  *
      90              :  * @param cls closure.
      91              :  * @param cmd the command to execute.
      92              :  * @param is the interpreter state.
      93              :  */
      94              : static void
      95            6 : auditor_add_run (void *cls,
      96              :                  const struct TALER_TESTING_Command *cmd,
      97              :                  struct TALER_TESTING_Interpreter *is)
      98              : {
      99            6 :   struct AuditorAddState *ds = cls;
     100              :   struct GNUNET_TIME_Timestamp now;
     101              :   struct TALER_MasterSignatureP master_sig;
     102              :   const struct TALER_AuditorPublicKeyP *auditor_pub;
     103              :   const struct TALER_TESTING_Command *auditor_cmd;
     104              :   const struct TALER_TESTING_Command *exchange_cmd;
     105              :   const char *exchange_url;
     106              :   const char *auditor_url;
     107              : 
     108              :   (void) cmd;
     109            6 :   now = GNUNET_TIME_timestamp_get ();
     110            6 :   ds->is = is;
     111              : 
     112            6 :   auditor_cmd = TALER_TESTING_interpreter_get_command (is,
     113              :                                                        "auditor");
     114            6 :   if (NULL == auditor_cmd)
     115              :   {
     116            0 :     GNUNET_break (0);
     117            0 :     TALER_TESTING_interpreter_fail (is);
     118            0 :     return;
     119              :   }
     120            6 :   GNUNET_assert (GNUNET_OK ==
     121              :                  TALER_TESTING_get_trait_auditor_pub (auditor_cmd,
     122              :                                                       &auditor_pub));
     123            6 :   GNUNET_assert (GNUNET_OK ==
     124              :                  TALER_TESTING_get_trait_auditor_url (auditor_cmd,
     125              :                                                       &auditor_url));
     126            6 :   exchange_cmd = TALER_TESTING_interpreter_get_command (is,
     127              :                                                         "exchange");
     128            6 :   if (NULL == exchange_cmd)
     129              :   {
     130            0 :     GNUNET_break (0);
     131            0 :     TALER_TESTING_interpreter_fail (is);
     132            0 :     return;
     133              :   }
     134            6 :   GNUNET_assert (GNUNET_OK ==
     135              :                  TALER_TESTING_get_trait_exchange_url (exchange_cmd,
     136              :                                                        &exchange_url));
     137              : 
     138              : 
     139            6 :   if (ds->bad_sig)
     140              :   {
     141            2 :     memset (&master_sig,
     142              :             42,
     143              :             sizeof (master_sig));
     144              :   }
     145              :   else
     146              :   {
     147              :     const struct TALER_MasterPrivateKeyP *master_priv;
     148              : 
     149            4 :     GNUNET_assert (GNUNET_OK ==
     150              :                    TALER_TESTING_get_trait_master_priv (exchange_cmd,
     151              :                                                         &master_priv));
     152            4 :     TALER_exchange_offline_auditor_add_sign (auditor_pub,
     153              :                                              auditor_url,
     154              :                                              now,
     155              :                                              master_priv,
     156              :                                              &master_sig);
     157              :   }
     158            6 :   ds->dh = TALER_EXCHANGE_management_enable_auditor (
     159              :     TALER_TESTING_interpreter_get_context (is),
     160              :     exchange_url,
     161              :     auditor_pub,
     162              :     auditor_url,
     163              :     "test-case auditor", /* human-readable auditor name */
     164              :     now,
     165              :     &master_sig,
     166              :     &auditor_add_cb,
     167              :     ds);
     168            6 :   if (NULL == ds->dh)
     169              :   {
     170            0 :     GNUNET_break (0);
     171            0 :     TALER_TESTING_interpreter_fail (is);
     172            0 :     return;
     173              :   }
     174              : }
     175              : 
     176              : 
     177              : /**
     178              :  * Free the state of a "auditor_add" CMD, and possibly cancel a
     179              :  * pending operation thereof.
     180              :  *
     181              :  * @param cls closure, must be a `struct AuditorAddState`.
     182              :  * @param cmd the command which is being cleaned up.
     183              :  */
     184              : static void
     185            6 : auditor_add_cleanup (void *cls,
     186              :                      const struct TALER_TESTING_Command *cmd)
     187              : {
     188            6 :   struct AuditorAddState *ds = cls;
     189              : 
     190            6 :   if (NULL != ds->dh)
     191              :   {
     192            0 :     TALER_TESTING_command_incomplete (ds->is,
     193              :                                       cmd->label);
     194            0 :     TALER_EXCHANGE_management_enable_auditor_cancel (ds->dh);
     195            0 :     ds->dh = NULL;
     196              :   }
     197            6 :   GNUNET_free (ds);
     198            6 : }
     199              : 
     200              : 
     201              : struct TALER_TESTING_Command
     202            6 : TALER_TESTING_cmd_auditor_add (const char *label,
     203              :                                unsigned int expected_http_status,
     204              :                                bool bad_sig)
     205              : {
     206              :   struct AuditorAddState *ds;
     207              : 
     208            6 :   ds = GNUNET_new (struct AuditorAddState);
     209            6 :   ds->expected_response_code = expected_http_status;
     210            6 :   ds->bad_sig = bad_sig;
     211              :   {
     212            6 :     struct TALER_TESTING_Command cmd = {
     213              :       .cls = ds,
     214              :       .label = label,
     215              :       .run = &auditor_add_run,
     216              :       .cleanup = &auditor_add_cleanup
     217              :     };
     218              : 
     219            6 :     return cmd;
     220              :   }
     221              : }
     222              : 
     223              : 
     224              : /* end of testing_api_cmd_auditor_add.c */
        

Generated by: LCOV version 2.0-1