exchange

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

test_exchange_api_twisted.c (10544B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2014-2023 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/test_exchange_api_twisted.c
     21  * @brief testcase to test exchange's HTTP API interface
     22  * @author Marcello Stanisci
     23  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
     24  * @author Christian Grothoff
     25  */
     26 #include "taler/platform.h"
     27 #include "taler/taler_util.h"
     28 #include "taler/taler_signatures.h"
     29 #include "taler/taler_exchange_service.h"
     30 #include "taler/taler_json_lib.h"
     31 #include <gnunet/gnunet_util_lib.h>
     32 #include <gnunet/gnunet_testing_lib.h>
     33 #include <microhttpd.h>
     34 #include "taler/taler_bank_service.h"
     35 #include "taler/taler_fakebank_lib.h"
     36 #include "taler/taler_testing_lib.h"
     37 #include "taler/taler_twister_testing_lib.h"
     38 #include <taler/taler_twister_service.h>
     39 
     40 /**
     41  * Configuration file we use.  One (big) configuration is used
     42  * for the various components for this test.
     43  */
     44 static char *config_file;
     45 
     46 /**
     47  * Our credentials.
     48  */
     49 static struct TALER_TESTING_Credentials cred;
     50 
     51 /**
     52  * (real) Twister URL.  Used at startup time to check if it runs.
     53  */
     54 static char *twister_url;
     55 
     56 /**
     57  * Twister process.
     58  */
     59 static struct GNUNET_OS_Process *twisterd;
     60 
     61 
     62 /**
     63  * Execute the taler-exchange-wirewatch command with
     64  * our configuration file.
     65  *
     66  * @param label label to use for the command.
     67  */
     68 static struct TALER_TESTING_Command
     69 CMD_EXEC_WIREWATCH (const char *label)
     70 {
     71   return TALER_TESTING_cmd_exec_wirewatch2 (label,
     72                                             config_file,
     73                                             "exchange-account-2");
     74 }
     75 
     76 
     77 /**
     78  * Run wire transfer of funds from some user's account to the
     79  * exchange.
     80  *
     81  * @param label label to use for the command.
     82  * @param amount amount to transfer, i.e. "EUR:1"
     83  * @param url exchange_url
     84  */
     85 static struct TALER_TESTING_Command
     86 CMD_TRANSFER_TO_EXCHANGE (const char *label,
     87                           const char *amount)
     88 {
     89   return TALER_TESTING_cmd_admin_add_incoming (label,
     90                                                amount,
     91                                                &cred.ba,
     92                                                cred.user42_payto);
     93 }
     94 
     95 
     96 /**
     97  * Main function that will tell the interpreter what commands to
     98  * run.
     99  *
    100  * @param cls closure
    101  */
    102 static void
    103 run (void *cls,
    104      struct TALER_TESTING_Interpreter *is)
    105 {
    106   /**
    107    * This batch aims to trigger the 409 Conflict
    108    * response from a refresh-reveal operation.
    109    */
    110   struct TALER_TESTING_Command refresh_409_conflict[] = {
    111     CMD_TRANSFER_TO_EXCHANGE (
    112       "refresh-create-reserve",
    113       "EUR:5.01"),
    114     /**
    115      * Make previous command effective.
    116      */
    117     CMD_EXEC_WIREWATCH ("wirewatch"),
    118     /**
    119      * Withdraw EUR:5.
    120      */
    121     TALER_TESTING_cmd_withdraw_amount (
    122       "refresh-withdraw-coin",
    123       "refresh-create-reserve",
    124       "EUR:5",
    125       0,                                  /* age restriction off */
    126       MHD_HTTP_OK),
    127     TALER_TESTING_cmd_set_var (
    128       "account-priv",
    129       TALER_TESTING_cmd_deposit (
    130         "refresh-deposit-partial-fail-kyc",
    131         "refresh-withdraw-coin",
    132         0,
    133         cred.user42_payto,
    134         "{\"items\":[{\"name\":\"ice cream\",\"value\":\"EUR:1\"}]}",
    135         GNUNET_TIME_UNIT_ZERO,
    136         "EUR:1",
    137         MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS)),
    138     TALER_TESTING_cmd_admin_add_kycauth (
    139       "kyc-auth-transfer",
    140       "EUR:0.01",
    141       &cred.ba,
    142       cred.user42_payto,
    143       "refresh-deposit-partial-fail-kyc"),
    144     CMD_EXEC_WIREWATCH (
    145       "import-kyc-account-withdraw"),
    146     TALER_TESTING_cmd_deposit (
    147       "refresh-deposit-partial",
    148       "refresh-withdraw-coin",
    149       0,
    150       cred.user42_payto,
    151       "{\"items\":[{\"name\":\"ice cream\",\"value\":\"EUR:1\"}]}",
    152       GNUNET_TIME_UNIT_ZERO,
    153       "EUR:1",
    154       MHD_HTTP_OK),
    155     /**
    156      * Melt the rest of the coin's value
    157      * (EUR:4.00 = 3x EUR:1.03 + 7x EUR:0.13) */
    158     TALER_TESTING_cmd_melt (
    159       "refresh-melt",
    160       "refresh-withdraw-coin",
    161       MHD_HTTP_OK,
    162       NULL),
    163 #if FIXME_409_CONFLICT
    164     /* Trigger 409 Conflict.  */
    165     TALER_TESTING_cmd_flip_upload (
    166       "flip-upload",
    167       config_file,
    168       "transfer_privs.0"),
    169 
    170     TALER_TESTING_cmd_melt_reveal (
    171       "refresh-(flipped-)reveal",
    172       "refresh-melt",
    173       MHD_HTTP_CONFLICT),
    174 #endif
    175     TALER_TESTING_cmd_end ()
    176   };
    177 #pragma message "conflict test must be adjusted to new melt/reveal API"
    178 
    179 
    180   /**
    181    * NOTE: not all CMDs actually need the twister,
    182    * so it may be better to move those into the "main"
    183    * lib test suite.
    184    */
    185   struct TALER_TESTING_Command refund[] = {
    186     CMD_TRANSFER_TO_EXCHANGE (
    187       "create-reserve-r1",
    188       "EUR:5.01"),
    189     CMD_EXEC_WIREWATCH ("wirewatch-r1"),
    190     TALER_TESTING_cmd_withdraw_amount (
    191       "withdraw-coin-r1",
    192       "create-reserve-r1",
    193       "EUR:5",
    194       0,                                  /* age restriction off */
    195       MHD_HTTP_OK),
    196     TALER_TESTING_cmd_deposit (
    197       "deposit-refund-1",
    198       "withdraw-coin-r1",
    199       0,
    200       cred.user42_payto,
    201       "{\"items\":[{\"name\":\"ice cream\",\"value\":\"EUR:5\"}]}",
    202       GNUNET_TIME_UNIT_MINUTES,
    203       "EUR:5",
    204       MHD_HTTP_OK),
    205     TALER_TESTING_cmd_refund ("refund-currency-mismatch",
    206                               MHD_HTTP_BAD_REQUEST,
    207                               "USD:5",
    208                               "deposit-refund-1"),
    209     TALER_TESTING_cmd_flip_upload ("flip-upload",
    210                                    config_file,
    211                                    "merchant_sig"),
    212     TALER_TESTING_cmd_refund ("refund-bad-sig",
    213                               MHD_HTTP_FORBIDDEN,
    214                               "EUR:5",
    215                               "deposit-refund-1"),
    216     /* This next deposit CMD is only used to provide a
    217      * good merchant signature to the next (failing) refund
    218      * operations.  */
    219     TALER_TESTING_cmd_deposit (
    220       "deposit-refund-to-fail",
    221       "withdraw-coin-r1",
    222       0,                          /* coin index.  */
    223       cred.user42_payto,
    224       /* This parameter will make any comparison about
    225          h_contract_terms fail, when /refund will be handled.
    226          So in other words, this is h_contract mismatch.  */
    227       "{\"items\":[{\"name\":\"ice skate\",\"value\":\"EUR:5\"}]}",
    228       GNUNET_TIME_UNIT_MINUTES,
    229       "EUR:5",
    230       MHD_HTTP_CONFLICT),
    231     TALER_TESTING_cmd_refund ("refund-deposit-not-found",
    232                               MHD_HTTP_NOT_FOUND,
    233                               "EUR:5",
    234                               "deposit-refund-to-fail"),
    235     TALER_TESTING_cmd_refund ("refund-insufficient-funds",
    236                               MHD_HTTP_CONFLICT,
    237                               "EUR:50",
    238                               "deposit-refund-1"),
    239     TALER_TESTING_cmd_end ()
    240   };
    241 
    242 #if 0
    243   /**
    244    * Test that we don't get errors when the keys from the exchange
    245    * are out of date.
    246    */
    247   struct TALER_TESTING_Command expired_keys[] = {
    248     TALER_TESTING_cmd_modify_header_dl (
    249       "modify-expiration",
    250       config_file,
    251       MHD_HTTP_HEADER_EXPIRES,
    252       "Wed, 19 Jan 586524 08:01:49 GMT"),
    253     TALER_TESTING_cmd_check_keys_pull_all_keys (
    254       "check-keys-expiration-0",
    255       2),
    256     /**
    257      * Run some normal commands after this to make sure everything is fine.
    258      */
    259     CMD_TRANSFER_TO_EXCHANGE ("create-reserve-r2",
    260                               "EUR:55.01"),
    261     CMD_EXEC_WIREWATCH ("wirewatch-r2"),
    262     TALER_TESTING_cmd_withdraw_amount (
    263       "withdraw-coin-r2",
    264       "create-reserve-r2",
    265       "EUR:5",
    266       0,                                  /* age restriction off */
    267       MHD_HTTP_OK),
    268     TALER_TESTING_cmd_end ()
    269   };
    270 #endif
    271 
    272   struct TALER_TESTING_Command commands[] = {
    273     TALER_TESTING_cmd_run_fakebank ("run-fakebank",
    274                                     cred.cfg,
    275                                     "exchange-account-2"),
    276     TALER_TESTING_cmd_system_start ("start-taler",
    277                                     config_file,
    278                                     "-e",
    279                                     NULL),
    280     TALER_TESTING_cmd_get_exchange ("get-exchange",
    281                                     cred.cfg,
    282                                     NULL,
    283                                     true,
    284                                     true),
    285     TALER_TESTING_cmd_batch (
    286       "refresh-reveal-409-conflict",
    287       refresh_409_conflict),
    288     TALER_TESTING_cmd_batch (
    289       "refund",
    290       refund),
    291 #if 0
    292     TALER_TESTING_cmd_batch ("expired-keys",
    293                              expired_keys),
    294 #endif
    295     TALER_TESTING_cmd_end ()
    296   };
    297 
    298   (void) cls;
    299   TALER_TESTING_run (is,
    300                      commands);
    301 }
    302 
    303 
    304 /**
    305  * Kill, wait, and destroy convenience function.
    306  *
    307  * @param[in] process process to purge.
    308  */
    309 static void
    310 purge_process (struct GNUNET_OS_Process *process)
    311 {
    312   GNUNET_OS_process_kill (process,
    313                           SIGINT);
    314   GNUNET_OS_process_wait (process);
    315   GNUNET_OS_process_destroy (process);
    316 }
    317 
    318 
    319 int
    320 main (int argc,
    321       char *const *argv)
    322 {
    323   int ret;
    324 
    325   (void) argc;
    326   {
    327     char *cipher;
    328 
    329     cipher = GNUNET_STRINGS_get_suffix_from_binary_name (argv[0]);
    330     GNUNET_assert (NULL != cipher);
    331     GNUNET_asprintf (&config_file,
    332                      "test_exchange_api_twisted-%s.conf",
    333                      cipher);
    334     GNUNET_free (cipher);
    335   }
    336   /* FIXME: introduce commands for twister! */
    337   twister_url = TALER_TWISTER_prepare_twister (config_file);
    338   if (NULL == twister_url)
    339     return 77;
    340   twisterd = TALER_TWISTER_run_twister (config_file);
    341   if (NULL == twisterd)
    342     return 77;
    343   ret = TALER_TESTING_main (argv,
    344                             "INFO",
    345                             config_file,
    346                             "exchange-account-2",
    347                             TALER_TESTING_BS_FAKEBANK,
    348                             &cred,
    349                             &run,
    350                             NULL);
    351   purge_process (twisterd);
    352   GNUNET_free (twister_url);
    353   return ret;
    354 }
    355 
    356 
    357 /* end of test_exchange_api_twisted.c */