diff options
Diffstat (limited to 'src/testing/testing_api_cmd_keyshare_lookup.c')
-rw-r--r-- | src/testing/testing_api_cmd_keyshare_lookup.c | 465 |
1 files changed, 465 insertions, 0 deletions
diff --git a/src/testing/testing_api_cmd_keyshare_lookup.c b/src/testing/testing_api_cmd_keyshare_lookup.c new file mode 100644 index 0000000..895d321 --- /dev/null +++ b/src/testing/testing_api_cmd_keyshare_lookup.c | |||
@@ -0,0 +1,465 @@ | |||
1 | /* | ||
2 | This file is part of Anastasis | ||
3 | Copyright (C) 2020 Taler Systems SA | ||
4 | |||
5 | Anastasis is free software; you can redistribute it and/or modify it under the | ||
6 | terms of the GNU Lesser General Public License as published by the Free Software | ||
7 | Foundation; either version 3, or (at your option) any later version. | ||
8 | |||
9 | Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
11 | A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with | ||
14 | Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> | ||
15 | */ | ||
16 | /** | ||
17 | * @file lib/anastasis_api_keyshare_lookup.c | ||
18 | * @brief Testing of Implementation of the /truth GET | ||
19 | * @author Christian Grothoff | ||
20 | * @author Dennis Neufeld | ||
21 | * @author Dominik Meister | ||
22 | */ | ||
23 | |||
24 | #include "platform.h" | ||
25 | #include "anastasis_testing_lib.h" | ||
26 | #include <taler/taler_util.h> | ||
27 | #include <taler/taler_testing_lib.h> | ||
28 | #include <taler/taler_merchant_service.h> | ||
29 | |||
30 | |||
31 | /** | ||
32 | * State for a "keyshare lookup" CMD. | ||
33 | */ | ||
34 | struct KeyShareLookupState | ||
35 | { | ||
36 | /** | ||
37 | * The interpreter state. | ||
38 | */ | ||
39 | struct TALER_TESTING_Interpreter *is; | ||
40 | |||
41 | /** | ||
42 | * URL of the anastasis backend. | ||
43 | */ | ||
44 | const char *anastasis_url; | ||
45 | |||
46 | /** | ||
47 | * Expected status code. | ||
48 | */ | ||
49 | enum ANASTASIS_KeyShareDownloadStatus expected_ksdd; | ||
50 | |||
51 | /** | ||
52 | * The /truth GET operation handle. | ||
53 | */ | ||
54 | struct ANASTASIS_KeyShareLookupOperation *kslo; | ||
55 | |||
56 | /** | ||
57 | * answer to a challenge | ||
58 | */ | ||
59 | const char *answer; | ||
60 | |||
61 | /** | ||
62 | * Reference to upload command we expect to lookup. | ||
63 | */ | ||
64 | const char *upload_reference; | ||
65 | |||
66 | /** | ||
67 | * Reference to upload command we expect to lookup. | ||
68 | */ | ||
69 | const char *payment_reference; | ||
70 | |||
71 | /** | ||
72 | * Payment secret requested by the service, if any. | ||
73 | */ | ||
74 | struct ANASTASIS_PaymentSecretP payment_secret_response; | ||
75 | |||
76 | /** | ||
77 | * Taler-URI with payment request, if any. | ||
78 | */ | ||
79 | char *pay_uri; | ||
80 | |||
81 | /** | ||
82 | * Order ID for payment request, if any. | ||
83 | */ | ||
84 | char *order_id; | ||
85 | |||
86 | /** | ||
87 | * Redirect-URI for challenge, if any. | ||
88 | */ | ||
89 | char *redirect_uri; | ||
90 | |||
91 | /** | ||
92 | * "code" returned by service, if any. | ||
93 | */ | ||
94 | char *code; | ||
95 | |||
96 | /** | ||
97 | * "instructions" for how to solve the challenge as returned by service, if any. | ||
98 | */ | ||
99 | char *instructions; | ||
100 | |||
101 | /** | ||
102 | * Name of the file where the service will write the challenge, if method is "file". | ||
103 | * Otherwise NULL. | ||
104 | */ | ||
105 | char *filename; | ||
106 | |||
107 | /** | ||
108 | * Mode for the lookup(0 = question, 1 = code based) | ||
109 | */ | ||
110 | int lookup_mode; | ||
111 | |||
112 | }; | ||
113 | |||
114 | |||
115 | static void | ||
116 | keyshare_lookup_cb (void *cls, | ||
117 | const struct ANASTASIS_KeyShareDownloadDetails *dd) | ||
118 | { | ||
119 | struct KeyShareLookupState *ksls = cls; | ||
120 | |||
121 | ksls->kslo = NULL; | ||
122 | if (dd->status != ksls->expected_ksdd) | ||
123 | { | ||
124 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
125 | "Unexpected response code %u to command %s in %s:%u\n", | ||
126 | dd->status, | ||
127 | ksls->is->commands[ksls->is->ip].label, | ||
128 | __FILE__, | ||
129 | __LINE__); | ||
130 | TALER_TESTING_interpreter_fail (ksls->is); | ||
131 | return; | ||
132 | } | ||
133 | switch (dd->status) | ||
134 | { | ||
135 | case ANASTASIS_KSD_SUCCESS: | ||
136 | break; | ||
137 | case ANASTASIS_KSD_PAYMENT_REQUIRED: | ||
138 | ksls->pay_uri = GNUNET_strdup (dd->details.payment_required.taler_pay_uri); | ||
139 | ksls->payment_secret_response = dd->details.payment_required.payment_secret; | ||
140 | { | ||
141 | struct TALER_MERCHANT_PayUriData pd; | ||
142 | |||
143 | if (GNUNET_OK != | ||
144 | TALER_MERCHANT_parse_pay_uri (ksls->pay_uri, | ||
145 | &pd)) | ||
146 | { | ||
147 | GNUNET_break (0); | ||
148 | TALER_TESTING_interpreter_fail (ksls->is); | ||
149 | return; | ||
150 | } | ||
151 | ksls->order_id = GNUNET_strdup (pd.order_id); | ||
152 | TALER_MERCHANT_parse_pay_uri_free (&pd); | ||
153 | } | ||
154 | |||
155 | break; | ||
156 | case ANASTASIS_KSD_INVALID_ANSWER: | ||
157 | if (ksls->filename) | ||
158 | { | ||
159 | FILE *file; | ||
160 | char code[22]; | ||
161 | |||
162 | file = fopen (ksls->filename, | ||
163 | "r"); | ||
164 | if (NULL == file) | ||
165 | { | ||
166 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
167 | "open", | ||
168 | ksls->filename); | ||
169 | TALER_TESTING_interpreter_fail (ksls->is); | ||
170 | return; | ||
171 | } | ||
172 | if (0 == fscanf (file, | ||
173 | "%21s", | ||
174 | code)) | ||
175 | { | ||
176 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
177 | "fscanf", | ||
178 | ksls->filename); | ||
179 | GNUNET_break (0 == fclose (file)); | ||
180 | TALER_TESTING_interpreter_fail (ksls->is); | ||
181 | return; | ||
182 | } | ||
183 | GNUNET_break (0 == fclose (file)); | ||
184 | ksls->code = GNUNET_strdup (code); | ||
185 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
186 | "Read code `%s'\n", | ||
187 | code); | ||
188 | } | ||
189 | else | ||
190 | { | ||
191 | ksls->instructions = GNUNET_strndup ( | ||
192 | dd->details.open_challenge.body, | ||
193 | dd->details.open_challenge.body_size); | ||
194 | } | ||
195 | break; | ||
196 | case ANASTASIS_KSD_REDIRECT_FOR_AUTHENTICATION: | ||
197 | ksls->redirect_uri = GNUNET_strdup (dd->details.redirect_url); | ||
198 | break; | ||
199 | case ANASTASIS_KSD_SERVER_ERROR: | ||
200 | break; | ||
201 | case ANASTASIS_KSD_CLIENT_FAILURE: | ||
202 | break; | ||
203 | case ANASTASIS_KSD_TRUTH_UNKNOWN: | ||
204 | break; | ||
205 | case ANASTASIS_KSD_RATE_LIMIT_EXCEEDED: | ||
206 | break; | ||
207 | } | ||
208 | TALER_TESTING_interpreter_next (ksls->is); | ||
209 | } | ||
210 | |||
211 | |||
212 | static void | ||
213 | keyshare_lookup_run (void *cls, | ||
214 | const struct TALER_TESTING_Command *cmd, | ||
215 | struct TALER_TESTING_Interpreter *is) | ||
216 | { | ||
217 | struct KeyShareLookupState *ksls = cls; | ||
218 | const struct ANASTASIS_CRYPTO_TruthKeyP *truth_key; | ||
219 | const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid; | ||
220 | const struct ANASTASIS_PaymentSecretP *payment_secret; | ||
221 | const char *answer; | ||
222 | |||
223 | ksls->is = is; | ||
224 | if (NULL == ksls->upload_reference) | ||
225 | { | ||
226 | GNUNET_break (0); | ||
227 | TALER_TESTING_interpreter_fail (ksls->is); | ||
228 | return; | ||
229 | } | ||
230 | { | ||
231 | const struct TALER_TESTING_Command *upload_cmd; | ||
232 | |||
233 | upload_cmd = TALER_TESTING_interpreter_lookup_command ( | ||
234 | is, | ||
235 | ksls->upload_reference); | ||
236 | if (NULL == upload_cmd) | ||
237 | { | ||
238 | GNUNET_break (0); | ||
239 | TALER_TESTING_interpreter_fail (ksls->is); | ||
240 | return; | ||
241 | } | ||
242 | { | ||
243 | const char *fn; | ||
244 | |||
245 | if (GNUNET_OK != | ||
246 | TALER_TESTING_get_trait_string (upload_cmd, | ||
247 | 0, | ||
248 | &fn)) | ||
249 | { | ||
250 | GNUNET_break (0); | ||
251 | TALER_TESTING_interpreter_fail (ksls->is); | ||
252 | return; | ||
253 | } | ||
254 | if (NULL != fn) | ||
255 | ksls->filename = GNUNET_strdup (fn); | ||
256 | } | ||
257 | if (GNUNET_OK != | ||
258 | ANASTASIS_TESTING_get_trait_truth_uuid (upload_cmd, | ||
259 | 0, | ||
260 | &truth_uuid)) | ||
261 | { | ||
262 | GNUNET_break (0); | ||
263 | TALER_TESTING_interpreter_fail (ksls->is); | ||
264 | return; | ||
265 | } | ||
266 | if (NULL == truth_uuid) | ||
267 | { | ||
268 | GNUNET_break (0); | ||
269 | TALER_TESTING_interpreter_fail (ksls->is); | ||
270 | return; | ||
271 | } | ||
272 | if (GNUNET_OK != | ||
273 | ANASTASIS_TESTING_get_trait_truth_key (upload_cmd, | ||
274 | 0, | ||
275 | &truth_key)) | ||
276 | { | ||
277 | GNUNET_break (0); | ||
278 | TALER_TESTING_interpreter_fail (ksls->is); | ||
279 | return; | ||
280 | } | ||
281 | if (NULL == truth_key) | ||
282 | { | ||
283 | GNUNET_break (0); | ||
284 | TALER_TESTING_interpreter_fail (ksls->is); | ||
285 | return; | ||
286 | } | ||
287 | } | ||
288 | |||
289 | if (ksls->lookup_mode == 1) | ||
290 | { | ||
291 | const struct TALER_TESTING_Command *download_cmd; | ||
292 | |||
293 | download_cmd = TALER_TESTING_interpreter_lookup_command (is, | ||
294 | ksls->answer); | ||
295 | if (NULL == download_cmd) | ||
296 | { | ||
297 | GNUNET_break (0); | ||
298 | TALER_TESTING_interpreter_fail (ksls->is); | ||
299 | return; | ||
300 | } | ||
301 | if (GNUNET_OK != | ||
302 | ANASTASIS_TESTING_get_trait_code (download_cmd, | ||
303 | 0, | ||
304 | &answer)) | ||
305 | { | ||
306 | GNUNET_break (0); | ||
307 | TALER_TESTING_interpreter_fail (ksls->is); | ||
308 | return; | ||
309 | } | ||
310 | if (NULL == answer) | ||
311 | { | ||
312 | GNUNET_break (0); | ||
313 | TALER_TESTING_interpreter_fail (ksls->is); | ||
314 | return; | ||
315 | } | ||
316 | } | ||
317 | else | ||
318 | { | ||
319 | /* answer is the answer */ | ||
320 | answer = ksls->answer; | ||
321 | } | ||
322 | |||
323 | if (NULL != ksls->payment_reference) | ||
324 | { | ||
325 | const struct TALER_TESTING_Command *payment_cmd; | ||
326 | |||
327 | payment_cmd = TALER_TESTING_interpreter_lookup_command | ||
328 | (is, | ||
329 | ksls->payment_reference); | ||
330 | if (GNUNET_OK != | ||
331 | ANASTASIS_TESTING_get_trait_payment_secret (payment_cmd, | ||
332 | 0, | ||
333 | &payment_secret)) | ||
334 | { | ||
335 | GNUNET_break (0); | ||
336 | TALER_TESTING_interpreter_fail (ksls->is); | ||
337 | return; | ||
338 | } | ||
339 | } | ||
340 | else | ||
341 | { | ||
342 | payment_secret = NULL; | ||
343 | } | ||
344 | |||
345 | { | ||
346 | struct GNUNET_HashCode h_answer; | ||
347 | |||
348 | if (NULL != answer) | ||
349 | GNUNET_CRYPTO_hash (answer, | ||
350 | strlen (answer), | ||
351 | &h_answer); | ||
352 | ksls->kslo = ANASTASIS_keyshare_lookup (is->ctx, | ||
353 | ksls->anastasis_url, | ||
354 | truth_uuid, | ||
355 | truth_key, | ||
356 | payment_secret, | ||
357 | GNUNET_TIME_UNIT_ZERO, | ||
358 | (NULL != answer) | ||
359 | ? &h_answer | ||
360 | : NULL, | ||
361 | &keyshare_lookup_cb, | ||
362 | ksls); | ||
363 | } | ||
364 | if (NULL == ksls->kslo) | ||
365 | { | ||
366 | GNUNET_break (0); | ||
367 | TALER_TESTING_interpreter_fail (ksls->is); | ||
368 | return; | ||
369 | } | ||
370 | } | ||
371 | |||
372 | |||
373 | static void | ||
374 | keyshare_lookup_cleanup (void *cls, | ||
375 | const struct TALER_TESTING_Command *cmd) | ||
376 | { | ||
377 | struct KeyShareLookupState *ksls = cls; | ||
378 | |||
379 | if (NULL != ksls->kslo) | ||
380 | { | ||
381 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
382 | "Command '%s' did not complete (keyshare lookup)\n", | ||
383 | cmd->label); | ||
384 | ANASTASIS_keyshare_lookup_cancel (ksls->kslo); | ||
385 | ksls->kslo = NULL; | ||
386 | } | ||
387 | GNUNET_free (ksls->pay_uri); | ||
388 | GNUNET_free (ksls->order_id); | ||
389 | GNUNET_free (ksls->code); | ||
390 | GNUNET_free (ksls->instructions); | ||
391 | GNUNET_free (ksls->redirect_uri); | ||
392 | GNUNET_free (ksls); | ||
393 | } | ||
394 | |||
395 | |||
396 | /** | ||
397 | * Offer internal data to other commands. | ||
398 | * | ||
399 | * @param cls closure | ||
400 | * @param[out] ret result (could be anything) | ||
401 | * @param[out] trait name of the trait | ||
402 | * @param index index number of the object to extract. | ||
403 | * @return #GNUNET_OK on success | ||
404 | */ | ||
405 | static int | ||
406 | keyshare_lookup_traits (void *cls, | ||
407 | const void **ret, | ||
408 | const char *trait, | ||
409 | unsigned int index) | ||
410 | { | ||
411 | struct KeyShareLookupState *ksls = cls; | ||
412 | struct TALER_TESTING_Trait traits[] = { | ||
413 | ANASTASIS_TESTING_make_trait_payment_secret (0, | ||
414 | &ksls->payment_secret_response), | ||
415 | TALER_TESTING_make_trait_url (TALER_TESTING_UT_TALER_URL, | ||
416 | ksls->pay_uri), | ||
417 | TALER_TESTING_make_trait_order_id (0, | ||
418 | ksls->order_id), | ||
419 | ANASTASIS_TESTING_make_trait_code (0, | ||
420 | ksls->code), | ||
421 | TALER_TESTING_trait_end () | ||
422 | }; | ||
423 | |||
424 | return TALER_TESTING_get_trait (traits, | ||
425 | ret, | ||
426 | trait, | ||
427 | index); | ||
428 | } | ||
429 | |||
430 | |||
431 | struct TALER_TESTING_Command | ||
432 | ANASTASIS_TESTING_cmd_keyshare_lookup ( | ||
433 | const char *label, | ||
434 | const char *anastasis_url, | ||
435 | const char *answer, | ||
436 | const char *payment_ref, | ||
437 | const char *upload_ref, | ||
438 | int lookup_mode, | ||
439 | enum ANASTASIS_KeyShareDownloadStatus ksdd) | ||
440 | { | ||
441 | struct KeyShareLookupState *ksls; | ||
442 | |||
443 | GNUNET_assert (NULL != upload_ref); | ||
444 | ksls = GNUNET_new (struct KeyShareLookupState); | ||
445 | ksls->expected_ksdd = ksdd; | ||
446 | ksls->anastasis_url = anastasis_url; | ||
447 | ksls->upload_reference = upload_ref; | ||
448 | ksls->payment_reference = payment_ref; | ||
449 | ksls->answer = answer; | ||
450 | ksls->lookup_mode = lookup_mode; | ||
451 | { | ||
452 | struct TALER_TESTING_Command cmd = { | ||
453 | .cls = ksls, | ||
454 | .label = label, | ||
455 | .run = &keyshare_lookup_run, | ||
456 | .cleanup = &keyshare_lookup_cleanup, | ||
457 | .traits = &keyshare_lookup_traits | ||
458 | }; | ||
459 | |||
460 | return cmd; | ||
461 | } | ||
462 | } | ||
463 | |||
464 | |||
465 | /* end of testing_api_cmd_keyshare_lookup.c */ | ||