testing_api_cmd_run_fakebank.c (5761B)
1 /* 2 This file is part of TALER 3 (C) 2023, 2024 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_run_fakebank.c 21 * @brief Command to run fakebank in-process 22 * @author Christian Grothoff 23 */ 24 #include "taler/taler_json_lib.h" 25 #include <gnunet/gnunet_curl_lib.h> 26 #include "taler/taler_testing_lib.h" 27 28 /** 29 * State for a "run fakebank" CMD. 30 */ 31 struct RunFakebankState 32 { 33 34 /** 35 * Our interpreter state. 36 */ 37 struct TALER_TESTING_Interpreter *is; 38 39 /** 40 * Handle to the fakebank we are running. 41 */ 42 struct TALER_FAKEBANK_Handle *fakebank; 43 44 /** 45 * URL of the bank. 46 */ 47 char *bank_url; 48 49 /** 50 * Currency to use. 51 */ 52 char *currency; 53 54 /** 55 * Data for access control. 56 */ 57 struct TALER_BANK_AuthenticationData ba; 58 59 /** 60 * Port to use. 61 */ 62 uint16_t port; 63 }; 64 65 66 /** 67 * Run the "get_exchange" command. 68 * 69 * @param cls closure. 70 * @param cmd the command currently being executed. 71 * @param is the interpreter state. 72 */ 73 static void 74 run_fakebank_run (void *cls, 75 const struct TALER_TESTING_Command *cmd, 76 struct TALER_TESTING_Interpreter *is) 77 { 78 struct RunFakebankState *rfs = cls; 79 80 (void) cmd; 81 rfs->fakebank = TALER_FAKEBANK_start (rfs->port, 82 rfs->currency); 83 if (NULL == rfs->fakebank) 84 { 85 GNUNET_break (0); 86 TALER_TESTING_interpreter_fail (is); 87 return; 88 } 89 TALER_TESTING_interpreter_next (is); 90 } 91 92 93 /** 94 * Cleanup the state. 95 * 96 * @param cls closure. 97 * @param cmd the command which is being cleaned up. 98 */ 99 static void 100 run_fakebank_cleanup (void *cls, 101 const struct TALER_TESTING_Command *cmd) 102 { 103 struct RunFakebankState *rfs = cls; 104 105 if (NULL != rfs->fakebank) 106 { 107 TALER_FAKEBANK_stop (rfs->fakebank); 108 rfs->fakebank = NULL; 109 } 110 GNUNET_free (rfs->ba.wire_gateway_url); 111 GNUNET_free (rfs->bank_url); 112 GNUNET_free (rfs->currency); 113 GNUNET_free (rfs); 114 } 115 116 117 /** 118 * Offer internal data to a "run_fakebank" CMD state to other commands. 119 * 120 * @param cls closure 121 * @param[out] ret result (could be anything) 122 * @param trait name of the trait 123 * @param index index number of the object to offer. 124 * @return #GNUNET_OK on success 125 */ 126 static enum GNUNET_GenericReturnValue 127 run_fakebank_traits (void *cls, 128 const void **ret, 129 const char *trait, 130 unsigned int index) 131 { 132 struct RunFakebankState *rfs = cls; 133 struct TALER_TESTING_Trait traits[] = { 134 TALER_TESTING_make_trait_bank_auth_data (&rfs->ba), 135 TALER_TESTING_make_trait_fakebank (rfs->fakebank), 136 TALER_TESTING_trait_end () 137 }; 138 139 return TALER_TESTING_get_trait (traits, 140 ret, 141 trait, 142 index); 143 } 144 145 146 struct TALER_TESTING_Command 147 TALER_TESTING_cmd_run_fakebank ( 148 const char *label, 149 const struct GNUNET_CONFIGURATION_Handle *cfg, 150 const char *exchange_account_section) 151 { 152 struct RunFakebankState *rfs; 153 unsigned long long fakebank_port; 154 struct TALER_FullPayto exchange_payto_uri; 155 156 if (GNUNET_OK != 157 GNUNET_CONFIGURATION_get_value_number (cfg, 158 "BANK", 159 "HTTP_PORT", 160 &fakebank_port)) 161 { 162 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 163 "BANK", 164 "HTTP_PORT"); 165 GNUNET_assert (0); 166 } 167 if (GNUNET_OK != 168 GNUNET_CONFIGURATION_get_value_string (cfg, 169 exchange_account_section, 170 "PAYTO_URI", 171 &exchange_payto_uri.full_payto)) 172 { 173 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 174 exchange_account_section, 175 "PAYTO_URI"); 176 GNUNET_assert (0); 177 } 178 rfs = GNUNET_new (struct RunFakebankState); 179 rfs->port = (uint16_t) fakebank_port; 180 GNUNET_asprintf (&rfs->bank_url, 181 "http://localhost:%u/", 182 (unsigned int) rfs->port); 183 GNUNET_assert (GNUNET_OK == 184 TALER_config_get_currency (cfg, 185 "exchange", 186 &rfs->currency)); 187 { 188 char *exchange_xtalerbank_account; 189 190 exchange_xtalerbank_account 191 = TALER_xtalerbank_account_from_payto (exchange_payto_uri); 192 GNUNET_assert (NULL != exchange_xtalerbank_account); 193 GNUNET_asprintf (&rfs->ba.wire_gateway_url, 194 "http://localhost:%u/%s/", 195 (unsigned int) fakebank_port, 196 exchange_xtalerbank_account); 197 GNUNET_free (exchange_xtalerbank_account); 198 } 199 GNUNET_free (exchange_payto_uri.full_payto); 200 rfs->ba.method = TALER_BANK_AUTH_NONE; 201 { 202 struct TALER_TESTING_Command cmd = { 203 .cls = rfs, 204 .label = label, 205 .run = &run_fakebank_run, 206 .cleanup = &run_fakebank_cleanup, 207 .traits = &run_fakebank_traits, 208 .name = "fakebank" 209 }; 210 211 return cmd; 212 } 213 }