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