LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_revoke.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 31 41 75.6 %
Date: 2021-08-30 06:43:37 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2018 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify
       6             :   it under the terms of the GNU General Public License as
       7             :   published by the Free Software Foundation; either version 3, or
       8             :   (at your 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
      13             :   GNU 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_revoke.c
      21             :  * @brief Implement the revoke test command.
      22             :  * @author Marcello Stanisci
      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             : 
      29             : 
      30             : /**
      31             :  * State for a "revoke" CMD.
      32             :  */
      33             : struct RevokeState
      34             : {
      35             :   /**
      36             :    * Expected HTTP status code.
      37             :    */
      38             :   unsigned int expected_response_code;
      39             : 
      40             :   /**
      41             :    * Command that offers a denomination to revoke.
      42             :    */
      43             :   const char *coin_reference;
      44             : 
      45             :   /**
      46             :    * The interpreter state.
      47             :    */
      48             :   struct TALER_TESTING_Interpreter *is;
      49             : 
      50             :   /**
      51             :    * The revoke process handle.
      52             :    */
      53             :   struct GNUNET_OS_Process *revoke_proc;
      54             : 
      55             :   /**
      56             :    * Configuration file name.
      57             :    */
      58             :   const char *config_filename;
      59             : 
      60             :   /**
      61             :    * Encoding of the denomination (to revoke) public key hash.
      62             :    */
      63             :   char *dhks;
      64             : 
      65             : };
      66             : 
      67             : 
      68             : /**
      69             :  * Cleanup the state.
      70             :  *
      71             :  * @param cls closure, must be a `struct RevokeState`.
      72             :  * @param cmd the command which is being cleaned up.
      73             :  */
      74             : static void
      75           8 : revoke_cleanup (void *cls,
      76             :                 const struct TALER_TESTING_Command *cmd)
      77             : {
      78           8 :   struct RevokeState *rs = cls;
      79             : 
      80           8 :   if (NULL != rs->revoke_proc)
      81             :   {
      82           0 :     GNUNET_break (0 ==
      83             :                   GNUNET_OS_process_kill (rs->revoke_proc,
      84             :                                           SIGKILL));
      85           0 :     GNUNET_OS_process_wait (rs->revoke_proc);
      86           0 :     GNUNET_OS_process_destroy (rs->revoke_proc);
      87           0 :     rs->revoke_proc = NULL;
      88             :   }
      89           8 :   GNUNET_free (rs->dhks);
      90           8 :   GNUNET_free (rs);
      91           8 : }
      92             : 
      93             : 
      94             : /**
      95             :  * Offer internal data from a "revoke" CMD to other CMDs.
      96             :  *
      97             :  * @param cls closure
      98             :  * @param[out] ret result (could be anything)
      99             :  * @param trait name of the trait
     100             :  * @param index index number of the object to offer.
     101             :  * @return #GNUNET_OK on success
     102             :  */
     103             : static int
     104          21 : revoke_traits (void *cls,
     105             :                const void **ret,
     106             :                const char *trait,
     107             :                unsigned int index)
     108             : {
     109          21 :   struct RevokeState *rs = cls;
     110             :   struct TALER_TESTING_Trait traits[] = {
     111             :     /* Needed by the handler which waits the proc'
     112             :      * death and calls the next command */
     113          21 :     TALER_TESTING_make_trait_process (0,
     114             :                                       &rs->revoke_proc),
     115          21 :     TALER_TESTING_trait_end ()
     116             :   };
     117             : 
     118          21 :   return TALER_TESTING_get_trait (traits,
     119             :                                   ret,
     120             :                                   trait,
     121             :                                   index);
     122             : }
     123             : 
     124             : 
     125             : /**
     126             :  * Run the "revoke" command.
     127             :  *
     128             :  * @param cls closure.
     129             :  * @param cmd the command to execute.
     130             :  * @param is the interpreter state.
     131             :  */
     132             : static void
     133           8 : revoke_run (void *cls,
     134             :             const struct TALER_TESTING_Command *cmd,
     135             :             struct TALER_TESTING_Interpreter *is)
     136             : {
     137           8 :   struct RevokeState *rs = cls;
     138             :   const struct TALER_TESTING_Command *coin_cmd;
     139             :   const struct TALER_EXCHANGE_DenomPublicKey *denom_pub;
     140             : 
     141           8 :   rs->is = is;
     142             :   /* Get denom pub from trait */
     143           8 :   coin_cmd = TALER_TESTING_interpreter_lookup_command (is,
     144             :                                                        rs->coin_reference);
     145             : 
     146           8 :   if (NULL == coin_cmd)
     147             :   {
     148           0 :     GNUNET_break (0);
     149           0 :     TALER_TESTING_interpreter_fail (is);
     150           0 :     return;
     151             :   }
     152             : 
     153           8 :   GNUNET_assert (GNUNET_OK ==
     154             :                  TALER_TESTING_get_trait_denom_pub (coin_cmd,
     155             :                                                     0,
     156             :                                                     &denom_pub));
     157             : 
     158           8 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     159             :               "Trying to revoke denom '%s..'\n",
     160             :               TALER_B2S (&denom_pub->h_key));
     161             : 
     162          16 :   rs->dhks = GNUNET_STRINGS_data_to_string_alloc (
     163           8 :     &denom_pub->h_key,
     164             :     sizeof (struct GNUNET_HashCode));
     165           8 :   rs->revoke_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ALL,
     166             :                                              NULL, NULL, NULL,
     167             :                                              "taler-exchange-offline",
     168             :                                              "taler-exchange-offline",
     169             :                                              "-c", rs->config_filename,
     170             :                                              "revoke-denomination", rs->dhks,
     171             :                                              "upload",
     172             :                                              NULL);
     173             : 
     174           8 :   if (NULL == rs->revoke_proc)
     175             :   {
     176           0 :     GNUNET_break (0);
     177           0 :     TALER_TESTING_interpreter_fail (is);
     178           0 :     return;
     179             :   }
     180           8 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     181             :               "Revoke is ongoing..\n");
     182           8 :   TALER_TESTING_wait_for_sigchld (is);
     183             : }
     184             : 
     185             : 
     186             : /**
     187             :  * Make a "revoke" command.
     188             :  *
     189             :  * @param label the command label.
     190             :  * @param expected_response_code expected HTTP status code.
     191             :  * @param coin_reference reference to a CMD that will offer the
     192             :  *        denomination to revoke.
     193             :  * @param config_filename configuration file name.
     194             :  * @return the command.
     195             :  */
     196             : struct TALER_TESTING_Command
     197           8 : TALER_TESTING_cmd_revoke (const char *label,
     198             :                           unsigned int expected_response_code,
     199             :                           const char *coin_reference,
     200             :                           const char *config_filename)
     201             : {
     202             : 
     203             :   struct RevokeState *rs;
     204             : 
     205           8 :   rs = GNUNET_new (struct RevokeState);
     206           8 :   rs->expected_response_code = expected_response_code;
     207           8 :   rs->coin_reference = coin_reference;
     208           8 :   rs->config_filename = config_filename;
     209             :   {
     210           8 :     struct TALER_TESTING_Command cmd = {
     211             :       .cls = rs,
     212             :       .label = label,
     213             :       .run = &revoke_run,
     214             :       .cleanup = &revoke_cleanup,
     215             :       .traits = &revoke_traits
     216             :     };
     217             : 
     218           8 :     return cmd;
     219             :   }
     220             : }

Generated by: LCOV version 1.14