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 (6955B)


      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_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_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
    106     = 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   if (NULL == as->closer_proc)
    114   {
    115     GNUNET_break (0);
    116     TALER_TESTING_interpreter_fail (is);
    117     return;
    118   }
    119   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 closer_cleanup (void *cls,
    132                 const struct TALER_TESTING_Command *cmd)
    133 {
    134   struct CloserState *as = cls;
    135 
    136   (void) cmd;
    137   if (NULL != as->closer_proc)
    138   {
    139     GNUNET_break (0 ==
    140                   GNUNET_OS_process_kill (as->closer_proc,
    141                                           SIGKILL));
    142     GNUNET_OS_process_wait (as->closer_proc);
    143     GNUNET_OS_process_destroy (as->closer_proc);
    144     as->closer_proc = NULL;
    145   }
    146   GNUNET_free (as);
    147 }
    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 enum GNUNET_GenericReturnValue
    160 closer_traits (void *cls,
    161                const void **ret,
    162                const char *trait,
    163                unsigned int index)
    164 {
    165   struct CloserState *as = cls;
    166   struct TALER_TESTING_Trait traits[] = {
    167     TALER_TESTING_make_trait_process (&as->closer_proc),
    168     TALER_TESTING_trait_end ()
    169   };
    170   struct TALER_TESTING_Trait xtraits[] = {
    171     TALER_TESTING_make_trait_process (&as->closer_proc),
    172     TALER_TESTING_make_trait_reserve_pub (&as->reserve_pub),
    173     TALER_TESTING_make_trait_reserve_history (0,
    174                                               &as->reserve_history),
    175     TALER_TESTING_trait_end ()
    176   };
    177 
    178   return TALER_TESTING_get_trait ((as->expect_close)
    179                                   ? xtraits
    180                                   : traits,
    181                                   ret,
    182                                   trait,
    183                                   index);
    184 }
    185 
    186 
    187 struct TALER_TESTING_Command
    188 TALER_TESTING_cmd_exec_closer (const char *label,
    189                                const char *config_filename,
    190                                const char *expected_amount,
    191                                const char *expected_fee,
    192                                const char *expected_reserve_ref)
    193 {
    194   struct CloserState *as;
    195 
    196   as = GNUNET_new (struct CloserState);
    197   as->config_filename = config_filename;
    198   if (NULL != expected_reserve_ref)
    199   {
    200     as->expect_close = true;
    201     as->reserve_ref = expected_reserve_ref;
    202     if (GNUNET_OK !=
    203         TALER_string_to_amount (expected_amount,
    204                                 &as->reserve_history.amount))
    205     {
    206       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    207                   "Failed to parse amount `%s' at %s\n",
    208                   expected_amount,
    209                   label);
    210       GNUNET_assert (0);
    211     }
    212     if (GNUNET_OK !=
    213         TALER_string_to_amount (expected_fee,
    214                                 &as->reserve_history.details.close_details.fee))
    215     {
    216       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    217                   "Failed to parse amount `%s' at %s\n",
    218                   expected_fee,
    219                   label);
    220       GNUNET_assert (0);
    221     }
    222     /* expected amount includes fee, while our argument
    223        gives the amount _without_ the fee. So add the fee. */
    224     GNUNET_assert (0 <=
    225                    TALER_amount_add (
    226                      &as->reserve_history.amount,
    227                      &as->reserve_history.amount,
    228                      &as->reserve_history.details.close_details.fee));
    229     as->reserve_history.type = TALER_EXCHANGE_RTT_CLOSING;
    230   }
    231   {
    232     struct TALER_TESTING_Command cmd = {
    233       .cls = as,
    234       .label = label,
    235       .run = &closer_run,
    236       .cleanup = &closer_cleanup,
    237       .traits = &closer_traits
    238     };
    239 
    240     return cmd;
    241   }
    242 }
    243 
    244 
    245 /* end of testing_api_cmd_exec_closer.c */