Detailed statistics
[oweals/gnunet.git] / src / psycstore / test_psycstore.c
1 /*
2  * This file is part of GNUnet
3  * Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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_ENV_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  * @param tc scheduler context
117  */
118 static void
119 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
120 {
121   res = 1;
122   cleanup ();
123 }
124
125
126 /**
127  * Terminate the testcase (success).
128  *
129  * @param cls NULL
130  * @param tc scheduler context
131  */
132 static void
133 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 {
135   res = 0;
136   cleanup ();
137 }
138
139
140 /**
141  * Finish the testcase (successfully).
142  */
143 static void
144 end ()
145 {
146   if (end_badly_task != NULL)
147   {
148     GNUNET_SCHEDULER_cancel (end_badly_task);
149     end_badly_task = NULL;
150   }
151   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
152                                 &end_normally, NULL);
153 }
154
155
156 void
157 state_reset_result (void *cls, int64_t result,
158                     const char *err_msg, uint16_t err_msg_size)
159 {
160   op = NULL;
161   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_reset_result:\t%d\n", 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, const char *name, const void *value, size_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, "  variable %s matches\n", name);
185     return GNUNET_YES;
186   }
187   else
188   {
189     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190                 "  variable %s differs\nReceived: %.*s\nExpected: %.*s\n",
191                 name, value_size, value, val_size, val);
192     GNUNET_assert (0);
193     return GNUNET_SYSERR;
194   }
195 }
196
197
198 void
199 state_get_prefix_result (void *cls, int64_t result,
200                          const char *err_msg, uint16_t err_msg_size)
201 {
202   struct StateClosure *scls = cls;
203   op = NULL;
204   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_prefix_result:\t%d\n", result);
205   GNUNET_assert (GNUNET_OK == result && 2 == scls->n);
206
207   op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
208                                      &state_reset_result, cls);
209 }
210
211
212 void
213 state_get_result (void *cls, int64_t result,
214                   const char *err_msg, uint16_t err_msg_size)
215 {
216   op = NULL;
217   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_result:\t%d\n", result);
218   GNUNET_assert (GNUNET_OK == result);
219
220   scls.n = 0;
221
222   scls.name[0] = "_sync_bar";
223   scls.value[0] = "ten eleven twelve";
224   scls.value_size[0] = sizeof ("ten eleven twelve") - 1;
225
226   scls.name[1] = "_sync_foo";
227   scls.value[1] = "one two three";
228   scls.value_size[1] = sizeof ("one two three") - 1;
229
230   op = GNUNET_PSYCSTORE_state_get_prefix (h, &channel_pub_key, "_sync",
231                                           &state_result,
232                                           &state_get_prefix_result, &scls);
233 }
234
235
236 void
237 counters_result (void *cls, int status, uint64_t max_fragment_id,
238                  uint64_t max_message_id, uint64_t max_group_generation,
239                  uint64_t max_state_message_id)
240 {
241   struct FragmentClosure *fcls = cls;
242   int result = 0;
243   op = NULL;
244
245   if (GNUNET_OK == status
246       && max_fragment_id == GNUNET_ntohll (fcls->msg[2]->fragment_id)
247       && max_message_id == GNUNET_ntohll (fcls->msg[2]->message_id)
248       && max_group_generation == GNUNET_ntohll (fcls->msg[2]->group_generation)
249       && max_state_message_id == GNUNET_ntohll (fcls->msg[0]->message_id))
250     result = 1;
251
252   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "counters_get:\t%d\n", result);
253   GNUNET_assert (result == 1);
254
255   scls.n = 0;
256   scls.name[0] = "_bar";
257   scls.value[0] = "four five six";
258   scls.value_size[0] = sizeof ("four five six") - 1;
259
260   op = GNUNET_PSYCSTORE_state_get (h, &channel_pub_key, "_bar_x_yy_zzz",
261                                    &state_result, &state_get_result, &scls);
262 }
263
264
265 void
266 state_modify_result (void *cls, int64_t result,
267                      const char *err_msg, uint16_t err_msg_size)
268 {
269   op = NULL;
270   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_modify_result:\t%d\n", result);
271   GNUNET_assert (GNUNET_OK == result);
272
273   op = GNUNET_PSYCSTORE_counters_get (h, &channel_pub_key,
274                                       &counters_result, cls);
275 }
276
277
278 void
279 state_sync_result (void *cls, int64_t result,
280                    const char *err_msg, uint16_t err_msg_size)
281 {
282   struct FragmentClosure *fcls = cls;
283   op = NULL;
284   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_sync_result:\t%d\n", result);
285   GNUNET_assert (GNUNET_OK == result);
286
287   modifiers[0] = (struct GNUNET_ENV_Modifier) {
288     .oper = '=',
289     .name = "_sync_foo",
290     .value = "one two three",
291     .value_size = sizeof ("one two three") - 1
292   };
293   modifiers[1] = (struct GNUNET_ENV_Modifier) {
294     .oper = '=',
295     .name = "_bar",
296     .value = "four five six",
297     .value_size = sizeof ("four five six") - 1
298   };
299
300   op = GNUNET_PSYCSTORE_state_modify (h, &channel_pub_key,
301                                       GNUNET_ntohll (fcls->msg[0]->message_id), 0,
302                                       2, modifiers, state_modify_result, fcls);
303 }
304
305
306 int
307 fragment_result (void *cls,
308                  struct GNUNET_MULTICAST_MessageHeader *msg,
309                  enum GNUNET_PSYCSTORE_MessageFlags flags)
310 {
311   struct FragmentClosure *fcls = cls;
312   GNUNET_assert (fcls->n < fcls->n_expected);
313   struct GNUNET_MULTICAST_MessageHeader *msg0 = fcls->msg[fcls->n];
314   uint64_t flags0 = fcls->flags[fcls->n++];
315
316   if (flags == flags0 && msg->header.size == msg0->header.size
317       && 0 == memcmp (msg, msg0, ntohs (msg->header.size)))
318   {
319     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  fragment %" PRIu64 " matches\n",
320                 GNUNET_ntohll (msg->fragment_id));
321     return GNUNET_YES;
322   }
323   else
324   {
325     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
326                 "  fragment differs: expected %" PRIu64 ", got %" PRIu64 "\n",
327                 GNUNET_ntohll (msg0->fragment_id),
328                 GNUNET_ntohll (msg->fragment_id));
329     GNUNET_assert (0);
330     return GNUNET_SYSERR;
331   }
332 }
333
334
335 void
336 message_get_latest_result (void *cls, int64_t result,
337                            const char *err_msg, uint16_t err_msg_size)
338 {
339   struct FragmentClosure *fcls = cls;
340   op = NULL;
341   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_latest:\t%d\n", result);
342   GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
343
344   modifiers[0] = (struct GNUNET_ENV_Modifier) {
345     .oper = '=',
346     .name = "_sync_foo",
347     .value = "three two one",
348     .value_size = sizeof ("three two one") - 1
349   };
350   modifiers[1] = (struct GNUNET_ENV_Modifier) {
351     .oper = '=',
352     .name = "_sync_bar",
353     .value = "ten eleven twelve",
354     .value_size = sizeof ("ten eleven twelve") - 1
355   };
356
357   op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
358                                     GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
359                                     2, modifiers, state_sync_result, fcls);
360 }
361
362
363 void
364 message_get_result (void *cls, int64_t result,
365                     const char *err_msg, uint16_t err_msg_size)
366 {
367   struct FragmentClosure *fcls = cls;
368   op = NULL;
369   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get:\t%d\n", result);
370   GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
371
372   fcls->n = 0;
373   fcls->n_expected = 3;
374   op = GNUNET_PSYCSTORE_message_get_latest (h, &channel_pub_key, &slave_pub_key,
375                                             1, "", &fragment_result,
376                                             &message_get_latest_result, fcls);
377 }
378
379
380 void
381 message_get_fragment_result (void *cls, int64_t result,
382                              const char *err_msg, uint16_t err_msg_size)
383 {
384   struct FragmentClosure *fcls = cls;
385   op = NULL;
386   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_fragment:\t%d\n", result);
387   GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
388
389   fcls->n = 0;
390   fcls->n_expected = 3;
391   uint64_t message_id = GNUNET_ntohll (fcls->msg[0]->message_id);
392   op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key, &slave_pub_key,
393                                      message_id, message_id, "",
394                                      &fragment_result,
395                                      &message_get_result, fcls);
396 }
397
398
399 void
400 fragment_get_latest_result (void *cls, int64_t result,
401                             const char *err_msg, uint16_t err_msg_size)
402 {
403   struct FragmentClosure *fcls = cls;
404   op = NULL;
405   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get_latest:\t%d\n", result);
406   GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
407
408   fcls->n = 1;
409   fcls->n_expected = 2;
410   op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key, &slave_pub_key,
411                                               GNUNET_ntohll (fcls->msg[1]->message_id),
412                                               GNUNET_ntohll (fcls->msg[1]->fragment_offset),
413                                               &fragment_result,
414                                               &message_get_fragment_result, fcls);
415 }
416
417
418 void
419 fragment_get_result (void *cls, int64_t result,
420                      const char *err_msg, uint16_t err_msg_size)
421 {
422   struct FragmentClosure *fcls = cls;
423   op = NULL;
424   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get:\t%d\n", result);
425   GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
426
427   fcls->n = 0;
428   fcls->n_expected = 3;
429   op = GNUNET_PSYCSTORE_fragment_get_latest (h, &channel_pub_key,
430                                              &slave_pub_key, fcls->n_expected,
431                                              &fragment_result,
432                                              &fragment_get_latest_result, fcls);
433 }
434
435
436 void
437 fragment_store_result (void *cls, int64_t result,
438                        const char *err_msg, uint16_t err_msg_size)
439 {
440   op = NULL;
441   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%d\n", result);
442   GNUNET_assert (GNUNET_OK == result);
443
444   if ((intptr_t) cls == GNUNET_YES)
445   { /* last fragment */
446     fcls.n = 0;
447     fcls.n_expected = 1;
448     uint64_t fragment_id = GNUNET_ntohll (fcls.msg[0]->fragment_id);
449     op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key, &slave_pub_key,
450                                         fragment_id, fragment_id,
451                                         &fragment_result,
452                                         &fragment_get_result, &fcls);
453   }
454 }
455
456
457 void
458 fragment_store ()
459 {
460   struct GNUNET_MULTICAST_MessageHeader *msg;
461   fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
462   fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
463   GNUNET_assert (msg != NULL);
464
465   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
466   msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
467
468   msg->hop_counter = htonl (9);
469   msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
470   msg->fragment_offset = GNUNET_htonll (0);
471   msg->message_id = GNUNET_htonll (INT64_MAX - 10);
472   msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
473   msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
474
475   memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
476
477   msg->purpose.size = htonl (ntohs (msg->header.size)
478                              - sizeof (msg->header)
479                              - sizeof (msg->hop_counter)
480                              - sizeof (msg->signature));
481   msg->purpose.purpose = htonl (234);
482   GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose,
483                                                         &msg->signature));
484
485   op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
486                                         &fragment_store_result, GNUNET_NO);
487
488   fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
489   fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
490   memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
491   msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
492   msg->fragment_offset = GNUNET_htonll (1024);
493
494   op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
495                                         &fragment_store_result, GNUNET_NO);
496
497   fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
498   fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
499   memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
500   msg->fragment_id = GNUNET_htonll (INT64_MAX);
501   msg->fragment_offset = GNUNET_htonll (16384);
502
503   op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
504                                         &fragment_store_result, (void *) GNUNET_YES);
505 }
506
507
508 void
509 membership_test_result (void *cls, int64_t result,
510                         const char *err_msg, uint16_t err_msg_size)
511 {
512   op = NULL;
513   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_test:\t%d\n", result);
514   GNUNET_assert (GNUNET_OK == result);
515
516   fragment_store ();
517 }
518
519
520 void
521 membership_store_result (void *cls, int64_t result,
522                          const char *err_msg, uint16_t err_msg_size)
523 {
524   op = NULL;
525   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_store:\t%d\n", result);
526   GNUNET_assert (GNUNET_OK == result);
527
528   op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
529                                          INT64_MAX - 10, 2,
530                                          &membership_test_result, NULL);
531 }
532
533
534 /**
535  * Main function of the test, run from scheduler.
536  *
537  * @param cls NULL
538  * @param cfg configuration we use (also to connect to PSYCstore service)
539  * @param peer handle to access more of the peer (not used)
540  */
541 static void
542 #if DEBUG_TEST_PSYCSTORE
543 run (void *cls, char *const *args, const char *cfgfile,
544      const struct GNUNET_CONFIGURATION_Handle *cfg)
545 #else
546 run (void *cls,
547      const struct GNUNET_CONFIGURATION_Handle *cfg,
548      struct GNUNET_TESTING_Peer *peer)
549 #endif
550 {
551   end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
552
553   h = GNUNET_PSYCSTORE_connect (cfg);
554   GNUNET_assert (NULL != h);
555
556   channel_key = GNUNET_CRYPTO_eddsa_key_create ();
557   slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
558
559   GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
560   GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
561
562   op = GNUNET_PSYCSTORE_membership_store (h, &channel_pub_key, &slave_pub_key,
563                                           GNUNET_YES, INT64_MAX - 5,
564                                           INT64_MAX - 10, 2,
565                                           &membership_store_result, NULL);
566 }
567
568
569 int
570 main (int argc, char *argv[])
571 {
572   res = 1;
573 #if DEBUG_TEST_PSYCSTORE
574   const struct GNUNET_GETOPT_CommandLineOption opts[] = {
575     GNUNET_GETOPT_OPTION_END
576   };
577   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psycstore",
578                                        "test-psycstore [options]",
579                                        opts, &run, NULL))
580     return 1;
581 #else
582   if (0 != GNUNET_TESTING_service_run ("test-psycstore", "psycstore",
583                                        "test_psycstore.conf", &run, NULL))
584     return 1;
585 #endif
586   return res;
587 }
588
589 /* end of test_psycstore.c */