testing_api_cmd_bank_check.c (8349B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2018-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_bank_check.c 21 * @brief command to check if a particular wire transfer took 22 * place. 23 * @author Marcello Stanisci 24 */ 25 #include "taler/platform.h" 26 #include "taler/taler_json_lib.h" 27 #include <gnunet/gnunet_curl_lib.h> 28 #include "taler/taler_testing_lib.h" 29 #include "taler/taler_fakebank_lib.h" 30 31 32 /** 33 * State for a "bank check" CMD. 34 */ 35 struct BankCheckState 36 { 37 38 /** 39 * Base URL of the exchange supposed to be 40 * involved in the bank transaction. 41 */ 42 const char *exchange_base_url; 43 44 /** 45 * Expected transferred amount. 46 */ 47 const char *amount; 48 49 /** 50 * Expected debit bank account. 51 */ 52 struct TALER_FullPayto debit_payto; 53 54 /** 55 * Expected credit bank account. 56 */ 57 struct TALER_FullPayto credit_payto; 58 59 /** 60 * Binary form of the wire transfer subject. 61 */ 62 struct TALER_WireTransferIdentifierRawP wtid; 63 64 /** 65 * Interpreter state. 66 */ 67 struct TALER_TESTING_Interpreter *is; 68 69 /** 70 * Reference to a CMD that provides all the data 71 * needed to issue the bank check. If NULL, that data 72 * must exist here in the state. 73 */ 74 const char *deposit_reference; 75 }; 76 77 78 /** 79 * Run the command. 80 * 81 * @param cls closure. 82 * @param cmd the command to execute. 83 * @param is the interpreter state. 84 */ 85 static void 86 check_bank_transfer_run (void *cls, 87 const struct TALER_TESTING_Command *cmd, 88 struct TALER_TESTING_Interpreter *is) 89 { 90 struct BankCheckState *bcs = cls; 91 struct TALER_Amount amount; 92 char *debit_account; 93 char *credit_account; 94 const char *exchange_base_url; 95 const struct TALER_FullPayto *debit_payto; 96 const struct TALER_FullPayto *credit_payto; 97 struct TALER_FAKEBANK_Handle *fakebank; 98 99 (void) cmd; 100 { 101 const struct TALER_TESTING_Command *fakebank_cmd; 102 103 fakebank_cmd 104 = TALER_TESTING_interpreter_get_command (is, 105 "fakebank"); 106 if (NULL == fakebank_cmd) 107 { 108 GNUNET_break (0); 109 TALER_TESTING_interpreter_fail (is); 110 return; 111 } 112 if (GNUNET_OK != 113 TALER_TESTING_get_trait_fakebank (fakebank_cmd, 114 &fakebank)) 115 { 116 GNUNET_break (0); 117 TALER_TESTING_interpreter_fail (is); 118 return; 119 } 120 } 121 if (NULL == bcs->deposit_reference) 122 { 123 TALER_LOG_INFO ("Deposit reference NOT given\n"); 124 debit_payto = &bcs->debit_payto; 125 credit_payto = &bcs->credit_payto; 126 exchange_base_url = bcs->exchange_base_url; 127 128 if (GNUNET_OK != 129 TALER_string_to_amount (bcs->amount, 130 &amount)) 131 { 132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 133 "Failed to parse amount `%s' at %s\n", 134 bcs->amount, 135 TALER_TESTING_interpreter_get_current_label (is)); 136 TALER_TESTING_interpreter_fail (is); 137 return; 138 } 139 } 140 else 141 { 142 const struct TALER_TESTING_Command *deposit_cmd; 143 const struct TALER_Amount *amount_ptr; 144 145 TALER_LOG_INFO ("`%s' uses reference (%s/%p)\n", 146 TALER_TESTING_interpreter_get_current_label 147 (is), 148 bcs->deposit_reference, 149 bcs->deposit_reference); 150 deposit_cmd 151 = TALER_TESTING_interpreter_lookup_command (is, 152 bcs->deposit_reference); 153 if (NULL == deposit_cmd) 154 TALER_TESTING_FAIL (is); 155 if ( (GNUNET_OK != 156 TALER_TESTING_get_trait_amount (deposit_cmd, 157 &amount_ptr)) || 158 (GNUNET_OK != 159 TALER_TESTING_get_trait_debit_payto_uri (deposit_cmd, 160 &debit_payto)) || 161 (GNUNET_OK != 162 TALER_TESTING_get_trait_credit_payto_uri (deposit_cmd, 163 &credit_payto)) || 164 (GNUNET_OK != 165 TALER_TESTING_get_trait_exchange_url (deposit_cmd, 166 &exchange_base_url)) ) 167 TALER_TESTING_FAIL (is); 168 amount = *amount_ptr; 169 } 170 debit_account = TALER_xtalerbank_account_from_payto (*debit_payto); 171 credit_account = TALER_xtalerbank_account_from_payto (*credit_payto); 172 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 173 "converted debit_payto (%s) to debit_account (%s)\n", 174 debit_payto->full_payto, 175 debit_account); 176 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 177 "converted credit_payto (%s) to credit_account (%s)\n", 178 credit_payto->full_payto, 179 credit_account); 180 if (GNUNET_OK != 181 TALER_FAKEBANK_check_debit (fakebank, 182 &amount, 183 debit_account, 184 credit_account, 185 exchange_base_url, 186 &bcs->wtid)) 187 { 188 GNUNET_break (0); 189 GNUNET_free (credit_account); 190 GNUNET_free (debit_account); 191 TALER_TESTING_interpreter_fail (is); 192 return; 193 } 194 GNUNET_free (credit_account); 195 GNUNET_free (debit_account); 196 TALER_TESTING_interpreter_next (is); 197 } 198 199 200 /** 201 * Free the state of a "bank check" CMD. 202 * 203 * @param cls closure. 204 * @param cmd the command which is being cleaned up. 205 */ 206 static void 207 check_bank_transfer_cleanup (void *cls, 208 const struct TALER_TESTING_Command *cmd) 209 { 210 struct BankCheckState *bcs = cls; 211 212 (void) cmd; 213 GNUNET_free (bcs); 214 } 215 216 217 /** 218 * Offer internal data from a "bank check" CMD state. 219 * 220 * @param cls closure. 221 * @param[out] ret result. 222 * @param trait name of the trait. 223 * @param index index number of the object to offer. 224 * @return #GNUNET_OK on success. 225 */ 226 static enum GNUNET_GenericReturnValue 227 check_bank_transfer_traits (void *cls, 228 const void **ret, 229 const char *trait, 230 unsigned int index) 231 { 232 struct BankCheckState *bcs = cls; 233 struct TALER_WireTransferIdentifierRawP *wtid_ptr = &bcs->wtid; 234 struct TALER_TESTING_Trait traits[] = { 235 TALER_TESTING_make_trait_wtid (wtid_ptr), 236 TALER_TESTING_make_trait_exchange_url ( 237 bcs->exchange_base_url), 238 TALER_TESTING_trait_end () 239 }; 240 241 return TALER_TESTING_get_trait (traits, 242 ret, 243 trait, 244 index); 245 } 246 247 248 struct TALER_TESTING_Command 249 TALER_TESTING_cmd_check_bank_transfer ( 250 const char *label, 251 const char *exchange_base_url, 252 const char *amount, 253 const struct TALER_FullPayto debit_payto, 254 const struct TALER_FullPayto credit_payto) 255 { 256 struct BankCheckState *bcs; 257 258 bcs = GNUNET_new (struct BankCheckState); 259 bcs->exchange_base_url = exchange_base_url; 260 bcs->amount = amount; 261 bcs->debit_payto = debit_payto; 262 bcs->credit_payto = credit_payto; 263 bcs->deposit_reference = NULL; 264 { 265 struct TALER_TESTING_Command cmd = { 266 .label = label, 267 .cls = bcs, 268 .run = &check_bank_transfer_run, 269 .cleanup = &check_bank_transfer_cleanup, 270 .traits = &check_bank_transfer_traits 271 }; 272 273 return cmd; 274 } 275 } 276 277 278 struct TALER_TESTING_Command 279 TALER_TESTING_cmd_check_bank_transfer_with_ref ( 280 const char *label, 281 const char *deposit_reference) 282 { 283 struct BankCheckState *bcs; 284 285 bcs = GNUNET_new (struct BankCheckState); 286 bcs->deposit_reference = deposit_reference; 287 { 288 struct TALER_TESTING_Command cmd = { 289 .label = label, 290 .cls = bcs, 291 .run = &check_bank_transfer_run, 292 .cleanup = &check_bank_transfer_cleanup, 293 .traits = &check_bank_transfer_traits 294 }; 295 296 return cmd; 297 } 298 }