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