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.
21 * @file social/test_social.c
22 * @brief Tests for the Social API.
23 * @author Gabor X Toth
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_common.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_testing_lib.h"
33 #include "gnunet_env_lib.h"
34 #include "gnunet_psyc_util_lib.h"
35 #include "gnunet_social_service.h"
36 #include "gnunet_core_service.h"
37 #include "gnunet_identity_service.h"
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
41 #define DATA2ARG(data) data, sizeof (data)
44 * Return value from 'main'.
49 * Handle for task for timeout termination.
51 GNUNET_SCHEDULER_TaskIdentifier end_badly_task;
53 const struct GNUNET_CONFIGURATION_Handle *cfg;
55 struct GNUNET_CORE_Handle *core;
56 struct GNUNET_PeerIdentity this_peer;
58 struct GNUNET_IDENTITY_Handle *id;
60 const struct GNUNET_IDENTITY_Ego *host_ego;
61 const struct GNUNET_IDENTITY_Ego *guest_ego;
63 const char *host_name = "Host One";
64 const char *guest_name = "Guest One";
66 struct GNUNET_CRYPTO_EddsaPrivateKey *place_key;
67 struct GNUNET_CRYPTO_EcdsaPrivateKey *guest_key;
69 struct GNUNET_CRYPTO_EddsaPublicKey place_pub_key;
70 struct GNUNET_CRYPTO_EcdsaPublicKey guest_pub_key;
72 struct GNUNET_SOCIAL_Slicer *host_slicer;
73 struct GNUNET_SOCIAL_Slicer *guest_slicer;
75 struct GNUNET_SOCIAL_Host *hst;
76 struct GNUNET_SOCIAL_Guest *gst;
78 struct GuestEnterMessage
80 struct GNUNET_PSYC_Message *msg;
81 const char *method_name;
82 struct GNUNET_ENV_Environment *env;
87 struct TransmitClosure
89 struct GNUNET_SOCIAL_Announcement *host_ann;
90 struct GNUNET_SOCIAL_TalkRequest *guest_talk;
91 struct GNUNET_ENV_Environment *env;
93 uint8_t data_delay[16];
99 uint8_t join_req_count;
100 struct GNUNET_PSYC_Message *join_resp;
105 TEST_HOST_ANSWER_DOOR_REFUSE = 1,
106 TEST_GUEST_RECV_ENTRY_DCSN_REFUSE = 2,
107 TEST_HOST_ANSWER_DOOR_ADMIT = 3,
108 TEST_GUEST_RECV_ENTRY_DCSN_ADMIT = 4,
109 TEST_HOST_ANNOUNCE = 5,
110 TEST_HOST_ANNOUNCE_END = 6,
112 TEST_GUEST_LEAVE = 8,
130 * Clean up all resources used.
137 GNUNET_CORE_disconnect (core);
143 GNUNET_IDENTITY_disconnect (id);
149 GNUNET_SOCIAL_guest_leave (gst, GNUNET_NO, NULL, NULL);
154 GNUNET_SOCIAL_host_leave (hst, GNUNET_NO, NULL, NULL);
157 GNUNET_SCHEDULER_shutdown ();
162 * Terminate the test case (failure).
165 * @param tc scheduler context
168 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n");
177 * Terminate the test case (success).
180 * @param tc scheduler context
183 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test PASSED.\n");
192 * Finish the test case (successfully).
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending tests.\n");
199 if (end_badly_task != GNUNET_SCHEDULER_NO_TASK)
201 GNUNET_SCHEDULER_cancel (end_badly_task);
202 end_badly_task = GNUNET_SCHEDULER_NO_TASK;
204 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
205 &end_normally, NULL);
210 transmit_resume (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission resumed.\n");
213 struct TransmitClosure *tmit = cls;
214 if (NULL != tmit->host_ann)
215 GNUNET_SOCIAL_host_announce_resume (tmit->host_ann);
217 GNUNET_SOCIAL_guest_talk_resume (tmit->guest_talk);
222 notify_data (void *cls, uint16_t *data_size, void *data)
224 struct TransmitClosure *tmit = cls;
225 if (NULL != tmit->env)
227 GNUNET_ENV_environment_destroy (tmit->env);
230 if (0 == tmit->data_count)
236 uint16_t size = strlen (tmit->data[tmit->n]) + 1;
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Transmit notify data: %u bytes available, "
239 "processing fragment %u/%u (size %u).\n",
240 *data_size, tmit->n + 1, tmit->data_count, size);
241 if (*data_size < size)
245 return GNUNET_SYSERR;
248 if (GNUNET_YES != tmit->paused && 0 < tmit->data_delay[tmit->n])
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission paused.\n");
251 tmit->paused = GNUNET_YES;
252 GNUNET_SCHEDULER_add_delayed (
253 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
254 tmit->data_delay[tmit->n]),
255 &transmit_resume, tmit);
259 tmit->paused = GNUNET_NO;
262 memcpy (data, tmit->data[tmit->n], size);
264 return ++tmit->n < tmit->data_count ? GNUNET_NO : GNUNET_YES;
270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
271 "The host has left the place.\n");
272 GNUNET_SOCIAL_slicer_destroy (host_slicer);
281 schedule_host_leave (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
283 test = TEST_HOST_LEAVE;
284 GNUNET_SOCIAL_host_leave (hst, GNUNET_NO, &host_left, NULL);
289 host_farewell (void *cls,
290 struct GNUNET_SOCIAL_Nym *nym,
291 struct GNUNET_ENV_Environment *env,
292 size_t variable_count,
293 struct GNUNET_ENV_Modifier *variables)
295 // FIXME: this function is not called yet
297 struct GNUNET_CRYPTO_EcdsaPublicKey *
298 nym_key = GNUNET_SOCIAL_nym_get_key (nym);
299 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
300 "Nym %s has left the place.\n",
301 GNUNET_CRYPTO_ecdsa_public_key_to_string (nym_key));
302 GNUNET_assert (0 == memcmp (&guest_pub_key, nym_key, sizeof (*nym_key)));
304 GNUNET_SCHEDULER_add_now (&schedule_host_leave, NULL);
309 guest_left (void *cls)
311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
312 "The guest has left the place.\n");
313 GNUNET_SOCIAL_slicer_destroy (guest_slicer);
317 GNUNET_SCHEDULER_add_now (&schedule_host_leave, NULL);
324 test = TEST_GUEST_LEAVE;
325 /* FIXME test keep_active */
326 GNUNET_SOCIAL_guest_leave (gst, GNUNET_NO, &guest_left, NULL);
332 guest_recv_method (void *cls,
333 const struct GNUNET_PSYC_MessageMethod *meth,
336 const struct GNUNET_SOCIAL_Nym *nym,
337 const char *method_name)
339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
340 "Test #%u: Guest received method for message ID %" PRIu64 ":\n"
342 test, message_id, method_name);
343 /* FIXME: check message */
348 guest_recv_modifier (void *cls,
349 const struct GNUNET_PSYC_MessageModifier *mod,
351 enum GNUNET_ENV_Operator oper,
356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
357 "Test #%u: Guest received modifier for message ID %" PRIu64 ":\n"
359 test, message_id, oper, name, value_size, value);
364 guest_recv_data (void *cls,
365 const struct GNUNET_MessageHeader *msg,
367 uint64_t data_offset,
371 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
372 "Test #%u: Guest received data for message ID %" PRIu64 ":\n"
374 test, message_id, data_size, data);
379 guest_recv_eom (void *cls,
380 const struct GNUNET_MessageHeader *msg,
384 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
385 "Test #%u: Guest received end of message ID %" PRIu64
387 test, message_id, cancelled);
391 case TEST_HOST_ANNOUNCE:
392 test = TEST_HOST_ANNOUNCE_END;
395 case TEST_HOST_ANNOUNCE_END:
406 host_recv_method (void *cls,
407 const struct GNUNET_PSYC_MessageMethod *meth,
410 const struct GNUNET_SOCIAL_Nym *nym,
411 const char *method_name)
413 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
414 "Test #%u: Host received method for message ID %" PRIu64 ":\n"
416 test, message_id, method_name);
417 /* FIXME: check message */
422 host_recv_modifier (void *cls,
423 const struct GNUNET_PSYC_MessageModifier *mod,
425 enum GNUNET_ENV_Operator oper,
430 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
431 "Test #%u: Host received modifier for message ID %" PRIu64 ":\n"
433 test, message_id, oper, name, value_size, value);
438 host_recv_data (void *cls,
439 const struct GNUNET_MessageHeader *msg,
441 uint64_t data_offset,
445 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
446 "Test #%u: Host received data for message ID %" PRIu64 ":\n"
448 test, message_id, data_size, data);
453 host_recv_eom (void *cls,
454 const struct GNUNET_MessageHeader *msg,
458 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
459 "Test #%u: Host received end of message ID %" PRIu64
461 test, message_id, cancelled);
465 case TEST_HOST_ANNOUNCE:
466 test = TEST_HOST_ANNOUNCE_END;
470 case TEST_HOST_ANNOUNCE_END:
474 case TEST_GUEST_TALK:
487 test = TEST_GUEST_TALK;
489 tmit = (struct TransmitClosure) {};
490 tmit.env = GNUNET_ENV_environment_create ();
491 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
492 "_bar_foo", DATA2ARG ("one two three"));
493 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
494 "_bar_baz", DATA2ARG ("four five"));
495 tmit.data[0] = "zzz xxx yyy";
496 tmit.data[1] = "zyx wvu tsr qpo";
497 tmit.data[2] = "testing ten nine eight";
501 = GNUNET_SOCIAL_guest_talk (gst, "_message_guest", tmit.env,
503 GNUNET_SOCIAL_TALK_NONE);
509 test = TEST_HOST_ANNOUNCE;
511 tmit = (struct TransmitClosure) {};
512 tmit.env = GNUNET_ENV_environment_create ();
513 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
514 "_foo", DATA2ARG ("bar baz"));
515 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
516 "_foo_bar", DATA2ARG ("foo bar"));
517 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
518 "_foo_bar_baz", DATA2ARG ("foo bar baz"));
519 tmit.data[0] = "aaa bbb ccc";
520 tmit.data[1] = "abc def ghi jkl";
521 tmit.data[2] = "testing one two three";
522 tmit.data[3] = "four five";
526 = GNUNET_SOCIAL_host_announce (hst, "_message_host", tmit.env,
528 GNUNET_SOCIAL_ANNOUNCE_NONE);
534 test = TEST_HOST_ANNOUNCE;
536 tmit = (struct TransmitClosure) {};
537 tmit.env = GNUNET_ENV_environment_create ();
538 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
539 "_foo2", DATA2ARG ("BAR BAZ"));
540 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
541 "_foo2_bar", DATA2ARG ("FOO BAR"));
542 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
543 "_foo2_bar", DATA2ARG ("FOO BAR BAZ"));
544 tmit.data[0] = "AAA BBB CCC";
545 tmit.data[1] = "ABC DEF GHI JKL";
546 tmit.data[2] = "TESTING ONE TWO THREE";
550 = GNUNET_SOCIAL_host_announce (hst, "_message_host_two", tmit.env,
552 GNUNET_SOCIAL_ANNOUNCE_NONE);
557 guest_recv_entry_decision (void *cls,
559 const struct GNUNET_PSYC_Message *entry_resp)
561 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
562 "Guest received entry decision (try %u): %d.\n",
563 join_req_count, is_admitted);
565 if (NULL != entry_resp)
567 struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
568 const char *method_name = NULL;
569 const void *data = NULL;
570 uint16_t data_size = 0;
571 GNUNET_PSYC_message_parse (entry_resp, &method_name, env, &data, &data_size);
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 method_name, data_size, data);
576 /* FIXME: check response message */
581 case TEST_GUEST_RECV_ENTRY_DCSN_REFUSE:
582 GNUNET_assert (GNUNET_NO == is_admitted);
586 case TEST_GUEST_RECV_ENTRY_DCSN_ADMIT:
587 GNUNET_assert (GNUNET_YES == is_admitted);
598 host_answer_door (void *cls,
599 struct GNUNET_SOCIAL_Nym *nym,
600 const char *method_name,
601 struct GNUNET_ENV_Environment *env,
607 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
608 "Host received entry request from guest (try %u).\n",
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
612 method_name, data_size, data);
616 case TEST_HOST_ANSWER_DOOR_REFUSE:
617 test = TEST_GUEST_RECV_ENTRY_DCSN_REFUSE;
618 join_resp = GNUNET_PSYC_message_create ("_refuse_nym", env,
619 DATA2ARG ("Go away!"));
620 GNUNET_SOCIAL_host_entry_decision (hst, nym, GNUNET_NO, join_resp);
623 case TEST_HOST_ANSWER_DOOR_ADMIT:
624 test = TEST_GUEST_RECV_ENTRY_DCSN_ADMIT;
625 join_resp = GNUNET_PSYC_message_create ("_admit_nym", env,
626 DATA2ARG ("Welcome, nym!"));
627 GNUNET_SOCIAL_host_entry_decision (hst, nym, GNUNET_YES, join_resp);
637 guest_recv_local_enter (void *cls, int result, uint64_t max_message_id)
639 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Guest entered to local place.\n");
647 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Entering to place as guest.\n");
649 struct GuestEnterMessage *emsg = &guest_enter_msg;
651 emsg->method_name = "_request_enter";
652 emsg->env = GNUNET_ENV_environment_create ();
653 GNUNET_ENV_environment_add (emsg->env, GNUNET_ENV_OP_ASSIGN,
654 "_abc", "abc def", 7);
655 GNUNET_ENV_environment_add (emsg->env, GNUNET_ENV_OP_ASSIGN,
656 "_abc_def", "abc def ghi", 11);
657 emsg->data = "let me in";
658 emsg->data_size = strlen (emsg->data) + 1;
659 emsg->msg = GNUNET_PSYC_message_create (emsg->method_name, emsg->env,
660 emsg->data, emsg->data_size);
662 gst = GNUNET_SOCIAL_guest_enter (cfg, guest_ego, &place_pub_key,
663 &this_peer, 0, NULL, emsg->msg,
664 guest_slicer, &guest_recv_local_enter,
665 &guest_recv_entry_decision, NULL);
669 void id_guest_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
671 GNUNET_assert (NULL != ego);
674 guest_slicer = GNUNET_SOCIAL_slicer_create ();
675 GNUNET_SOCIAL_slicer_add (guest_slicer, "",
676 &guest_recv_method, &guest_recv_modifier,
677 &guest_recv_data, &guest_recv_eom, NULL);
678 test = TEST_HOST_ANSWER_DOOR_ADMIT;
684 void id_guest_created (void *cls, const char *emsg)
688 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
689 "Could not create guest identity: %s\n", emsg);
690 #if ! DEBUG_TEST_SOCIAL
695 GNUNET_IDENTITY_ego_lookup (cfg, guest_name, &id_guest_ego_cb, NULL);
699 void host_entered (void *cls, int result, uint64_t max_message_id)
701 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Host entered to place.\n");
703 GNUNET_IDENTITY_create (id, guest_name, &id_guest_created, NULL);
707 void id_host_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
709 GNUNET_assert (NULL != ego);
712 host_slicer = GNUNET_SOCIAL_slicer_create ();
713 GNUNET_SOCIAL_slicer_add (host_slicer, "",
714 &host_recv_method, &host_recv_modifier,
715 &host_recv_data, &host_recv_eom, NULL);
717 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Entering to place as host.\n");
718 hst = GNUNET_SOCIAL_host_enter (cfg, host_ego, place_key,
719 GNUNET_PSYC_CHANNEL_PRIVATE, host_slicer,
720 &host_entered, &host_answer_door,
721 &host_farewell, NULL);
725 void id_host_created (void *cls, const char *emsg)
729 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
730 "Could not create host identity: %s\n", emsg);
731 #if ! DEBUG_TEST_SOCIAL
736 GNUNET_IDENTITY_ego_lookup (cfg, host_name, &id_host_ego_cb, NULL);
740 void identity_ego_cb (void *cls, struct GNUNET_IDENTITY_Ego *ego,
741 void **ctx, const char *name)
748 core_connected (void *cls, const struct GNUNET_PeerIdentity *my_identity)
750 this_peer = *my_identity;
752 id = GNUNET_IDENTITY_connect (cfg, &identity_ego_cb, NULL);
753 GNUNET_IDENTITY_create (id, host_name, &id_host_created, NULL);
758 * Main function of the test, run from scheduler.
761 * @param cfg configuration we use (also to connect to Social service)
762 * @param peer handle to access more of the peer (not used)
765 #if DEBUG_TEST_SOCIAL
766 run (void *cls, char *const *args, const char *cfgfile,
767 const struct GNUNET_CONFIGURATION_Handle *c)
770 const struct GNUNET_CONFIGURATION_Handle *c,
771 struct GNUNET_TESTING_Peer *peer)
775 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
777 place_key = GNUNET_CRYPTO_eddsa_key_create ();
778 guest_key = GNUNET_CRYPTO_ecdsa_key_create ();
780 GNUNET_CRYPTO_eddsa_key_get_public (place_key, &place_pub_key);
781 GNUNET_CRYPTO_ecdsa_key_get_public (guest_key, &guest_pub_key);
783 core = GNUNET_CORE_connect (cfg, NULL, &core_connected, NULL, NULL,
784 NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
789 main (int argc, char *argv[])
792 #if DEBUG_TEST_SOCIAL
793 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
794 GNUNET_GETOPT_OPTION_END
796 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-social",
797 "test-social [options]",
801 if (0 != GNUNET_TESTING_peer_run ("test-social", "test_social.conf", &run, NULL))
807 /* end of test_social.c */