Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2022 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU General Public License as
7 : published by the Free Software Foundation; either version 3, or
8 : (at your 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
13 : GNU 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_reserve_get.c
21 : * @brief Implement the GET /reserve/$RID test command.
22 : * @author Marcello Stanisci
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 :
29 :
30 : /**
31 : * State for a "poll" CMD.
32 : */
33 : struct PollState
34 : {
35 :
36 : /**
37 : * How long do we give the exchange to respond?
38 : */
39 : struct GNUNET_TIME_Relative timeout;
40 :
41 : /**
42 : * Label to the command which created the reserve to check,
43 : * needed to resort the reserve key.
44 : */
45 : const char *poll_reference;
46 :
47 : /**
48 : * Timeout to wait for at most.
49 : */
50 : struct GNUNET_SCHEDULER_Task *tt;
51 :
52 : /**
53 : * The interpreter we are using.
54 : */
55 : struct TALER_TESTING_Interpreter *is;
56 : };
57 :
58 :
59 : /**
60 : * State for a "status" CMD.
61 : */
62 : struct StatusState
63 : {
64 :
65 : /**
66 : * How long do we give the exchange to respond?
67 : */
68 : struct GNUNET_TIME_Relative timeout;
69 :
70 : /**
71 : * Poller waiting for us.
72 : */
73 : struct PollState *ps;
74 :
75 : /**
76 : * Label to the command which created the reserve to check,
77 : * needed to resort the reserve key.
78 : */
79 : const char *reserve_reference;
80 :
81 : /**
82 : * Handle to the "reserve status" operation.
83 : */
84 : struct TALER_EXCHANGE_GetReservesHandle *rsh;
85 :
86 : /**
87 : * Expected reserve balance.
88 : */
89 : const char *expected_balance;
90 :
91 : /**
92 : * Public key of the reserve being analyzed.
93 : */
94 : const struct TALER_ReservePublicKeyP *reserve_pubp;
95 :
96 : /**
97 : * Expected HTTP response code.
98 : */
99 : unsigned int expected_response_code;
100 :
101 : /**
102 : * Interpreter state.
103 : */
104 : struct TALER_TESTING_Interpreter *is;
105 :
106 : };
107 :
108 :
109 : /**
110 : * Check that the reserve balance and HTTP response code are
111 : * both acceptable.
112 : *
113 : * @param cls closure.
114 : * @param rs HTTP response details
115 : */
116 : static void
117 30 : reserve_status_cb (void *cls,
118 : const struct TALER_EXCHANGE_GetReservesResponse *rs)
119 : {
120 30 : struct StatusState *ss = cls;
121 30 : struct TALER_TESTING_Interpreter *is = ss->is;
122 : struct TALER_Amount eb;
123 :
124 30 : ss->rsh = NULL;
125 30 : if (ss->expected_response_code != rs->hr.http_status)
126 : {
127 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
128 : "Unexpected HTTP response code: %d in %s:%u\n",
129 : rs->hr.http_status,
130 : __FILE__,
131 : __LINE__);
132 0 : json_dumpf (rs->hr.reply,
133 : stderr,
134 : 0);
135 0 : TALER_TESTING_interpreter_fail (ss->is);
136 0 : return;
137 : }
138 30 : if (MHD_HTTP_OK == ss->expected_response_code)
139 : {
140 30 : GNUNET_assert (GNUNET_OK ==
141 : TALER_string_to_amount (ss->expected_balance,
142 : &eb));
143 30 : if (0 != TALER_amount_cmp (&eb,
144 : &rs->details.ok.balance))
145 : {
146 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147 : "Unexpected amount %s in reserve, wanted %s\n",
148 : TALER_amount_to_string (&rs->details.ok.balance),
149 : ss->expected_balance);
150 0 : TALER_TESTING_interpreter_fail (ss->is);
151 0 : return;
152 : }
153 : }
154 30 : if (NULL != ss->ps)
155 : {
156 : /* force continuation on long poller */
157 0 : GNUNET_SCHEDULER_cancel (ss->ps->tt);
158 0 : ss->ps->tt = NULL;
159 0 : TALER_TESTING_interpreter_next (is);
160 0 : return;
161 : }
162 30 : if (GNUNET_TIME_relative_is_zero (ss->timeout))
163 23 : TALER_TESTING_interpreter_next (is);
164 : }
165 :
166 :
167 : /**
168 : * Run the command.
169 : *
170 : * @param cls closure.
171 : * @param cmd the command being executed.
172 : * @param is the interpreter state.
173 : */
174 : static void
175 30 : status_run (void *cls,
176 : const struct TALER_TESTING_Command *cmd,
177 : struct TALER_TESTING_Interpreter *is)
178 : {
179 30 : struct StatusState *ss = cls;
180 : const struct TALER_TESTING_Command *create_reserve;
181 : const char *exchange_url;
182 :
183 30 : ss->is = is;
184 30 : exchange_url = TALER_TESTING_get_exchange_url (is);
185 30 : if (NULL == exchange_url)
186 : {
187 0 : GNUNET_break (0);
188 0 : return;
189 : }
190 : create_reserve
191 30 : = TALER_TESTING_interpreter_lookup_command (is,
192 : ss->reserve_reference);
193 30 : GNUNET_assert (NULL != create_reserve);
194 30 : if (GNUNET_OK !=
195 30 : TALER_TESTING_get_trait_reserve_pub (create_reserve,
196 : &ss->reserve_pubp))
197 : {
198 0 : GNUNET_break (0);
199 0 : TALER_LOG_ERROR ("Failed to find reserve_pub for status query\n");
200 0 : TALER_TESTING_interpreter_fail (is);
201 0 : return;
202 : }
203 30 : ss->rsh = TALER_EXCHANGE_get_reserves_create (
204 : TALER_TESTING_interpreter_get_context (is),
205 : exchange_url,
206 : ss->reserve_pubp);
207 30 : if (NULL == ss->rsh)
208 : {
209 0 : GNUNET_break (0);
210 0 : TALER_TESTING_interpreter_fail (is);
211 0 : return;
212 : }
213 30 : if (! GNUNET_TIME_relative_is_zero (ss->timeout))
214 7 : TALER_EXCHANGE_get_reserves_set_options (
215 : ss->rsh,
216 : TALER_EXCHANGE_get_reserves_option_timeout (ss->timeout));
217 30 : if (TALER_EC_NONE !=
218 30 : TALER_EXCHANGE_get_reserves_start (ss->rsh,
219 : &reserve_status_cb,
220 : ss))
221 : {
222 0 : GNUNET_break (0);
223 0 : TALER_EXCHANGE_get_reserves_cancel (ss->rsh);
224 0 : ss->rsh = NULL;
225 0 : TALER_TESTING_interpreter_fail (is);
226 0 : return;
227 : }
228 30 : if (! GNUNET_TIME_relative_is_zero (ss->timeout))
229 : {
230 7 : TALER_TESTING_interpreter_next (is);
231 7 : return;
232 : }
233 : }
234 :
235 :
236 : /**
237 : * Cleanup the state from a "reserve status" CMD, and possibly
238 : * cancel a pending operation thereof.
239 : *
240 : * @param cls closure.
241 : * @param cmd the command which is being cleaned up.
242 : */
243 : static void
244 30 : status_cleanup (void *cls,
245 : const struct TALER_TESTING_Command *cmd)
246 : {
247 30 : struct StatusState *ss = cls;
248 :
249 30 : if (NULL != ss->rsh)
250 : {
251 0 : TALER_TESTING_command_incomplete (ss->is,
252 : cmd->label);
253 0 : TALER_EXCHANGE_get_reserves_cancel (ss->rsh);
254 0 : ss->rsh = NULL;
255 : }
256 30 : GNUNET_free (ss);
257 30 : }
258 :
259 :
260 : struct TALER_TESTING_Command
261 23 : TALER_TESTING_cmd_status (const char *label,
262 : const char *reserve_reference,
263 : const char *expected_balance,
264 : unsigned int expected_response_code)
265 : {
266 : struct StatusState *ss;
267 :
268 23 : GNUNET_assert (NULL != reserve_reference);
269 23 : ss = GNUNET_new (struct StatusState);
270 23 : ss->reserve_reference = reserve_reference;
271 23 : ss->expected_balance = expected_balance;
272 23 : ss->expected_response_code = expected_response_code;
273 : {
274 23 : struct TALER_TESTING_Command cmd = {
275 : .cls = ss,
276 : .label = label,
277 : .run = &status_run,
278 : .cleanup = &status_cleanup
279 : };
280 :
281 23 : return cmd;
282 : }
283 : }
284 :
285 :
286 : struct TALER_TESTING_Command
287 7 : TALER_TESTING_cmd_reserve_poll (const char *label,
288 : const char *reserve_reference,
289 : const char *expected_balance,
290 : struct GNUNET_TIME_Relative timeout,
291 : unsigned int expected_response_code)
292 : {
293 : struct StatusState *ss;
294 :
295 7 : GNUNET_assert (NULL != reserve_reference);
296 7 : ss = GNUNET_new (struct StatusState);
297 7 : ss->reserve_reference = reserve_reference;
298 7 : ss->expected_balance = expected_balance;
299 7 : ss->expected_response_code = expected_response_code;
300 7 : ss->timeout = timeout;
301 : {
302 7 : struct TALER_TESTING_Command cmd = {
303 : .cls = ss,
304 : .label = label,
305 : .run = &status_run,
306 : .cleanup = &status_cleanup
307 : };
308 :
309 7 : return cmd;
310 : }
311 : }
312 :
313 :
314 : /**
315 : * Long poller timed out. Fail the test.
316 : *
317 : * @param cls a `struct PollState`
318 : */
319 : static void
320 0 : finish_timeout (void *cls)
321 : {
322 0 : struct PollState *ps = cls;
323 :
324 0 : ps->tt = NULL;
325 0 : GNUNET_break (0);
326 0 : TALER_TESTING_interpreter_fail (ps->is);
327 0 : }
328 :
329 :
330 : /**
331 : * Run the command.
332 : *
333 : * @param cls closure.
334 : * @param cmd the command being executed.
335 : * @param is the interpreter state.
336 : */
337 : static void
338 7 : finish_run (void *cls,
339 : const struct TALER_TESTING_Command *cmd,
340 : struct TALER_TESTING_Interpreter *is)
341 : {
342 7 : struct PollState *ps = cls;
343 : const struct TALER_TESTING_Command *poll_reserve;
344 : struct StatusState *ss;
345 :
346 7 : ps->is = is;
347 : poll_reserve
348 7 : = TALER_TESTING_interpreter_lookup_command (is,
349 : ps->poll_reference);
350 7 : GNUNET_assert (NULL != poll_reserve);
351 7 : GNUNET_assert (poll_reserve->run == &status_run);
352 7 : ss = poll_reserve->cls;
353 7 : if (NULL == ss->rsh)
354 : {
355 7 : TALER_TESTING_interpreter_next (is);
356 7 : return;
357 : }
358 0 : GNUNET_assert (NULL == ss->ps);
359 0 : ss->ps = ps;
360 0 : ps->tt = GNUNET_SCHEDULER_add_delayed (ps->timeout,
361 : &finish_timeout,
362 : ps);
363 : }
364 :
365 :
366 : /**
367 : * Cleanup the state from a "reserve finish" CMD.
368 : *
369 : * @param cls closure.
370 : * @param cmd the command which is being cleaned up.
371 : */
372 : static void
373 7 : finish_cleanup (void *cls,
374 : const struct TALER_TESTING_Command *cmd)
375 : {
376 7 : struct PollState *ps = cls;
377 :
378 7 : if (NULL != ps->tt)
379 : {
380 0 : GNUNET_SCHEDULER_cancel (ps->tt);
381 0 : ps->tt = NULL;
382 : }
383 7 : GNUNET_free (ps);
384 7 : }
385 :
386 :
387 : struct TALER_TESTING_Command
388 7 : TALER_TESTING_cmd_reserve_poll_finish (const char *label,
389 : struct GNUNET_TIME_Relative timeout,
390 : const char *poll_reference)
391 : {
392 : struct PollState *ps;
393 :
394 7 : GNUNET_assert (NULL != poll_reference);
395 7 : ps = GNUNET_new (struct PollState);
396 7 : ps->timeout = timeout;
397 7 : ps->poll_reference = poll_reference;
398 : {
399 7 : struct TALER_TESTING_Command cmd = {
400 : .cls = ps,
401 : .label = label,
402 : .run = &finish_run,
403 : .cleanup = &finish_cleanup
404 : };
405 :
406 7 : return cmd;
407 : }
408 : }
|