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