exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_exec_closer.c (7135B)


      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 "taler/platform.h"
     25 #include "taler/taler_json_lib.h"
     26 #include <gnunet/gnunet_curl_lib.h>
     27 #include "taler/taler_signatures.h"
     28 #include "taler/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_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_ReserveHistoryEntry 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   bool 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 closer_run (void *cls,
     81             const struct TALER_TESTING_Command *cmd,
     82             struct TALER_TESTING_Interpreter *is)
     83 {
     84   struct CloserState *as = cls;
     85 
     86   (void) cmd;
     87   if (NULL != as->reserve_ref)
     88   {
     89     const struct TALER_TESTING_Command *rcmd;
     90     const struct TALER_ReservePublicKeyP *reserve_pubp;
     91 
     92     rcmd = TALER_TESTING_interpreter_lookup_command (is,
     93                                                      as->reserve_ref);
     94     GNUNET_assert (NULL != rcmd);
     95     if (GNUNET_OK !=
     96         TALER_TESTING_get_trait_reserve_pub (rcmd,
     97                                              &reserve_pubp))
     98     {
     99       GNUNET_break (0);
    100       TALER_TESTING_interpreter_fail (is);
    101       return;
    102     }
    103     as->reserve_pub = *reserve_pubp;
    104   }
    105   as->closer_proc = GNUNET_process_create (GNUNET_OS_INHERIT_STD_ERR);
    106   if (GNUNET_OK !=
    107       GNUNET_process_run_command_va (as->closer_proc,
    108                                      "taler-exchange-closer",
    109                                      "taler-exchange-closer",
    110                                      "-c", as->config_filename,
    111                                      "-t",   /* exit when done */
    112                                      NULL))
    113   {
    114     GNUNET_break (0);
    115     GNUNET_process_destroy (as->closer_proc);
    116     as->closer_proc = NULL;
    117     TALER_TESTING_interpreter_fail (is);
    118     return;
    119   }
    120   TALER_TESTING_wait_for_sigchld (is);
    121 }
    122 
    123 
    124 /**
    125  * Free the state of a "closer" CMD, and possibly kill its
    126  * process if it did not terminate correctly.
    127  *
    128  * @param cls closure.
    129  * @param cmd the command being freed.
    130  */
    131 static void
    132 closer_cleanup (void *cls,
    133                 const struct TALER_TESTING_Command *cmd)
    134 {
    135   struct CloserState *as = cls;
    136 
    137   (void) cmd;
    138   if (NULL != as->closer_proc)
    139   {
    140     GNUNET_break (GNUNET_OK ==
    141                   GNUNET_process_kill (as->closer_proc,
    142                                        SIGKILL));
    143     GNUNET_process_wait (as->closer_proc,
    144                          true,
    145                          NULL,
    146                          NULL);
    147     GNUNET_process_destroy (as->closer_proc);
    148     as->closer_proc = NULL;
    149   }
    150   GNUNET_free (as);
    151 }
    152 
    153 
    154 /**
    155  * Offer "closer" CMD internal data to other commands.
    156  *
    157  * @param cls closure.
    158  * @param[out] ret result.
    159  * @param trait name of the trait.
    160  * @param index index number of the object to offer.
    161  * @return #GNUNET_OK on success
    162  */
    163 static enum GNUNET_GenericReturnValue
    164 closer_traits (void *cls,
    165                const void **ret,
    166                const char *trait,
    167                unsigned int index)
    168 {
    169   struct CloserState *as = cls;
    170   struct TALER_TESTING_Trait traits[] = {
    171     TALER_TESTING_make_trait_process (&as->closer_proc),
    172     TALER_TESTING_trait_end ()
    173   };
    174   struct TALER_TESTING_Trait xtraits[] = {
    175     TALER_TESTING_make_trait_process (&as->closer_proc),
    176     TALER_TESTING_make_trait_reserve_pub (&as->reserve_pub),
    177     TALER_TESTING_make_trait_reserve_history (0,
    178                                               &as->reserve_history),
    179     TALER_TESTING_trait_end ()
    180   };
    181 
    182   return TALER_TESTING_get_trait ((as->expect_close)
    183                                   ? xtraits
    184                                   : traits,
    185                                   ret,
    186                                   trait,
    187                                   index);
    188 }
    189 
    190 
    191 struct TALER_TESTING_Command
    192 TALER_TESTING_cmd_exec_closer (const char *label,
    193                                const char *config_filename,
    194                                const char *expected_amount,
    195                                const char *expected_fee,
    196                                const char *expected_reserve_ref)
    197 {
    198   struct CloserState *as;
    199 
    200   as = GNUNET_new (struct CloserState);
    201   as->config_filename = config_filename;
    202   if (NULL != expected_reserve_ref)
    203   {
    204     as->expect_close = true;
    205     as->reserve_ref = expected_reserve_ref;
    206     if (GNUNET_OK !=
    207         TALER_string_to_amount (expected_amount,
    208                                 &as->reserve_history.amount))
    209     {
    210       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    211                   "Failed to parse amount `%s' at %s\n",
    212                   expected_amount,
    213                   label);
    214       GNUNET_assert (0);
    215     }
    216     if (GNUNET_OK !=
    217         TALER_string_to_amount (expected_fee,
    218                                 &as->reserve_history.details.close_details.fee))
    219     {
    220       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    221                   "Failed to parse amount `%s' at %s\n",
    222                   expected_fee,
    223                   label);
    224       GNUNET_assert (0);
    225     }
    226     /* expected amount includes fee, while our argument
    227        gives the amount _without_ the fee. So add the fee. */
    228     GNUNET_assert (0 <=
    229                    TALER_amount_add (
    230                      &as->reserve_history.amount,
    231                      &as->reserve_history.amount,
    232                      &as->reserve_history.details.close_details.fee));
    233     as->reserve_history.type = TALER_EXCHANGE_RTT_CLOSING;
    234   }
    235   {
    236     struct TALER_TESTING_Command cmd = {
    237       .cls = as,
    238       .label = label,
    239       .run = &closer_run,
    240       .cleanup = &closer_cleanup,
    241       .traits = &closer_traits
    242     };
    243 
    244     return cmd;
    245   }
    246 }
    247 
    248 
    249 /* end of testing_api_cmd_exec_closer.c */