Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2023 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 : struct TALER_FullPayto 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 gtr response details
87 : */
88 : static void
89 4 : get_transfers_cb (
90 : void *cls,
91 : const struct TALER_MERCHANT_GetTransfersResponse *gtr)
92 : {
93 4 : struct GetTransfersState *gts = cls;
94 :
95 4 : gts->gth = NULL;
96 4 : if (gts->http_status != gtr->hr.http_status)
97 : {
98 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
99 : "Unexpected response code %u (%d) to command %s\n",
100 : gtr->hr.http_status,
101 : (int) gtr->hr.ec,
102 : TALER_TESTING_interpreter_get_current_label (gts->is));
103 0 : TALER_TESTING_interpreter_fail (gts->is);
104 0 : return;
105 : }
106 4 : switch (gtr->hr.http_status)
107 : {
108 4 : case MHD_HTTP_OK:
109 4 : if (gtr->details.ok.transfers_length != gts->transfers_length)
110 : {
111 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
112 : "Transfers length does not match (got %u/want %u)\n",
113 : gtr->details.ok.transfers_length,
114 : gts->transfers_length);
115 0 : TALER_TESTING_interpreter_fail (gts->is);
116 0 : return;
117 : }
118 11 : for (unsigned int i = 0; i < gtr->details.ok.transfers_length; ++i)
119 : {
120 7 : const struct TALER_MERCHANT_TransferData *transfer
121 7 : = >r->details.ok.transfers[i];
122 : const struct TALER_TESTING_Command *transfer_cmd;
123 :
124 7 : transfer_cmd = TALER_TESTING_interpreter_lookup_command (
125 : gts->is,
126 7 : gts->transfers[i]);
127 7 : 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 7 : if (GNUNET_OK !=
139 7 : 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 7 : if (0 != GNUNET_memcmp (wtid,
148 : &transfer->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 7 : TALER_TESTING_cmd_merchant_post_transfer_set_serial (
156 : (struct TALER_TESTING_Command *) transfer_cmd,
157 7 : transfer->credit_serial);
158 : }
159 : {
160 : const struct TALER_FullPayto *payto_uri;
161 :
162 7 : if (GNUNET_OK !=
163 7 : 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 7 : if (0 !=
172 7 : TALER_full_payto_cmp (*payto_uri,
173 : transfer->payto_uri))
174 : {
175 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176 : "Wire transfer payto uri does not match: %s != %s\n",
177 : payto_uri->full_payto,
178 : transfer->payto_uri.full_payto);
179 0 : TALER_TESTING_interpreter_fail (gts->is);
180 0 : return;
181 : }
182 : }
183 : {
184 : const struct TALER_Amount *credit_amount;
185 :
186 7 : if (GNUNET_OK !=
187 7 : TALER_TESTING_get_trait_amount (transfer_cmd,
188 : &credit_amount))
189 : {
190 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
191 : "Could not fetch wire transfer credit amount\n");
192 0 : TALER_TESTING_interpreter_fail (gts->is);
193 0 : return;
194 : }
195 7 : if ( (GNUNET_OK !=
196 7 : TALER_amount_cmp_currency (credit_amount,
197 7 : &transfer->credit_amount)) ||
198 7 : (0 != TALER_amount_cmp (credit_amount,
199 : &transfer->credit_amount)))
200 : {
201 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 : "Wire transfer credit amount does not match\n");
203 0 : TALER_TESTING_interpreter_fail (gts->is);
204 0 : return;
205 : }
206 : }
207 : {
208 : const char *exchange_url;
209 :
210 7 : if (GNUNET_OK !=
211 7 : TALER_TESTING_get_trait_exchange_url (transfer_cmd,
212 : &exchange_url))
213 : {
214 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
215 : "Could not fetch wire transfer exchange url\n");
216 0 : TALER_TESTING_interpreter_fail (gts->is);
217 0 : return;
218 : }
219 7 : if (0 != strcmp (exchange_url,
220 7 : transfer->exchange_url))
221 : {
222 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
223 : "Wire transfer exchange url does not match\n");
224 0 : TALER_TESTING_interpreter_fail (gts->is);
225 0 : return;
226 : }
227 : }
228 : }
229 4 : break;
230 0 : default:
231 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
232 : "Unhandled HTTP status %u.\n",
233 : gtr->hr.http_status);
234 0 : break;
235 : }
236 4 : TALER_TESTING_interpreter_next (gts->is);
237 : }
238 :
239 :
240 : /**
241 : * Run the "get transfer" CMD.
242 : *
243 : * @param cls closure.
244 : * @param cmd command being run now.
245 : * @param is interpreter state.
246 : */
247 : static void
248 4 : get_transfers_run (void *cls,
249 : const struct TALER_TESTING_Command *cmd,
250 : struct TALER_TESTING_Interpreter *is)
251 : {
252 4 : struct GetTransfersState *gts = cls;
253 :
254 4 : gts->is = is;
255 4 : gts->gth = TALER_MERCHANT_transfers_get (
256 : TALER_TESTING_interpreter_get_context (is),
257 : gts->merchant_url,
258 : gts->payto_uri,
259 4 : GNUNET_TIME_UNIT_FOREVER_TS,
260 4 : GNUNET_TIME_UNIT_ZERO_TS,
261 : INT64_MAX,
262 : 0,
263 : TALER_EXCHANGE_YNA_ALL,
264 : &get_transfers_cb,
265 : gts);
266 4 : GNUNET_assert (NULL != gts->gth);
267 4 : }
268 :
269 :
270 : /**
271 : * Free the state of a "get transfer" CMD, and possibly
272 : * cancel a pending operation thereof.
273 : *
274 : * @param cls closure.
275 : * @param cmd command being run.
276 : */
277 : static void
278 4 : get_transfers_cleanup (void *cls,
279 : const struct TALER_TESTING_Command *cmd)
280 : {
281 4 : struct GetTransfersState *gts = cls;
282 :
283 4 : if (NULL != gts->gth)
284 : {
285 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
286 : "GET /transfer operation did not complete\n");
287 0 : TALER_MERCHANT_transfers_get_cancel (gts->gth);
288 : }
289 4 : GNUNET_array_grow (gts->transfers,
290 : gts->transfers_length,
291 : 0);
292 4 : GNUNET_free (gts);
293 4 : }
294 :
295 :
296 : struct TALER_TESTING_Command
297 4 : TALER_TESTING_cmd_merchant_get_transfers (
298 : const char *label,
299 : const char *merchant_url,
300 : struct TALER_FullPayto payto_uri,
301 : unsigned int http_code,
302 : ...)
303 : {
304 : struct GetTransfersState *gts;
305 :
306 4 : gts = GNUNET_new (struct GetTransfersState);
307 4 : gts->merchant_url = merchant_url;
308 4 : gts->payto_uri = payto_uri;
309 4 : gts->http_status = http_code;
310 : {
311 : const char *clabel;
312 : va_list ap;
313 :
314 4 : va_start (ap, http_code);
315 11 : while (NULL != (clabel = va_arg (ap, const char *)))
316 : {
317 7 : GNUNET_array_append (gts->transfers,
318 : gts->transfers_length,
319 : clabel);
320 : }
321 4 : va_end (ap);
322 : }
323 : {
324 4 : struct TALER_TESTING_Command cmd = {
325 : .cls = gts,
326 : .label = label,
327 : .run = &get_transfers_run,
328 : .cleanup = &get_transfers_cleanup
329 : };
330 :
331 4 : return cmd;
332 : }
333 : }
334 :
335 :
336 : /* end of testing_api_cmd_get_transfers.c */
|