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