Line data Source code
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 "platform.h"
25 : #include "taler_json_lib.h"
26 : #include <gnunet/gnunet_curl_lib.h>
27 : #include "taler_testing_lib.h"
28 : #include "taler_signatures.h"
29 : #include "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 : * Wad fee amount to use.
65 : */
66 : const char *wad_fee;
67 :
68 : /**
69 : * Expected HTTP response code.
70 : */
71 : unsigned int expected_response_code;
72 :
73 : /**
74 : * Should we make the request with a bad master_sig signature?
75 : */
76 : bool bad_sig;
77 : };
78 :
79 :
80 : /**
81 : * Callback to analyze the /management/wire response, just used to check
82 : * if the response code is acceptable.
83 : *
84 : * @param cls closure.
85 : * @param hr HTTP response details
86 : */
87 : static void
88 0 : wire_add_cb (void *cls,
89 : const struct TALER_EXCHANGE_HttpResponse *hr)
90 : {
91 0 : struct WireFeeState *ds = cls;
92 :
93 0 : ds->dh = NULL;
94 0 : if (ds->expected_response_code != hr->http_status)
95 : {
96 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
97 : "Unexpected response code %u to command %s in %s:%u\n",
98 : hr->http_status,
99 : ds->is->commands[ds->is->ip].label,
100 : __FILE__,
101 : __LINE__);
102 0 : json_dumpf (hr->reply,
103 : stderr,
104 : 0);
105 0 : TALER_TESTING_interpreter_fail (ds->is);
106 0 : return;
107 : }
108 0 : TALER_TESTING_interpreter_next (ds->is);
109 : }
110 :
111 :
112 : /**
113 : * Run the command.
114 : *
115 : * @param cls closure.
116 : * @param cmd the command to execute.
117 : * @param is the interpreter state.
118 : */
119 : static void
120 0 : wire_add_run (void *cls,
121 : const struct TALER_TESTING_Command *cmd,
122 : struct TALER_TESTING_Interpreter *is)
123 : {
124 0 : struct WireFeeState *ds = cls;
125 : struct TALER_MasterSignatureP master_sig;
126 : struct GNUNET_TIME_Absolute now;
127 : struct GNUNET_TIME_Timestamp start_time;
128 : struct GNUNET_TIME_Timestamp end_time;
129 : struct TALER_WireFeeSet fees;
130 :
131 : (void) cmd;
132 0 : ds->is = is;
133 0 : now = GNUNET_TIME_absolute_get ();
134 0 : start_time = GNUNET_TIME_absolute_to_timestamp (
135 : GNUNET_TIME_absolute_subtract (now,
136 : GNUNET_TIME_UNIT_HOURS));
137 0 : end_time = GNUNET_TIME_absolute_to_timestamp (
138 : GNUNET_TIME_absolute_add (now,
139 : GNUNET_TIME_UNIT_HOURS));
140 0 : if ( (GNUNET_OK !=
141 0 : TALER_string_to_amount (ds->closing_fee,
142 0 : &fees.closing)) ||
143 : (GNUNET_OK !=
144 0 : TALER_string_to_amount (ds->wad_fee,
145 0 : &fees.wad)) ||
146 : (GNUNET_OK !=
147 0 : TALER_string_to_amount (ds->wire_fee,
148 : &fees.wire)) )
149 : {
150 0 : GNUNET_break (0);
151 0 : TALER_TESTING_interpreter_fail (is);
152 0 : return;
153 : }
154 :
155 0 : if (ds->bad_sig)
156 : {
157 0 : memset (&master_sig,
158 : 42,
159 : sizeof (master_sig));
160 : }
161 : else
162 : {
163 0 : TALER_exchange_offline_wire_fee_sign (ds->wire_method,
164 : start_time,
165 : end_time,
166 : &fees,
167 0 : &is->master_priv,
168 : &master_sig);
169 : }
170 0 : ds->dh = TALER_EXCHANGE_management_set_wire_fees (
171 : is->ctx,
172 0 : is->exchange_url,
173 : ds->wire_method,
174 : start_time,
175 : end_time,
176 : &fees,
177 : &master_sig,
178 : &wire_add_cb,
179 : ds);
180 0 : if (NULL == ds->dh)
181 : {
182 0 : GNUNET_break (0);
183 0 : TALER_TESTING_interpreter_fail (is);
184 0 : return;
185 : }
186 : }
187 :
188 :
189 : /**
190 : * Free the state of a "wire_add" CMD, and possibly cancel a
191 : * pending operation thereof.
192 : *
193 : * @param cls closure, must be a `struct WireFeeState`.
194 : * @param cmd the command which is being cleaned up.
195 : */
196 : static void
197 0 : wire_add_cleanup (void *cls,
198 : const struct TALER_TESTING_Command *cmd)
199 : {
200 0 : struct WireFeeState *ds = cls;
201 :
202 0 : if (NULL != ds->dh)
203 : {
204 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
205 : "Command %u (%s) did not complete\n",
206 : ds->is->ip,
207 : cmd->label);
208 0 : TALER_EXCHANGE_management_set_wire_fees_cancel (ds->dh);
209 0 : ds->dh = NULL;
210 : }
211 0 : GNUNET_free (ds);
212 0 : }
213 :
214 :
215 : struct TALER_TESTING_Command
216 0 : TALER_TESTING_cmd_set_wire_fee (const char *label,
217 : const char *wire_method,
218 : const char *wire_fee,
219 : const char *closing_fee,
220 : const char *wad_fee,
221 : unsigned int expected_http_status,
222 : bool bad_sig)
223 : {
224 : struct WireFeeState *ds;
225 :
226 0 : ds = GNUNET_new (struct WireFeeState);
227 0 : ds->expected_response_code = expected_http_status;
228 0 : ds->bad_sig = bad_sig;
229 0 : ds->wire_method = wire_method;
230 0 : ds->wire_fee = wire_fee;
231 0 : ds->closing_fee = closing_fee;
232 0 : ds->wad_fee = wad_fee;
233 : {
234 0 : struct TALER_TESTING_Command cmd = {
235 : .cls = ds,
236 : .label = label,
237 : .run = &wire_add_run,
238 : .cleanup = &wire_add_cleanup
239 : };
240 :
241 0 : return cmd;
242 : }
243 : }
244 :
245 :
246 : /* end of testing_api_cmd_set_wire_fee.c */
|