aboutsummaryrefslogtreecommitdiff
path: root/src/psycstore/test_psycstore.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/psycstore/test_psycstore.c')
-rw-r--r--src/psycstore/test_psycstore.c586
1 files changed, 586 insertions, 0 deletions
diff --git a/src/psycstore/test_psycstore.c b/src/psycstore/test_psycstore.c
new file mode 100644
index 0000000..ca50904
--- /dev/null
+++ b/src/psycstore/test_psycstore.c
@@ -0,0 +1,586 @@
1/*
2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
4 *
5 * GNUnet is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License,
8 * or (at your option) any later version.
9 *
10 * GNUnet 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 GNU
13 * Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file psycstore/test_psycstore.c
23 * @brief Test for the PSYCstore service.
24 * @author Gabor X Toth
25 * @author Christian Grothoff
26 */
27
28#include <inttypes.h>
29
30#include "platform.h"
31#include "gnunet_util_lib.h"
32#include "gnunet_common.h"
33#include "gnunet_testing_lib.h"
34#include "gnunet_psycstore_service.h"
35
36#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
37
38
39/**
40 * Return value from 'main'.
41 */
42static int res;
43
44/**
45 * Handle to PSYCstore service.
46 */
47static struct GNUNET_PSYCSTORE_Handle *h;
48
49/**
50 * Handle to PSYCstore operation.
51 */
52static struct GNUNET_PSYCSTORE_OperationHandle *op;
53
54/**
55 * Handle for task for timeout termination.
56 */
57static struct GNUNET_SCHEDULER_Task *end_badly_task;
58
59static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
60static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
61
62static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
63static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
64
65static struct FragmentClosure
66{
67 uint8_t n;
68 uint8_t n_expected;
69 uint64_t flags[16];
70 struct GNUNET_MULTICAST_MessageHeader *msg[16];
71} fcls;
72
73struct StateClosure {
74 size_t n;
75 char *name[16];
76 void *value[16];
77 size_t value_size[16];
78} scls;
79
80static struct GNUNET_PSYC_Modifier modifiers[16];
81
82/**
83 * Clean up all resources used.
84 */
85static void
86cleanup ()
87{
88 if (NULL != op)
89 {
90 GNUNET_PSYCSTORE_operation_cancel (op);
91 op = NULL;
92 }
93 if (NULL != h)
94 {
95 GNUNET_PSYCSTORE_disconnect (h);
96 h = NULL;
97 }
98 if (NULL != channel_key)
99 {
100 GNUNET_free (channel_key);
101 channel_key = NULL;
102 }
103 if (NULL != slave_key)
104 {
105 GNUNET_free (slave_key);
106 slave_key = NULL;
107 }
108 GNUNET_SCHEDULER_shutdown ();
109}
110
111
112/**
113 * Terminate the testcase (failure).
114 *
115 * @param cls NULL
116 */
117static void
118end_badly (void *cls)
119{
120 res = 1;
121 cleanup ();
122}
123
124
125/**
126 * Terminate the testcase (success).
127 *
128 * @param cls NULL
129 */
130static void
131end_normally (void *cls)
132{
133 res = 0;
134 cleanup ();
135}
136
137
138/**
139 * Finish the testcase (successfully).
140 */
141static void
142end ()
143{
144 if (NULL != end_badly_task)
145 {
146 GNUNET_SCHEDULER_cancel (end_badly_task);
147 end_badly_task = NULL;
148 }
149 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
150 &end_normally, NULL);
151}
152
153
154static void
155state_reset_result (void *cls,
156 int64_t result,
157 const char *err_msg,
158 uint16_t err_msg_size)
159{
160 op = NULL;
161 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
162 "state_reset_result:\t%d\n",
163 (int) result);
164 GNUNET_assert (GNUNET_OK == result);
165
166 op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
167 &state_reset_result, cls);
168 GNUNET_PSYCSTORE_operation_cancel (op);
169 op = NULL;
170 end ();
171}
172
173
174static int
175state_result (void *cls,
176 const char *name,
177 const void *value,
178 uint32_t value_size)
179{
180 struct StateClosure *scls = cls;
181 const char *nam = scls->name[scls->n];
182 const void *val = scls->value[scls->n];
183 size_t val_size = scls->value_size[scls->n++];
184
185 if (value_size == val_size
186 && 0 == memcmp (value, val, val_size)
187 && 0 == strcmp (name, nam))
188 {
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190 " variable %s matches\n",
191 name);
192 return GNUNET_YES;
193 }
194 else
195 {
196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
197 " variable %s differs\nReceived: %.*s\nExpected: %.*s\n",
198 name, (int) value_size, (char*) value, (int) val_size, (char*) val);
199 GNUNET_assert (0);
200 return GNUNET_SYSERR;
201 }
202}
203
204
205static void
206state_get_prefix_result (void *cls, int64_t result,
207 const char *err_msg, uint16_t err_msg_size)
208{
209 struct StateClosure *scls = cls;
210 op = NULL;
211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_prefix_result:\t%ld\n", (long int) result);
212 GNUNET_assert (GNUNET_OK == result && 2 == scls->n);
213
214 op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
215 &state_reset_result, cls);
216}
217
218
219static void
220state_get_result (void *cls, int64_t result,
221 const char *err_msg, uint16_t err_msg_size)
222{
223 op = NULL;
224 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_result:\t%ld\n", (long int) result);
225 GNUNET_assert (GNUNET_OK == result);
226
227 scls.n = 0;
228
229 scls.name[0] = "_sync_bar";
230 scls.value[0] = "ten eleven twelve";
231 scls.value_size[0] = sizeof ("ten eleven twelve") - 1;
232
233 scls.name[1] = "_sync_foo";
234 scls.value[1] = "three two one";
235 scls.value_size[1] = sizeof ("three two one") - 1;
236
237 op = GNUNET_PSYCSTORE_state_get_prefix (h, &channel_pub_key, "_sync",
238 &state_result,
239 &state_get_prefix_result, &scls);
240}
241
242
243static void
244counters_result (void *cls, int status, uint64_t max_fragment_id,
245 uint64_t max_message_id, uint64_t max_group_generation,
246 uint64_t max_state_message_id)
247{
248 struct FragmentClosure *fcls = cls;
249 int result = 0;
250 op = NULL;
251
252 if (GNUNET_OK == status
253 && max_fragment_id == GNUNET_ntohll (fcls->msg[2]->fragment_id)
254 && max_message_id == GNUNET_ntohll (fcls->msg[2]->message_id)
255 && max_group_generation == GNUNET_ntohll (fcls->msg[2]->group_generation)
256 && max_state_message_id == GNUNET_ntohll (fcls->msg[0]->message_id))
257 result = 1;
258
259 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "counters_get:\t%d\n", result);
260 GNUNET_assert (result == 1);
261
262 scls.n = 0;
263 scls.name[0] = "_sync_bar";
264 scls.value[0] = "ten eleven twelve";
265 scls.value_size[0] = sizeof ("ten eleven twelve") - 1;
266
267 op = GNUNET_PSYCSTORE_state_get (h, &channel_pub_key, "_sync_bar_x_yy_zzz",
268 &state_result, &state_get_result, &scls);
269}
270
271
272static void
273state_modify_result (void *cls, int64_t result,
274 const char *err_msg, uint16_t err_msg_size)
275{
276 op = NULL;
277 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_modify_result:\t%ld\n", (long int) result);
278 GNUNET_assert (GNUNET_OK == result);
279
280 op = GNUNET_PSYCSTORE_counters_get (h, &channel_pub_key,
281 &counters_result, cls);
282}
283
284
285static void
286state_sync_result (void *cls, int64_t result,
287 const char *err_msg, uint16_t err_msg_size)
288{
289 struct FragmentClosure *fcls = cls;
290 op = NULL;
291 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_sync_result:\t%ld\n", (long int) result);
292 GNUNET_assert (GNUNET_OK == result);
293
294 op = GNUNET_PSYCSTORE_state_modify (h, &channel_pub_key,
295 GNUNET_ntohll (fcls->msg[0]->message_id),
296 0, state_modify_result, fcls);
297}
298
299
300static int
301fragment_result (void *cls,
302 struct GNUNET_MULTICAST_MessageHeader *msg,
303 enum GNUNET_PSYCSTORE_MessageFlags flags)
304{
305 struct FragmentClosure *fcls = cls;
306 GNUNET_assert (fcls->n < fcls->n_expected);
307 struct GNUNET_MULTICAST_MessageHeader *msg0 = fcls->msg[fcls->n];
308 uint64_t flags0 = fcls->flags[fcls->n++];
309
310 if (flags == flags0 && msg->header.size == msg0->header.size
311 && 0 == memcmp (msg, msg0, ntohs (msg->header.size)))
312 {
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " fragment %" PRIu64 " matches\n",
314 GNUNET_ntohll (msg->fragment_id));
315 return GNUNET_YES;
316 }
317 else
318 {
319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
320 " fragment differs: expected %" PRIu64 ", got %" PRIu64 "\n",
321 GNUNET_ntohll (msg0->fragment_id),
322 GNUNET_ntohll (msg->fragment_id));
323 GNUNET_assert (0);
324 return GNUNET_SYSERR;
325 }
326}
327
328
329static void
330message_get_latest_result (void *cls, int64_t result,
331 const char *err_msg, uint16_t err_msg_size)
332{
333 struct FragmentClosure *fcls = cls;
334 op = NULL;
335 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_latest:\t%ld\n", (long int) result);
336 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
337
338 modifiers[0] = (struct GNUNET_PSYC_Modifier) {
339 .oper = '=',
340 .name = "_sync_foo",
341 .value = "three two one",
342 .value_size = sizeof ("three two one") - 1
343 };
344 modifiers[1] = (struct GNUNET_PSYC_Modifier) {
345 .oper = '=',
346 .name = "_sync_bar",
347 .value = "ten eleven twelve",
348 .value_size = sizeof ("ten eleven twelve") - 1
349 };
350
351 op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
352 GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
353 GNUNET_ntohll (fcls->msg[0]->message_id) + 2,
354 2, modifiers, state_sync_result, fcls);
355}
356
357
358static void
359message_get_result (void *cls, int64_t result,
360 const char *err_msg, uint16_t err_msg_size)
361{
362 struct FragmentClosure *fcls = cls;
363 op = NULL;
364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get:\t%ld\n", (long int) result);
365 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
366
367 fcls->n = 0;
368 fcls->n_expected = 3;
369 op = GNUNET_PSYCSTORE_message_get_latest (h, &channel_pub_key, &slave_pub_key,
370 1, "", &fragment_result,
371 &message_get_latest_result, fcls);
372}
373
374
375static void
376message_get_fragment_result (void *cls, int64_t result,
377 const char *err_msg, uint16_t err_msg_size)
378{
379 struct FragmentClosure *fcls = cls;
380 op = NULL;
381 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_fragment:\t%ld\n", (long int) result);
382 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
383
384 fcls->n = 0;
385 fcls->n_expected = 3;
386 uint64_t message_id = GNUNET_ntohll (fcls->msg[0]->message_id);
387 op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key, &slave_pub_key,
388 message_id, message_id, 0, "",
389 &fragment_result,
390 &message_get_result, fcls);
391}
392
393
394static void
395fragment_get_latest_result (void *cls, int64_t result,
396 const char *err_msg, uint16_t err_msg_size)
397{
398 struct FragmentClosure *fcls = cls;
399 op = NULL;
400 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get_latest:\t%ld\n", (long int) result);
401 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
402
403 fcls->n = 1;
404 fcls->n_expected = 2;
405 op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key, &slave_pub_key,
406 GNUNET_ntohll (fcls->msg[1]->message_id),
407 GNUNET_ntohll (fcls->msg[1]->fragment_offset),
408 &fragment_result,
409 &message_get_fragment_result, fcls);
410}
411
412
413static void
414fragment_get_result (void *cls, int64_t result,
415 const char *err_msg, uint16_t err_msg_size)
416{
417 struct FragmentClosure *fcls = cls;
418 op = NULL;
419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
420 "fragment_get:\t%d\n",
421 (int) result);
422 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
423
424 fcls->n = 0;
425 fcls->n_expected = 3;
426 op = GNUNET_PSYCSTORE_fragment_get_latest (h, &channel_pub_key,
427 &slave_pub_key, fcls->n_expected,
428 &fragment_result,
429 &fragment_get_latest_result, fcls);
430}
431
432
433static void
434fragment_store_result (void *cls, int64_t result,
435 const char *err_msg, uint16_t err_msg_size)
436{
437 op = NULL;
438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%ld\n", (long int) result);
439 GNUNET_assert (GNUNET_OK == result);
440
441 if ((intptr_t) cls == GNUNET_YES)
442 { /* last fragment */
443 fcls.n = 0;
444 fcls.n_expected = 1;
445 uint64_t fragment_id = GNUNET_ntohll (fcls.msg[0]->fragment_id);
446 op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key, &slave_pub_key,
447 fragment_id, fragment_id,
448 &fragment_result,
449 &fragment_get_result, &fcls);
450 }
451}
452
453
454static void
455fragment_store ()
456{
457 struct GNUNET_MULTICAST_MessageHeader *msg;
458 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
459 fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
460 GNUNET_assert (msg != NULL);
461
462 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
463 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
464
465 msg->hop_counter = htonl (9);
466 msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
467 msg->fragment_offset = GNUNET_htonll (0);
468 msg->message_id = GNUNET_htonll (INT64_MAX - 10);
469 msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
470 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
471
472 GNUNET_memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
473
474 msg->purpose.size = htonl (ntohs (msg->header.size)
475 - sizeof (msg->header)
476 - sizeof (msg->hop_counter)
477 - sizeof (msg->signature));
478 msg->purpose.purpose = htonl (234);
479 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose,
480 &msg->signature));
481
482 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
483 &fragment_store_result, GNUNET_NO);
484
485 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
486 fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
487 GNUNET_memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
488 msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
489 msg->fragment_offset = GNUNET_htonll (1024);
490
491 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
492 &fragment_store_result, GNUNET_NO);
493
494 fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
495 fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
496 GNUNET_memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
497 msg->fragment_id = GNUNET_htonll (INT64_MAX);
498 msg->fragment_offset = GNUNET_htonll (16384);
499
500 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
501 &fragment_store_result, (void *) GNUNET_YES);
502}
503
504
505static void
506membership_test_result (void *cls, int64_t result,
507 const char *err_msg, uint16_t err_msg_size)
508{
509 op = NULL;
510 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_test:\t%ld\n", (long int) result);
511 GNUNET_assert (GNUNET_OK == result);
512
513 fragment_store ();
514}
515
516
517static void
518membership_store_result (void *cls, int64_t result,
519 const char *err_msg, uint16_t err_msg_size)
520{
521 op = NULL;
522 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_store:\t%ld\n", (long int) result);
523 GNUNET_assert (GNUNET_OK == result);
524
525 op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
526 INT64_MAX - 10, 2,
527 &membership_test_result, NULL);
528}
529
530
531/**
532 * Main function of the test, run from scheduler.
533 *
534 * @param cls NULL
535 * @param cfg configuration we use (also to connect to PSYCstore service)
536 * @param peer handle to access more of the peer (not used)
537 */
538static void
539#if DEBUG_TEST_PSYCSTORE
540run (void *cls, char *const *args, const char *cfgfile,
541 const struct GNUNET_CONFIGURATION_Handle *cfg)
542#else
543run (void *cls,
544 const struct GNUNET_CONFIGURATION_Handle *cfg,
545 struct GNUNET_TESTING_Peer *peer)
546#endif
547{
548 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
549
550 h = GNUNET_PSYCSTORE_connect (cfg);
551 GNUNET_assert (NULL != h);
552
553 channel_key = GNUNET_CRYPTO_eddsa_key_create ();
554 slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
555
556 GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
557 GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
558
559 op = GNUNET_PSYCSTORE_membership_store (h, &channel_pub_key, &slave_pub_key,
560 GNUNET_YES, INT64_MAX - 5,
561 INT64_MAX - 10, 2,
562 &membership_store_result, NULL);
563}
564
565
566int
567main (int argc, char *argv[])
568{
569 res = 1;
570#if DEBUG_TEST_PSYCSTORE
571 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
572 GNUNET_GETOPT_OPTION_END
573 };
574 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psycstore",
575 "test-psycstore [options]",
576 opts, &run, NULL))
577 return 1;
578#else
579 if (0 != GNUNET_TESTING_service_run ("test-psycstore", "psycstore",
580 "test_psycstore.conf", &run, NULL))
581 return 1;
582#endif
583 return res;
584}
585
586/* end of test_psycstore.c */