LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_auditor_add.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 71.4 % 49 35
Test Date: 2026-04-14 15:39:31 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/taler_json_lib.h"
      25              : #include <gnunet/gnunet_curl_lib.h>
      26              : #include "taler/taler_testing_lib.h"
      27              : 
      28              : 
      29              : /**
      30              :  * State for a "auditor_add" CMD.
      31              :  */
      32              : struct AuditorAddState
      33              : {
      34              : 
      35              :   /**
      36              :    * Auditor enable handle while operation is running.
      37              :    */
      38              :   struct TALER_EXCHANGE_PostManagementAuditorsHandle *dh;
      39              : 
      40              :   /**
      41              :    * Our interpreter.
      42              :    */
      43              :   struct TALER_TESTING_Interpreter *is;
      44              : 
      45              :   /**
      46              :    * Expected HTTP response code.
      47              :    */
      48              :   unsigned int expected_response_code;
      49              : 
      50              :   /**
      51              :    * Should we make the request with a bad master_sig signature?
      52              :    */
      53              :   bool bad_sig;
      54              : };
      55              : 
      56              : 
      57              : /**
      58              :  * Callback to analyze the /management/auditors response, just used to check
      59              :  * if the response code is acceptable.
      60              :  *
      61              :  * @param cls closure.
      62              :  * @param r response details
      63              :  */
      64              : static void
      65            6 : auditor_add_cb (
      66              :   void *cls,
      67              :   const struct TALER_EXCHANGE_PostManagementAuditorsResponse *r)
      68              : {
      69            6 :   struct AuditorAddState *ds = cls;
      70            6 :   const struct TALER_EXCHANGE_HttpResponse *hr = &r->hr;
      71              : 
      72            6 :   ds->dh = NULL;
      73            6 :   if (ds->expected_response_code != hr->http_status)
      74              :   {
      75            0 :     TALER_TESTING_unexpected_status (ds->is,
      76              :                                      hr->http_status,
      77              :                                      ds->expected_response_code);
      78            0 :     return;
      79              :   }
      80            6 :   TALER_TESTING_interpreter_next (ds->is);
      81              : }
      82              : 
      83              : 
      84              : /**
      85              :  * Run the command.
      86              :  *
      87              :  * @param cls closure.
      88              :  * @param cmd the command to execute.
      89              :  * @param is the interpreter state.
      90              :  */
      91              : static void
      92            6 : auditor_add_run (void *cls,
      93              :                  const struct TALER_TESTING_Command *cmd,
      94              :                  struct TALER_TESTING_Interpreter *is)
      95              : {
      96            6 :   struct AuditorAddState *ds = cls;
      97              :   struct GNUNET_TIME_Timestamp now;
      98              :   struct TALER_MasterSignatureP master_sig;
      99              :   const struct TALER_AuditorPublicKeyP *auditor_pub;
     100              :   const struct TALER_TESTING_Command *auditor_cmd;
     101              :   const struct TALER_TESTING_Command *exchange_cmd;
     102              :   const char *exchange_url;
     103              :   const char *auditor_url;
     104              : 
     105              :   (void) cmd;
     106            6 :   now = GNUNET_TIME_timestamp_get ();
     107            6 :   ds->is = is;
     108              : 
     109            6 :   auditor_cmd = TALER_TESTING_interpreter_get_command (is,
     110              :                                                        "auditor");
     111            6 :   if (NULL == auditor_cmd)
     112              :   {
     113            0 :     GNUNET_break (0);
     114            0 :     TALER_TESTING_interpreter_fail (is);
     115            0 :     return;
     116              :   }
     117            6 :   GNUNET_assert (GNUNET_OK ==
     118              :                  TALER_TESTING_get_trait_auditor_pub (auditor_cmd,
     119              :                                                       &auditor_pub));
     120            6 :   GNUNET_assert (GNUNET_OK ==
     121              :                  TALER_TESTING_get_trait_auditor_url (auditor_cmd,
     122              :                                                       &auditor_url));
     123            6 :   exchange_cmd = TALER_TESTING_interpreter_get_command (is,
     124              :                                                         "exchange");
     125            6 :   if (NULL == exchange_cmd)
     126              :   {
     127            0 :     GNUNET_break (0);
     128            0 :     TALER_TESTING_interpreter_fail (is);
     129            0 :     return;
     130              :   }
     131            6 :   GNUNET_assert (GNUNET_OK ==
     132              :                  TALER_TESTING_get_trait_exchange_url (exchange_cmd,
     133              :                                                        &exchange_url));
     134              : 
     135              : 
     136            6 :   if (ds->bad_sig)
     137              :   {
     138            2 :     memset (&master_sig,
     139              :             42,
     140              :             sizeof (master_sig));
     141              :   }
     142              :   else
     143              :   {
     144              :     const struct TALER_MasterPrivateKeyP *master_priv;
     145              : 
     146            4 :     GNUNET_assert (GNUNET_OK ==
     147              :                    TALER_TESTING_get_trait_master_priv (exchange_cmd,
     148              :                                                         &master_priv));
     149            4 :     TALER_exchange_offline_auditor_add_sign (auditor_pub,
     150              :                                              auditor_url,
     151              :                                              now,
     152              :                                              master_priv,
     153              :                                              &master_sig);
     154              :   }
     155            6 :   ds->dh = TALER_EXCHANGE_post_management_auditors_create (
     156              :     TALER_TESTING_interpreter_get_context (is),
     157              :     exchange_url,
     158              :     auditor_pub,
     159              :     auditor_url,
     160              :     "test-case auditor", /* human-readable auditor name */
     161              :     now,
     162              :     &master_sig);
     163            6 :   if (NULL == ds->dh)
     164              :   {
     165            0 :     GNUNET_break (0);
     166            0 :     TALER_TESTING_interpreter_fail (is);
     167            0 :     return;
     168              :   }
     169            6 :   TALER_EXCHANGE_post_management_auditors_start (ds->dh, &auditor_add_cb, ds);
     170              : }
     171              : 
     172              : 
     173              : /**
     174              :  * Free the state of a "auditor_add" CMD, and possibly cancel a
     175              :  * pending operation thereof.
     176              :  *
     177              :  * @param cls closure, must be a `struct AuditorAddState`.
     178              :  * @param cmd the command which is being cleaned up.
     179              :  */
     180              : static void
     181            6 : auditor_add_cleanup (void *cls,
     182              :                      const struct TALER_TESTING_Command *cmd)
     183              : {
     184            6 :   struct AuditorAddState *ds = cls;
     185              : 
     186            6 :   if (NULL != ds->dh)
     187              :   {
     188            0 :     TALER_TESTING_command_incomplete (ds->is,
     189              :                                       cmd->label);
     190            0 :     TALER_EXCHANGE_post_management_auditors_cancel (ds->dh);
     191            0 :     ds->dh = NULL;
     192              :   }
     193            6 :   GNUNET_free (ds);
     194            6 : }
     195              : 
     196              : 
     197              : struct TALER_TESTING_Command
     198            6 : TALER_TESTING_cmd_auditor_add (const char *label,
     199              :                                unsigned int expected_http_status,
     200              :                                bool bad_sig)
     201              : {
     202              :   struct AuditorAddState *ds;
     203              : 
     204            6 :   ds = GNUNET_new (struct AuditorAddState);
     205            6 :   ds->expected_response_code = expected_http_status;
     206            6 :   ds->bad_sig = bad_sig;
     207              :   {
     208            6 :     struct TALER_TESTING_Command cmd = {
     209              :       .cls = ds,
     210              :       .label = label,
     211              :       .run = &auditor_add_run,
     212              :       .cleanup = &auditor_add_cleanup
     213              :     };
     214              : 
     215            6 :     return cmd;
     216              :   }
     217              : }
     218              : 
     219              : 
     220              : /* end of testing_api_cmd_auditor_add.c */
        

Generated by: LCOV version 2.0-1