psycstore: add option to perform membership test when retrieving fragment or message
[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   modifiers[0] = (struct GNUNET_ENV_Modifier) {
335     .oper = '=',
336     .name = "_sync_foo",
337     .value = "three two one",
338     .value_size = sizeof ("three two one") - 1
339   };
340   modifiers[1] = (struct GNUNET_ENV_Modifier) {
341     .oper = '=',
342     .name = "_sync_bar",
343     .value = "ten eleven twelve",
344     .value_size = sizeof ("ten eleven twelve") - 1
345   };
346
347   op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
348                                     GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
349                                     2, modifiers, state_sync_result, fcls);
350 }
351
352
353 void
354 message_get_fragment_result (void *cls, int64_t result, const char *err_msg)
355 {
356   struct FragmentClosure *fcls = cls;
357   op = NULL;
358   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_fragment:\t%d\n", result);
359   GNUNET_assert (result > 0 && fcls->n && fcls->n_expected);
360
361   fcls->n = 0;
362   fcls->n_expected = 3;
363   op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key, &slave_pub_key,
364                                      GNUNET_ntohll (fcls->msg[0]->message_id),
365                                      &fragment_result,
366                                      &message_get_result, fcls);
367 }
368
369
370 void
371 fragment_get_result (void *cls, int64_t result, const char *err_msg)
372 {
373   struct FragmentClosure *fcls = cls;
374   op = NULL;
375   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get:\t%d\n", result);
376   GNUNET_assert (result > 0 && fcls->n && fcls->n_expected);
377
378   fcls->n = 1;
379   fcls->n_expected = 2;
380   op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key, &slave_pub_key,
381                                               GNUNET_ntohll (fcls->msg[1]->message_id),
382                                               GNUNET_ntohll (fcls->msg[1]->fragment_offset),
383                                               &fragment_result,
384                                               &message_get_fragment_result,
385                                               fcls);
386
387 }
388
389
390 void
391 fragment_store_result (void *cls, int64_t result, const char *err_msg)
392 {
393   op = NULL;
394   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%d\n", result);
395   GNUNET_assert (GNUNET_OK == result);
396
397   if ((intptr_t) cls == GNUNET_YES)
398   { /* last fragment */
399     fcls.n = 0;
400     fcls.n_expected = 1;
401     op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key, &slave_pub_key,
402                                         GNUNET_ntohll (fcls.msg[0]->fragment_id),
403                                         &fragment_result,
404                                         &fragment_get_result, &fcls);
405   }
406 }
407
408
409 void
410 fragment_store ()
411 {
412   struct GNUNET_MULTICAST_MessageHeader *msg;
413   fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
414   fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
415   GNUNET_assert (msg != NULL);
416
417   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
418   msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
419
420   msg->hop_counter = htonl (9);
421   msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
422   msg->fragment_offset = GNUNET_htonll (0);
423   msg->message_id = GNUNET_htonll (INT64_MAX - 10);
424   msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
425   msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
426
427   memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
428
429   msg->purpose.size = htonl (ntohs (msg->header.size)
430                              - sizeof (msg->header)
431                              - sizeof (msg->hop_counter)
432                              - sizeof (msg->signature));
433   msg->purpose.purpose = htonl (234);
434   GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose,
435                                                         &msg->signature));
436
437   op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
438                                         &fragment_store_result, GNUNET_NO);
439
440   fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
441   fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
442   memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
443   msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
444   msg->fragment_offset = GNUNET_htonll (1024);
445
446   op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
447                                         &fragment_store_result, GNUNET_NO);
448
449   fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
450   fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
451   memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
452   msg->fragment_id = GNUNET_htonll (INT64_MAX);
453   msg->fragment_offset = GNUNET_htonll (16384);
454
455   op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
456                                         &fragment_store_result, (void *) GNUNET_YES);
457 }
458
459
460 void
461 membership_test_result (void *cls, int64_t result, const char *err_msg)
462 {
463   op = NULL;
464   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_test:\t%d\n", result);
465   GNUNET_assert (GNUNET_OK == result);
466
467   fragment_store ();
468 }
469
470
471 void
472 membership_store_result (void *cls, int64_t result, const char *err_msg)
473 {
474   op = NULL;
475   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_store:\t%d\n", result);
476   GNUNET_assert (GNUNET_OK == result);
477
478   op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
479                                          INT64_MAX - 10, 2,
480                                          &membership_test_result, NULL);
481 }
482
483 /**
484  * Main function of the test, run from scheduler.
485  *
486  * @param cls NULL
487  * @param cfg configuration we use (also to connect to PSYCstore service)
488  * @param peer handle to access more of the peer (not used)
489  */
490 static void
491 #if DEBUG_TEST_PSYCSTORE
492 run (void *cls, char *const *args, const char *cfgfile,
493      const struct GNUNET_CONFIGURATION_Handle *cfg)
494 #else
495 run (void *cls,
496      const struct GNUNET_CONFIGURATION_Handle *cfg,
497      struct GNUNET_TESTING_Peer *peer)
498 #endif
499 {
500   end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
501
502   h = GNUNET_PSYCSTORE_connect (cfg);
503   GNUNET_assert (NULL != h);
504
505   channel_key = GNUNET_CRYPTO_eddsa_key_create ();
506   slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
507
508   GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
509   GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
510
511   op = GNUNET_PSYCSTORE_membership_store (h, &channel_pub_key, &slave_pub_key,
512                                           GNUNET_YES, INT64_MAX - 5,
513                                           INT64_MAX - 10, 2,
514                                           &membership_store_result, NULL);
515 }
516
517
518 int
519 main (int argc, char *argv[])
520 {
521   res = 1;
522 #if DEBUG_TEST_PSYCSTORE
523   const struct GNUNET_GETOPT_CommandLineOption opts[] = {
524     GNUNET_GETOPT_OPTION_END
525   };
526   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psycstore",
527                                        "test-psycstore [options]",
528                                        opts, &run, NULL))
529     return 1;
530 #else
531   if (0 != GNUNET_TESTING_service_run ("test-psycstore", "psycstore",
532                                        "test_psycstore.conf", &run, NULL))
533     return 1;
534 #endif
535   return res;
536 }
537
538 /* end of test_psycstore.c */