diff options
Diffstat (limited to 'src/testing/testing_cmd_truth_upload.c')
-rw-r--r-- | src/testing/testing_cmd_truth_upload.c | 383 |
1 files changed, 383 insertions, 0 deletions
diff --git a/src/testing/testing_cmd_truth_upload.c b/src/testing/testing_cmd_truth_upload.c new file mode 100644 index 0000000..f9149d5 --- /dev/null +++ b/src/testing/testing_cmd_truth_upload.c | |||
@@ -0,0 +1,383 @@ | |||
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/testing_cmd_truth_upload.c | ||
18 | * @brief command to execute the anastasis secret share service | ||
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 | |||
29 | |||
30 | /** | ||
31 | * State for a "truth upload" CMD. | ||
32 | */ | ||
33 | struct TruthUploadState | ||
34 | { | ||
35 | /** | ||
36 | * The interpreter state. | ||
37 | */ | ||
38 | struct TALER_TESTING_Interpreter *is; | ||
39 | |||
40 | /** | ||
41 | * URL of the anastasis backend. | ||
42 | */ | ||
43 | const char *anastasis_url; | ||
44 | |||
45 | /** | ||
46 | * Label of this command. | ||
47 | */ | ||
48 | const char *label; | ||
49 | |||
50 | /** | ||
51 | * The ID data to generate user identifier | ||
52 | */ | ||
53 | json_t *id_data; | ||
54 | |||
55 | /** | ||
56 | * The escrow method | ||
57 | */ | ||
58 | const char *method; | ||
59 | |||
60 | /** | ||
61 | * Instructions to be returned to client/user | ||
62 | * (e.g. "Look at your smartphone. SMS was sent to you") | ||
63 | */ | ||
64 | const char *instructions; | ||
65 | |||
66 | /** | ||
67 | * Mime type of truth_data (eg. jpeg, string etc.) | ||
68 | */ | ||
69 | const char *mime_type; | ||
70 | |||
71 | /** | ||
72 | * The truth_data (e.g. hash of answer to a secure question) | ||
73 | */ | ||
74 | void *truth_data; | ||
75 | |||
76 | /** | ||
77 | * Requested order ID for this upload (if unpaid). | ||
78 | */ | ||
79 | struct ANASTASIS_PaymentSecretP payment_secret_response; | ||
80 | |||
81 | /** | ||
82 | * Size of truth_data | ||
83 | */ | ||
84 | size_t truth_data_size; | ||
85 | |||
86 | /** | ||
87 | * Expected status code. | ||
88 | */ | ||
89 | unsigned int http_status; | ||
90 | |||
91 | /** | ||
92 | * The /truth POST operation handle. | ||
93 | */ | ||
94 | struct ANASTASIS_TruthUpload *tuo; | ||
95 | |||
96 | /** | ||
97 | * closure for the payment callback | ||
98 | */ | ||
99 | void *tpc_cls; | ||
100 | |||
101 | /** | ||
102 | * Reference to salt download. | ||
103 | */ | ||
104 | const char *salt_reference; | ||
105 | |||
106 | /** | ||
107 | * Options for how we are supposed to do the upload. | ||
108 | */ | ||
109 | enum ANASTASIS_TESTING_TruthStoreOption tsopt; | ||
110 | |||
111 | /** | ||
112 | * Truth object | ||
113 | */ | ||
114 | struct ANASTASIS_Truth *truth; | ||
115 | }; | ||
116 | |||
117 | |||
118 | /** | ||
119 | * Upload information | ||
120 | * caller MUST free 't' using ANASTASIS_truth_free() | ||
121 | * | ||
122 | * @param cls closure for callback | ||
123 | * @param t Truth object (contains provider url and truth public key) | ||
124 | * @param ud upload details, useful to continue in case of errors, NULL on success | ||
125 | */ | ||
126 | static void | ||
127 | truth_upload_cb (void *cls, | ||
128 | struct ANASTASIS_Truth *t, | ||
129 | const struct ANASTASIS_UploadDetails *ud) | ||
130 | { | ||
131 | struct TruthUploadState *tus = cls; | ||
132 | |||
133 | tus->tuo = NULL; | ||
134 | if (NULL == ud) | ||
135 | { | ||
136 | GNUNET_break (0); | ||
137 | TALER_TESTING_interpreter_fail (tus->is); | ||
138 | return; | ||
139 | } | ||
140 | if (ud->http_status != tus->http_status) | ||
141 | { | ||
142 | GNUNET_break (0); | ||
143 | TALER_TESTING_interpreter_fail (tus->is); | ||
144 | return; | ||
145 | } | ||
146 | if (MHD_HTTP_PAYMENT_REQUIRED == ud->http_status) | ||
147 | { | ||
148 | if (ANASTASIS_US_PAYMENT_REQUIRED != ud->us) | ||
149 | { | ||
150 | GNUNET_break (0); | ||
151 | TALER_TESTING_interpreter_fail (tus->is); | ||
152 | return; | ||
153 | } | ||
154 | tus->payment_secret_response = ud->details.payment.ps; | ||
155 | TALER_TESTING_interpreter_next (tus->is); | ||
156 | return; | ||
157 | } | ||
158 | if ( (ANASTASIS_US_SUCCESS == ud->us) && | ||
159 | (NULL == t) ) | ||
160 | { | ||
161 | GNUNET_break (0); | ||
162 | TALER_TESTING_interpreter_next (tus->is); | ||
163 | return; | ||
164 | } | ||
165 | tus->truth = t; | ||
166 | TALER_TESTING_interpreter_next (tus->is); | ||
167 | } | ||
168 | |||
169 | |||
170 | /** | ||
171 | * Run a "truth upload" CMD. | ||
172 | * | ||
173 | * @param cls closure. | ||
174 | * @param cmd command currently being run. | ||
175 | * @param is interpreter state. | ||
176 | */ | ||
177 | static void | ||
178 | truth_upload_run (void *cls, | ||
179 | const struct TALER_TESTING_Command *cmd, | ||
180 | struct TALER_TESTING_Interpreter *is) | ||
181 | { | ||
182 | struct TruthUploadState *tus = cls; | ||
183 | const struct TALER_TESTING_Command *ref; | ||
184 | const struct ANASTASIS_CRYPTO_ProviderSaltP *salt; | ||
185 | struct ANASTASIS_CRYPTO_UserIdentifierP user_id; | ||
186 | |||
187 | tus->is = is; | ||
188 | if (NULL != tus->salt_reference) | ||
189 | { | ||
190 | ref = TALER_TESTING_interpreter_lookup_command | ||
191 | (is, | ||
192 | tus->salt_reference); | ||
193 | if (NULL == ref) | ||
194 | { | ||
195 | GNUNET_break (0); | ||
196 | TALER_TESTING_interpreter_fail (tus->is); | ||
197 | return; | ||
198 | } | ||
199 | if (GNUNET_OK != | ||
200 | ANASTASIS_TESTING_get_trait_salt (ref, | ||
201 | 0, | ||
202 | &salt)) | ||
203 | { | ||
204 | GNUNET_break (0); | ||
205 | TALER_TESTING_interpreter_fail (tus->is); | ||
206 | return; | ||
207 | } | ||
208 | } | ||
209 | |||
210 | ANASTASIS_CRYPTO_user_identifier_derive (tus->id_data, | ||
211 | salt, | ||
212 | &user_id); | ||
213 | |||
214 | tus->tuo = ANASTASIS_truth_upload (is->ctx, | ||
215 | &user_id, | ||
216 | tus->anastasis_url, | ||
217 | tus->method, | ||
218 | tus->instructions, | ||
219 | tus->mime_type, | ||
220 | salt, | ||
221 | tus->truth_data, | ||
222 | tus->truth_data_size, | ||
223 | false, /* force payment */ | ||
224 | GNUNET_TIME_UNIT_ZERO, | ||
225 | &truth_upload_cb, | ||
226 | tus); | ||
227 | if (NULL == tus->tuo) | ||
228 | { | ||
229 | GNUNET_break (0); | ||
230 | TALER_TESTING_interpreter_fail (tus->is); | ||
231 | } | ||
232 | } | ||
233 | |||
234 | |||
235 | /** | ||
236 | * Free the state of a "truth upload" CMD, and possibly | ||
237 | * cancel it if it did not complete. | ||
238 | * | ||
239 | * @param cls closure. | ||
240 | * @param cmd command being freed. | ||
241 | */ | ||
242 | static void | ||
243 | truth_upload_cleanup (void *cls, | ||
244 | const struct TALER_TESTING_Command *cmd) | ||
245 | { | ||
246 | struct TruthUploadState *tus = cls; | ||
247 | |||
248 | if (NULL != tus->tuo) | ||
249 | { | ||
250 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
251 | "Command '%s' did not complete\n", | ||
252 | cmd->label); | ||
253 | ANASTASIS_truth_upload_cancel (tus->tuo); | ||
254 | tus->tuo = NULL; | ||
255 | } | ||
256 | if (NULL != tus->id_data) | ||
257 | { | ||
258 | json_decref (tus->id_data); | ||
259 | tus->id_data = NULL; | ||
260 | } | ||
261 | if (NULL != tus->truth) | ||
262 | { | ||
263 | ANASTASIS_truth_free (tus->truth); | ||
264 | tus->truth = NULL; | ||
265 | } | ||
266 | GNUNET_free (tus->truth_data); | ||
267 | GNUNET_free (tus); | ||
268 | } | ||
269 | |||
270 | |||
271 | /** | ||
272 | * Offer internal data to other commands. | ||
273 | * | ||
274 | * @param cls closure | ||
275 | * @param ret[out] result (could be anything) | ||
276 | * @param trait name of the trait | ||
277 | * @param index index number of the object to extract. | ||
278 | * @return #GNUNET_OK on success | ||
279 | */ | ||
280 | static int | ||
281 | truth_upload_traits (void *cls, | ||
282 | const void **ret, | ||
283 | const char *trait, | ||
284 | unsigned int index) | ||
285 | { | ||
286 | struct TruthUploadState *tus = cls; | ||
287 | struct TALER_TESTING_Trait traits[] = { | ||
288 | ANASTASIS_TESTING_make_trait_truth (0, | ||
289 | tus->truth), | ||
290 | ANASTASIS_TESTING_make_trait_payment_secret (0, | ||
291 | &tus->payment_secret_response), | ||
292 | TALER_TESTING_trait_end () | ||
293 | }; | ||
294 | |||
295 | return TALER_TESTING_get_trait (traits, | ||
296 | ret, | ||
297 | trait, | ||
298 | index); | ||
299 | } | ||
300 | |||
301 | |||
302 | json_t * | ||
303 | ANASTASIS_TESTING_make_id_data_example (const char *id_data) | ||
304 | { | ||
305 | json_t *id; | ||
306 | |||
307 | id = json_pack ("{s:s}", | ||
308 | "id_data", id_data); | ||
309 | GNUNET_assert (NULL != id); | ||
310 | return id; | ||
311 | } | ||
312 | |||
313 | |||
314 | struct TALER_TESTING_Command | ||
315 | ANASTASIS_TESTING_cmd_truth_upload ( | ||
316 | const char *label, | ||
317 | const char *anastasis_url, | ||
318 | const json_t *id_data, | ||
319 | const char *method, | ||
320 | const char *instructions, | ||
321 | const char *mime_type, | ||
322 | const void *truth_data, | ||
323 | size_t truth_data_size, | ||
324 | unsigned int http_status, | ||
325 | enum ANASTASIS_TESTING_TruthStoreOption tso, | ||
326 | const char *salt_ref) | ||
327 | { | ||
328 | struct TruthUploadState *tus; | ||
329 | |||
330 | tus = GNUNET_new (struct TruthUploadState); | ||
331 | tus->label = label; | ||
332 | tus->http_status = http_status; | ||
333 | tus->tsopt = tso; | ||
334 | tus->anastasis_url = anastasis_url; | ||
335 | tus->salt_reference = salt_ref; | ||
336 | tus->id_data = json_incref ((json_t *) id_data); | ||
337 | tus->method = method; | ||
338 | tus->instructions = instructions; | ||
339 | tus->mime_type = mime_type; | ||
340 | tus->truth_data_size = truth_data_size; | ||
341 | tus->truth_data = GNUNET_memdup (truth_data, | ||
342 | truth_data_size); | ||
343 | { | ||
344 | struct TALER_TESTING_Command cmd = { | ||
345 | .cls = tus, | ||
346 | .label = label, | ||
347 | .run = &truth_upload_run, | ||
348 | .cleanup = &truth_upload_cleanup, | ||
349 | .traits = &truth_upload_traits | ||
350 | }; | ||
351 | |||
352 | return cmd; | ||
353 | } | ||
354 | } | ||
355 | |||
356 | |||
357 | struct TALER_TESTING_Command | ||
358 | ANASTASIS_TESTING_cmd_truth_upload_question ( | ||
359 | const char *label, | ||
360 | const char *anastasis_url, | ||
361 | const json_t *id_data, | ||
362 | const char *instructions, | ||
363 | const char *mime_type, | ||
364 | const void *answer, | ||
365 | unsigned int http_status, | ||
366 | enum ANASTASIS_TESTING_TruthStoreOption tso, | ||
367 | const char *salt_ref) | ||
368 | { | ||
369 | return ANASTASIS_TESTING_cmd_truth_upload (label, | ||
370 | anastasis_url, | ||
371 | id_data, | ||
372 | "question", | ||
373 | instructions, | ||
374 | mime_type, | ||
375 | answer, | ||
376 | strlen (answer), | ||
377 | http_status, | ||
378 | tso, | ||
379 | salt_ref); | ||
380 | } | ||
381 | |||
382 | |||
383 | /* end of testing_cmd_truth_upload.c */ | ||