testing_api_cmd_get_transfers.c (9959B)
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_api_cmd_get_transfers.c 21 * @brief command to test GET /transfers. 22 * @author Marcello Stanisci 23 * @author Christian Grothoff 24 */ 25 #include "taler/platform.h" 26 #include <taler/taler_exchange_service.h> 27 #include <taler/taler_testing_lib.h> 28 #include "taler/taler_merchant_service.h" 29 #include "taler/taler_merchant_testing_lib.h" 30 #include <taler/taler-merchant/get-private-transfers.h> 31 32 33 /** 34 * State of a GET transfers CMD. 35 */ 36 struct GetTransfersState 37 { 38 39 /** 40 * Handle for a "get transfer" request. 41 */ 42 struct TALER_MERCHANT_GetPrivateTransfersHandle *gth; 43 44 /** 45 * The interpreter state. 46 */ 47 struct TALER_TESTING_Interpreter *is; 48 49 /** 50 * Base URL of the merchant serving the request. 51 */ 52 const char *merchant_url; 53 54 /** 55 * payto URI of the merchant to filter by. 56 */ 57 struct TALER_FullPayto payto_uri; 58 59 /** 60 * Expected HTTP response code. 61 */ 62 unsigned int http_status; 63 64 /** 65 * Reference for a "check bank" CMD. It offers the 66 * WTID to get. 67 */ 68 const char *check_bank_reference; 69 70 /** 71 * Array of POST /transfer command labels we expect to see listed. 72 */ 73 const char **transfers; 74 75 /** 76 * Length of @e transfers. 77 */ 78 unsigned int transfers_length; 79 80 }; 81 82 83 /** 84 * Check the result of our GET /transfers request to a merchant 85 * 86 * @param cls closure 87 * @param gtr response details 88 */ 89 static void 90 get_transfers_cb ( 91 void *cls, 92 const struct TALER_MERCHANT_GetPrivateTransfersResponse *gtr) 93 { 94 struct GetTransfersState *gts = cls; 95 96 gts->gth = NULL; 97 if (gts->http_status != gtr->hr.http_status) 98 { 99 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 100 "Unexpected response code %u (%d) to command %s\n", 101 gtr->hr.http_status, 102 (int) gtr->hr.ec, 103 TALER_TESTING_interpreter_get_current_label (gts->is)); 104 TALER_TESTING_interpreter_fail (gts->is); 105 return; 106 } 107 switch (gtr->hr.http_status) 108 { 109 case MHD_HTTP_OK: 110 if (gtr->details.ok.transfers_length != gts->transfers_length) 111 { 112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 113 "Transfers length does not match (got %u/want %u)\n", 114 gtr->details.ok.transfers_length, 115 gts->transfers_length); 116 TALER_TESTING_interpreter_fail (gts->is); 117 return; 118 } 119 for (unsigned int i = 0; i < gtr->details.ok.transfers_length; ++i) 120 { 121 const struct TALER_MERCHANT_GetPrivateTransfersTransferData *transfer 122 = >r->details.ok.transfers[i]; 123 const struct TALER_TESTING_Command *transfer_cmd; 124 125 transfer_cmd = TALER_TESTING_interpreter_lookup_command ( 126 gts->is, 127 gts->transfers[i]); 128 if (NULL == transfer_cmd) 129 { 130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 131 "Command `%s' not found!\n", 132 gts->transfers[i]); 133 TALER_TESTING_interpreter_fail (gts->is); 134 return; 135 } 136 { 137 const struct TALER_WireTransferIdentifierRawP *wtid; 138 139 if (GNUNET_OK != 140 TALER_TESTING_get_trait_wtid (transfer_cmd, 141 &wtid)) 142 { 143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 144 "Could not fetch wire transfer id\n"); 145 TALER_TESTING_interpreter_fail (gts->is); 146 return; 147 } 148 if (0 != GNUNET_memcmp (wtid, 149 &transfer->wtid)) 150 { 151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 152 "Wire transfer id does not match\n"); 153 TALER_TESTING_interpreter_fail (gts->is); 154 return; 155 } 156 TALER_TESTING_cmd_merchant_post_transfer_set_serial ( 157 (struct TALER_TESTING_Command *) transfer_cmd, 158 transfer->transfer_serial_id); 159 } 160 { 161 const struct TALER_FullPayto *payto_uri; 162 163 if (GNUNET_OK != 164 TALER_TESTING_get_trait_credit_payto_uri (transfer_cmd, 165 &payto_uri)) 166 { 167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 168 "Could not fetch wire transfer payto uri\n"); 169 TALER_TESTING_interpreter_fail (gts->is); 170 return; 171 } 172 if (0 != 173 TALER_full_payto_cmp (*payto_uri, 174 transfer->payto_uri)) 175 { 176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 177 "Wire transfer payto uri does not match: %s != %s\n", 178 payto_uri->full_payto, 179 transfer->payto_uri.full_payto); 180 TALER_TESTING_interpreter_fail (gts->is); 181 return; 182 } 183 } 184 { 185 const struct TALER_Amount *credit_amount; 186 187 if (GNUNET_OK != 188 TALER_TESTING_get_trait_amount (transfer_cmd, 189 &credit_amount)) 190 { 191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 192 "Could not fetch wire transfer credit amount\n"); 193 TALER_TESTING_interpreter_fail (gts->is); 194 return; 195 } 196 if ( (GNUNET_OK != 197 TALER_amount_cmp_currency (credit_amount, 198 &transfer->credit_amount)) || 199 (0 != TALER_amount_cmp (credit_amount, 200 &transfer->credit_amount))) 201 { 202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 203 "Wire transfer credit amount does not match\n"); 204 TALER_TESTING_interpreter_fail (gts->is); 205 return; 206 } 207 } 208 { 209 const char *exchange_url; 210 211 if (GNUNET_OK != 212 TALER_TESTING_get_trait_exchange_url (transfer_cmd, 213 &exchange_url)) 214 { 215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 216 "Could not fetch wire transfer exchange url\n"); 217 TALER_TESTING_interpreter_fail (gts->is); 218 return; 219 } 220 if (0 != strcmp (exchange_url, 221 transfer->exchange_url)) 222 { 223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 224 "Wire transfer exchange url does not match\n"); 225 TALER_TESTING_interpreter_fail (gts->is); 226 return; 227 } 228 } 229 } 230 break; 231 default: 232 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 233 "Unhandled HTTP status %u.\n", 234 gtr->hr.http_status); 235 break; 236 } 237 TALER_TESTING_interpreter_next (gts->is); 238 } 239 240 241 /** 242 * Run the "get transfer" CMD. 243 * 244 * @param cls closure. 245 * @param cmd command being run now. 246 * @param is interpreter state. 247 */ 248 static void 249 get_transfers_run (void *cls, 250 const struct TALER_TESTING_Command *cmd, 251 struct TALER_TESTING_Interpreter *is) 252 { 253 struct GetTransfersState *gts = cls; 254 255 gts->is = is; 256 gts->gth = TALER_MERCHANT_get_private_transfers_create ( 257 TALER_TESTING_interpreter_get_context (is), 258 gts->merchant_url); 259 TALER_MERCHANT_get_private_transfers_set_options ( 260 gts->gth, 261 TALER_MERCHANT_get_private_transfers_option_payto_uri (gts->payto_uri), 262 TALER_MERCHANT_get_private_transfers_option_before ( 263 GNUNET_TIME_UNIT_FOREVER_TS), 264 TALER_MERCHANT_get_private_transfers_option_after (GNUNET_TIME_UNIT_ZERO_TS) 265 , 266 TALER_MERCHANT_get_private_transfers_option_limit (INT64_MAX), 267 TALER_MERCHANT_get_private_transfers_option_offset (0), 268 TALER_MERCHANT_get_private_transfers_option_expected (TALER_EXCHANGE_YNA_ALL 269 )); 270 { 271 enum TALER_ErrorCode ec; 272 273 ec = TALER_MERCHANT_get_private_transfers_start ( 274 gts->gth, 275 &get_transfers_cb, 276 gts); 277 GNUNET_assert (TALER_EC_NONE == ec); 278 } 279 } 280 281 282 /** 283 * Free the state of a "get transfer" CMD, and possibly 284 * cancel a pending operation thereof. 285 * 286 * @param cls closure. 287 * @param cmd command being run. 288 */ 289 static void 290 get_transfers_cleanup (void *cls, 291 const struct TALER_TESTING_Command *cmd) 292 { 293 struct GetTransfersState *gts = cls; 294 295 if (NULL != gts->gth) 296 { 297 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 298 "GET /transfer operation did not complete\n"); 299 TALER_MERCHANT_get_private_transfers_cancel (gts->gth); 300 } 301 GNUNET_array_grow (gts->transfers, 302 gts->transfers_length, 303 0); 304 GNUNET_free (gts); 305 } 306 307 308 struct TALER_TESTING_Command 309 TALER_TESTING_cmd_merchant_get_transfers ( 310 const char *label, 311 const char *merchant_url, 312 struct TALER_FullPayto payto_uri, 313 unsigned int http_code, 314 ...) 315 { 316 struct GetTransfersState *gts; 317 318 gts = GNUNET_new (struct GetTransfersState); 319 gts->merchant_url = merchant_url; 320 gts->payto_uri = payto_uri; 321 gts->http_status = http_code; 322 { 323 const char *clabel; 324 va_list ap; 325 326 va_start (ap, http_code); 327 while (NULL != (clabel = va_arg (ap, const char *))) 328 { 329 GNUNET_array_append (gts->transfers, 330 gts->transfers_length, 331 clabel); 332 } 333 va_end (ap); 334 } 335 { 336 struct TALER_TESTING_Command cmd = { 337 .cls = gts, 338 .label = label, 339 .run = &get_transfers_run, 340 .cleanup = &get_transfers_cleanup 341 }; 342 343 return cmd; 344 } 345 } 346 347 348 /* end of testing_api_cmd_get_transfers.c */