moving away from DEFAULTSERVICES to per-section FORCESTART, thus addressing #3565...
[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 #include "gnunet_core_service.h"
39
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
41
42 /**
43  * Return value from 'main'.
44  */
45 int res;
46
47 const struct GNUNET_CONFIGURATION_Handle *cfg;
48
49 struct GNUNET_CORE_Handle *core;
50 struct GNUNET_PeerIdentity this_peer;
51
52 /**
53  * Handle for task for timeout termination.
54  */
55 GNUNET_SCHEDULER_TaskIdentifier end_badly_task;
56
57 struct GNUNET_PSYC_Master *mst;
58 struct GNUNET_PSYC_Slave *slv;
59
60 struct GNUNET_PSYC_Channel *mst_chn, *slv_chn;
61
62 struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
63 struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
64
65 struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
66 struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
67
68 struct TransmitClosure
69 {
70   struct GNUNET_PSYC_MasterTransmitHandle *mst_tmit;
71   struct GNUNET_PSYC_SlaveTransmitHandle *slv_tmit;
72   struct GNUNET_ENV_Environment *env;
73   struct GNUNET_ENV_Modifier *mod;
74   char *data[16];
75   const char *mod_value;
76   size_t mod_value_size;
77   uint8_t data_delay[16];
78   uint8_t data_count;
79   uint8_t paused;
80   uint8_t n;
81 };
82
83 struct TransmitClosure *tmit;
84
85 uint8_t join_req_count;
86
87 enum
88 {
89   TEST_NONE = 0,
90   TEST_MASTER_START = 1,
91   TEST_SLAVE_JOIN = 2,
92   TEST_SLAVE_TRANSMIT = 3,
93   TEST_MASTER_TRANSMIT = 4,
94   TEST_MASTER_HISTORY_REPLAY_LATEST = 5,
95   TEST_SLAVE_HISTORY_REPLAY_LATEST = 6,
96   TEST_MASTER_HISTORY_REPLAY = 7,
97   TEST_SLAVE_HISTORY_REPLAY = 8,
98   TEST_MASTER_STATE_GET = 9,
99   TEST_SLAVE_STATE_GET = 10,
100   TEST_MASTER_STATE_GET_PREFIX = 11,
101   TEST_SLAVE_STATE_GET_PREFIX = 12,
102 } test;
103
104
105 void
106 master_transmit ();
107
108
109 void master_stopped (void *cls)
110 {
111   if (NULL != tmit)
112   {
113     GNUNET_ENV_environment_destroy (tmit->env);
114     GNUNET_free (tmit);
115     tmit = NULL;
116   }
117   GNUNET_SCHEDULER_shutdown ();
118 }
119
120 void slave_parted (void *cls)
121 {
122   if (NULL != mst)
123   {
124     GNUNET_PSYC_master_stop (mst, GNUNET_NO, &master_stopped, NULL);
125     mst = NULL;
126   }
127   else
128     master_stopped (NULL);
129 }
130
131 /**
132  * Clean up all resources used.
133  */
134 void
135 cleanup ()
136 {
137   if (NULL != core)
138   {
139     GNUNET_CORE_disconnect (core);
140     core = NULL;
141   }
142   if (NULL != slv)
143   {
144     GNUNET_PSYC_slave_part (slv, GNUNET_NO, &slave_parted, NULL);
145     slv = NULL;
146   }
147   else
148     slave_parted (NULL);
149 }
150
151
152 /**
153  * Terminate the test case (failure).
154  *
155  * @param cls NULL
156  * @param tc scheduler context
157  */
158 void
159 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 {
161   res = 1;
162   cleanup ();
163   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n");
164 }
165
166
167 /**
168  * Terminate the test case (success).
169  *
170  * @param cls NULL
171  * @param tc scheduler context
172  */
173 void
174 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
175 {
176   res = 0;
177   cleanup ();
178   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test PASSED.\n");
179 }
180
181
182 /**
183  * Finish the test case (successfully).
184  */
185 void
186 end ()
187 {
188   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending tests.\n");
189
190   if (end_badly_task != GNUNET_SCHEDULER_NO_TASK)
191   {
192     GNUNET_SCHEDULER_cancel (end_badly_task);
193     end_badly_task = GNUNET_SCHEDULER_NO_TASK;
194   }
195   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
196                                 &end_normally, NULL);
197 }
198
199
200 void
201 state_get_var (void *cls, const char *name, const void *value, size_t value_size)
202 {
203   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204               "Got state var: %s\n%.*s\n", name, value_size, value);  
205 }
206
207
208 /*** Slave state_get_prefix() ***/
209
210 void
211 slave_state_get_prefix_result (void *cls, int64_t result, const char *err_msg)
212 {
213   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
214               "slave_state_get_prefix:\t%" PRId64 " (%s)\n", result, err_msg);
215   // FIXME: GNUNET_assert (2 == result);
216   end ();
217 }
218
219
220 void
221 slave_state_get_prefix ()
222 {
223   test = TEST_SLAVE_STATE_GET_PREFIX;
224   GNUNET_PSYC_channel_state_get_prefix (slv_chn, "_foo", &state_get_var,
225                                         &slave_state_get_prefix_result, NULL);
226 }
227
228
229 /*** Master state_get_prefix() ***/
230
231
232 void
233 master_state_get_prefix_result (void *cls, int64_t result, const char *err_msg)
234 {
235   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
236               "master_state_get_prefix:\t%" PRId64 " (%s)\n", result, err_msg);
237   // FIXME: GNUNET_assert (2 == result);
238   slave_state_get_prefix ();
239 }
240
241
242 void
243 master_state_get_prefix ()
244 {
245   test = TEST_MASTER_STATE_GET_PREFIX;
246   GNUNET_PSYC_channel_state_get_prefix (mst_chn, "_foo", &state_get_var,
247                                         &master_state_get_prefix_result, NULL);
248 }
249
250
251 /*** Slave state_get() ***/
252
253
254 void
255 slave_state_get_result (void *cls, int64_t result, const char *err_msg)
256 {
257   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
258               "slave_state_get:\t%" PRId64 " (%s)\n", result, err_msg);
259   // FIXME: GNUNET_assert (2 == result);
260   master_state_get_prefix ();
261 }
262
263
264 void
265 slave_state_get ()
266 {
267   test = TEST_SLAVE_STATE_GET;
268   GNUNET_PSYC_channel_state_get (slv_chn, "_foo_bar_baz", &state_get_var,
269                                  &slave_state_get_result, NULL);
270 }
271
272
273 /*** Master state_get() ***/
274
275
276 void
277 master_state_get_result (void *cls, int64_t result, const char *err_msg)
278 {
279   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
280               "master_state_get:\t%" PRId64 " (%s)\n", result, err_msg);
281   // FIXME: GNUNET_assert (1 == result);
282   slave_state_get ();
283 }
284
285
286 void
287 master_state_get ()
288 {
289   test = TEST_MASTER_STATE_GET;
290   GNUNET_PSYC_channel_state_get (mst_chn, "_foo_bar_baz", &state_get_var,
291                                  &master_state_get_result, NULL);
292 }
293
294
295 /*** Slave history_replay() ***/
296
297 void
298 slave_history_replay_result (void *cls, int64_t result, const char *err_msg)
299 {
300   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
301               "slave_history_replay:\t%" PRId64 " (%s)\n", result, err_msg);
302   GNUNET_assert (9 == result);
303
304   master_state_get ();
305 }
306
307
308 void
309 slave_history_replay ()
310 {
311   test = TEST_SLAVE_HISTORY_REPLAY;
312   GNUNET_PSYC_channel_history_replay (slv_chn, 1, 1,
313                                       &slave_history_replay_result,
314                                       NULL);
315 }
316
317
318 /*** Master history_replay() ***/
319
320
321 void
322 master_history_replay_result (void *cls, int64_t result, const char *err_msg)
323 {
324   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
325               "master_history_replay:\t%" PRId64 " (%s)\n", result, err_msg);
326   GNUNET_assert (9 == result);
327
328   slave_history_replay ();
329 }
330
331
332 void
333 master_history_replay ()
334 {
335   test = TEST_MASTER_HISTORY_REPLAY;
336   GNUNET_PSYC_channel_history_replay (mst_chn, 1, 1,
337                                       &master_history_replay_result,
338                                       NULL);
339 }
340
341
342 /*** Slave history_replay_latest() ***/
343
344
345 void
346 slave_history_replay_latest_result (void *cls, int64_t result, const char *err_msg)
347 {
348   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
349               "slave_history_replay_latest:\t%" PRId64 " (%s)\n", result, err_msg);
350   GNUNET_assert (9 == result);
351
352   master_history_replay ();
353 }
354
355
356 void
357 slave_history_replay_latest ()
358 {
359   test = TEST_SLAVE_HISTORY_REPLAY_LATEST;
360   GNUNET_PSYC_channel_history_replay_latest (slv_chn, 1,
361                                              &slave_history_replay_latest_result,
362                                              NULL);
363 }
364
365
366 /*** Master history_replay_latest() ***/
367
368
369 void
370 master_history_replay_latest_result (void *cls, int64_t result, const char *err_msg)
371 {
372   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
373               "master_history_replay_latest:\t%" PRId64 " (%s)\n", result, err_msg);
374   GNUNET_assert (9 == result);
375
376   slave_history_replay_latest ();
377 }
378
379
380 void
381 master_history_replay_latest ()
382 {
383   test = TEST_MASTER_HISTORY_REPLAY_LATEST;
384   GNUNET_PSYC_channel_history_replay_latest (mst_chn, 1,
385                                              &master_history_replay_latest_result,
386                                              NULL);
387 }
388
389
390 void
391 master_message_cb (void *cls, uint64_t message_id, uint32_t flags,
392                    const struct GNUNET_PSYC_MessageHeader *msg)
393 {
394   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
395               "Test #%d: Master got PSYC message fragment of size %u "
396               "belonging to message ID %" PRIu64 " with flags %x\n",
397               test, ntohs (msg->header.size), message_id, flags);
398   // FIXME
399 }
400
401
402 void
403 master_message_part_cb (void *cls, uint64_t message_id,
404                         uint64_t data_offset, uint32_t flags,
405                         const struct GNUNET_MessageHeader *msg)
406 {
407   if (NULL == msg)
408   {
409     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
410                 "Error while receiving message %" PRIu64 "\n", message_id);
411     return;
412   }
413
414   uint16_t type = ntohs (msg->type);
415   uint16_t size = ntohs (msg->size);
416
417   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
418               "Test #%d: Master got message part of type %u and size %u "
419               "belonging to message ID %" PRIu64 " with flags %x\n",
420               test, type, size, message_id, flags);
421
422   switch (test)
423   {
424   case TEST_SLAVE_TRANSMIT:
425     if (GNUNET_PSYC_MESSAGE_REQUEST != flags)
426     {
427       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
428                   "Unexpected request flags: %x" PRIu32 "\n", flags);
429       GNUNET_assert (0);
430       return;
431     }
432     // FIXME: check rest of message
433
434     if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END == type)
435       master_transmit ();
436     break;
437
438   case TEST_MASTER_TRANSMIT:
439     break;
440
441   case TEST_MASTER_HISTORY_REPLAY:
442   case TEST_MASTER_HISTORY_REPLAY_LATEST:
443     if (GNUNET_PSYC_MESSAGE_HISTORIC != flags)
444     {
445       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
446                   "Test #%d: Unexpected flags for historic message: %x" PRIu32 "\n",
447                   flags);
448       GNUNET_assert (0);
449       return;
450     }
451     break;
452
453   default:
454     GNUNET_assert (0);
455   }
456 }
457
458
459 void
460 slave_message_cb (void *cls, uint64_t message_id, uint32_t flags,
461                   const struct GNUNET_PSYC_MessageHeader *msg)
462 {
463   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
464               "Test #%d: Slave got PSYC message fragment of size %u "
465               "belonging to message ID %" PRIu64 " with flags %x\n",
466               test, ntohs (msg->header.size), message_id, flags);
467   // FIXME
468 }
469
470
471 void
472 slave_message_part_cb (void *cls, uint64_t message_id,
473                        uint64_t data_offset, uint32_t flags,
474                        const struct GNUNET_MessageHeader *msg)
475 {
476   if (NULL == msg)
477   {
478     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
479                 "Error while receiving message " PRIu64 "\n", message_id);
480     return;
481   }
482
483   uint16_t type = ntohs (msg->type);
484   uint16_t size = ntohs (msg->size);
485
486   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
487               "Test #%d: Slave got message part of type %u and size %u "
488               "belonging to message ID %" PRIu64 " with flags %x\n",
489               test, type, size, message_id, flags);
490
491   switch (test)
492   {
493   case TEST_MASTER_TRANSMIT:
494     if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END == type)
495       master_history_replay_latest ();
496     break;
497
498   case TEST_SLAVE_HISTORY_REPLAY:
499   case TEST_SLAVE_HISTORY_REPLAY_LATEST:
500     if (GNUNET_PSYC_MESSAGE_HISTORIC != flags)
501     {
502       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
503                   "Test #%d: Unexpected flags for historic message: %x" PRIu32 "\n",
504                   flags);
505       GNUNET_assert (0);
506       return;
507     }
508     break;
509
510   default:
511     GNUNET_assert (0);
512   }
513 }
514
515
516 void
517 transmit_resume (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
518 {
519   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission resumed.\n");
520   struct TransmitClosure *tmit = cls;
521   if (NULL != tmit->mst_tmit)
522     GNUNET_PSYC_master_transmit_resume (tmit->mst_tmit);
523   else
524     GNUNET_PSYC_slave_transmit_resume (tmit->slv_tmit);
525 }
526
527
528 int
529 tmit_notify_data (void *cls, uint16_t *data_size, void *data)
530 {
531   struct TransmitClosure *tmit = cls;
532   if (0 == tmit->data_count)
533   {
534     *data_size = 0;
535     return GNUNET_YES;
536   }
537
538   uint16_t size = strlen (tmit->data[tmit->n]);
539   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
540               "Transmit notify data: %u bytes available, "
541               "processing fragment %u/%u (size %u).\n",
542               *data_size, tmit->n + 1, tmit->data_count, size);
543   if (*data_size < size)
544   {
545     *data_size = 0;
546     GNUNET_assert (0);
547     return GNUNET_SYSERR;
548   }
549
550   if (GNUNET_YES != tmit->paused && 0 < tmit->data_delay[tmit->n])
551   {
552     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission paused.\n");
553     tmit->paused = GNUNET_YES;
554     GNUNET_SCHEDULER_add_delayed (
555       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
556                                      tmit->data_delay[tmit->n]),
557       &transmit_resume, tmit);
558     *data_size = 0;
559     return GNUNET_NO;
560   }
561   tmit->paused = GNUNET_NO;
562
563   *data_size = size;
564   memcpy (data, tmit->data[tmit->n], size);
565
566   return ++tmit->n < tmit->data_count ? GNUNET_NO : GNUNET_YES;
567 }
568
569
570 int
571 tmit_notify_mod (void *cls, uint16_t *data_size, void *data, uint8_t *oper,
572                  uint32_t *full_value_size)
573 {
574   struct TransmitClosure *tmit = cls;
575   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576               "Transmit notify modifier: %lu bytes available, "
577               "%u modifiers left to process.\n",
578               *data_size, GNUNET_ENV_environment_get_count (tmit->env));
579
580   uint16_t name_size = 0;
581   size_t value_size = 0;
582   const char *value = NULL;
583
584   if (NULL != oper && NULL != tmit->mod)
585   { /* New modifier */
586     tmit->mod = tmit->mod->next;
587     if (NULL == tmit->mod)
588     { /* No more modifiers, continue with data */
589       *data_size = 0;
590       return GNUNET_YES;
591     }
592
593     GNUNET_assert (tmit->mod->value_size < UINT32_MAX);
594     *full_value_size = tmit->mod->value_size;
595     *oper = tmit->mod->oper;
596     name_size = strlen (tmit->mod->name);
597
598     if (name_size + 1 + tmit->mod->value_size <= *data_size)
599     {
600       *data_size = name_size + 1 + tmit->mod->value_size;
601     }
602     else
603     {
604       tmit->mod_value_size = tmit->mod->value_size;
605       value_size = *data_size - name_size - 1;
606       tmit->mod_value_size -= value_size;
607       tmit->mod_value = tmit->mod->value + value_size;
608     }
609
610     memcpy (data, tmit->mod->name, name_size);
611     ((char *)data)[name_size] = '\0';
612     memcpy ((char *)data + name_size + 1, tmit->mod->value, value_size);
613   }
614   else if (NULL != tmit->mod_value && 0 < tmit->mod_value_size)
615   { /* Modifier continuation */
616     value = tmit->mod_value;
617     if (tmit->mod_value_size <= *data_size)
618     {
619       value_size = tmit->mod_value_size;
620       tmit->mod_value = NULL;
621     }
622     else
623     {
624       value_size = *data_size;
625       tmit->mod_value += value_size;
626     }
627     tmit->mod_value_size -= value_size;
628
629     if (*data_size < value_size)
630     {
631       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632                   "value larger than buffer: %u < %zu\n",
633                   *data_size, value_size);
634       *data_size = 0;
635       return GNUNET_NO;
636     }
637
638     *data_size = value_size;
639     memcpy (data, value, value_size);
640   }
641
642   return GNUNET_NO;
643 }
644
645
646 void
647 slave_join ();
648
649
650 void
651 slave_transmit ()
652 {
653
654   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Slave sending request to master.\n");
655   test = TEST_SLAVE_TRANSMIT;
656
657   tmit = GNUNET_new (struct TransmitClosure);
658   tmit->env = GNUNET_ENV_environment_create ();
659   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
660                               "_abc", "abc def", 7);
661   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
662                               "_abc_def", "abc def ghi", 11);
663   tmit->mod = GNUNET_ENV_environment_head (tmit->env);
664   tmit->n = 0;
665   tmit->data[0] = "slave test";
666   tmit->data_count = 1;
667   tmit->slv_tmit
668     = GNUNET_PSYC_slave_transmit (slv, "_request_test", tmit_notify_mod,
669                                   tmit_notify_data, tmit,
670                                   GNUNET_PSYC_SLAVE_TRANSMIT_NONE);
671 }
672
673
674 void
675 slave_remove_cb (void *cls, int64_t result, const char *err_msg)
676 {
677   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
678               "slave_remove:\t%" PRId64 " (%s)\n", result, err_msg);
679
680   slave_transmit ();
681 }
682
683
684 void
685 slave_add_cb (void *cls, int64_t result, const char *err_msg)
686 {
687   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
688               "slave_add:\t%" PRId64 " (%s)\n", result, err_msg);
689
690   struct GNUNET_PSYC_Channel *chn = cls;
691   GNUNET_PSYC_channel_slave_remove (chn, &slave_pub_key, 2,
692                                     &slave_remove_cb, chn);
693
694 }
695
696
697 void
698 join_decision_cb (void *cls,
699                   const struct GNUNET_PSYC_JoinDecisionMessage *dcsn,
700                   int is_admitted,
701                   const struct GNUNET_PSYC_Message *join_msg)
702 {
703   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
704               "Slave got join decision: %d\n", is_admitted);
705
706   if (GNUNET_YES != is_admitted)
707   { /* First join request is refused, retry. */
708     GNUNET_assert (1 == join_req_count);
709     slave_join ();
710     return;
711   }
712
713   struct GNUNET_PSYC_Channel *chn = GNUNET_PSYC_master_get_channel (mst);
714   GNUNET_PSYC_channel_slave_add (chn, &slave_pub_key, 2, 2, &slave_add_cb, chn);
715 }
716
717
718 void
719 join_request_cb (void *cls,
720                  const struct GNUNET_PSYC_JoinRequestMessage *req,
721                  const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
722                  const struct GNUNET_PSYC_Message *join_msg,
723                  struct GNUNET_PSYC_JoinHandle *jh)
724 {
725   struct GNUNET_HashCode slave_key_hash;
726   GNUNET_CRYPTO_hash (slave_key, sizeof (*slave_key), &slave_key_hash);
727   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
728               "Got join request #%u from %s.\n",
729               join_req_count, GNUNET_h2s (&slave_key_hash));
730
731   /* Reject first request */
732   int is_admitted = (0 < join_req_count++) ? GNUNET_YES : GNUNET_NO;
733   GNUNET_PSYC_join_decision (jh, is_admitted, 0, NULL, NULL);
734 }
735
736
737 void
738 slave_connect_cb (void *cls, int result, uint64_t max_message_id)
739 {
740   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
741               "Slave connected: %d, max_message_id: %" PRIu64 "\n",
742               result, max_message_id);
743   GNUNET_assert (TEST_SLAVE_JOIN == test);
744   GNUNET_assert (GNUNET_OK == result || GNUNET_NO == result);
745 }
746
747
748 void
749 slave_join ()
750 {
751   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Joining slave.\n");
752   test = TEST_SLAVE_JOIN;
753
754   struct GNUNET_PeerIdentity origin = this_peer;
755   struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
756   GNUNET_ENV_environment_add (env, GNUNET_ENV_OP_ASSIGN,
757                               "_foo", "bar baz", 7);
758   GNUNET_ENV_environment_add (env, GNUNET_ENV_OP_ASSIGN,
759                               "_foo_bar", "foo bar baz", 11);
760   struct GNUNET_PSYC_Message *
761     join_msg = GNUNET_PSYC_message_create ("_request_join", env, "some data", 9);
762
763   slv = GNUNET_PSYC_slave_join (cfg, &channel_pub_key, slave_key, &origin, 0, NULL,
764                                 &slave_message_cb, &slave_message_part_cb,
765                                 &slave_connect_cb, &join_decision_cb, NULL,
766                                 join_msg);
767   slv_chn = GNUNET_PSYC_slave_get_channel (slv);
768   GNUNET_ENV_environment_destroy (env);
769 }
770
771
772 void
773 master_transmit ()
774 {
775   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Master sending message to all.\n");
776   test = TEST_MASTER_TRANSMIT;
777   uint32_t i, j;
778
779   char *name_max = "_test_max";
780   uint8_t name_max_size = sizeof ("_test_max");
781   char *val_max = GNUNET_malloc (GNUNET_PSYC_MODIFIER_MAX_PAYLOAD);
782   for (i = 0; i < GNUNET_PSYC_MODIFIER_MAX_PAYLOAD; i++)
783     val_max[i] = (0 == i % 10000) ? '0' + i / 10000 : '.';
784
785   char *name_cont = "_test_cont";
786   uint8_t name_cont_size = sizeof ("_test_cont");
787   char *val_cont = GNUNET_malloc (GNUNET_PSYC_MODIFIER_MAX_PAYLOAD
788                                   + GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD);
789   for (i = 0; i < GNUNET_PSYC_MODIFIER_MAX_PAYLOAD - name_cont_size; i++)
790     val_cont[i] = (0 == i % 10000) ? '0' + i / 10000 : ':';
791   for (j = 0; j < GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD; j++, i++)
792     val_cont[i] = (0 == j % 10000) ? '0' + j / 10000 : '!';
793
794   tmit = GNUNET_new (struct TransmitClosure);
795   tmit->env = GNUNET_ENV_environment_create ();
796   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
797                               "_foo", "bar baz", 7);
798   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
799                               name_max, val_max,
800                               GNUNET_PSYC_MODIFIER_MAX_PAYLOAD
801                               - name_max_size);
802   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
803                               "_foo_bar", "foo bar baz", 11);
804   GNUNET_ENV_environment_add (tmit->env, GNUNET_ENV_OP_ASSIGN,
805                               name_cont, val_cont,
806                               GNUNET_PSYC_MODIFIER_MAX_PAYLOAD - name_cont_size
807                               + GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD);
808   tmit->mod = GNUNET_ENV_environment_head (tmit->env);
809   tmit->data[0] = "foo";
810   tmit->data[1] =  GNUNET_malloc (GNUNET_PSYC_DATA_MAX_PAYLOAD + 1);
811   for (i = 0; i < GNUNET_PSYC_DATA_MAX_PAYLOAD; i++)
812     tmit->data[1][i] = (0 == i % 10000) ? '0' + i / 10000 : '_';
813   tmit->data[2] = "foo bar";
814   tmit->data[3] = "foo bar baz";
815   tmit->data_delay[1] = 3;
816   tmit->data_count = 4;
817   tmit->mst_tmit
818     = GNUNET_PSYC_master_transmit (mst, "_notice_test", tmit_notify_mod,
819                                    tmit_notify_data, tmit,
820                                    GNUNET_PSYC_MASTER_TRANSMIT_INC_GROUP_GEN);
821 }
822
823
824 void
825 master_start_cb (void *cls, int result, uint64_t max_message_id)
826 {
827   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
828               "Master started: %d, max_message_id: %" PRIu64 "\n",
829               result, max_message_id);
830   GNUNET_assert (TEST_MASTER_START == test);
831   GNUNET_assert (GNUNET_OK == result || GNUNET_NO == result);
832   slave_join ();
833 }
834
835
836 void
837 master_start ()
838 {
839   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Starting master.\n");
840   test = TEST_MASTER_START;
841   mst = GNUNET_PSYC_master_start (cfg, channel_key, GNUNET_PSYC_CHANNEL_PRIVATE,
842                                   &master_start_cb, &join_request_cb,
843                                   &master_message_cb, &master_message_part_cb,
844                                   NULL);
845   mst_chn = GNUNET_PSYC_master_get_channel (mst);
846 }
847
848 void
849 schedule_master_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
850 {
851   master_start ();
852 }
853
854
855 void
856 core_connected (void *cls, const struct GNUNET_PeerIdentity *my_identity)
857 {
858   this_peer = *my_identity;
859
860 #if DEBUG_TEST_PSYC
861   master_start ();
862 #else
863   /* Allow some time for the services to initialize. */
864   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
865                                 &schedule_master_start, NULL);
866 #endif
867
868 }
869
870 /**
871  * Main function of the test, run from scheduler.
872  *
873  * @param cls NULL
874  * @param cfg configuration we use (also to connect to PSYC service)
875  * @param peer handle to access more of the peer (not used)
876  */
877 void
878 #if DEBUG_TEST_PSYC
879 run (void *cls, char *const *args, const char *cfgfile,
880      const struct GNUNET_CONFIGURATION_Handle *c)
881 #else
882 run (void *cls,
883      const struct GNUNET_CONFIGURATION_Handle *c,
884      struct GNUNET_TESTING_Peer *peer)
885 #endif
886 {
887   cfg = c;
888   end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
889
890   channel_key = GNUNET_CRYPTO_eddsa_key_create ();
891   slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
892
893   GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
894   GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
895
896   core = GNUNET_CORE_connect (cfg, NULL, &core_connected, NULL, NULL,
897                               NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
898 }
899
900
901 int
902 main (int argc, char *argv[])
903 {
904   res = 1;
905 #if DEBUG_TEST_PSYC
906   const struct GNUNET_GETOPT_CommandLineOption opts[] = {
907     GNUNET_GETOPT_OPTION_END
908   };
909   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psyc",
910                                        "test-psyc [options]",
911                                        opts, &run, NULL))
912     return 1;
913 #else
914   if (0 != GNUNET_TESTING_peer_run ("test-psyc", "test_psyc.conf", &run, NULL))
915     return 1;
916 #endif
917   return res;
918 }
919
920 /* end of test_psyc.c */