2 * This file is part of GNUnet
3 * Copyright (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 struct GNUNET_SCHEDULER_Task * 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 != NULL)
201 GNUNET_SCHEDULER_cancel (end_badly_task);
202 end_badly_task = NULL;
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;
271 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
272 "The host has left the place.\n");
273 GNUNET_SOCIAL_slicer_destroy (host_slicer);
282 schedule_host_leave (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
284 test = TEST_HOST_LEAVE;
285 GNUNET_SOCIAL_host_leave (hst, GNUNET_NO, &host_left, NULL);
290 host_farewell (void *cls,
291 struct GNUNET_SOCIAL_Nym *nym,
292 struct GNUNET_ENV_Environment *env,
293 size_t variable_count,
294 struct GNUNET_ENV_Modifier *variables)
296 // FIXME: this function is not called yet
297 struct GNUNET_CRYPTO_EcdsaPublicKey *nym_key = GNUNET_SOCIAL_nym_get_key (nym);
300 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (nym_key);
301 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
302 "Nym %s has left the place.\n",
305 GNUNET_assert (0 == memcmp (&guest_pub_key, nym_key, sizeof (*nym_key)));
307 GNUNET_SCHEDULER_add_now (&schedule_host_leave, NULL);
312 guest_left (void *cls)
314 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
315 "The guest has left the place.\n");
316 GNUNET_SOCIAL_slicer_destroy (guest_slicer);
320 GNUNET_SCHEDULER_add_now (&schedule_host_leave, NULL);
327 test = TEST_GUEST_LEAVE;
328 /* FIXME test keep_active */
329 GNUNET_SOCIAL_guest_leave (gst, GNUNET_NO, &guest_left, NULL);
334 guest_recv_method (void *cls,
335 const struct GNUNET_PSYC_MessageMethod *meth,
338 const struct GNUNET_SOCIAL_Nym *nym,
339 const char *method_name)
341 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
342 "Test #%u: Guest received method for message ID %" PRIu64 ":\n"
344 test, message_id, method_name);
345 /* FIXME: check message */
350 guest_recv_modifier (void *cls,
351 const struct GNUNET_PSYC_MessageModifier *mod,
353 enum GNUNET_ENV_Operator oper,
358 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
359 "Test #%u: Guest received modifier for message ID %" PRIu64 ":\n"
361 test, message_id, oper, name, value_size, value);
366 guest_recv_data (void *cls,
367 const struct GNUNET_MessageHeader *msg,
369 uint64_t data_offset,
373 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
374 "Test #%u: Guest received data for message ID %" PRIu64 ":\n"
376 test, message_id, data_size, data);
381 guest_recv_eom (void *cls,
382 const struct GNUNET_MessageHeader *msg,
386 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
387 "Test #%u: Guest received end of message ID %" PRIu64
389 test, message_id, cancelled);
393 case TEST_HOST_ANNOUNCE:
394 test = TEST_HOST_ANNOUNCE_END;
397 case TEST_HOST_ANNOUNCE_END:
408 host_recv_method (void *cls,
409 const struct GNUNET_PSYC_MessageMethod *meth,
412 const struct GNUNET_SOCIAL_Nym *nym,
413 const char *method_name)
415 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
416 "Test #%u: Host received method for message ID %" PRIu64 ":\n"
418 test, message_id, method_name);
419 /* FIXME: check message */
424 host_recv_modifier (void *cls,
425 const struct GNUNET_PSYC_MessageModifier *mod,
427 enum GNUNET_ENV_Operator oper,
432 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
433 "Test #%u: Host received modifier for message ID %" PRIu64 ":\n"
435 test, message_id, oper, name, value_size, value);
440 host_recv_data (void *cls,
441 const struct GNUNET_MessageHeader *msg,
443 uint64_t data_offset,
447 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
448 "Test #%u: Host received data for message ID %" PRIu64 ":\n"
450 test, message_id, data_size, data);
455 host_recv_eom (void *cls,
456 const struct GNUNET_MessageHeader *msg,
460 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
461 "Test #%u: Host received end of message ID %" PRIu64
463 test, message_id, cancelled);
467 case TEST_HOST_ANNOUNCE:
468 test = TEST_HOST_ANNOUNCE_END;
472 case TEST_HOST_ANNOUNCE_END:
476 case TEST_GUEST_TALK:
489 test = TEST_GUEST_TALK;
491 tmit = (struct TransmitClosure) {};
492 tmit.env = GNUNET_ENV_environment_create ();
493 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
494 "_bar_foo", DATA2ARG ("one two three"));
495 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
496 "_bar_baz", DATA2ARG ("four five"));
497 tmit.data[0] = "zzz xxx yyy";
498 tmit.data[1] = "zyx wvu tsr qpo";
499 tmit.data[2] = "testing ten nine eight";
503 = GNUNET_SOCIAL_guest_talk (gst, "_message_guest", tmit.env,
505 GNUNET_SOCIAL_TALK_NONE);
512 test = TEST_HOST_ANNOUNCE;
514 tmit = (struct TransmitClosure) {};
515 tmit.env = GNUNET_ENV_environment_create ();
516 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
517 "_foo", DATA2ARG ("bar baz"));
518 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
519 "_foo_bar", DATA2ARG ("foo bar"));
520 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
521 "_foo_bar_baz", DATA2ARG ("foo bar baz"));
522 tmit.data[0] = "aaa bbb ccc";
523 tmit.data[1] = "abc def ghi jkl";
524 tmit.data[2] = "testing one two three";
525 tmit.data[3] = "four five";
529 = GNUNET_SOCIAL_host_announce (hst, "_message_host", tmit.env,
531 GNUNET_SOCIAL_ANNOUNCE_NONE);
538 test = TEST_HOST_ANNOUNCE;
540 tmit = (struct TransmitClosure) {};
541 tmit.env = GNUNET_ENV_environment_create ();
542 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
543 "_foo2", DATA2ARG ("BAR BAZ"));
544 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
545 "_foo2_bar", DATA2ARG ("FOO BAR"));
546 GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
547 "_foo2_bar", DATA2ARG ("FOO BAR BAZ"));
548 tmit.data[0] = "AAA BBB CCC";
549 tmit.data[1] = "ABC DEF GHI JKL";
550 tmit.data[2] = "TESTING ONE TWO THREE";
554 = GNUNET_SOCIAL_host_announce (hst, "_message_host_two", tmit.env,
556 GNUNET_SOCIAL_ANNOUNCE_NONE);
561 guest_recv_entry_decision (void *cls,
563 const struct GNUNET_PSYC_Message *entry_resp)
565 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
566 "Guest received entry decision (try %u): %d.\n",
567 join_req_count, is_admitted);
569 if (NULL != entry_resp)
571 struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
572 const char *method_name = NULL;
573 const void *data = NULL;
574 uint16_t data_size = 0;
575 GNUNET_PSYC_message_parse (entry_resp, &method_name, env, &data, &data_size);
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
579 method_name, data_size, data);
580 /* FIXME: check response message */
585 case TEST_GUEST_RECV_ENTRY_DCSN_REFUSE:
586 GNUNET_assert (GNUNET_NO == is_admitted);
590 case TEST_GUEST_RECV_ENTRY_DCSN_ADMIT:
591 GNUNET_assert (GNUNET_YES == is_admitted);
602 host_answer_door (void *cls,
603 struct GNUNET_SOCIAL_Nym *nym,
604 const char *method_name,
605 struct GNUNET_ENV_Environment *env,
611 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
612 "Host received entry request from guest (try %u).\n",
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 method_name, data_size, data);
620 case TEST_HOST_ANSWER_DOOR_REFUSE:
621 test = TEST_GUEST_RECV_ENTRY_DCSN_REFUSE;
622 join_resp = GNUNET_PSYC_message_create ("_refuse_nym", env,
623 DATA2ARG ("Go away!"));
624 GNUNET_SOCIAL_host_entry_decision (hst, nym, GNUNET_NO, join_resp);
627 case TEST_HOST_ANSWER_DOOR_ADMIT:
628 test = TEST_GUEST_RECV_ENTRY_DCSN_ADMIT;
629 join_resp = GNUNET_PSYC_message_create ("_admit_nym", env,
630 DATA2ARG ("Welcome, nym!"));
631 GNUNET_SOCIAL_host_entry_decision (hst, nym, GNUNET_YES, join_resp);
641 guest_recv_local_enter (void *cls, int result, uint64_t max_message_id)
643 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Guest entered to local place.\n");
651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Entering to place as guest.\n");
653 struct GuestEnterMessage *emsg = &guest_enter_msg;
655 emsg->method_name = "_request_enter";
656 emsg->env = GNUNET_ENV_environment_create ();
657 GNUNET_ENV_environment_add (emsg->env, GNUNET_ENV_OP_ASSIGN,
658 "_abc", "abc def", 7);
659 GNUNET_ENV_environment_add (emsg->env, GNUNET_ENV_OP_ASSIGN,
660 "_abc_def", "abc def ghi", 11);
661 emsg->data = "let me in";
662 emsg->data_size = strlen (emsg->data) + 1;
663 emsg->msg = GNUNET_PSYC_message_create (emsg->method_name, emsg->env,
664 emsg->data, emsg->data_size);
666 gst = GNUNET_SOCIAL_guest_enter (cfg, guest_ego, &place_pub_key,
667 &this_peer, 0, NULL, emsg->msg,
668 guest_slicer, &guest_recv_local_enter,
669 &guest_recv_entry_decision, NULL);
674 id_guest_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
676 GNUNET_assert (NULL != ego);
679 guest_slicer = GNUNET_SOCIAL_slicer_create ();
680 GNUNET_SOCIAL_slicer_add (guest_slicer, "",
681 &guest_recv_method, &guest_recv_modifier,
682 &guest_recv_data, &guest_recv_eom, NULL);
683 test = TEST_HOST_ANSWER_DOOR_ADMIT;
690 id_guest_created (void *cls, const char *emsg)
694 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
695 "Could not create guest identity: %s\n", emsg);
696 #if ! DEBUG_TEST_SOCIAL
701 GNUNET_IDENTITY_ego_lookup (cfg, guest_name, &id_guest_ego_cb, NULL);
706 host_entered (void *cls, int result, uint64_t max_message_id)
708 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Host entered to place.\n");
710 GNUNET_IDENTITY_create (id, guest_name, &id_guest_created, NULL);
715 id_host_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
717 GNUNET_assert (NULL != ego);
720 host_slicer = GNUNET_SOCIAL_slicer_create ();
721 GNUNET_SOCIAL_slicer_add (host_slicer, "",
722 &host_recv_method, &host_recv_modifier,
723 &host_recv_data, &host_recv_eom, NULL);
725 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Entering to place as host.\n");
726 hst = GNUNET_SOCIAL_host_enter (cfg, host_ego, place_key,
727 GNUNET_PSYC_CHANNEL_PRIVATE, host_slicer,
728 &host_entered, &host_answer_door,
729 &host_farewell, NULL);
734 id_host_created (void *cls, const char *emsg)
738 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
739 "Could not create host identity: %s\n", emsg);
740 #if ! DEBUG_TEST_SOCIAL
745 GNUNET_IDENTITY_ego_lookup (cfg, host_name, &id_host_ego_cb, NULL);
750 identity_ego_cb (void *cls, struct GNUNET_IDENTITY_Ego *ego,
751 void **ctx, const char *name)
758 core_connected (void *cls, const struct GNUNET_PeerIdentity *my_identity)
760 this_peer = *my_identity;
762 id = GNUNET_IDENTITY_connect (cfg, &identity_ego_cb, NULL);
763 GNUNET_IDENTITY_create (id, host_name, &id_host_created, NULL);
768 * Main function of the test, run from scheduler.
771 * @param cfg configuration we use (also to connect to Social service)
772 * @param peer handle to access more of the peer (not used)
775 #if DEBUG_TEST_SOCIAL
776 run (void *cls, char *const *args, const char *cfgfile,
777 const struct GNUNET_CONFIGURATION_Handle *c)
780 const struct GNUNET_CONFIGURATION_Handle *c,
781 struct GNUNET_TESTING_Peer *peer)
785 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
787 place_key = GNUNET_CRYPTO_eddsa_key_create ();
788 guest_key = GNUNET_CRYPTO_ecdsa_key_create ();
790 GNUNET_CRYPTO_eddsa_key_get_public (place_key, &place_pub_key);
791 GNUNET_CRYPTO_ecdsa_key_get_public (guest_key, &guest_pub_key);
793 core = GNUNET_CORE_connect (cfg, NULL, &core_connected, NULL, NULL,
794 NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
799 main (int argc, char *argv[])
802 #if DEBUG_TEST_SOCIAL
803 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
804 GNUNET_GETOPT_OPTION_END
806 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-social",
807 "test-social [options]",
811 if (0 != GNUNET_TESTING_peer_run ("test-social", "test_social.conf", &run, NULL))
817 /* end of test_social.c */