testing_api_cmd_wire_add.c (6927B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2020-2024 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 by 7 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 GNU 13 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_wire_add.c 21 * @brief command for testing POST to /management/wire 22 * @author Christian Grothoff 23 */ 24 #include "taler/platform.h" 25 #include "taler/taler_json_lib.h" 26 #include <gnunet/gnunet_curl_lib.h> 27 #include "taler/taler_testing_lib.h" 28 #include "taler/taler_signatures.h" 29 #include "taler/backoff.h" 30 31 32 /** 33 * State for a "wire_add" CMD. 34 */ 35 struct WireAddState 36 { 37 38 /** 39 * Wire enable handle while operation is running. 40 */ 41 struct TALER_EXCHANGE_PostManagementWireHandle *dh; 42 43 /** 44 * Our interpreter. 45 */ 46 struct TALER_TESTING_Interpreter *is; 47 48 /** 49 * Account to add. 50 */ 51 struct TALER_FullPayto payto_uri; 52 53 /** 54 * Expected HTTP response code. 55 */ 56 unsigned int expected_response_code; 57 58 /** 59 * Should we make the request with a bad master_sig signature? 60 */ 61 bool bad_sig; 62 }; 63 64 65 /** 66 * Callback to analyze the /management/wire response, just used to check 67 * if the response code is acceptable. 68 * 69 * @param cls closure. 70 * @param wer response details 71 */ 72 static void 73 wire_add_cb (void *cls, 74 const struct TALER_EXCHANGE_PostManagementWireResponse *wer) 75 { 76 struct WireAddState *ds = cls; 77 const struct TALER_EXCHANGE_HttpResponse *hr = &wer->hr; 78 79 ds->dh = NULL; 80 if (ds->expected_response_code != hr->http_status) 81 { 82 TALER_TESTING_unexpected_status (ds->is, 83 hr->http_status, 84 ds->expected_response_code); 85 return; 86 } 87 TALER_TESTING_interpreter_next (ds->is); 88 } 89 90 91 /** 92 * Run the command. 93 * 94 * @param cls closure. 95 * @param cmd the command to execute. 96 * @param is the interpreter state. 97 */ 98 static void 99 wire_add_run (void *cls, 100 const struct TALER_TESTING_Command *cmd, 101 struct TALER_TESTING_Interpreter *is) 102 { 103 struct WireAddState *ds = cls; 104 struct TALER_MasterSignatureP master_sig1; 105 struct TALER_MasterSignatureP master_sig2; 106 struct GNUNET_TIME_Timestamp now; 107 json_t *credit_rest; 108 json_t *debit_rest; 109 const char *exchange_url; 110 111 (void) cmd; 112 { 113 const struct TALER_TESTING_Command *exchange_cmd; 114 115 exchange_cmd = TALER_TESTING_interpreter_get_command (is, 116 "exchange"); 117 if (NULL == exchange_cmd) 118 { 119 GNUNET_break (0); 120 TALER_TESTING_interpreter_fail (is); 121 return; 122 } 123 GNUNET_assert (GNUNET_OK == 124 TALER_TESTING_get_trait_exchange_url (exchange_cmd, 125 &exchange_url)); 126 } 127 now = GNUNET_TIME_timestamp_get (); 128 ds->is = is; 129 debit_rest = json_array (); 130 credit_rest = json_array (); 131 if (ds->bad_sig) 132 { 133 memset (&master_sig1, 134 42, 135 sizeof (master_sig1)); 136 memset (&master_sig2, 137 42, 138 sizeof (master_sig2)); 139 } 140 else 141 { 142 const struct TALER_TESTING_Command *exchange_cmd; 143 const struct TALER_MasterPrivateKeyP *master_priv; 144 145 exchange_cmd = TALER_TESTING_interpreter_get_command (is, 146 "exchange"); 147 if (NULL == exchange_cmd) 148 { 149 GNUNET_break (0); 150 TALER_TESTING_interpreter_fail (is); 151 return; 152 } 153 GNUNET_assert (GNUNET_OK == 154 TALER_TESTING_get_trait_master_priv (exchange_cmd, 155 &master_priv)); 156 TALER_exchange_offline_wire_add_sign (ds->payto_uri, 157 NULL, 158 NULL, 159 NULL, 160 debit_rest, 161 credit_rest, 162 now, 163 master_priv, 164 &master_sig1); 165 TALER_exchange_wire_signature_make (ds->payto_uri, 166 NULL, 167 NULL, 168 NULL, 169 debit_rest, 170 credit_rest, 171 master_priv, 172 &master_sig2); 173 } 174 ds->dh = TALER_EXCHANGE_post_management_wire_create ( 175 TALER_TESTING_interpreter_get_context (is), 176 exchange_url, 177 ds->payto_uri, 178 now, 179 &master_sig1, 180 &master_sig2); 181 if (NULL == ds->dh) 182 { 183 GNUNET_break (0); 184 TALER_TESTING_interpreter_fail (is); 185 return; 186 } 187 TALER_EXCHANGE_post_management_wire_set_options ( 188 ds->dh, 189 TALER_EXCHANGE_post_management_wire_option_bank_label ("test-account"), 190 TALER_EXCHANGE_post_management_wire_option_debit_restrictions ( 191 debit_rest), 192 TALER_EXCHANGE_post_management_wire_option_credit_restrictions ( 193 credit_rest)); 194 json_decref (debit_rest); 195 json_decref (credit_rest); 196 TALER_EXCHANGE_post_management_wire_start (ds->dh, &wire_add_cb, ds); 197 } 198 199 200 /** 201 * Free the state of a "wire_add" CMD, and possibly cancel a 202 * pending operation thereof. 203 * 204 * @param cls closure, must be a `struct WireAddState`. 205 * @param cmd the command which is being cleaned up. 206 */ 207 static void 208 wire_add_cleanup (void *cls, 209 const struct TALER_TESTING_Command *cmd) 210 { 211 struct WireAddState *ds = cls; 212 213 if (NULL != ds->dh) 214 { 215 TALER_TESTING_command_incomplete (ds->is, 216 cmd->label); 217 TALER_EXCHANGE_post_management_wire_cancel (ds->dh); 218 ds->dh = NULL; 219 } 220 GNUNET_free (ds); 221 } 222 223 224 struct TALER_TESTING_Command 225 TALER_TESTING_cmd_wire_add (const char *label, 226 struct TALER_FullPayto payto_uri, 227 unsigned int expected_http_status, 228 bool bad_sig) 229 { 230 struct WireAddState *ds; 231 232 ds = GNUNET_new (struct WireAddState); 233 ds->expected_response_code = expected_http_status; 234 ds->bad_sig = bad_sig; 235 ds->payto_uri = payto_uri; 236 { 237 struct TALER_TESTING_Command cmd = { 238 .cls = ds, 239 .label = label, 240 .run = &wire_add_run, 241 .cleanup = &wire_add_cleanup 242 }; 243 244 return cmd; 245 } 246 } 247 248 249 /* end of testing_api_cmd_wire_add.c */