testing_api_cmd_set_wire_fee.c (6814B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2020, 2022 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_set_wire_fee.c 21 * @brief command for testing POST to /management/wire-fees 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 WireFeeState 36 { 37 38 /** 39 * Wire enable handle while operation is running. 40 */ 41 struct TALER_EXCHANGE_ManagementSetWireFeeHandle *dh; 42 43 /** 44 * Our interpreter. 45 */ 46 struct TALER_TESTING_Interpreter *is; 47 48 /** 49 * Wire method to configure fee for. 50 */ 51 const char *wire_method; 52 53 /** 54 * Wire fee amount to use. 55 */ 56 const char *wire_fee; 57 58 /** 59 * Closing fee amount to use. 60 */ 61 const char *closing_fee; 62 63 /** 64 * Expected HTTP response code. 65 */ 66 unsigned int expected_response_code; 67 68 /** 69 * Should we make the request with a bad master_sig signature? 70 */ 71 bool bad_sig; 72 }; 73 74 75 /** 76 * Callback to analyze the /management/wire response, just used to check 77 * if the response code is acceptable. 78 * 79 * @param cls closure. 80 * @param sfr response details 81 */ 82 static void 83 wire_add_cb (void *cls, 84 const struct TALER_EXCHANGE_ManagementSetWireFeeResponse *sfr) 85 { 86 struct WireFeeState *ds = cls; 87 const struct TALER_EXCHANGE_HttpResponse *hr = &sfr->hr; 88 89 ds->dh = NULL; 90 if (ds->expected_response_code != hr->http_status) 91 { 92 TALER_TESTING_unexpected_status (ds->is, 93 hr->http_status, 94 ds->expected_response_code); 95 return; 96 } 97 TALER_TESTING_interpreter_next (ds->is); 98 } 99 100 101 /** 102 * Run the command. 103 * 104 * @param cls closure. 105 * @param cmd the command to execute. 106 * @param is the interpreter state. 107 */ 108 static void 109 wire_add_run (void *cls, 110 const struct TALER_TESTING_Command *cmd, 111 struct TALER_TESTING_Interpreter *is) 112 { 113 struct WireFeeState *ds = cls; 114 struct TALER_MasterSignatureP master_sig; 115 struct GNUNET_TIME_Absolute now; 116 struct GNUNET_TIME_Timestamp start_time; 117 struct GNUNET_TIME_Timestamp end_time; 118 struct TALER_WireFeeSet fees; 119 const char *exchange_url; 120 121 (void) cmd; 122 { 123 const struct TALER_TESTING_Command *exchange_cmd; 124 125 exchange_cmd = TALER_TESTING_interpreter_get_command (is, 126 "exchange"); 127 if (NULL == exchange_cmd) 128 { 129 GNUNET_break (0); 130 TALER_TESTING_interpreter_fail (is); 131 return; 132 } 133 GNUNET_assert (GNUNET_OK == 134 TALER_TESTING_get_trait_exchange_url (exchange_cmd, 135 &exchange_url)); 136 } 137 ds->is = is; 138 now = GNUNET_TIME_absolute_get (); 139 start_time = GNUNET_TIME_absolute_to_timestamp ( 140 GNUNET_TIME_absolute_subtract (now, 141 GNUNET_TIME_UNIT_HOURS)); 142 end_time = GNUNET_TIME_absolute_to_timestamp ( 143 GNUNET_TIME_absolute_add (now, 144 GNUNET_TIME_UNIT_HOURS)); 145 if ( (GNUNET_OK != 146 TALER_string_to_amount (ds->closing_fee, 147 &fees.closing)) || 148 (GNUNET_OK != 149 TALER_string_to_amount (ds->wire_fee, 150 &fees.wire)) ) 151 { 152 GNUNET_break (0); 153 TALER_TESTING_interpreter_fail (is); 154 return; 155 } 156 157 if (ds->bad_sig) 158 { 159 memset (&master_sig, 160 42, 161 sizeof (master_sig)); 162 } 163 else 164 { 165 const struct TALER_TESTING_Command *exchange_cmd; 166 const struct TALER_MasterPrivateKeyP *master_priv; 167 168 exchange_cmd = TALER_TESTING_interpreter_get_command (is, 169 "exchange"); 170 if (NULL == exchange_cmd) 171 { 172 GNUNET_break (0); 173 TALER_TESTING_interpreter_fail (is); 174 return; 175 } 176 GNUNET_assert (GNUNET_OK == 177 TALER_TESTING_get_trait_master_priv (exchange_cmd, 178 &master_priv)); 179 TALER_exchange_offline_wire_fee_sign (ds->wire_method, 180 start_time, 181 end_time, 182 &fees, 183 master_priv, 184 &master_sig); 185 } 186 ds->dh = TALER_EXCHANGE_management_set_wire_fees ( 187 TALER_TESTING_interpreter_get_context (is), 188 exchange_url, 189 ds->wire_method, 190 start_time, 191 end_time, 192 &fees, 193 &master_sig, 194 &wire_add_cb, 195 ds); 196 if (NULL == ds->dh) 197 { 198 GNUNET_break (0); 199 TALER_TESTING_interpreter_fail (is); 200 return; 201 } 202 } 203 204 205 /** 206 * Free the state of a "wire_add" CMD, and possibly cancel a 207 * pending operation thereof. 208 * 209 * @param cls closure, must be a `struct WireFeeState`. 210 * @param cmd the command which is being cleaned up. 211 */ 212 static void 213 wire_add_cleanup (void *cls, 214 const struct TALER_TESTING_Command *cmd) 215 { 216 struct WireFeeState *ds = cls; 217 218 if (NULL != ds->dh) 219 { 220 TALER_TESTING_command_incomplete (ds->is, 221 cmd->label); 222 TALER_EXCHANGE_management_set_wire_fees_cancel (ds->dh); 223 ds->dh = NULL; 224 } 225 GNUNET_free (ds); 226 } 227 228 229 struct TALER_TESTING_Command 230 TALER_TESTING_cmd_set_wire_fee (const char *label, 231 const char *wire_method, 232 const char *wire_fee, 233 const char *closing_fee, 234 unsigned int expected_http_status, 235 bool bad_sig) 236 { 237 struct WireFeeState *ds; 238 239 ds = GNUNET_new (struct WireFeeState); 240 ds->expected_response_code = expected_http_status; 241 ds->bad_sig = bad_sig; 242 ds->wire_method = wire_method; 243 ds->wire_fee = wire_fee; 244 ds->closing_fee = closing_fee; 245 { 246 struct TALER_TESTING_Command cmd = { 247 .cls = ds, 248 .label = label, 249 .run = &wire_add_run, 250 .cleanup = &wire_add_cleanup 251 }; 252 253 return cmd; 254 } 255 } 256 257 258 /* end of testing_api_cmd_set_wire_fee.c */