LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_exec_closer.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 40 54 74.1 %
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) 2018 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
       7             :   by 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
      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,
      17             :   see <http://www.gnu.org/licenses/>
      18             : */
      19             : /**
      20             :  * @file testing/testing_api_cmd_exec_closer.c
      21             :  * @brief run the taler-exchange-closer 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_signatures.h"
      28             : #include "taler_testing_lib.h"
      29             : 
      30             : 
      31             : /**
      32             :  * State for a "closer" CMD.
      33             :  */
      34             : struct CloserState
      35             : {
      36             : 
      37             :   /**
      38             :    * Closer process.
      39             :    */
      40             :   struct GNUNET_OS_Process *closer_proc;
      41             : 
      42             :   /**
      43             :    * Configuration file used by the closer.
      44             :    */
      45             :   const char *config_filename;
      46             : 
      47             :   /**
      48             :    * Reserve history entry that corresponds to this operation.  Set if @e
      49             :    * expect_close is true.  Will be of type
      50             :    * #TALER_EXCHANGE_RTT_RESERVE_CLOSED.
      51             :    */
      52             :   struct TALER_EXCHANGE_ReserveHistory reserve_history;
      53             : 
      54             :   /**
      55             :    * If the closer filled a reserve (@e expect_close is set), this is set to
      56             :    * the reserve's public key.
      57             :    */
      58             :   struct TALER_ReservePublicKeyP reserve_pub;
      59             : 
      60             :   /**
      61             :    * Reference to a command to get the @e reserve_pub.
      62             :    */
      63             :   const char *reserve_ref;
      64             : 
      65             :   /**
      66             :    * Do we expect the command to actually close a reserve?
      67             :    */
      68             :   int expect_close;
      69             : };
      70             : 
      71             : 
      72             : /**
      73             :  * Run the command.  Use the `taler-exchange-closer' program.
      74             :  *
      75             :  * @param cls closure.
      76             :  * @param cmd command being run.
      77             :  * @param is interpreter state.
      78             :  */
      79             : static void
      80           6 : closer_run (void *cls,
      81             :             const struct TALER_TESTING_Command *cmd,
      82             :             struct TALER_TESTING_Interpreter *is)
      83             : {
      84           6 :   struct CloserState *as = cls;
      85             : 
      86             :   (void) cmd;
      87           6 :   if (NULL != as->reserve_ref)
      88             :   {
      89             :     const struct TALER_TESTING_Command *rcmd;
      90             :     const struct TALER_ReservePublicKeyP *reserve_pubp;
      91             : 
      92           5 :     rcmd = TALER_TESTING_interpreter_lookup_command (is,
      93             :                                                      as->reserve_ref);
      94           5 :     if (GNUNET_OK !=
      95           5 :         TALER_TESTING_get_trait_reserve_pub (rcmd,
      96             :                                              0,
      97             :                                              &reserve_pubp))
      98             :     {
      99           0 :       GNUNET_break (0);
     100           0 :       TALER_TESTING_interpreter_fail (is);
     101           0 :       return;
     102             :     }
     103           5 :     as->reserve_pub = *reserve_pubp;
     104             :   }
     105             :   as->closer_proc
     106           6 :     = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ALL,
     107             :                                NULL, NULL, NULL,
     108             :                                "taler-exchange-closer",
     109             :                                "taler-exchange-closer",
     110             :                                "-c", as->config_filename,
     111             :                                "-t", /* exit when done */
     112             :                                NULL);
     113           6 :   if (NULL == as->closer_proc)
     114             :   {
     115           0 :     GNUNET_break (0);
     116           0 :     TALER_TESTING_interpreter_fail (is);
     117           0 :     return;
     118             :   }
     119           6 :   TALER_TESTING_wait_for_sigchld (is);
     120             : }
     121             : 
     122             : 
     123             : /**
     124             :  * Free the state of a "closer" CMD, and possibly kill its
     125             :  * process if it did not terminate correctly.
     126             :  *
     127             :  * @param cls closure.
     128             :  * @param cmd the command being freed.
     129             :  */
     130             : static void
     131           6 : closer_cleanup (void *cls,
     132             :                 const struct TALER_TESTING_Command *cmd)
     133             : {
     134           6 :   struct CloserState *as = cls;
     135             : 
     136             :   (void) cmd;
     137           6 :   if (NULL != as->closer_proc)
     138             :   {
     139           0 :     GNUNET_break (0 ==
     140             :                   GNUNET_OS_process_kill (as->closer_proc,
     141             :                                           SIGKILL));
     142           0 :     GNUNET_OS_process_wait (as->closer_proc);
     143           0 :     GNUNET_OS_process_destroy (as->closer_proc);
     144           0 :     as->closer_proc = NULL;
     145             :   }
     146           6 :   GNUNET_free (as);
     147           6 : }
     148             : 
     149             : 
     150             : /**
     151             :  * Offer "closer" CMD internal data to other commands.
     152             :  *
     153             :  * @param cls closure.
     154             :  * @param[out] ret result.
     155             :  * @param trait name of the trait.
     156             :  * @param index index number of the object to offer.
     157             :  * @return #GNUNET_OK on success
     158             :  */
     159             : static int
     160          20 : closer_traits (void *cls,
     161             :                const void **ret,
     162             :                const char *trait,
     163             :                unsigned int index)
     164             : {
     165          20 :   struct CloserState *as = cls;
     166             :   struct TALER_TESTING_Trait traits[] = {
     167          20 :     TALER_TESTING_make_trait_process (0, &as->closer_proc),
     168          20 :     TALER_TESTING_trait_end ()
     169             :   };
     170             :   struct TALER_TESTING_Trait xtraits[] = {
     171          20 :     TALER_TESTING_make_trait_process (0, &as->closer_proc),
     172          20 :     TALER_TESTING_make_trait_reserve_pub (0,
     173          20 :                                           &as->reserve_pub),
     174          20 :     TALER_TESTING_make_trait_reserve_history (0,
     175          20 :                                               &as->reserve_history),
     176          20 :     TALER_TESTING_trait_end ()
     177             :   };
     178             : 
     179          20 :   return TALER_TESTING_get_trait ((as->expect_close)
     180             :                                   ? xtraits
     181             :                                   : traits,
     182             :                                   ret,
     183             :                                   trait,
     184             :                                   index);
     185             : }
     186             : 
     187             : 
     188             : /**
     189             :  * Make a "closer" CMD.  Note that it is right now not supported to run the
     190             :  * closer to close multiple reserves in combination with a subsequent reserve
     191             :  * status call, as we cannot generate the traits necessary for multiple closed
     192             :  * reserves.  You can work around this by using multiple closer commands, one
     193             :  * per reserve that is being closed.
     194             :  *
     195             :  * @param label command label.
     196             :  * @param config_filename configuration file for the
     197             :  *                        closer to use.
     198             :  * @param expected_amount amount we expect to see wired from a @a expected_reserve_ref
     199             :  * @param expected_fee closing fee we expect to see
     200             :  * @param expected_reserve_ref reference to a reserve we expect the closer to drain;
     201             :  *          NULL if we do not expect the closer to do anything
     202             :  * @return the command.
     203             :  */
     204             : struct TALER_TESTING_Command
     205           6 : TALER_TESTING_cmd_exec_closer (const char *label,
     206             :                                const char *config_filename,
     207             :                                const char *expected_amount,
     208             :                                const char *expected_fee,
     209             :                                const char *expected_reserve_ref)
     210             : {
     211             :   struct CloserState *as;
     212             : 
     213           6 :   as = GNUNET_new (struct CloserState);
     214           6 :   as->config_filename = config_filename;
     215           6 :   if (NULL != expected_reserve_ref)
     216             :   {
     217           5 :     as->expect_close = GNUNET_YES;
     218           5 :     as->reserve_ref = expected_reserve_ref;
     219           5 :     if (GNUNET_OK !=
     220           5 :         TALER_string_to_amount (expected_amount,
     221             :                                 &as->reserve_history.amount))
     222             :     {
     223           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     224             :                   "Failed to parse amount `%s' at %s\n",
     225             :                   expected_amount,
     226             :                   label);
     227           0 :       GNUNET_assert (0);
     228             :     }
     229           5 :     if (GNUNET_OK !=
     230           5 :         TALER_string_to_amount (expected_fee,
     231             :                                 &as->reserve_history.details.close_details.fee))
     232             :     {
     233           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     234             :                   "Failed to parse amount `%s' at %s\n",
     235             :                   expected_fee,
     236             :                   label);
     237           0 :       GNUNET_assert (0);
     238             :     }
     239             :     /* expected amount includes fee, while our argument
     240             :        gives the amount _without_ the fee. So add the fee. */
     241           5 :     GNUNET_assert (0 <=
     242             :                    TALER_amount_add (&as->reserve_history.amount,
     243             :                                      &as->reserve_history.amount,
     244             :                                      &as->reserve_history.details.close_details.
     245             :                                      fee));
     246           5 :     as->reserve_history.type = TALER_EXCHANGE_RTT_CLOSE;
     247             :   }
     248             :   {
     249           6 :     struct TALER_TESTING_Command cmd = {
     250             :       .cls = as,
     251             :       .label = label,
     252             :       .run = &closer_run,
     253             :       .cleanup = &closer_cleanup,
     254             :       .traits = &closer_traits
     255             :     };
     256             : 
     257           6 :     return cmd;
     258             :   }
     259             : }
     260             : 
     261             : 
     262             : /* end of testing_api_cmd_exec_closer.c */

Generated by: LCOV version 1.14