2 * This file is part of GNUnet
3 * (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file psyc/test_psyc.c
23 * @brief Tests for the PSYC API.
24 * @author Gabor X Toth
25 * @author Christian Grothoff
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"
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
42 * Return value from 'main'.
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Handle for task for timeout termination.
51 GNUNET_SCHEDULER_TaskIdentifier end_badly_task;
53 struct GNUNET_PSYC_Master *mst;
54 struct GNUNET_PSYC_Slave *slv;
56 struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
57 struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
59 struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
60 struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
62 struct TransmitClosure
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;
69 const char *mod_value;
70 size_t mod_value_size;
71 uint8_t data_delay[16];
77 struct TransmitClosure *tmit;
79 uint8_t join_req_count;
93 void master_stopped (void *cls)
97 GNUNET_ENV_environment_destroy (tmit->env);
101 GNUNET_SCHEDULER_shutdown ();
104 void slave_parted (void *cls)
108 GNUNET_PSYC_master_stop (mst, GNUNET_NO, &master_stopped, NULL);
112 master_stopped (NULL);
116 * Clean up all resources used.
123 GNUNET_PSYC_slave_part (slv, GNUNET_NO, &slave_parted, NULL);
132 * Terminate the test case (failure).
135 * @param tc scheduler context
138 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n");
147 * Terminate the test case (success).
150 * @param tc scheduler context
153 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test PASSED.\n");
162 * Finish the test case (successfully).
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending tests.\n");
169 if (end_badly_task != GNUNET_SCHEDULER_NO_TASK)
171 GNUNET_SCHEDULER_cancel (end_badly_task);
172 end_badly_task = GNUNET_SCHEDULER_NO_TASK;
174 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
175 &end_normally, NULL);
180 master_message_cb (void *cls, uint64_t message_id, uint32_t flags,
181 const struct GNUNET_PSYC_MessageHeader *msg)
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);
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)
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
199 "Error while receiving message %llu\n", message_id);
203 uint16_t type = ntohs (msg->type);
204 uint16_t size = ntohs (msg->size);
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);
213 case TEST_SLAVE_TRANSMIT:
214 if (GNUNET_PSYC_MESSAGE_REQUEST != flags)
216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
217 "Unexpected request flags: %x" PRIu32 "\n", flags);
221 // FIXME: check rest of message
223 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END == type)
227 case TEST_MASTER_TRANSMIT:
237 slave_message_cb (void *cls, uint64_t message_id, uint32_t flags,
238 const struct GNUNET_PSYC_MessageHeader *msg)
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);
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)
255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256 "Error while receiving message %llu\n", message_id);
260 uint16_t type = ntohs (msg->type);
261 uint16_t size = ntohs (msg->size);
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);
270 case TEST_MASTER_TRANSMIT:
271 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END == type)
282 transmit_resume (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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);
289 GNUNET_PSYC_slave_transmit_resume (tmit->slv_tmit);
294 tmit_notify_data (void *cls, uint16_t *data_size, void *data)
296 struct TransmitClosure *tmit = cls;
297 if (0 == tmit->data_count)
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)
312 return GNUNET_SYSERR;
315 if (GNUNET_YES != tmit->paused && 0 < tmit->data_delay[tmit->n])
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);
326 tmit->paused = GNUNET_NO;
329 memcpy (data, tmit->data[tmit->n], size);
331 return ++tmit->n < tmit->data_count ? GNUNET_NO : GNUNET_YES;
336 tmit_notify_mod (void *cls, uint16_t *data_size, void *data, uint8_t *oper,
337 uint32_t *full_value_size)
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));
345 uint16_t name_size = 0;
346 size_t value_size = 0;
347 const char *value = NULL;
349 if (NULL != oper && NULL != tmit->mod)
351 tmit->mod = tmit->mod->next;
352 if (NULL == tmit->mod)
353 { /* No more modifiers, continue with data */
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);
363 if (name_size + 1 + tmit->mod->value_size <= *data_size)
365 *data_size = name_size + 1 + tmit->mod->value_size;
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;
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);
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)
384 value_size = tmit->mod_value_size;
385 tmit->mod_value = NULL;
389 value_size = *data_size;
390 tmit->mod_value += value_size;
392 tmit->mod_value_size -= value_size;
394 if (*data_size < value_size)
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "value larger than buffer: %u < %zu\n",
398 *data_size, value_size);
403 *data_size = value_size;
404 memcpy (data, value, value_size);
416 join_decision_cb (void *cls,
417 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn,
419 const struct GNUNET_PSYC_Message *join_msg)
421 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
422 "Slave got join decision: %d\n", is_admitted);
424 if (GNUNET_YES != is_admitted)
425 { /* First join request is refused, retry. */
426 GNUNET_assert (1 == join_req_count);
431 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Slave sending request to master.\n");
433 test = TEST_SLAVE_TRANSMIT;
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);
443 tmit->data[0] = "slave test";
444 tmit->data_count = 1;
446 = GNUNET_PSYC_slave_transmit (slv, "_request_test", tmit_notify_mod,
447 tmit_notify_data, tmit,
448 GNUNET_PSYC_SLAVE_TRANSMIT_NONE);
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)
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));
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);
472 slave_connect_cb (void *cls, uint64_t max_message_id)
474 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
475 "Slave connected: %lu\n", max_message_id);
482 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Joining slave.\n");
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);
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,
497 GNUNET_ENV_environment_destroy (env);
504 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Master sending message to all.\n");
505 test = TEST_MASTER_TRANSMIT;
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 : '.';
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 : '!';
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,
529 GNUNET_PSYC_MODIFIER_MAX_PAYLOAD
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,
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;
547 = GNUNET_PSYC_master_transmit (mst, "_notice_test", tmit_notify_mod,
548 tmit_notify_data, tmit,
549 GNUNET_PSYC_MASTER_TRANSMIT_INC_GROUP_GEN);
554 master_start_cb (void *cls, uint64_t max_message_id)
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Master started: %" PRIu64 "\n", max_message_id);
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,
573 schedule_master_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
580 * Main function of the test, run from scheduler.
583 * @param cfg configuration we use (also to connect to PSYC service)
584 * @param peer handle to access more of the peer (not used)
588 run (void *cls, char *const *args, const char *cfgfile,
589 const struct GNUNET_CONFIGURATION_Handle *c)
592 const struct GNUNET_CONFIGURATION_Handle *c,
593 struct GNUNET_TESTING_Peer *peer)
597 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
599 channel_key = GNUNET_CRYPTO_eddsa_key_create ();
600 slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
602 GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
603 GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
608 /* Allow some time for the services to initialize. */
609 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
610 &schedule_master_start, NULL);
617 main (int argc, char *argv[])
621 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
622 GNUNET_GETOPT_OPTION_END
624 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psyc",
625 "test-psyc [options]",
629 if (0 != GNUNET_TESTING_peer_run ("test-psyc", "test_psyc.conf", &run, NULL))
635 /* end of test_psyc.c */