Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2018, 2020 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_api_cmd_get_transfers.c
21 : * @brief command to test GET /transfers.
22 : * @author Marcello Stanisci
23 : * @author Christian Grothoff
24 : */
25 : #include "platform.h"
26 : #include <taler/taler_exchange_service.h>
27 : #include <taler/taler_testing_lib.h>
28 : #include "taler_merchant_service.h"
29 : #include "taler_merchant_testing_lib.h"
30 :
31 :
32 : /**
33 : * State of a GET transfers CMD.
34 : */
35 : struct GetTransfersState
36 : {
37 :
38 : /**
39 : * Handle for a "get transfer" request.
40 : */
41 : struct TALER_MERCHANT_GetTransfersHandle *gth;
42 :
43 : /**
44 : * The interpreter state.
45 : */
46 : struct TALER_TESTING_Interpreter *is;
47 :
48 : /**
49 : * Base URL of the merchant serving the request.
50 : */
51 : const char *merchant_url;
52 :
53 : /**
54 : * payto URI of the merchant to filter by.
55 : */
56 : const char *payto_uri;
57 :
58 : /**
59 : * Expected HTTP response code.
60 : */
61 : unsigned int http_status;
62 :
63 : /**
64 : * Reference for a "check bank" CMD. It offers the
65 : * WTID to get.
66 : */
67 : const char *check_bank_reference;
68 :
69 : /**
70 : * Array of POST /transfer command labels we expect to see listed.
71 : */
72 : const char **transfers;
73 :
74 : /**
75 : * Length of @e transfers.
76 : */
77 : unsigned int transfers_length;
78 :
79 : };
80 :
81 :
82 : /**
83 : * Check the result of our GET /transfers request to a merchant
84 : *
85 : * @param cls closure
86 : * @param hr HTTP response details
87 : * @param transfers_length length of the @a transfers array
88 : * @param transfers array with details about the transfers we received
89 : */
90 : static void
91 0 : get_transfers_cb (
92 : void *cls,
93 : const struct TALER_MERCHANT_HttpResponse *hr,
94 : unsigned int transfers_length,
95 : const struct TALER_MERCHANT_TransferData transfers[])
96 : {
97 0 : struct GetTransfersState *gts = cls;
98 :
99 0 : gts->gth = NULL;
100 0 : if (gts->http_status != hr->http_status)
101 : {
102 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
103 : "Unexpected response code %u (%d) to command %s\n",
104 : hr->http_status,
105 : (int) hr->ec,
106 : TALER_TESTING_interpreter_get_current_label (gts->is));
107 0 : TALER_TESTING_interpreter_fail (gts->is);
108 0 : return;
109 : }
110 0 : switch (hr->http_status)
111 : {
112 0 : case MHD_HTTP_OK:
113 0 : if (transfers_length != gts->transfers_length)
114 : {
115 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 : "Transfers length does not match\n");
117 0 : TALER_TESTING_interpreter_fail (gts->is);
118 0 : return;
119 : }
120 0 : for (unsigned int i = 0; i < transfers_length; ++i)
121 : {
122 : const struct TALER_TESTING_Command *transfer_cmd;
123 :
124 0 : transfer_cmd = TALER_TESTING_interpreter_lookup_command (
125 : gts->is,
126 0 : gts->transfers[i]);
127 0 : if (NULL == transfer_cmd)
128 : {
129 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
130 : "Command `%s' not found!\n",
131 : gts->transfers[i]);
132 0 : TALER_TESTING_interpreter_fail (gts->is);
133 0 : return;
134 : }
135 : {
136 : const struct TALER_WireTransferIdentifierRawP *wtid;
137 :
138 0 : if (GNUNET_OK !=
139 0 : TALER_TESTING_get_trait_wtid (transfer_cmd,
140 : &wtid))
141 : {
142 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
143 : "Could not fetch wire transfer id\n");
144 0 : TALER_TESTING_interpreter_fail (gts->is);
145 0 : return;
146 : }
147 0 : if (0 != GNUNET_memcmp (wtid,
148 : &transfers[i].wtid))
149 : {
150 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151 : "Wire transfer id does not match\n");
152 0 : TALER_TESTING_interpreter_fail (gts->is);
153 0 : return;
154 : }
155 0 : TALER_TESTING_cmd_merchant_post_transfer_set_serial (
156 : (struct TALER_TESTING_Command *) transfer_cmd,
157 0 : transfers[i].credit_serial);
158 : }
159 : {
160 : const char **payto_uri;
161 :
162 0 : if (GNUNET_OK !=
163 0 : TALER_TESTING_get_trait_credit_payto_uri (transfer_cmd,
164 : &payto_uri))
165 : {
166 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
167 : "Could not fetch wire transfer payto uri\n");
168 0 : TALER_TESTING_interpreter_fail (gts->is);
169 0 : return;
170 : }
171 0 : if (0 != strcmp (*payto_uri,
172 0 : transfers[i].payto_uri))
173 : {
174 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
175 : "Wire transfer payto uri does not match: %s != %s\n",
176 : *payto_uri,
177 : transfers[i].payto_uri);
178 0 : TALER_TESTING_interpreter_fail (gts->is);
179 0 : return;
180 : }
181 : }
182 : {
183 : const struct TALER_Amount *credit_amount;
184 :
185 0 : if (GNUNET_OK !=
186 0 : TALER_TESTING_get_trait_amount (transfer_cmd,
187 : &credit_amount))
188 : {
189 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 : "Could not fetch wire transfer credit amount\n");
191 0 : TALER_TESTING_interpreter_fail (gts->is);
192 0 : return;
193 : }
194 0 : if ( (GNUNET_OK !=
195 0 : TALER_amount_cmp_currency (credit_amount,
196 0 : &transfers[i].credit_amount)) ||
197 0 : (0 != TALER_amount_cmp (credit_amount,
198 0 : &transfers[i].credit_amount)))
199 : {
200 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
201 : "Wire transfer credit amount does not match\n");
202 0 : TALER_TESTING_interpreter_fail (gts->is);
203 0 : return;
204 : }
205 : }
206 : {
207 : const char **exchange_url;
208 :
209 0 : if (GNUNET_OK !=
210 0 : TALER_TESTING_get_trait_exchange_url (transfer_cmd,
211 : &exchange_url))
212 : {
213 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
214 : "Could not fetch wire transfer exchange url\n");
215 0 : TALER_TESTING_interpreter_fail (gts->is);
216 0 : return;
217 : }
218 0 : if (0 != strcmp (*exchange_url,
219 0 : transfers[i].exchange_url))
220 : {
221 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
222 : "Wire transfer exchange url does not match\n");
223 0 : TALER_TESTING_interpreter_fail (gts->is);
224 0 : return;
225 : }
226 : }
227 : {
228 : const struct GNUNET_TIME_Timestamp *execution_time;
229 :
230 0 : if (GNUNET_OK !=
231 0 : TALER_TESTING_get_trait_timestamp (transfer_cmd,
232 : 0,
233 : &execution_time))
234 : {
235 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236 : "Could not fetch wire transfer execution time\n");
237 0 : TALER_TESTING_interpreter_fail (gts->is);
238 0 : return;
239 : }
240 0 : if (GNUNET_TIME_timestamp_cmp (*execution_time,
241 : !=,
242 : transfers[i].execution_time))
243 : {
244 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
245 : "Wire transfer execution time does not match\n");
246 0 : TALER_TESTING_interpreter_fail (gts->is);
247 0 : return;
248 : }
249 : }
250 : }
251 0 : break;
252 0 : default:
253 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
254 : "Unhandled HTTP status.\n");
255 : }
256 0 : TALER_TESTING_interpreter_next (gts->is);
257 : }
258 :
259 :
260 : /**
261 : * Run the "get transfer" CMD.
262 : *
263 : * @param cls closure.
264 : * @param cmd command being run now.
265 : * @param is interpreter state.
266 : */
267 : static void
268 0 : get_transfers_run (void *cls,
269 : const struct TALER_TESTING_Command *cmd,
270 : struct TALER_TESTING_Interpreter *is)
271 : {
272 0 : struct GetTransfersState *gts = cls;
273 :
274 0 : gts->is = is;
275 0 : gts->gth = TALER_MERCHANT_transfers_get (is->ctx,
276 : gts->merchant_url,
277 : gts->payto_uri,
278 0 : GNUNET_TIME_UNIT_FOREVER_TS,
279 0 : GNUNET_TIME_UNIT_ZERO_TS,
280 : INT64_MAX,
281 : 0,
282 : TALER_EXCHANGE_YNA_ALL,
283 : &get_transfers_cb,
284 : gts);
285 0 : GNUNET_assert (NULL != gts->gth);
286 0 : }
287 :
288 :
289 : /**
290 : * Free the state of a "get transfer" CMD, and possibly
291 : * cancel a pending operation thereof.
292 : *
293 : * @param cls closure.
294 : * @param cmd command being run.
295 : */
296 : static void
297 0 : get_transfers_cleanup (void *cls,
298 : const struct TALER_TESTING_Command *cmd)
299 : {
300 0 : struct GetTransfersState *gts = cls;
301 :
302 0 : if (NULL != gts->gth)
303 : {
304 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
305 : "GET /transfer operation did not complete\n");
306 0 : TALER_MERCHANT_transfers_get_cancel (gts->gth);
307 : }
308 0 : GNUNET_array_grow (gts->transfers,
309 : gts->transfers_length,
310 : 0);
311 0 : GNUNET_free (gts);
312 0 : }
313 :
314 :
315 : struct TALER_TESTING_Command
316 0 : TALER_TESTING_cmd_merchant_get_transfers (const char *label,
317 : const char *merchant_url,
318 : const char *payto_uri,
319 : unsigned int http_code,
320 : ...)
321 : {
322 : struct GetTransfersState *gts;
323 :
324 0 : gts = GNUNET_new (struct GetTransfersState);
325 0 : gts->merchant_url = merchant_url;
326 0 : gts->payto_uri = payto_uri;
327 0 : gts->http_status = http_code;
328 : {
329 : const char *clabel;
330 : va_list ap;
331 :
332 0 : va_start (ap, http_code);
333 0 : while (NULL != (clabel = va_arg (ap, const char *)))
334 : {
335 0 : GNUNET_array_append (gts->transfers,
336 : gts->transfers_length,
337 : clabel);
338 : }
339 0 : va_end (ap);
340 : }
341 : {
342 0 : struct TALER_TESTING_Command cmd = {
343 : .cls = gts,
344 : .label = label,
345 : .run = &get_transfers_run,
346 : .cleanup = &get_transfers_cleanup
347 : };
348 :
349 0 : return cmd;
350 : }
351 : }
352 :
353 :
354 : /* end of testing_api_cmd_get_transfers.c */
|