psyc service skeleton
[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 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_EccPrivateKey *channel_key;
60 static struct GNUNET_CRYPTO_EccPrivateKey *slave_key;
61
62 static struct GNUNET_CRYPTO_EccPublicSignKey channel_pub_key;
63 static struct GNUNET_CRYPTO_EccPublicSignKey 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_slave_result (void *cls, uint64_t max_state_msg_id)
235 {
236   struct FragmentClosure *fcls = cls;
237   int result = 0;
238   op = NULL;
239
240   if (max_state_msg_id == GNUNET_ntohll (fcls->msg[0]->message_id))
241     result = 1;
242
243   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "counters_get_slave:\t%d\n", result);
244   GNUNET_assert (result == 1);
245
246   scls.n = 0;
247   scls.name[0] = "_bar";
248   scls.value[0] = "four five six";
249   scls.value_size[0] = sizeof ("four five six") - 1;
250
251   op = GNUNET_PSYCSTORE_state_get (h, &channel_pub_key, "_bar_x_yy_zzz",
252                                    &state_result, &state_get_result, &scls);
253 }
254
255
256 void
257 state_modify_result (void *cls, int64_t result, const char *err_msg)
258 {
259   op = NULL;
260   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_modify_result:\t%d\n", result);
261   GNUNET_assert (GNUNET_OK == result);
262
263   op = GNUNET_PSYCSTORE_counters_get_slave (h, &channel_pub_key,
264                                             &counters_slave_result, cls);
265 }
266
267
268 void
269 state_sync_result (void *cls, int64_t result, const char *err_msg)
270 {
271   struct FragmentClosure *fcls = cls;
272   op = NULL;
273   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_sync_result:\t%d\n", result);
274   GNUNET_assert (GNUNET_OK == result);
275
276   modifiers[0] = (struct GNUNET_ENV_Modifier) {
277     .oper = '=',
278     .name = "_sync_foo",
279     .value = "one two three",
280     .value_size = sizeof ("one two three") - 1
281   };
282   modifiers[1] = (struct GNUNET_ENV_Modifier) {
283     .oper = '=',
284     .name = "_bar",
285     .value = "four five six",
286     .value_size = sizeof ("four five six") - 1
287   };
288
289   op = GNUNET_PSYCSTORE_state_modify (h, &channel_pub_key,
290                                       GNUNET_ntohll (fcls->msg[0]->message_id), 0,
291                                       2, modifiers, state_modify_result, fcls);
292 }
293
294
295 void
296 counters_master_result (void *cls, uint64_t fragment_id, uint64_t message_id,
297                         uint64_t group_generation)
298 {
299   struct FragmentClosure *fcls = cls;
300   int result = 0;
301   op = NULL;
302
303   if (fragment_id == GNUNET_ntohll (fcls->msg[2]->fragment_id) &&
304       message_id == GNUNET_ntohll (fcls->msg[2]->message_id) &&
305       group_generation == GNUNET_ntohll (fcls->msg[2]->group_generation))
306     result = 1;
307
308   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "counters_get_master:\t%d\n", result);
309   GNUNET_assert (result == 1);
310
311   modifiers[0] = (struct GNUNET_ENV_Modifier) {
312     .oper = '=',
313     .name = "_sync_foo",
314     .value = "three two one",
315     .value_size = sizeof ("three two one") - 1
316   };
317   modifiers[1] = (struct GNUNET_ENV_Modifier) {
318     .oper = '=',
319     .name = "_sync_bar",
320     .value = "ten eleven twelve",
321     .value_size = sizeof ("ten eleven twelve") - 1
322   };
323
324   op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
325                                     GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
326                                     2, modifiers, state_sync_result, fcls);
327 }
328
329
330 int
331 fragment_result (void *cls,
332                  struct GNUNET_MULTICAST_MessageHeader *msg,
333                  enum GNUNET_PSYCSTORE_MessageFlags flags)
334 {
335   struct FragmentClosure *fcls = cls;
336   struct GNUNET_MULTICAST_MessageHeader *msg0 = fcls->msg[fcls->n];
337   uint64_t flags0 = fcls->flags[fcls->n++];
338
339   if (flags == flags0 && msg->header.size == msg0->header.size
340       && 0 == memcmp (msg, msg0, ntohs (msg->header.size)))
341   {
342     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  fragment %llu matches\n",
343                 GNUNET_ntohll (msg->fragment_id));
344     return GNUNET_YES;
345   }
346   else
347   {
348     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  fragment %llu differs\n",
349                 GNUNET_ntohll (msg->fragment_id));
350     GNUNET_assert (0);
351     return GNUNET_SYSERR;
352   }
353 }
354
355
356 void
357 message_get_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:\t%d\n", result);
362   GNUNET_assert (result > 0 && fcls->n && fcls->n_expected);
363
364   op = GNUNET_PSYCSTORE_counters_get_master (h, &channel_pub_key,
365                                              &counters_master_result, fcls);
366 }
367
368
369 void
370 message_get_fragment_result (void *cls, int64_t result, const char *err_msg)
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 (result > 0 && fcls->n && fcls->n_expected);
376
377   fcls->n = 0;
378   fcls->n_expected = 3;
379   op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key,
380                                      GNUNET_ntohll (fcls->msg[0]->message_id),
381                                      &fragment_result,
382                                      &message_get_result, fcls);
383 }
384
385
386 void
387 fragment_get_result (void *cls, int64_t result, const char *err_msg)
388 {
389   struct FragmentClosure *fcls = cls;
390   op = NULL;
391   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get:\t%d\n", result);
392   GNUNET_assert (result > 0 && fcls->n && fcls->n_expected);
393
394   fcls->n = 1;
395   fcls->n_expected = 2;
396   op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key,
397                                               GNUNET_ntohll (fcls->msg[1]->message_id),
398                                               GNUNET_ntohll (fcls->msg[1]->fragment_offset),
399                                               &fragment_result,
400                                               &message_get_fragment_result,
401                                               fcls);
402
403 }
404
405
406 void
407 fragment_store_result (void *cls, int64_t result, const char *err_msg)
408 {
409   op = NULL;
410   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%d\n", result);
411   GNUNET_assert (GNUNET_OK == result);
412
413   if ((intptr_t) cls == GNUNET_YES)
414   {
415     fcls.n = 0;
416     fcls.n_expected = 1;
417     op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key,
418                                         GNUNET_ntohll (fcls.msg[0]->fragment_id),
419                                         &fragment_result,
420                                         &fragment_get_result, &fcls);
421   }
422 }
423
424
425 void
426 membership_test_result (void *cls, int64_t result, const char *err_msg)
427 {
428   op = NULL;
429   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_test:\t%d\n", result);
430   GNUNET_assert (GNUNET_OK == result);
431
432   struct GNUNET_MULTICAST_MessageHeader *msg;
433   fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
434   fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
435   GNUNET_assert (msg != NULL);
436
437   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
438   msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
439
440   msg->hop_counter = htonl (9);
441   msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
442   msg->fragment_offset = GNUNET_htonll (0);
443   msg->message_id = GNUNET_htonll (INT64_MAX - 10);
444   msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
445   msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
446
447   memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
448
449   msg->purpose.size = htonl (ntohs (msg->header.size)
450                              - sizeof (msg->header)
451                              - sizeof (msg->hop_counter)
452                              - sizeof (msg->signature));
453   msg->purpose.purpose = htonl (234);
454   GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecc_sign (slave_key, &msg->purpose,
455                                                       &msg->signature));
456
457   op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
458                                         &fragment_store_result, GNUNET_NO);
459
460   fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
461   fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
462   memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
463   msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
464   msg->fragment_offset = GNUNET_htonll (1024);
465
466   op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
467                                         &fragment_store_result, GNUNET_NO);
468
469   fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
470   fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
471   memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
472   msg->fragment_id = GNUNET_htonll (INT64_MAX);
473   msg->fragment_offset = GNUNET_htonll (16384);
474
475   op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
476                                         &fragment_store_result, (void *) GNUNET_YES);
477 }
478
479 void
480 membership_store_result (void *cls, int64_t result, const char *err_msg)
481 {
482   op = NULL;
483   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_store:\t%d\n", result);
484   GNUNET_assert (GNUNET_OK == result);
485
486   op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
487                                          4, 1,
488                                          &membership_test_result, NULL);
489 }
490
491 /**
492  * Main function of the test, run from scheduler.
493  *
494  * @param cls NULL
495  * @param cfg configuration we use (also to connect to PSYCstore service)
496  * @param peer handle to access more of the peer (not used)
497  */
498 static void
499 #if DEBUG_SERVICE
500 run (void *cls, char *const *args, const char *cfgfile,
501      const struct GNUNET_CONFIGURATION_Handle *cfg)
502 #else
503 run (void *cls,
504      const struct GNUNET_CONFIGURATION_Handle *cfg,
505      struct GNUNET_TESTING_Peer *peer)
506 #endif
507 {
508   end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
509
510   h = GNUNET_PSYCSTORE_connect (cfg);
511   GNUNET_assert (NULL != h);
512
513   channel_key = GNUNET_CRYPTO_ecc_key_create ();
514   slave_key = GNUNET_CRYPTO_ecc_key_create ();
515
516   GNUNET_CRYPTO_ecc_key_get_public_for_signature (channel_key, &channel_pub_key);
517   GNUNET_CRYPTO_ecc_key_get_public_for_signature (slave_key, &slave_pub_key);
518
519   op = GNUNET_PSYCSTORE_membership_store (h, &channel_pub_key, &slave_pub_key,
520                                           GNUNET_YES, 4, 2, 1,
521                                           &membership_store_result, NULL);
522 }
523
524
525 int
526 main (int argc, char *argv[])
527 {
528   res = 1;
529 #if DEBUG_SERVICE
530   const struct GNUNET_GETOPT_CommandLineOption opts[] = {
531     GNUNET_GETOPT_OPTION_END
532   };
533   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psycstore",
534                                        "test-psycstore [options]",
535                                        opts, &run, NULL))
536     return 1;
537 #else
538   if (0 != GNUNET_TESTING_service_run ("test-psycstore", "psycstore",
539                                        "test_psycstore.conf", &run, NULL))
540     return 1;
541 #endif
542   return res;
543 }
544
545 /* end of test_psycstore.c */