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