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