5eadef62c842c7d4bda40ed0c4d664b210634467
[oweals/gnunet.git] / src / psyc / test_psyc.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 psyc/test_psyc.c
23  * @brief Tests for the PSYC API.
24  * @author Gabor X Toth
25  * @author Christian Grothoff
26  */
27
28 #include <inttypes.h>
29
30 #include "platform.h"
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_common.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_testing_lib.h"
35 #include "gnunet_env_lib.h"
36 #include "gnunet_psyc_util_lib.h"
37 #include "gnunet_psyc_service.h"
38
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
40
41 /**
42  * Return value from 'main'.
43  */
44 int res;
45
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
47
48 /**
49  * Handle for task for timeout termination.
50  */
51 GNUNET_SCHEDULER_TaskIdentifier end_badly_task;
52
53 struct GNUNET_PSYC_Master *mst;
54 struct GNUNET_PSYC_Slave *slv;
55
56 struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
57 struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
58
59 struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
60 struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
61
62 struct TransmitClosure
63 {
64   struct GNUNET_PSYC_MasterTransmitHandle *mst_tmit;
65   struct GNUNET_PSYC_SlaveTransmitHandle *slv_tmit;
66   struct GNUNET_ENV_Environment *env;
67   struct GNUNET_ENV_Modifier *mod;
68   char *data[16];
69   const char *mod_value;
70   size_t mod_value_size;
71   uint8_t data_delay[16];
72   uint8_t data_count;
73   uint8_t paused;
74   uint8_t n;
75 };
76
77 struct TransmitClosure *tmit;
78
79 uint8_t join_req_count;
80
81 enum
82 {
83   TEST_NONE,
84   TEST_SLAVE_TRANSMIT,
85   TEST_MASTER_TRANSMIT,
86 } test;
87
88
89 void
90 master_transmit ();
91
92
93 void master_stopped (void *cls)
94 {
95   if (NULL != tmit)
96   {
97     GNUNET_ENV_environment_destroy (tmit->env);
98     GNUNET_free (tmit);
99     tmit = NULL;
100   }
101   GNUNET_SCHEDULER_shutdown ();
102 }
103
104 void slave_parted (void *cls)
105 {
106   if (NULL != mst)
107   {
108     GNUNET_PSYC_master_stop (mst, GNUNET_NO, &master_stopped, NULL);
109     mst = NULL;
110   }
111   else
112     master_stopped (NULL);
113 }
114
115 /**
116  * Clean up all resources used.
117  */
118 void
119 cleanup ()
120 {
121   if (NULL != slv)
122   {
123     GNUNET_PSYC_slave_part (slv, GNUNET_NO, &slave_parted, NULL);
124     slv = NULL;
125   }
126   else
127     slave_parted (NULL);
128 }
129
130
131 /**
132  * Terminate the test case (failure).
133  *
134  * @param cls NULL
135  * @param tc scheduler context
136  */
137 void
138 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
139 {
140   res = 1;
141   cleanup ();
142   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n");
143 }
144
145
146 /**
147  * Terminate the test case (success).
148  *
149  * @param cls NULL
150  * @param tc scheduler context
151  */
152 void
153 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
154 {
155   res = 0;
156   cleanup ();
157   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test PASSED.\n");
158 }
159
160
161 /**
162  * Finish the test case (successfully).
163  */
164 void
165 end ()
166 {
167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending tests.\n");
168
169   if (end_badly_task != GNUNET_SCHEDULER_NO_TASK)
170   {
171     GNUNET_SCHEDULER_cancel (end_badly_task);
172     end_badly_task = GNUNET_SCHEDULER_NO_TASK;
173   }
174   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
175                                 &end_normally, NULL);
176 }
177
178
179 void
180 master_message_cb (void *cls, uint64_t message_id, uint32_t flags,
181                    const struct GNUNET_PSYC_MessageHeader *msg)
182 {
183   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
184               "Master got PSYC message fragment of size %u "
185               "belonging to message ID %llu with flags %x\n",
186               ntohs (msg->header.size), message_id, flags);
187   // FIXME
188 }
189
190
191 void
192 master_message_part_cb (void *cls, uint64_t message_id,
193                         uint64_t data_offset, uint32_t flags,
194                         const struct GNUNET_MessageHeader *msg)
195 {
196   if (NULL == msg)
197   {
198     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
199                 "Error while receiving message %llu\n", message_id);
200     return;
201   }
202
203   uint16_t type = ntohs (msg->type);
204   uint16_t size = ntohs (msg->size);
205
206   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
207               "Master got message part of type %u and size %u "
208               "belonging to message ID %llu with flags %x\n",
209               type, size, message_id, flags);
210
211   switch (test)
212   {
213   case TEST_SLAVE_TRANSMIT:
214     if (GNUNET_PSYC_MESSAGE_REQUEST != flags)
215     {
216       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
217                   "Unexpected request flags: %x" PRIu32 "\n", flags);
218       GNUNET_assert (0);
219       return;
220     }
221     // FIXME: check rest of message
222
223     if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END == type)
224       master_transmit ();
225     break;
226
227   case TEST_MASTER_TRANSMIT:
228     break;
229
230   default:
231     GNUNET_assert (0);
232   }
233 }
234
235
236 void
237 slave_message_cb (void *cls, uint64_t message_id, uint32_t flags,
238                   const struct GNUNET_PSYC_MessageHeader *msg)
239 {
240   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
241               "Slave got PSYC message fragment of size %u "
242               "belonging to message ID %llu with flags %x\n",
243               ntohs (msg->header.size), message_id, flags);
244   // FIXME
245 }
246
247
248 void
249 slave_message_part_cb (void *cls, uint64_t message_id,
250                        uint64_t data_offset, uint32_t flags,
251                        const struct GNUNET_MessageHeader *msg)
252 {
253   if (NULL == msg)
254   {
255     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256                 "Error while receiving message %llu\n", message_id);
257     return;
258   }
259
260   uint16_t type = ntohs (msg->type);
261   uint16_t size = ntohs (msg->size);
262
263   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
264               "Slave got message part of type %u and size %u "
265               "belonging to message ID %llu with flags %x\n",
266               type, size, message_id, flags);
267
268   switch (test)
269   {
270   case TEST_MASTER_TRANSMIT:
271     if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END == type)
272       end ();
273     break;
274
275   default:
276     GNUNET_assert (0);
277   }
278 }
279
280
281 void
282 transmit_resume (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
283 {
284   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission resumed.\n");
285   struct TransmitClosure *tmit = cls;
286   if (NULL != tmit->mst_tmit)
287     GNUNET_PSYC_master_transmit_resume (tmit->mst_tmit);
288   else
289     GNUNET_PSYC_slave_transmit_resume (tmit->slv_tmit);
290 }
291
292
293 int
294 tmit_notify_data (void *cls, uint16_t *data_size, void *data)
295 {
296   struct TransmitClosure *tmit = cls;
297   if (0 == tmit->data_count)
298   {
299     *data_size = 0;
300     return GNUNET_YES;
301   }
302
303   uint16_t size = strlen (tmit->data[tmit->n]);
304   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305               "Transmit notify data: %u bytes available, "
306               "processing fragment %u/%u (size %u).\n",
307               *data_size, tmit->n + 1, tmit->data_count, size);
308   if (*data_size < size)
309   {
310     *data_size = 0;
311     GNUNET_assert (0);
312     return GNUNET_SYSERR;
313   }
314
315   if (GNUNET_YES != tmit->paused && 0 < tmit->data_delay[tmit->n])
316   {
317     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission paused.\n");
318     tmit->paused = GNUNET_YES;
319     GNUNET_SCHEDULER_add_delayed (
320       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
321                                      tmit->data_delay[tmit->n]),
322       &transmit_resume, tmit);
323     *data_size = 0;
324     return GNUNET_NO;
325   }
326   tmit->paused = GNUNET_NO;
327
328   *data_size = size;
329   memcpy (data, tmit->data[tmit->n], size);
330
331   return ++tmit->n < tmit->data_count ? GNUNET_NO : GNUNET_YES;
332 }
333
334
335 int
336 tmit_notify_mod (void *cls, uint16_t *data_size, void *data, uint8_t *oper,
337                  uint32_t *full_value_size)
338 {
339   struct TransmitClosure *tmit = cls;
340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341               "Transmit notify modifier: %lu bytes available, "
342               "%u modifiers left to process.\n",
343               *data_size, GNUNET_ENV_environment_get_count (tmit->env));
344
345   uint16_t name_size = 0;
346   size_t value_size = 0;
347   const char *value = NULL;
348
349   if (NULL != oper && NULL != tmit->mod)
350   { /* New modifier */
351     tmit->mod = tmit->mod->next;
352     if (NULL == tmit->mod)
353     { /* No more modifiers, continue with data */
354       *data_size = 0;
355       return GNUNET_YES;
356     }
357
358     GNUNET_assert (tmit->mod->value_size < UINT32_MAX);
359     *full_value_size = tmit->mod->value_size;
360     *oper = tmit->mod->oper;
361     name_size = strlen (tmit->mod->name);
362
363     if (name_size + 1 + tmit->mod->value_size <= *data_size)
364     {
365       *data_size = name_size + 1 + tmit->mod->value_size;
366     }
367     else
368     {
369       tmit->mod_value_size = tmit->mod->value_size;
370       value_size = *data_size - name_size - 1;
371       tmit->mod_value_size -= value_size;
372       tmit->mod_value = tmit->mod->value + value_size;
373     }
374
375     memcpy (data, tmit->mod->name, name_size);
376     ((char *)data)[name_size] = '\0';
377     memcpy ((char *)data + name_size + 1, tmit->mod->value, value_size);
378   }
379   else if (NULL != tmit->mod_value && 0 < tmit->mod_value_size)
380   { /* Modifier continuation */
381     value = tmit->mod_value;
382     if (tmit->mod_value_size <= *data_size)
383     {
384       value_size = tmit->mod_value_size;
385       tmit->mod_value = NULL;
386     }
387     else
388     {
389       value_size = *data_size;
390       tmit->mod_value += value_size;
391     }
392     tmit->mod_value_size -= value_size;
393
394     if (*data_size < value_size)
395     {
396       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397                   "value larger than buffer: %u < %zu\n",
398                   *data_size, value_size);
399       *data_size = 0;
400       return GNUNET_NO;
401     }
402
403     *data_size = value_size;
404     memcpy (data, value, value_size);
405   }
406
407   return GNUNET_NO;
408 }
409
410
411 void
412 slave_join ();
413
414
415 void
416 slave_transmit ()
417 {
418
419   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Slave sending request to master.\n");
420
421   test = TEST_SLAVE_TRANSMIT;
422
423   tmit = GNUNET_new (struct TransmitClosure);
424   tmit->env = GNUNET_ENV_environment_create ();
425   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
426                               "_abc", "abc def", 7);
427   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
428                               "_abc_def", "abc def ghi", 11);
429   tmit->mod = GNUNET_ENV_environment_head (tmit->env);
430   tmit->n = 0;
431   tmit->data[0] = "slave test";
432   tmit->data_count = 1;
433   tmit->slv_tmit
434     = GNUNET_PSYC_slave_transmit (slv, "_request_test", tmit_notify_mod,
435                                   tmit_notify_data, tmit,
436                                   GNUNET_PSYC_SLAVE_TRANSMIT_NONE);
437 }
438
439
440 void
441 join_decision_cb (void *cls,
442                   const struct GNUNET_PSYC_JoinDecisionMessage *dcsn,
443                   int is_admitted,
444                   const struct GNUNET_PSYC_Message *join_msg)
445 {
446   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
447               "Slave got join decision: %d\n", is_admitted);
448
449   if (GNUNET_YES != is_admitted)
450   { /* First join request is refused, retry. */
451     GNUNET_assert (1 == join_req_count);
452     slave_join ();
453     return;
454   }
455
456   slave_transmit ();
457 }
458
459
460 void
461 join_request_cb (void *cls,
462                  const struct GNUNET_PSYC_JoinRequestMessage *req,
463                  const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
464                  const struct GNUNET_PSYC_Message *join_msg,
465                  struct GNUNET_PSYC_JoinHandle *jh)
466 {
467   struct GNUNET_HashCode slave_key_hash;
468   GNUNET_CRYPTO_hash (slave_key, sizeof (*slave_key), &slave_key_hash);
469   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
470               "Got join request #%u from %s.\n",
471               join_req_count, GNUNET_h2s (&slave_key_hash));
472
473   /* Reject first request */
474   int is_admitted = (0 < join_req_count++) ? GNUNET_YES : GNUNET_NO;
475   GNUNET_PSYC_join_decision (jh, is_admitted, 0, NULL, NULL);
476
477   /* Membership store */
478   struct GNUNET_PSYC_Channel *chn = GNUNET_PSYC_master_get_channel (mst);
479   GNUNET_PSYC_channel_slave_add (chn, slave_key, 2, 2);
480   GNUNET_PSYC_channel_slave_remove (chn, &slave_pub_key, 2);
481 }
482
483
484 void
485 slave_connect_cb (void *cls, uint64_t max_message_id)
486 {
487   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
488               "Slave connected: %lu\n", max_message_id);
489 }
490
491
492 void
493 slave_join ()
494 {
495   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Joining slave.\n");
496
497   struct GNUNET_PeerIdentity origin = {}; // FIXME: this peer
498   struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
499   GNUNET_ENV_environment_add (env, GNUNET_ENV_OP_ASSIGN,
500                               "_foo", "bar baz", 7);
501   GNUNET_ENV_environment_add (env, GNUNET_ENV_OP_ASSIGN,
502                               "_foo_bar", "foo bar baz", 11);
503   struct GNUNET_PSYC_Message *
504     join_msg = GNUNET_PSYC_message_create ("_request_join", env, "some data", 9);
505
506   slv = GNUNET_PSYC_slave_join (cfg, &channel_pub_key, slave_key, &origin, 0, NULL,
507                                 &slave_message_cb, &slave_message_part_cb,
508                                 &slave_connect_cb, &join_decision_cb, NULL,
509                                 join_msg);
510   GNUNET_ENV_environment_destroy (env);
511 }
512
513
514 void
515 master_transmit ()
516 {
517   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Master sending message to all.\n");
518   test = TEST_MASTER_TRANSMIT;
519   uint32_t i, j;
520
521   char *name_max = "_test_max";
522   uint8_t name_max_size = sizeof ("_test_max");
523   char *val_max = GNUNET_malloc (GNUNET_PSYC_MODIFIER_MAX_PAYLOAD);
524   for (i = 0; i < GNUNET_PSYC_MODIFIER_MAX_PAYLOAD; i++)
525     val_max[i] = (0 == i % 10000) ? '0' + i / 10000 : '.';
526
527   char *name_cont = "_test_cont";
528   uint8_t name_cont_size = sizeof ("_test_cont");
529   char *val_cont = GNUNET_malloc (GNUNET_PSYC_MODIFIER_MAX_PAYLOAD
530                                   + GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD);
531   for (i = 0; i < GNUNET_PSYC_MODIFIER_MAX_PAYLOAD - name_cont_size; i++)
532     val_cont[i] = (0 == i % 10000) ? '0' + i / 10000 : ':';
533   for (j = 0; j < GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD; j++, i++)
534     val_cont[i] = (0 == j % 10000) ? '0' + j / 10000 : '!';
535
536   tmit = GNUNET_new (struct TransmitClosure);
537   tmit->env = GNUNET_ENV_environment_create ();
538   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
539                               "_foo", "bar baz", 7);
540   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
541                               name_max, val_max,
542                               GNUNET_PSYC_MODIFIER_MAX_PAYLOAD
543                               - name_max_size);
544   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
545                               "_foo_bar", "foo bar baz", 11);
546   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
547                               name_cont, val_cont,
548                               GNUNET_PSYC_MODIFIER_MAX_PAYLOAD - name_cont_size
549                               + GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD);
550   tmit->mod = GNUNET_ENV_environment_head (tmit->env);
551   tmit->data[0] = "foo";
552   tmit->data[1] =  GNUNET_malloc (GNUNET_PSYC_DATA_MAX_PAYLOAD + 1);
553   for (i = 0; i < GNUNET_PSYC_DATA_MAX_PAYLOAD; i++)
554     tmit->data[1][i] = (0 == i % 10000) ? '0' + i / 10000 : '_';
555   tmit->data[2] = "foo bar";
556   tmit->data[3] = "foo bar baz";
557   tmit->data_delay[1] = 3;
558   tmit->data_count = 4;
559   tmit->mst_tmit
560     = GNUNET_PSYC_master_transmit (mst, "_notice_test", tmit_notify_mod,
561                                    tmit_notify_data, tmit,
562                                    GNUNET_PSYC_MASTER_TRANSMIT_INC_GROUP_GEN);
563 }
564
565
566 void
567 master_start_cb (void *cls, uint64_t max_message_id)
568 {
569   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
570               "Master started: %" PRIu64 "\n", max_message_id);
571   slave_join ();
572 }
573
574
575 void
576 master_start ()
577 {
578   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting master.\n");
579   mst = GNUNET_PSYC_master_start (cfg, channel_key, GNUNET_PSYC_CHANNEL_PRIVATE,
580                                   &master_start_cb, &join_request_cb,
581                                   &master_message_cb, &master_message_part_cb,
582                                   NULL);
583 }
584
585 void
586 schedule_master_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
587 {
588   master_start ();
589 }
590
591
592 /**
593  * Main function of the test, run from scheduler.
594  *
595  * @param cls NULL
596  * @param cfg configuration we use (also to connect to PSYC service)
597  * @param peer handle to access more of the peer (not used)
598  */
599 void
600 #if DEBUG_TEST_PSYC
601 run (void *cls, char *const *args, const char *cfgfile,
602      const struct GNUNET_CONFIGURATION_Handle *c)
603 #else
604 run (void *cls,
605      const struct GNUNET_CONFIGURATION_Handle *c,
606      struct GNUNET_TESTING_Peer *peer)
607 #endif
608 {
609   cfg = c;
610   end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
611
612   channel_key = GNUNET_CRYPTO_eddsa_key_create ();
613   slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
614
615   GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
616   GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
617
618 #if DEBUG_TEST_PSYC
619   master_start ();
620 #else
621   /* Allow some time for the services to initialize. */
622   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
623                                 &schedule_master_start, NULL);
624 #endif
625   return;
626 }
627
628
629 int
630 main (int argc, char *argv[])
631 {
632   res = 1;
633 #if DEBUG_TEST_PSYC
634   const struct GNUNET_GETOPT_CommandLineOption opts[] = {
635     GNUNET_GETOPT_OPTION_END
636   };
637   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psyc",
638                                        "test-psyc [options]",
639                                        opts, &run, NULL))
640     return 1;
641 #else
642   if (0 != GNUNET_TESTING_peer_run ("test-psyc", "test_psyc.conf", &run, NULL))
643     return 1;
644 #endif
645   return res;
646 }
647
648 /* end of test_psyc.c */