2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, 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_psyc_util_lib.h"
34 #include "gnunet_social_service.h"
35 #include "gnunet_identity_service.h"
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
39 #define DATA2ARG(data) data, sizeof (data)
42 * Return value from 'main'.
46 struct GNUNET_SOCIAL_App *app;
47 const char *app_id = "test";
50 * Handle for task for timeout termination.
52 struct GNUNET_SCHEDULER_Task *end_badly_task;
54 const struct GNUNET_CONFIGURATION_Handle *cfg;
56 struct GNUNET_PeerIdentity this_peer;
58 struct GNUNET_IDENTITY_Handle *id;
60 const struct GNUNET_IDENTITY_Ego *identity_host_ego;
61 const struct GNUNET_IDENTITY_Ego *identity_guest_ego;
63 const struct GNUNET_SOCIAL_Ego *host_ego;
64 const struct GNUNET_SOCIAL_Ego *guest_ego;
66 const char *host_name = "Host One";
67 const char *guest_name = "Guest One";
69 struct GNUNET_CRYPTO_EddsaPrivateKey *place_key;
70 struct GNUNET_CRYPTO_EcdsaPrivateKey *guest_key;
72 struct GNUNET_CRYPTO_EddsaPublicKey place_pub_key;
73 struct GNUNET_HashCode place_pub_hash;
75 const struct GNUNET_CRYPTO_EcdsaPublicKey *guest_pub_key;
76 const struct GNUNET_CRYPTO_EcdsaPublicKey *host_pub_key;
78 struct GNUNET_PSYC_Slicer *host_slicer;
79 struct GNUNET_PSYC_Slicer *guest_slicer;
81 struct GNUNET_SOCIAL_Host *hst;
82 struct GNUNET_SOCIAL_Guest *gst;
84 struct GNUNET_SOCIAL_Place *hst_plc;
85 struct GNUNET_SOCIAL_Place *gst_plc;
87 struct GNUNET_SOCIAL_Nym *nym_eject;
89 struct GuestEnterMessage
91 struct GNUNET_PSYC_Message *msg;
92 const char *method_name;
93 struct GNUNET_PSYC_Environment *env;
98 struct TransmitClosure
100 struct GNUNET_SOCIAL_Announcement *host_ann;
101 struct GNUNET_SOCIAL_TalkRequest *guest_talk;
102 struct GNUNET_PSYC_Environment *env;
104 uint8_t data_delay[16];
110 struct ResultClosure {
114 uint8_t join_req_count;
115 struct GNUNET_PSYC_Message *join_resp;
119 uint8_t is_guest_nym_added = GNUNET_NO;
120 uint8_t is_host_reconnected = GNUNET_NO;
121 uint8_t is_guest_reconnected = GNUNET_NO;
126 TEST_IDENTITIES_CREATE = 1,
128 TEST_GUEST_ENTER = 3,
129 TEST_HOST_ANSWER_DOOR_REFUSE = 4,
130 TEST_GUEST_RECV_ENTRY_DCSN_REFUSE = 5,
131 TEST_HOST_ANSWER_DOOR_ADMIT = 6,
132 TEST_GUEST_RECV_ENTRY_DCSN_ADMIT = 7,
133 TEST_HOST_ANNOUNCE = 8,
134 TEST_HOST_ANNOUNCE_END = 9,
135 TEST_GUEST_TALK = 10,
136 TEST_HOST_ANNOUNCE2 = 11,
137 TEST_HOST_ANNOUNCE2_END = 12,
138 TEST_GUEST_HISTORY_REPLAY = 13,
139 TEST_GUEST_HISTORY_REPLAY_LATEST = 14,
140 TEST_GUEST_LOOK_AT = 15,
141 TEST_GUEST_LOOK_FOR = 16,
142 TEST_GUEST_LEAVE = 17,
143 TEST_ZONE_ADD_PLACE = 18,
144 TEST_GUEST_ENTER_BY_NAME = 19,
146 TEST_GUEST_LEAVE2 = 21,
147 TEST_HOST_LEAVE = 22,
152 schedule_guest_leave (void *cls);
156 host_answer_door (void *cls,
157 struct GNUNET_SOCIAL_Nym *nym,
158 const char *method_name,
159 struct GNUNET_PSYC_Environment *env,
173 guest_enter_by_name ();
183 * Clean up all resources used.
190 GNUNET_IDENTITY_disconnect (id);
194 if (NULL != guest_slicer)
196 GNUNET_PSYC_slicer_destroy (guest_slicer);
200 if (NULL != host_slicer)
202 GNUNET_PSYC_slicer_destroy (host_slicer);
208 GNUNET_SOCIAL_guest_leave (gst, NULL, NULL, NULL);
214 GNUNET_SOCIAL_host_leave (hst, NULL, NULL, NULL);
218 GNUNET_SOCIAL_app_disconnect (app, NULL, NULL);
219 GNUNET_SCHEDULER_shutdown ();
224 * Terminate the test case (failure).
229 end_badly (void *cls)
233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n");
238 * Terminate the test case (success).
243 end_normally (void *cls)
247 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Test PASSED.\n");
252 * Finish the test case (successfully).
257 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
258 "Test #%u: Ending tests.\n", test);
260 if (end_badly_task != NULL)
262 GNUNET_SCHEDULER_cancel (end_badly_task);
263 end_badly_task = NULL;
265 GNUNET_SCHEDULER_add_now (&end_normally, NULL);
270 transmit_resume (void *cls)
272 struct TransmitClosure *tmit = cls;
274 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
275 "Test #%u: Transmission resumed.\n", test);
276 if (NULL != tmit->host_ann)
277 GNUNET_SOCIAL_host_announce_resume (tmit->host_ann);
279 GNUNET_SOCIAL_guest_talk_resume (tmit->guest_talk);
284 notify_data (void *cls, uint16_t *data_size, void *data)
286 struct TransmitClosure *tmit = cls;
287 if (NULL != tmit->env)
289 GNUNET_PSYC_env_destroy (tmit->env);
292 if (0 == tmit->data_count)
298 uint16_t size = strlen (tmit->data[tmit->n]);
299 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
300 "Test #%u: Transmit notify data: %u bytes available, "
301 "processing fragment %u/%u (size %u).\n",
302 test, *data_size, tmit->n + 1, tmit->data_count, size);
303 if (*data_size < size)
307 return GNUNET_SYSERR;
310 if (GNUNET_YES != tmit->paused && 0 < tmit->data_delay[tmit->n])
312 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
313 "Test #%u: Transmission paused.\n", test);
314 tmit->paused = GNUNET_YES;
315 GNUNET_SCHEDULER_add_delayed (
316 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
317 tmit->data_delay[tmit->n]),
318 &transmit_resume, tmit);
322 tmit->paused = GNUNET_NO;
325 GNUNET_memcpy (data, tmit->data[tmit->n], size);
327 return ++tmit->n < tmit->data_count ? GNUNET_NO : GNUNET_YES;
334 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
335 "Test #%u: The host has left the place.\n", test);
341 schedule_host_leave (void *cls)
343 test = TEST_HOST_LEAVE;
344 GNUNET_SOCIAL_host_leave (hst, NULL, &host_left, NULL);
351 host_farewell2 (void *cls,
352 const struct GNUNET_SOCIAL_Nym *nym,
353 struct GNUNET_PSYC_Environment *env)
355 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
356 "Nym left the place again.\n");
357 GNUNET_SCHEDULER_add_now (&schedule_host_leave, NULL);
362 host_reconnected (void *cls, int result,
363 const struct GNUNET_CRYPTO_EddsaPublicKey *home_pub_key,
364 uint64_t max_message_id)
366 place_pub_key = *home_pub_key;
367 GNUNET_CRYPTO_hash (&place_pub_key, sizeof (place_pub_key), &place_pub_hash);
368 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
369 "Test #%u: Host reconnected to place %s\n",
370 test, GNUNET_h2s (&place_pub_hash));
372 is_host_reconnected = GNUNET_YES;
373 if (GNUNET_YES == is_guest_reconnected)
375 GNUNET_SCHEDULER_add_now (&schedule_guest_leave, NULL);
381 guest_reconnected (void *cls, int result,
382 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
383 uint64_t max_message_id)
385 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
386 "Test #%u: Guest reconnected to place: %d\n",
388 GNUNET_assert (0 <= result);
390 is_guest_reconnected = GNUNET_YES;
391 if (GNUNET_YES == is_host_reconnected)
393 GNUNET_SCHEDULER_add_now (&schedule_guest_leave, NULL);
399 app_connected (void *cls)
401 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
402 "Test #%u: App connected: %p\n", test, cls);
407 app_recv_host (void *cls,
408 struct GNUNET_SOCIAL_HostConnection *hconn,
409 struct GNUNET_SOCIAL_Ego *ego,
410 const struct GNUNET_CRYPTO_EddsaPublicKey *host_pub_key,
411 enum GNUNET_SOCIAL_AppPlaceState place_state)
413 struct GNUNET_HashCode host_pub_hash;
414 GNUNET_CRYPTO_hash (host_pub_key, sizeof (*host_pub_key), &host_pub_hash);
416 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
417 "Test #%u: Got app host place notification: %s\n",
418 test, GNUNET_h2s (&host_pub_hash));
420 if (test == TEST_RECONNECT)
422 if (0 == memcmp (&place_pub_key, host_pub_key, sizeof (*host_pub_key)))
424 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
425 "Test #%u: Reconnecting to host place: %s\n",
426 test, GNUNET_h2s (&host_pub_hash));
427 hst = GNUNET_SOCIAL_host_enter_reconnect (hconn, host_slicer, host_reconnected,
428 host_answer_door, host_farewell2, NULL);
435 app_recv_guest (void *cls,
436 struct GNUNET_SOCIAL_GuestConnection *gconn,
437 struct GNUNET_SOCIAL_Ego *ego,
438 const struct GNUNET_CRYPTO_EddsaPublicKey *guest_pub_key,
439 enum GNUNET_SOCIAL_AppPlaceState place_state)
441 struct GNUNET_HashCode guest_pub_hash;
442 GNUNET_CRYPTO_hash (guest_pub_key, sizeof (*guest_pub_key), &guest_pub_hash);
444 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
445 "Test #%u: Got app guest place notification: %s\n",
446 test, GNUNET_h2s (&guest_pub_hash));
448 if (test == TEST_RECONNECT)
450 if (0 == memcmp (&place_pub_key, guest_pub_key, sizeof (*guest_pub_key)))
452 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
453 "Test #%u: Reconnecting to guest place: %s\n",
454 test, GNUNET_h2s (&guest_pub_hash));
455 gst = GNUNET_SOCIAL_guest_enter_reconnect (gconn, GNUNET_PSYC_SLAVE_JOIN_NONE,
456 guest_slicer, guest_reconnected, NULL);
465 if (NULL == host_ego || NULL == guest_ego)
475 app_recv_ego (void *cls,
476 struct GNUNET_SOCIAL_Ego *ego,
477 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
480 char *ego_pub_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (ego_pub_key);
481 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
482 "Test #%u: Got app ego notification: %p %s %s\n",
483 test, ego, name, ego_pub_str);
484 GNUNET_free (ego_pub_str);
486 if (NULL != strstr (name, host_name))
489 host_pub_key = ego_pub_key;
490 if (TEST_IDENTITIES_CREATE == test)
496 GNUNET_assert (TEST_RECONNECT == test);
499 else if (NULL != strstr (name, guest_name))
502 guest_pub_key = ego_pub_key;
503 if (TEST_IDENTITIES_CREATE == test)
509 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
512 GNUNET_assert (TEST_RECONNECT == test);
519 schedule_reconnect (void *cls)
521 test = TEST_RECONNECT;
523 GNUNET_SOCIAL_host_disconnect (hst, NULL, NULL);
524 GNUNET_SOCIAL_guest_disconnect (gst, NULL, NULL);
528 GNUNET_SOCIAL_app_disconnect (app, NULL, NULL);
529 app = GNUNET_SOCIAL_app_connect (cfg, app_id,
539 host_recv_zone_add_place_result (void *cls, int64_t result,
540 const void *data, uint16_t data_size)
542 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
543 "Test #%u: Zone add place result: %" PRId64 " (%.*s).\n",
544 test, result, data_size, (const char *) data);
545 GNUNET_assert (GNUNET_YES == result);
547 GNUNET_assert (GNUNET_YES == is_guest_nym_added);
548 guest_enter_by_name ();
555 test = TEST_ZONE_ADD_PLACE;
556 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
557 "Test #%u: Adding place to zone.\n", test);
559 GNUNET_SOCIAL_zone_add_place (app, host_ego, "home", "let.me*in!",
560 &place_pub_key, &this_peer, 1, &this_peer,
561 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES),
562 host_recv_zone_add_place_result, app);
567 host_farewell (void *cls,
568 const struct GNUNET_SOCIAL_Nym *nym,
569 struct GNUNET_PSYC_Environment *env)
571 const struct GNUNET_CRYPTO_EcdsaPublicKey *
572 nym_key = GNUNET_SOCIAL_nym_get_pub_key (nym);
574 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (nym_key);
575 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
576 "Test #%u: Farewell: nym %s (%s) has left the place.\n",
577 test, GNUNET_h2s (GNUNET_SOCIAL_nym_get_pub_key_hash (nym)), str);
579 GNUNET_assert (1 == GNUNET_PSYC_env_get_count (env));
580 if (0 != memcmp (guest_pub_key, nym_key, sizeof (*nym_key)))
582 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (guest_pub_key);
583 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
584 "Test #%u: Farewell: nym does not match guest: %s\n",
594 guest_left (void *cls)
596 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
597 "Test #%u: The guest has left the place.\n", test);
604 if (test < TEST_RECONNECT)
605 test = TEST_GUEST_LEAVE;
607 test = TEST_GUEST_LEAVE2;
609 struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
610 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
611 "_notice_place_leave", DATA2ARG ("Leaving."));
612 GNUNET_SOCIAL_guest_leave (gst, env, &guest_left, NULL);
613 GNUNET_PSYC_env_destroy (env);
620 schedule_guest_leave (void *cls)
627 guest_look_for_result (void *cls,
632 struct ResultClosure *rcls = cls;
633 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
634 "Test #%u: guest_look_for_result: %" PRId64 "\n",
636 GNUNET_assert (GNUNET_OK == result_code);
637 GNUNET_assert (6 == rcls->n);
639 GNUNET_SCHEDULER_add_now (&schedule_guest_leave, NULL);
644 guest_look_for_var (void *cls,
645 const struct GNUNET_MessageHeader *mod,
649 uint32_t full_value_size)
651 struct ResultClosure *rcls = cls;
653 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
654 "Test #%u: guest_look_for_var: %s\n%.*s\n",
655 test, name, value_size, (const char *) value);
662 test = TEST_GUEST_LOOK_FOR;
663 struct ResultClosure *rcls = GNUNET_malloc (sizeof (*rcls));
664 GNUNET_SOCIAL_place_look_for (gst_plc, "_foo", guest_look_for_var, guest_look_for_result, rcls);
669 guest_look_at_result (void *cls, int64_t result_code,
670 const void *data, uint16_t data_size)
672 struct ResultClosure *rcls = cls;
674 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
675 "Test #%u: guest_look_at_result: %" PRId64 "\n",
677 GNUNET_assert (GNUNET_OK == result_code);
678 GNUNET_assert (1 == rcls->n);
685 guest_look_at_var (void *cls,
686 const struct GNUNET_MessageHeader *mod,
690 uint32_t full_value_size)
692 struct ResultClosure *rcls = cls;
695 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
696 "Test #%u: guest_look_at_var: %s\n%.*s\n",
697 test ,name, value_size, (const char *) value);
704 test = TEST_GUEST_LOOK_AT;
705 struct ResultClosure *rcls = GNUNET_malloc (sizeof (*rcls));
706 GNUNET_SOCIAL_place_look_at (gst_plc, "_foo_bar", guest_look_at_var, guest_look_at_result, rcls);
711 guest_recv_history_replay_latest_result (void *cls, int64_t result,
712 const void *data, uint16_t data_size)
714 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
715 "Test #%u: Guest received latest history replay result "
716 "(%" PRIu32 " messages, %" PRId64 " fragments):\n"
718 test, counter, result, data_size, (const char *) data);
719 //GNUNET_assert (2 == counter); /* message count */
720 //GNUNET_assert (7 == result); /* fragment count */
727 guest_history_replay_latest ()
729 test = TEST_GUEST_HISTORY_REPLAY_LATEST;
731 GNUNET_SOCIAL_place_history_replay_latest (gst_plc, 3, "",
732 GNUNET_PSYC_HISTORY_REPLAY_LOCAL,
734 &guest_recv_history_replay_latest_result,
740 guest_recv_history_replay_result (void *cls, int64_t result,
741 const void *data, uint16_t data_size)
743 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
744 "Test #%u: Guest received history replay result: %" PRId64 "\n"
746 test, result, data_size, (const char *) data);
747 // GNUNET_assert (2 == counter); /* message count */
748 // GNUNET_assert (7 == result); /* fragment count */
750 guest_history_replay_latest ();
755 guest_history_replay ()
757 test = TEST_GUEST_HISTORY_REPLAY;
759 GNUNET_SOCIAL_place_history_replay (gst_plc, 1, 3, "",
760 GNUNET_PSYC_HISTORY_REPLAY_LOCAL,
762 &guest_recv_history_replay_result,
768 guest_recv_method (void *cls,
769 const struct GNUNET_PSYC_MessageHeader *msg,
770 const struct GNUNET_PSYC_MessageMethod *meth,
772 const char *method_name)
774 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
775 "Test #%u: Guest received method for message ID %" PRIu64 ":\n"
777 test, message_id, method_name, ntohl (meth->flags));
778 /** @todo FIXME: check message */
783 guest_recv_modifier (void *cls,
784 const struct GNUNET_PSYC_MessageHeader *msg,
785 const struct GNUNET_MessageHeader *pmsg,
787 enum GNUNET_PSYC_Operator oper,
791 uint16_t full_value_size)
793 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
794 "Test #%u: Guest received modifier for message ID %" PRIu64 ":\n"
795 "%c%s: %.*s (size: %u)\n",
796 test, message_id, oper, name, value_size, (const char *) value, value_size);
797 /** @todo FIXME: check modifier */
801 guest_recv_mod_foo_bar (void *cls,
802 const struct GNUNET_PSYC_MessageHeader *msg,
803 const struct GNUNET_MessageHeader *pmsg,
805 enum GNUNET_PSYC_Operator oper,
809 uint16_t full_value_size)
811 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
812 "Test #%u: Guest received modifier matching _foo_bar for message ID %" PRIu64 ":\n"
813 "%c%s: %.*s (size: %u)\n",
814 test, message_id, oper, name, value_size, (const char *) value, value_size);
815 struct ResultClosure *rc = cls;
817 /** @todo FIXME: check modifier */
822 guest_recv_data (void *cls,
823 const struct GNUNET_PSYC_MessageHeader *msg,
824 const struct GNUNET_MessageHeader *pmsg,
829 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
830 "Test #%u: Guest received data for message ID %" PRIu64 ":\n"
832 test, message_id, data_size, (const char *) data);
833 /** @todo FIXME: check data */
838 guest_recv_eom (void *cls,
839 const struct GNUNET_PSYC_MessageHeader *msg,
840 const struct GNUNET_MessageHeader *pmsg,
842 uint8_t is_cancelled)
844 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
845 "Test #%u: Guest received end of message ID %" PRIu64
847 test, message_id, is_cancelled);
851 case TEST_HOST_ANNOUNCE:
852 test = TEST_HOST_ANNOUNCE_END;
855 case TEST_HOST_ANNOUNCE_END:
859 case TEST_HOST_ANNOUNCE2:
860 test = TEST_HOST_ANNOUNCE2_END;
863 case TEST_HOST_ANNOUNCE2_END:
864 guest_history_replay ();
867 case TEST_GUEST_HISTORY_REPLAY:
868 case TEST_GUEST_HISTORY_REPLAY_LATEST:
873 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "invalid test: %d\n", test);
880 host_recv_method (void *cls,
881 const struct GNUNET_PSYC_MessageHeader *msg,
882 const struct GNUNET_PSYC_MessageMethod *meth,
884 const char *method_name)
886 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
887 "Test #%u: Host received method for message ID %" PRIu64 ":\n"
889 test, message_id, method_name);
890 /** @todo FIXME: check message */
895 host_recv_modifier (void *cls,
896 const struct GNUNET_PSYC_MessageHeader *msg,
897 const struct GNUNET_MessageHeader *pmsg,
899 enum GNUNET_PSYC_Operator oper,
903 uint16_t full_value_size)
905 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
906 "Test #%u: Host received modifier for message ID %" PRIu64 ":\n"
908 test, message_id, oper, name, value_size, (const char *) value);
913 host_recv_data (void *cls,
914 const struct GNUNET_PSYC_MessageHeader *msg,
915 const struct GNUNET_MessageHeader *pmsg,
920 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
921 "Test #%u: Host received data for message ID %" PRIu64 ":\n"
923 test, message_id, data_size, (const char *) data);
928 host_recv_eom (void *cls,
929 const struct GNUNET_PSYC_MessageHeader *msg,
930 const struct GNUNET_MessageHeader *pmsg,
932 uint8_t is_cancelled)
934 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
935 "Test #%u: Host received end of message ID %" PRIu64
937 test, message_id, is_cancelled);
941 case TEST_HOST_ANNOUNCE:
942 test = TEST_HOST_ANNOUNCE_END;
945 case TEST_HOST_ANNOUNCE_END:
949 case TEST_HOST_ANNOUNCE2:
950 test = TEST_HOST_ANNOUNCE2_END;
953 case TEST_HOST_ANNOUNCE2_END:
954 guest_history_replay ();
957 case TEST_GUEST_TALK:
962 if (TEST_GUEST_LEAVE <= test)
964 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid test: #%u\n", test);
973 test = TEST_GUEST_TALK;
975 tmit = (struct TransmitClosure) {};
976 tmit.env = GNUNET_PSYC_env_create ();
977 GNUNET_PSYC_env_add (tmit.env, GNUNET_PSYC_OP_ASSIGN,
978 "_bar_foo", DATA2ARG ("one two three"));
979 GNUNET_PSYC_env_add (tmit.env, GNUNET_PSYC_OP_ASSIGN,
980 "_bar_baz", DATA2ARG ("four five"));
981 tmit.data[0] = "zzz xxx yyy ";
982 tmit.data[1] = "zyx wvu tsr qpo.\n";
983 tmit.data_delay[1] = 1;
984 tmit.data[2] = "testing ten nine eight.\n";
988 = GNUNET_SOCIAL_guest_talk (gst, "_converse_guest", tmit.env,
990 GNUNET_SOCIAL_TALK_NONE);
997 test = TEST_HOST_ANNOUNCE;
999 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1000 "Test #%u: Host announcement.\n", test);
1002 tmit = (struct TransmitClosure) {};
1003 tmit.env = GNUNET_PSYC_env_create ();
1004 GNUNET_PSYC_env_add (tmit.env, GNUNET_PSYC_OP_ASSIGN,
1005 "_foo", DATA2ARG ("bar baz"));
1006 GNUNET_PSYC_env_add (tmit.env, GNUNET_PSYC_OP_ASSIGN,
1007 "_foo_bar", DATA2ARG ("foo bar"));
1008 GNUNET_PSYC_env_add (tmit.env, GNUNET_PSYC_OP_ASSIGN,
1009 "_foo_bar_baz", DATA2ARG ("foo bar baz"));
1010 tmit.data[0] = "aaa bbb ccc ";
1011 tmit.data[1] = "abc def ghi jkl.\n";
1012 tmit.data_delay[1] = 1;
1013 tmit.data[2] = "testing one two three ";
1014 tmit.data[3] = "four five.\n";
1015 tmit.data_count = 4;
1018 = GNUNET_SOCIAL_host_announce (hst, "_converse_host", tmit.env,
1019 ¬ify_data, &tmit,
1020 GNUNET_SOCIAL_ANNOUNCE_NONE);
1027 GNUNET_assert (2 == mod_foo_bar_rcls.n);
1028 GNUNET_PSYC_slicer_modifier_remove (guest_slicer, "_foo_bar",
1029 guest_recv_mod_foo_bar);
1031 test = TEST_HOST_ANNOUNCE2;
1033 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1034 "Test #%u: Host announcement 2.\n", test);
1036 tmit = (struct TransmitClosure) {};
1037 tmit.env = GNUNET_PSYC_env_create ();
1038 GNUNET_PSYC_env_add (tmit.env, GNUNET_PSYC_OP_ASSIGN,
1039 "_foo2", DATA2ARG ("BAR BAZ"));
1040 GNUNET_PSYC_env_add (tmit.env, GNUNET_PSYC_OP_ASSIGN,
1041 "_foo2_bar", DATA2ARG ("FOO BAR"));
1042 GNUNET_PSYC_env_add (tmit.env, GNUNET_PSYC_OP_ASSIGN,
1043 "_foo2_bar_baz", DATA2ARG ("FOO BAR BAZ"));
1044 tmit.data[0] = "AAA BBB CCC ";
1045 tmit.data[1] = "ABC DEF GHI JKL.\n";
1046 tmit.data[2] = "TESTING ONE TWO THREE.\n";
1047 tmit.data_count = 3;
1050 = GNUNET_SOCIAL_host_announce (hst, "_converse_host_two", tmit.env,
1051 ¬ify_data, &tmit,
1052 GNUNET_SOCIAL_ANNOUNCE_NONE);
1057 guest_recv_entry_decision (void *cls,
1059 const struct GNUNET_PSYC_Message *entry_msg)
1061 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1062 "Test #%u: Guest received entry decision (try %u): %d.\n",
1063 test, join_req_count, is_admitted);
1065 if (NULL != entry_msg)
1067 struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
1068 const char *method_name = NULL;
1069 const void *data = NULL;
1070 uint16_t data_size = 0;
1071 struct GNUNET_PSYC_MessageHeader *
1072 pmsg = GNUNET_PSYC_message_header_create_from_psyc (entry_msg);
1073 GNUNET_PSYC_message_parse (pmsg, &method_name, env, &data, &data_size);
1076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1078 method_name, data_size, (const char *) data);
1079 /** @todo FIXME: check response message */
1084 case TEST_GUEST_RECV_ENTRY_DCSN_REFUSE:
1085 GNUNET_assert (GNUNET_NO == is_admitted);
1089 case TEST_GUEST_RECV_ENTRY_DCSN_ADMIT:
1090 GNUNET_assert (GNUNET_YES == is_admitted);
1094 case TEST_GUEST_ENTER_BY_NAME:
1095 GNUNET_SCHEDULER_add_now (&schedule_reconnect, NULL);
1099 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "invalid test: %d\n", test);
1106 host_answer_door (void *cls,
1107 struct GNUNET_SOCIAL_Nym *nym,
1108 const char *method_name,
1109 struct GNUNET_PSYC_Environment *env,
1115 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1116 "Test #%u: Host received entry request from guest (try %u).\n",
1117 (uint8_t) test, join_req_count);
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1120 method_name, (int) data_size, (const char *) data);
1124 case TEST_HOST_ANSWER_DOOR_REFUSE:
1125 test = TEST_GUEST_RECV_ENTRY_DCSN_REFUSE;
1126 join_resp = GNUNET_PSYC_message_create ("_notice_place_refuse", env,
1127 DATA2ARG ("Go away!"));
1128 GNUNET_SOCIAL_host_entry_decision (hst, nym, GNUNET_NO, join_resp);
1131 case TEST_HOST_ANSWER_DOOR_ADMIT:
1132 test = TEST_GUEST_RECV_ENTRY_DCSN_ADMIT;
1135 case TEST_GUEST_ENTER_BY_NAME:
1136 join_resp = GNUNET_PSYC_message_create ("_notice_place_admit", env,
1137 DATA2ARG ("Welcome, nym!"));
1138 GNUNET_SOCIAL_host_entry_decision (hst, nym, GNUNET_YES, join_resp);
1142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid test: #%u\n", test);
1149 guest_recv_local_enter (void *cls, int result,
1150 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1151 uint64_t max_message_id)
1153 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1154 "Test #%u: Guest entered local place: %d\n",
1156 GNUNET_assert (0 <= result);
1163 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1164 "Test #%u: Entering place as guest.\n", test);
1166 struct GuestEnterMessage *emsg = &guest_enter_msg;
1168 emsg->method_name = "_request_enter";
1169 emsg->env = GNUNET_PSYC_env_create ();
1170 GNUNET_PSYC_env_add (emsg->env, GNUNET_PSYC_OP_ASSIGN,
1171 "_abc", "abc def", 7);
1172 GNUNET_PSYC_env_add (emsg->env, GNUNET_PSYC_OP_ASSIGN,
1173 "_abc_def", "abc def ghi", 11);
1174 emsg->data = "let me in";
1175 emsg->data_size = strlen (emsg->data) + 1;
1176 emsg->msg = GNUNET_PSYC_message_create (emsg->method_name, emsg->env,
1177 emsg->data, emsg->data_size);
1179 gst = GNUNET_SOCIAL_guest_enter (app, guest_ego, &place_pub_key,
1180 GNUNET_PSYC_SLAVE_JOIN_NONE,
1181 &this_peer, 0, NULL, emsg->msg, guest_slicer,
1182 guest_recv_local_enter,
1183 guest_recv_entry_decision, NULL);
1184 gst_plc = GNUNET_SOCIAL_guest_get_place (gst);
1186 GNUNET_SOCIAL_place_msg_proc_set (gst_plc, "_converse",
1187 GNUNET_SOCIAL_MSG_PROC_SAVE);
1192 guest_enter_by_name ()
1194 test = TEST_GUEST_ENTER_BY_NAME;
1195 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1196 "Test #%u: Entering place by name as guest.\n", test);
1198 struct GuestEnterMessage *emsg = &guest_enter_msg;
1200 emsg->method_name = "_request_enter";
1201 emsg->env = GNUNET_PSYC_env_create ();
1202 GNUNET_PSYC_env_add (emsg->env, GNUNET_PSYC_OP_ASSIGN,
1203 "_abc", "abc def", 7);
1204 GNUNET_PSYC_env_add (emsg->env, GNUNET_PSYC_OP_ASSIGN,
1205 "_abc_def", "abc def ghi", 11);
1206 emsg->data = "let me in";
1207 emsg->data_size = strlen (emsg->data) + 1;
1208 emsg->msg = GNUNET_PSYC_message_create (emsg->method_name, emsg->env,
1209 emsg->data, emsg->data_size);
1211 gst = GNUNET_SOCIAL_guest_enter_by_name (app, guest_ego,
1212 "home.host.gnu", "let.me*in!",
1213 emsg->msg, guest_slicer,
1214 guest_recv_local_enter,
1215 guest_recv_entry_decision, NULL);
1216 gst_plc = GNUNET_SOCIAL_guest_get_place (gst);
1221 app_recv_zone_add_nym_result (void *cls, int64_t result,
1222 const void *data, uint16_t data_size)
1224 GNUNET_assert (GNUNET_YES == result);
1225 is_guest_nym_added = GNUNET_YES;
1232 guest_pub_key = GNUNET_SOCIAL_ego_get_pub_key (guest_ego);
1234 guest_slicer = GNUNET_PSYC_slicer_create ();
1235 GNUNET_PSYC_slicer_method_add (guest_slicer, "", NULL,
1236 guest_recv_method, guest_recv_modifier,
1237 guest_recv_data, guest_recv_eom, NULL);
1238 GNUNET_PSYC_slicer_modifier_add (guest_slicer, "_foo_bar",
1239 guest_recv_mod_foo_bar, &mod_foo_bar_rcls);
1240 test = TEST_HOST_ANSWER_DOOR_ADMIT;
1242 GNUNET_SOCIAL_zone_add_nym (app, guest_ego, "host", host_pub_key,
1243 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES),
1244 app_recv_zone_add_nym_result, NULL);
1249 id_host_created (void *cls, const char *emsg)
1253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1254 "Test #%u: Could not create host identity: %s\n",
1256 #if ! DEBUG_TEST_SOCIAL
1265 id_guest_created (void *cls, const char *emsg)
1269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1270 "Test #%u: Could not create guest identity: %s\n",
1272 #if ! DEBUG_TEST_SOCIAL
1276 //if (NULL != guest_ego)
1282 host_entered (void *cls, int result,
1283 const struct GNUNET_CRYPTO_EddsaPublicKey *home_pub_key,
1284 uint64_t max_message_id)
1286 place_pub_key = *home_pub_key;
1287 GNUNET_CRYPTO_hash (&place_pub_key, sizeof (place_pub_key), &place_pub_hash);
1288 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1289 "Test #%u: Host entered place %s\n",
1290 test, GNUNET_h2s (&place_pub_hash));
1298 host_slicer = GNUNET_PSYC_slicer_create ();
1299 GNUNET_PSYC_slicer_method_add (host_slicer, "", NULL,
1300 host_recv_method, host_recv_modifier,
1301 host_recv_data, host_recv_eom, NULL);
1303 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1304 "Test #%u: Entering place as host.\n", test);
1305 test = TEST_HOST_ENTER;
1306 hst = GNUNET_SOCIAL_host_enter (app, host_ego,
1307 GNUNET_PSYC_CHANNEL_PRIVATE,
1308 host_slicer, host_entered,
1309 host_answer_door, host_farewell, NULL);
1310 hst_plc = GNUNET_SOCIAL_host_get_place (hst);
1312 GNUNET_SOCIAL_place_msg_proc_set (hst_plc, "_converse",
1313 GNUNET_SOCIAL_MSG_PROC_RELAY);
1318 start_app_if_ready ()
1320 if (NULL == identity_host_ego || NULL == identity_guest_ego)
1324 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1325 "starting app...\n");
1326 app = GNUNET_SOCIAL_app_connect (cfg,
1337 identity_ego_cb (void *cls, struct GNUNET_IDENTITY_Ego *ego,
1338 void **ctx, const char *name)
1342 if (ego == identity_host_ego)
1344 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1345 "Host ego deleted\n");
1347 else if (ego == identity_guest_ego)
1349 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1350 "Guest ego deleted\n");
1352 else if (0 == strcmp (name, host_name))
1354 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1357 identity_host_ego = ego;
1358 start_app_if_ready ();
1360 else if (0 == strcmp (name, guest_name))
1362 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
1363 "Created guest ego %s\n",
1365 identity_guest_ego = ego;
1366 start_app_if_ready ();
1373 * Main function of the test, run from scheduler.
1376 * @param cfg configuration we use (also to connect to Social service)
1377 * @param peer handle to access more of the peer (not used)
1380 #if DEBUG_TEST_SOCIAL
1381 run (void *cls, char *const *args, const char *cfgfile,
1382 const struct GNUNET_CONFIGURATION_Handle *c)
1385 const struct GNUNET_CONFIGURATION_Handle *c,
1386 struct GNUNET_TESTING_Peer *peer)
1390 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1393 GNUNET_CRYPTO_get_peer_identity (cfg, &this_peer);
1395 id = GNUNET_IDENTITY_connect (cfg, &identity_ego_cb, NULL);
1397 test = TEST_IDENTITIES_CREATE;
1398 GNUNET_IDENTITY_create (id, host_name, &id_host_created, NULL);
1399 GNUNET_IDENTITY_create (id, guest_name, &id_guest_created, NULL);
1404 main (int argc, char *argv[])
1407 #if DEBUG_TEST_SOCIAL
1408 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
1409 GNUNET_GETOPT_OPTION_END
1411 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-social",
1412 "test-social [options]",
1416 if (0 != GNUNET_TESTING_peer_run ("test-social", "test_social.conf", &run, NULL))
1422 /* end of test_social.c */