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