- fix
[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] = "three two one";
228   scls.value_size[1] = sizeof ("three two one") - 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] = "_sync_bar";
257   scls.value[0] = "ten eleven twelve";
258   scls.value_size[0] = sizeof ("ten eleven twelve") - 1;
259
260   op = GNUNET_PSYCSTORE_state_get (h, &channel_pub_key, "_sync_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   op = GNUNET_PSYCSTORE_state_modify (h, &channel_pub_key,
288                                       GNUNET_ntohll (fcls->msg[0]->message_id),
289                                       0, state_modify_result, fcls);
290 }
291
292
293 int
294 fragment_result (void *cls,
295                  struct GNUNET_MULTICAST_MessageHeader *msg,
296                  enum GNUNET_PSYCSTORE_MessageFlags flags)
297 {
298   struct FragmentClosure *fcls = cls;
299   GNUNET_assert (fcls->n < fcls->n_expected);
300   struct GNUNET_MULTICAST_MessageHeader *msg0 = fcls->msg[fcls->n];
301   uint64_t flags0 = fcls->flags[fcls->n++];
302
303   if (flags == flags0 && msg->header.size == msg0->header.size
304       && 0 == memcmp (msg, msg0, ntohs (msg->header.size)))
305   {
306     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  fragment %" PRIu64 " matches\n",
307                 GNUNET_ntohll (msg->fragment_id));
308     return GNUNET_YES;
309   }
310   else
311   {
312     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
313                 "  fragment differs: expected %" PRIu64 ", got %" PRIu64 "\n",
314                 GNUNET_ntohll (msg0->fragment_id),
315                 GNUNET_ntohll (msg->fragment_id));
316     GNUNET_assert (0);
317     return GNUNET_SYSERR;
318   }
319 }
320
321
322 void
323 message_get_latest_result (void *cls, int64_t result,
324                            const char *err_msg, uint16_t err_msg_size)
325 {
326   struct FragmentClosure *fcls = cls;
327   op = NULL;
328   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_latest:\t%d\n", result);
329   GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
330
331   modifiers[0] = (struct GNUNET_ENV_Modifier) {
332     .oper = '=',
333     .name = "_sync_foo",
334     .value = "three two one",
335     .value_size = sizeof ("three two one") - 1
336   };
337   modifiers[1] = (struct GNUNET_ENV_Modifier) {
338     .oper = '=',
339     .name = "_sync_bar",
340     .value = "ten eleven twelve",
341     .value_size = sizeof ("ten eleven twelve") - 1
342   };
343
344   op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
345                                     GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
346                                     GNUNET_ntohll (fcls->msg[0]->message_id) + 2,
347                                     2, modifiers, state_sync_result, fcls);
348 }
349
350
351 void
352 message_get_result (void *cls, int64_t result,
353                     const char *err_msg, uint16_t err_msg_size)
354 {
355   struct FragmentClosure *fcls = cls;
356   op = NULL;
357   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get:\t%d\n", result);
358   GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
359
360   fcls->n = 0;
361   fcls->n_expected = 3;
362   op = GNUNET_PSYCSTORE_message_get_latest (h, &channel_pub_key, &slave_pub_key,
363                                             1, "", &fragment_result,
364                                             &message_get_latest_result, fcls);
365 }
366
367
368 void
369 message_get_fragment_result (void *cls, int64_t result,
370                              const char *err_msg, uint16_t err_msg_size)
371 {
372   struct FragmentClosure *fcls = cls;
373   op = NULL;
374   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_fragment:\t%d\n", result);
375   GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
376
377   fcls->n = 0;
378   fcls->n_expected = 3;
379   uint64_t message_id = GNUNET_ntohll (fcls->msg[0]->message_id);
380   op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key, &slave_pub_key,
381                                      message_id, message_id, "",
382                                      &fragment_result,
383                                      &message_get_result, fcls);
384 }
385
386
387 void
388 fragment_get_latest_result (void *cls, int64_t result,
389                             const char *err_msg, uint16_t err_msg_size)
390 {
391   struct FragmentClosure *fcls = cls;
392   op = NULL;
393   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get_latest:\t%d\n", result);
394   GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
395
396   fcls->n = 1;
397   fcls->n_expected = 2;
398   op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key, &slave_pub_key,
399                                               GNUNET_ntohll (fcls->msg[1]->message_id),
400                                               GNUNET_ntohll (fcls->msg[1]->fragment_offset),
401                                               &fragment_result,
402                                               &message_get_fragment_result, fcls);
403 }
404
405
406 void
407 fragment_get_result (void *cls, int64_t result,
408                      const char *err_msg, uint16_t err_msg_size)
409 {
410   struct FragmentClosure *fcls = cls;
411   op = NULL;
412   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get:\t%d\n", result);
413   GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
414
415   fcls->n = 0;
416   fcls->n_expected = 3;
417   op = GNUNET_PSYCSTORE_fragment_get_latest (h, &channel_pub_key,
418                                              &slave_pub_key, fcls->n_expected,
419                                              &fragment_result,
420                                              &fragment_get_latest_result, fcls);
421 }
422
423
424 void
425 fragment_store_result (void *cls, int64_t result,
426                        const char *err_msg, uint16_t err_msg_size)
427 {
428   op = NULL;
429   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%d\n", result);
430   GNUNET_assert (GNUNET_OK == result);
431
432   if ((intptr_t) cls == GNUNET_YES)
433   { /* last fragment */
434     fcls.n = 0;
435     fcls.n_expected = 1;
436     uint64_t fragment_id = GNUNET_ntohll (fcls.msg[0]->fragment_id);
437     op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key, &slave_pub_key,
438                                         fragment_id, fragment_id,
439                                         &fragment_result,
440                                         &fragment_get_result, &fcls);
441   }
442 }
443
444
445 void
446 fragment_store ()
447 {
448   struct GNUNET_MULTICAST_MessageHeader *msg;
449   fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
450   fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
451   GNUNET_assert (msg != NULL);
452
453   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
454   msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
455
456   msg->hop_counter = htonl (9);
457   msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
458   msg->fragment_offset = GNUNET_htonll (0);
459   msg->message_id = GNUNET_htonll (INT64_MAX - 10);
460   msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
461   msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
462
463   memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
464
465   msg->purpose.size = htonl (ntohs (msg->header.size)
466                              - sizeof (msg->header)
467                              - sizeof (msg->hop_counter)
468                              - sizeof (msg->signature));
469   msg->purpose.purpose = htonl (234);
470   GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose,
471                                                         &msg->signature));
472
473   op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
474                                         &fragment_store_result, GNUNET_NO);
475
476   fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
477   fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
478   memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
479   msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
480   msg->fragment_offset = GNUNET_htonll (1024);
481
482   op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
483                                         &fragment_store_result, GNUNET_NO);
484
485   fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
486   fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
487   memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
488   msg->fragment_id = GNUNET_htonll (INT64_MAX);
489   msg->fragment_offset = GNUNET_htonll (16384);
490
491   op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
492                                         &fragment_store_result, (void *) GNUNET_YES);
493 }
494
495
496 void
497 membership_test_result (void *cls, int64_t result,
498                         const char *err_msg, uint16_t err_msg_size)
499 {
500   op = NULL;
501   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_test:\t%d\n", result);
502   GNUNET_assert (GNUNET_OK == result);
503
504   fragment_store ();
505 }
506
507
508 void
509 membership_store_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_store:\t%d\n", result);
514   GNUNET_assert (GNUNET_OK == result);
515
516   op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
517                                          INT64_MAX - 10, 2,
518                                          &membership_test_result, NULL);
519 }
520
521
522 /**
523  * Main function of the test, run from scheduler.
524  *
525  * @param cls NULL
526  * @param cfg configuration we use (also to connect to PSYCstore service)
527  * @param peer handle to access more of the peer (not used)
528  */
529 static void
530 #if DEBUG_TEST_PSYCSTORE
531 run (void *cls, char *const *args, const char *cfgfile,
532      const struct GNUNET_CONFIGURATION_Handle *cfg)
533 #else
534 run (void *cls,
535      const struct GNUNET_CONFIGURATION_Handle *cfg,
536      struct GNUNET_TESTING_Peer *peer)
537 #endif
538 {
539   end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
540
541   h = GNUNET_PSYCSTORE_connect (cfg);
542   GNUNET_assert (NULL != h);
543
544   channel_key = GNUNET_CRYPTO_eddsa_key_create ();
545   slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
546
547   GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
548   GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
549
550   op = GNUNET_PSYCSTORE_membership_store (h, &channel_pub_key, &slave_pub_key,
551                                           GNUNET_YES, INT64_MAX - 5,
552                                           INT64_MAX - 10, 2,
553                                           &membership_store_result, NULL);
554 }
555
556
557 int
558 main (int argc, char *argv[])
559 {
560   res = 1;
561 #if DEBUG_TEST_PSYCSTORE
562   const struct GNUNET_GETOPT_CommandLineOption opts[] = {
563     GNUNET_GETOPT_OPTION_END
564   };
565   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psycstore",
566                                        "test-psycstore [options]",
567                                        opts, &run, NULL))
568     return 1;
569 #else
570   if (0 != GNUNET_TESTING_service_run ("test-psycstore", "psycstore",
571                                        "test_psycstore.conf", &run, NULL))
572     return 1;
573 #endif
574   return res;
575 }
576
577 /* end of test_psycstore.c */