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