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