da69f40b123262cbcfa29809e00d9a71b1861f41
[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 join_decision_cb (void *cls,
417                   const struct GNUNET_PSYC_JoinDecisionMessage *dcsn,
418                   int is_admitted,
419                   const struct GNUNET_PSYC_Message *join_msg)
420 {
421   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
422               "Slave got join decision: %d\n", is_admitted);
423
424   if (GNUNET_YES != is_admitted)
425   { /* First join request is refused, retry. */
426     GNUNET_assert (1 == join_req_count);
427     slave_join ();
428     return;
429   }
430
431   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Slave sending request to master.\n");
432
433   test = TEST_SLAVE_TRANSMIT;
434
435   tmit = GNUNET_new (struct TransmitClosure);
436   tmit->env = GNUNET_ENV_environment_create ();
437   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
438                               "_abc", "abc def", 7);
439   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
440                               "_abc_def", "abc def ghi", 11);
441   tmit->mod = GNUNET_ENV_environment_head (tmit->env);
442   tmit->n = 0;
443   tmit->data[0] = "slave test";
444   tmit->data_count = 1;
445   tmit->slv_tmit
446     = GNUNET_PSYC_slave_transmit (slv, "_request_test", tmit_notify_mod,
447                                   tmit_notify_data, tmit,
448                                   GNUNET_PSYC_SLAVE_TRANSMIT_NONE);
449 }
450
451
452 void
453 join_request_cb (void *cls,
454                  const struct GNUNET_PSYC_JoinRequestMessage *req,
455                  const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
456                  const struct GNUNET_PSYC_Message *join_msg,
457                  struct GNUNET_PSYC_JoinHandle *jh)
458 {
459   struct GNUNET_HashCode slave_key_hash;
460   GNUNET_CRYPTO_hash (slave_key, sizeof (*slave_key), &slave_key_hash);
461   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
462               "Got join request #%u from %s.\n",
463               join_req_count, GNUNET_h2s (&slave_key_hash));
464
465   /* Reject first request */
466   int is_admitted = (0 < join_req_count++) ? GNUNET_YES : GNUNET_NO;
467   GNUNET_PSYC_join_decision (jh, is_admitted, 0, NULL, NULL);
468 }
469
470
471 void
472 slave_connect_cb (void *cls, uint64_t max_message_id)
473 {
474   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
475               "Slave connected: %lu\n", max_message_id);
476 }
477
478
479 void
480 slave_join ()
481 {
482   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Joining slave.\n");
483
484   struct GNUNET_PeerIdentity origin = {}; // FIXME: this peer
485   struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
486   GNUNET_ENV_environment_add (env, GNUNET_ENV_OP_ASSIGN,
487                               "_foo", "bar baz", 7);
488   GNUNET_ENV_environment_add (env, GNUNET_ENV_OP_ASSIGN,
489                               "_foo_bar", "foo bar baz", 11);
490   struct GNUNET_PSYC_Message *
491     join_msg = GNUNET_PSYC_message_create ("_request_join", env, "some data", 9);
492
493   slv = GNUNET_PSYC_slave_join (cfg, &channel_pub_key, slave_key, &origin, 0, NULL,
494                                 &slave_message_cb, &slave_message_part_cb,
495                                 &slave_connect_cb, &join_decision_cb, NULL,
496                                 join_msg);
497   GNUNET_ENV_environment_destroy (env);
498 }
499
500
501 void
502 master_transmit ()
503 {
504   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Master sending message to all.\n");
505   test = TEST_MASTER_TRANSMIT;
506   uint32_t i, j;
507
508   char *name_max = "_test_max";
509   uint8_t name_max_size = sizeof ("_test_max");
510   char *val_max = GNUNET_malloc (GNUNET_PSYC_MODIFIER_MAX_PAYLOAD);
511   for (i = 0; i < GNUNET_PSYC_MODIFIER_MAX_PAYLOAD; i++)
512     val_max[i] = (0 == i % 10000) ? '0' + i / 10000 : '.';
513
514   char *name_cont = "_test_cont";
515   uint8_t name_cont_size = sizeof ("_test_cont");
516   char *val_cont = GNUNET_malloc (GNUNET_PSYC_MODIFIER_MAX_PAYLOAD
517                                   + GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD);
518   for (i = 0; i < GNUNET_PSYC_MODIFIER_MAX_PAYLOAD - name_cont_size; i++)
519     val_cont[i] = (0 == i % 10000) ? '0' + i / 10000 : ':';
520   for (j = 0; j < GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD; j++, i++)
521     val_cont[i] = (0 == j % 10000) ? '0' + j / 10000 : '!';
522
523   tmit = GNUNET_new (struct TransmitClosure);
524   tmit->env = GNUNET_ENV_environment_create ();
525   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
526                               "_foo", "bar baz", 7);
527   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
528                               name_max, val_max,
529                               GNUNET_PSYC_MODIFIER_MAX_PAYLOAD
530                               - name_max_size);
531   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
532                               "_foo_bar", "foo bar baz", 11);
533   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
534                               name_cont, val_cont,
535                               GNUNET_PSYC_MODIFIER_MAX_PAYLOAD - name_cont_size
536                               + GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD);
537   tmit->mod = GNUNET_ENV_environment_head (tmit->env);
538   tmit->data[0] = "foo";
539   tmit->data[1] =  GNUNET_malloc (GNUNET_PSYC_DATA_MAX_PAYLOAD + 1);
540   for (i = 0; i < GNUNET_PSYC_DATA_MAX_PAYLOAD; i++)
541     tmit->data[1][i] = (0 == i % 10000) ? '0' + i / 10000 : '_';
542   tmit->data[2] = "foo bar";
543   tmit->data[3] = "foo bar baz";
544   tmit->data_delay[1] = 3;
545   tmit->data_count = 4;
546   tmit->mst_tmit
547     = GNUNET_PSYC_master_transmit (mst, "_notice_test", tmit_notify_mod,
548                                    tmit_notify_data, tmit,
549                                    GNUNET_PSYC_MASTER_TRANSMIT_INC_GROUP_GEN);
550 }
551
552
553 void
554 master_start_cb (void *cls, uint64_t max_message_id)
555 {
556   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557               "Master started: %" PRIu64 "\n", max_message_id);
558   slave_join ();
559 }
560
561
562 void
563 master_start ()
564 {
565   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting master.\n");
566   mst = GNUNET_PSYC_master_start (cfg, channel_key, GNUNET_PSYC_CHANNEL_PRIVATE,
567                                   &master_start_cb, &join_request_cb,
568                                   &master_message_cb, &master_message_part_cb,
569                                   NULL);
570 }
571
572 void
573 schedule_master_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
574 {
575   master_start ();
576 }
577
578
579 /**
580  * Main function of the test, run from scheduler.
581  *
582  * @param cls NULL
583  * @param cfg configuration we use (also to connect to PSYC service)
584  * @param peer handle to access more of the peer (not used)
585  */
586 void
587 #if DEBUG_TEST_PSYC
588 run (void *cls, char *const *args, const char *cfgfile,
589      const struct GNUNET_CONFIGURATION_Handle *c)
590 #else
591 run (void *cls,
592      const struct GNUNET_CONFIGURATION_Handle *c,
593      struct GNUNET_TESTING_Peer *peer)
594 #endif
595 {
596   cfg = c;
597   end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
598
599   channel_key = GNUNET_CRYPTO_eddsa_key_create ();
600   slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
601
602   GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
603   GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
604
605 #if DEBUG_TEST_PSYC
606   master_start ();
607 #else
608   /* Allow some time for the services to initialize. */
609   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
610                                 &schedule_master_start, NULL);
611 #endif
612   return;
613 }
614
615
616 int
617 main (int argc, char *argv[])
618 {
619   res = 1;
620 #if DEBUG_TEST_PSYC
621   const struct GNUNET_GETOPT_CommandLineOption opts[] = {
622     GNUNET_GETOPT_OPTION_END
623   };
624   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psyc",
625                                        "test-psyc [options]",
626                                        opts, &run, NULL))
627     return 1;
628 #else
629   if (0 != GNUNET_TESTING_peer_run ("test-psyc", "test_psyc.conf", &run, NULL))
630     return 1;
631 #endif
632   return res;
633 }
634
635 /* end of test_psyc.c */