-improve indentation, reduce duplication of PIDs in core's neighbour map
[oweals/gnunet.git] / src / social / test_social.c
1 /*
2  * This file is part of GNUnet
3  * Copyright (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., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 /**
21  * @file social/test_social.c
22  * @brief Tests for the Social API.
23  * @author Gabor X Toth
24  */
25
26 #include <inttypes.h>
27
28 #include "platform.h"
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"
38
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
40
41 #define DATA2ARG(data) data, sizeof (data)
42
43 /**
44  * Return value from 'main'.
45  */
46 int res;
47
48 /**
49  * Handle for task for timeout termination.
50  */
51 struct GNUNET_SCHEDULER_Task * end_badly_task;
52
53 const struct GNUNET_CONFIGURATION_Handle *cfg;
54
55 struct GNUNET_CORE_Handle *core;
56 struct GNUNET_PeerIdentity this_peer;
57
58 struct GNUNET_IDENTITY_Handle *id;
59
60 const struct GNUNET_IDENTITY_Ego *host_ego;
61 const struct GNUNET_IDENTITY_Ego *guest_ego;
62
63 const char *host_name = "Host One";
64 const char *guest_name = "Guest One";
65
66 struct GNUNET_CRYPTO_EddsaPrivateKey *place_key;
67 struct GNUNET_CRYPTO_EcdsaPrivateKey *guest_key;
68
69 struct GNUNET_CRYPTO_EddsaPublicKey place_pub_key;
70 struct GNUNET_CRYPTO_EcdsaPublicKey guest_pub_key;
71
72 struct GNUNET_SOCIAL_Slicer *host_slicer;
73 struct GNUNET_SOCIAL_Slicer *guest_slicer;
74
75 struct GNUNET_SOCIAL_Host *hst;
76 struct GNUNET_SOCIAL_Guest *gst;
77
78 struct GNUNET_SOCIAL_Place *hst_plc;
79 struct GNUNET_SOCIAL_Place *gst_plc;
80
81 struct GuestEnterMessage
82 {
83   struct GNUNET_PSYC_Message *msg;
84   const char *method_name;
85   struct GNUNET_ENV_Environment *env;
86   void *data;
87   uint16_t data_size;
88 } guest_enter_msg;
89
90 struct TransmitClosure
91 {
92   struct GNUNET_SOCIAL_Announcement *host_ann;
93   struct GNUNET_SOCIAL_TalkRequest *guest_talk;
94   struct GNUNET_ENV_Environment *env;
95   char *data[16];
96   uint8_t data_delay[16];
97   uint8_t data_count;
98   uint8_t paused;
99   uint8_t n;
100 } tmit;
101
102 uint8_t join_req_count;
103 struct GNUNET_PSYC_Message *join_resp;
104
105 uint32_t counter;
106
107 enum
108 {
109   TEST_NONE = 0,
110   TEST_HOST_ANSWER_DOOR_REFUSE      = 1,
111   TEST_GUEST_RECV_ENTRY_DCSN_REFUSE = 2,
112   TEST_HOST_ANSWER_DOOR_ADMIT       = 3,
113   TEST_GUEST_RECV_ENTRY_DCSN_ADMIT  = 4,
114   TEST_HOST_ANNOUNCE                = 5,
115   TEST_HOST_ANNOUNCE_END            = 6,
116   TEST_HOST_ANNOUNCE2               = 7,
117   TEST_HOST_ANNOUNCE2_END           = 8,
118   TEST_GUEST_TALK                   = 9,
119   TEST_GUEST_HISTORY_REPLAY        = 10,
120   TEST_GUEST_HISTORY_REPLAY_LATEST = 11,
121   TEST_GUEST_LEAVE                 = 12,
122   TEST_HOST_LEAVE                  = 13,
123 } test;
124
125
126 static void
127 guest_enter ();
128
129
130 static void
131 guest_talk ();
132
133
134 static void
135 host_announce2 ();
136
137
138 /**
139  * Clean up all resources used.
140  */
141 static void
142 cleanup ()
143 {
144   if (NULL != core)
145   {
146     GNUNET_CORE_disconnect (core);
147     core = NULL;
148   }
149
150   if (NULL != id)
151   {
152     GNUNET_IDENTITY_disconnect (id);
153     id = NULL;
154   }
155
156   if (NULL != gst)
157   {
158     GNUNET_SOCIAL_guest_leave (gst, GNUNET_NO, NULL, NULL);
159     gst = NULL;
160     gst_plc = NULL;
161   }
162   if (NULL != hst)
163   {
164     GNUNET_SOCIAL_host_leave (hst, GNUNET_NO, NULL, NULL);
165     hst = NULL;
166     hst_plc = NULL;
167   }
168   GNUNET_SCHEDULER_shutdown ();
169 }
170
171
172 /**
173  * Terminate the test case (failure).
174  *
175  * @param cls NULL
176  * @param tc scheduler context
177  */
178 static void
179 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
180 {
181   res = 1;
182   cleanup ();
183   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n");
184 }
185
186
187 /**
188  * Terminate the test case (success).
189  *
190  * @param cls NULL
191  * @param tc scheduler context
192  */
193 static void
194 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
195 {
196   res = 0;
197   cleanup ();
198   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test PASSED.\n");
199 }
200
201
202 /**
203  * Finish the test case (successfully).
204  */
205 static void
206 end ()
207 {
208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending tests.\n");
209
210   if (end_badly_task != NULL)
211   {
212     GNUNET_SCHEDULER_cancel (end_badly_task);
213     end_badly_task = NULL;
214   }
215   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
216                                 &end_normally, NULL);
217 }
218
219
220 static void
221 transmit_resume (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
222 {
223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission resumed.\n");
224   struct TransmitClosure *tmit = cls;
225   if (NULL != tmit->host_ann)
226     GNUNET_SOCIAL_host_announce_resume (tmit->host_ann);
227   else
228     GNUNET_SOCIAL_guest_talk_resume (tmit->guest_talk);
229 }
230
231
232 static int
233 notify_data (void *cls, uint16_t *data_size, void *data)
234 {
235   struct TransmitClosure *tmit = cls;
236   if (NULL != tmit->env)
237   {
238     GNUNET_ENV_environment_destroy (tmit->env);
239     tmit->env = NULL;
240   }
241   if (0 == tmit->data_count)
242   {
243     *data_size = 0;
244     return GNUNET_YES;
245   }
246
247   uint16_t size = strlen (tmit->data[tmit->n]) + 1;
248   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249               "Transmit notify data: %u bytes available, "
250               "processing fragment %u/%u (size %u).\n",
251               *data_size, tmit->n + 1, tmit->data_count, size);
252   if (*data_size < size)
253   {
254     *data_size = 0;
255     GNUNET_assert (0);
256     return GNUNET_SYSERR;
257   }
258
259   if (GNUNET_YES != tmit->paused && 0 < tmit->data_delay[tmit->n])
260   {
261     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission paused.\n");
262     tmit->paused = GNUNET_YES;
263     GNUNET_SCHEDULER_add_delayed (
264       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
265                                      tmit->data_delay[tmit->n]),
266       &transmit_resume, tmit);
267     *data_size = 0;
268     return GNUNET_NO;
269   }
270   tmit->paused = GNUNET_NO;
271
272   *data_size = size;
273   memcpy (data, tmit->data[tmit->n], size);
274
275   return ++tmit->n < tmit->data_count ? GNUNET_NO : GNUNET_YES;
276 }
277
278
279 static void
280 host_left ()
281 {
282   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
283               "The host has left the place.\n");
284   GNUNET_SOCIAL_slicer_destroy (host_slicer);
285   host_slicer = NULL;
286   hst = NULL;
287   hst_plc = NULL;
288
289   end ();
290 }
291
292
293 static void
294 schedule_host_leave (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
295 {
296   test = TEST_HOST_LEAVE;
297   GNUNET_SOCIAL_host_leave (hst, GNUNET_NO, &host_left, NULL);
298 }
299
300
301 static void
302 host_farewell (void *cls,
303                struct GNUNET_SOCIAL_Nym *nym,
304                struct GNUNET_ENV_Environment *env,
305                size_t variable_count,
306                struct GNUNET_ENV_Modifier *variables)
307 {
308   // FIXME: this function is not called yet
309   struct GNUNET_CRYPTO_EcdsaPublicKey *nym_key = GNUNET_SOCIAL_nym_get_key (nym);
310   char *str;
311
312   str = GNUNET_CRYPTO_ecdsa_public_key_to_string (nym_key);
313   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
314               "Nym %s has left the place.\n",
315               str);
316   GNUNET_free (str);
317   GNUNET_assert (0 == memcmp (&guest_pub_key, nym_key, sizeof (*nym_key)));
318
319   GNUNET_SCHEDULER_add_now (&schedule_host_leave, NULL);
320 }
321
322
323 static void
324 guest_left (void *cls)
325 {
326   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
327               "The guest has left the place.\n");
328   GNUNET_SOCIAL_slicer_destroy (guest_slicer);
329   guest_slicer = NULL;
330   gst = NULL;
331   gst_plc = NULL;
332
333   GNUNET_SCHEDULER_add_now (&schedule_host_leave, NULL);
334 }
335
336
337 static void
338 guest_leave()
339 {
340   test = TEST_GUEST_LEAVE;
341   /* FIXME test keep_active */
342   GNUNET_SOCIAL_guest_leave (gst, GNUNET_NO, &guest_left, NULL);
343 }
344
345
346 static void
347 schedule_guest_leave (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
348 {
349   guest_leave ();
350 }
351
352
353 static void
354 guest_recv_history_replay_latest_result (void *cls, int64_t result,
355                                          const void *data, uint16_t data_size)
356 {
357   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
358               "Test #%u: Guest received latest history replay result: %" PRId64 "\n"
359               "%.*s\n",
360               test, result, data_size, data);
361   GNUNET_assert (2 == counter); /* message count */
362   GNUNET_assert (7 == result); /* fragment count */
363
364   GNUNET_SCHEDULER_add_now (&schedule_guest_leave, NULL);
365 }
366
367
368 static void
369 guest_history_replay_latest ()
370 {
371   test = TEST_GUEST_HISTORY_REPLAY_LATEST;
372   counter = 0;
373   GNUNET_SOCIAL_place_history_replay_latest (gst_plc, 3, "",
374                                              GNUNET_PSYC_HISTORY_REPLAY_LOCAL,
375                                              guest_slicer,
376                                              &guest_recv_history_replay_latest_result,
377                                              NULL);
378 }
379
380
381 static void
382 guest_recv_history_replay_result (void *cls, int64_t result,
383                                   const void *data, uint16_t data_size)
384 {
385   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
386               "Test #%u: Guest received history replay result: %" PRId64 "\n"
387               "%.*s\n",
388               test, result, data_size, data);
389   GNUNET_assert (2 == counter); /* message count */
390   GNUNET_assert (7 == result); /* fragment count */
391
392   guest_history_replay_latest ();
393 }
394
395
396 static void
397 guest_history_replay ()
398 {
399   test = TEST_GUEST_HISTORY_REPLAY;
400   counter = 0;
401   GNUNET_SOCIAL_place_history_replay (gst_plc, 1, 3, "",
402                                       GNUNET_PSYC_HISTORY_REPLAY_LOCAL,
403                                       guest_slicer,
404                                       &guest_recv_history_replay_result,
405                                       NULL);
406 }
407
408
409 static void
410 guest_recv_method (void *cls,
411                   const struct GNUNET_PSYC_MessageMethod *meth,
412                   uint64_t message_id,
413                   uint32_t flags,
414                   const struct GNUNET_SOCIAL_Nym *nym,
415                   const char *method_name)
416 {
417   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
418               "Test #%u: Guest received method for message ID %" PRIu64 ":\n"
419               "%s (flags: %x)\n",
420               test, message_id, method_name, flags);
421   /* FIXME: check message */
422 }
423
424
425 static void
426 guest_recv_modifier (void *cls,
427                     const struct GNUNET_PSYC_MessageModifier *mod,
428                     uint64_t message_id,
429                     enum GNUNET_ENV_Operator oper,
430                     const char *name,
431                     const void *value,
432                     uint16_t value_size)
433 {
434   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
435               "Test #%u: Guest received modifier for message ID %" PRIu64 ":\n"
436               "%c%s: %.*s\n",
437               test, message_id, oper, name, value_size, value);
438 }
439
440
441 static void
442 guest_recv_data (void *cls,
443                 const struct GNUNET_MessageHeader *msg,
444                 uint64_t message_id,
445                 uint64_t data_offset,
446                 const void *data,
447                 uint16_t data_size)
448 {
449   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
450               "Test #%u: Guest received data for message ID %" PRIu64 ":\n"
451               "%.*s\n",
452               test, message_id, data_size, data);
453 }
454
455
456 static void
457 guest_recv_eom (void *cls,
458                const struct GNUNET_MessageHeader *msg,
459                uint64_t message_id,
460                uint8_t cancelled)
461 {
462   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
463               "Test #%u: Guest received end of message ID %" PRIu64
464               ", cancelled: %u\n",
465               test, message_id, cancelled);
466
467   switch (test)
468   {
469   case TEST_HOST_ANNOUNCE:
470     test = TEST_HOST_ANNOUNCE_END;
471     break;
472
473   case TEST_HOST_ANNOUNCE_END:
474     host_announce2 ();
475     break;
476
477   case TEST_HOST_ANNOUNCE2:
478     test = TEST_HOST_ANNOUNCE2_END;
479     break;
480
481   case TEST_HOST_ANNOUNCE2_END:
482     guest_talk ();
483     break;
484
485   case TEST_GUEST_HISTORY_REPLAY:
486   case TEST_GUEST_HISTORY_REPLAY_LATEST:
487     counter++;
488     break;
489
490   default:
491     GNUNET_assert (0);
492   }
493 }
494
495
496 static void
497 host_recv_method (void *cls,
498                   const struct GNUNET_PSYC_MessageMethod *meth,
499                   uint64_t message_id,
500                   uint32_t flags,
501                   const struct GNUNET_SOCIAL_Nym *nym,
502                   const char *method_name)
503 {
504   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
505               "Test #%u: Host received method for message ID %" PRIu64 ":\n"
506               "%s\n",
507               test, message_id, method_name);
508   /* FIXME: check message */
509 }
510
511
512 static void
513 host_recv_modifier (void *cls,
514                     const struct GNUNET_PSYC_MessageModifier *mod,
515                     uint64_t message_id,
516                     enum GNUNET_ENV_Operator oper,
517                     const char *name,
518                     const void *value,
519                     uint16_t value_size)
520 {
521   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
522               "Test #%u: Host received modifier for message ID %" PRIu64 ":\n"
523               "%c%s: %.*s\n",
524               test, message_id, oper, name, value_size, value);
525 }
526
527
528 static void
529 host_recv_data (void *cls,
530                 const struct GNUNET_MessageHeader *msg,
531                 uint64_t message_id,
532                 uint64_t data_offset,
533                 const void *data,
534                 uint16_t data_size)
535 {
536   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
537               "Test #%u: Host received data for message ID %" PRIu64 ":\n"
538               "%.*s\n",
539               test, message_id, data_size, data);
540 }
541
542
543 static void
544 host_recv_eom (void *cls,
545                const struct GNUNET_MessageHeader *msg,
546                uint64_t message_id,
547                uint8_t cancelled)
548 {
549   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
550               "Test #%u: Host received end of message ID %" PRIu64
551               ", cancelled: %u\n",
552               test, message_id, cancelled);
553
554   switch (test)
555   {
556   case TEST_HOST_ANNOUNCE:
557     test = TEST_HOST_ANNOUNCE_END;
558     break;
559
560   case TEST_HOST_ANNOUNCE_END:
561     host_announce2 ();
562     break;
563
564   case TEST_HOST_ANNOUNCE2:
565     test = TEST_HOST_ANNOUNCE2_END;
566     break;
567
568   case TEST_HOST_ANNOUNCE2_END:
569     guest_talk ();
570     break;
571
572   case TEST_GUEST_TALK:
573     guest_history_replay ();
574     break;
575
576   default:
577     GNUNET_assert (0);
578   }
579 }
580
581
582 static void
583 guest_talk ()
584 {
585   test = TEST_GUEST_TALK;
586
587   tmit = (struct TransmitClosure) {};
588   tmit.env = GNUNET_ENV_environment_create ();
589   GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
590                               "_bar_foo", DATA2ARG ("one two three"));
591   GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
592                               "_bar_baz", DATA2ARG ("four five"));
593   tmit.data[0] = "zzz xxx yyy";
594   tmit.data[1] = "zyx wvu tsr qpo";
595   tmit.data[2] = "testing ten nine eight";
596   tmit.data_count = 3;
597
598   tmit.guest_talk
599     = GNUNET_SOCIAL_guest_talk (gst, "_message_guest", tmit.env,
600                                 &notify_data, &tmit,
601                                 GNUNET_SOCIAL_TALK_NONE);
602 }
603
604
605 static void
606 host_announce ()
607 {
608   test = TEST_HOST_ANNOUNCE;
609
610   tmit = (struct TransmitClosure) {};
611   tmit.env = GNUNET_ENV_environment_create ();
612   GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
613                               "_foo", DATA2ARG ("bar baz"));
614   GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
615                               "_foo_bar", DATA2ARG ("foo bar"));
616   GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
617                               "_foo_bar_baz", DATA2ARG ("foo bar baz"));
618   tmit.data[0] = "aaa bbb ccc";
619   tmit.data[1] = "abc def ghi jkl";
620   tmit.data[2] = "testing one two three";
621   tmit.data[3] = "four five";
622   tmit.data_count = 4;
623
624   tmit.host_ann
625     = GNUNET_SOCIAL_host_announce (hst, "_message_host", tmit.env,
626                                    &notify_data, &tmit,
627                                    GNUNET_SOCIAL_ANNOUNCE_NONE);
628 }
629
630
631 static void
632 host_announce2 ()
633 {
634   test = TEST_HOST_ANNOUNCE2;
635
636   tmit = (struct TransmitClosure) {};
637   tmit.env = GNUNET_ENV_environment_create ();
638   GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
639                               "_foo2", DATA2ARG ("BAR BAZ"));
640   GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
641                               "_foo2_bar", DATA2ARG ("FOO BAR"));
642   GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
643                               "_foo2_bar", DATA2ARG ("FOO BAR BAZ"));
644   tmit.data[0] = "AAA BBB CCC";
645   tmit.data[1] = "ABC DEF GHI JKL";
646   tmit.data[2] = "TESTING ONE TWO THREE";
647   tmit.data_count = 3;
648
649   tmit.host_ann
650     = GNUNET_SOCIAL_host_announce (hst, "_message_host_two", tmit.env,
651                                    &notify_data, &tmit,
652                                    GNUNET_SOCIAL_ANNOUNCE_NONE);
653 }
654
655
656 static void
657 guest_recv_entry_decision (void *cls,
658                            int is_admitted,
659                            const struct GNUNET_PSYC_Message *entry_resp)
660 {
661   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
662               "Guest received entry decision (try %u): %d.\n",
663               join_req_count, is_admitted);
664
665   if (NULL != entry_resp)
666   {
667     struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
668     const char *method_name = NULL;
669     const void *data = NULL;
670     uint16_t data_size = 0;
671     GNUNET_PSYC_message_parse (entry_resp, &method_name, env, &data, &data_size);
672
673     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674                 "%s\n%.*s\n",
675                 method_name, data_size, data);
676     /* FIXME: check response message */
677   }
678
679   switch (test)
680   {
681   case TEST_GUEST_RECV_ENTRY_DCSN_REFUSE:
682     GNUNET_assert (GNUNET_NO == is_admitted);
683     guest_enter ();
684     break;
685
686   case TEST_GUEST_RECV_ENTRY_DCSN_ADMIT:
687     GNUNET_assert (GNUNET_YES == is_admitted);
688     host_announce ();
689     break;
690
691   default:
692     GNUNET_assert (0);
693   }
694 }
695
696
697 static void
698 host_answer_door (void *cls,
699                   struct GNUNET_SOCIAL_Nym *nym,
700                   const char *method_name,
701                   struct GNUNET_ENV_Environment *env,
702                   size_t data_size,
703                   const void *data)
704 {
705   join_req_count++;
706
707   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
708                 "Host received entry request from guest (try %u).\n",
709                 join_req_count);
710   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
711               "%s\n%.*s\n",
712               method_name, data_size, data);
713
714   switch (test)
715   {
716   case TEST_HOST_ANSWER_DOOR_REFUSE:
717     test = TEST_GUEST_RECV_ENTRY_DCSN_REFUSE;
718     join_resp = GNUNET_PSYC_message_create ("_refuse_nym", env,
719                                             DATA2ARG ("Go away!"));
720     GNUNET_SOCIAL_host_entry_decision (hst, nym, GNUNET_NO, join_resp);
721     break;
722
723   case TEST_HOST_ANSWER_DOOR_ADMIT:
724     test = TEST_GUEST_RECV_ENTRY_DCSN_ADMIT;
725     join_resp = GNUNET_PSYC_message_create ("_admit_nym", env,
726                                             DATA2ARG ("Welcome, nym!"));
727     GNUNET_SOCIAL_host_entry_decision (hst, nym, GNUNET_YES, join_resp);
728     break;
729
730   default:
731     GNUNET_assert (0);
732   }
733 }
734
735
736 static void
737 guest_recv_local_enter (void *cls, int result, uint64_t max_message_id)
738 {
739   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Guest entered to local place.\n");
740
741 }
742
743
744 static void
745 guest_enter ()
746 {
747   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Entering to place as guest.\n");
748
749   struct GuestEnterMessage *emsg = &guest_enter_msg;
750
751   emsg->method_name = "_request_enter";
752   emsg->env = GNUNET_ENV_environment_create ();
753   GNUNET_ENV_environment_add (emsg->env, GNUNET_ENV_OP_ASSIGN,
754                               "_abc", "abc def", 7);
755   GNUNET_ENV_environment_add (emsg->env, GNUNET_ENV_OP_ASSIGN,
756                               "_abc_def", "abc def ghi", 11);
757   emsg->data = "let me in";
758   emsg->data_size = strlen (emsg->data) + 1;
759   emsg->msg = GNUNET_PSYC_message_create (emsg->method_name, emsg->env,
760                                           emsg->data, emsg->data_size);
761
762   gst = GNUNET_SOCIAL_guest_enter (cfg, guest_ego, &place_pub_key,
763                                    &this_peer, 0, NULL, emsg->msg,
764                                    guest_slicer, &guest_recv_local_enter,
765                                    &guest_recv_entry_decision, NULL);
766   gst_plc = GNUNET_SOCIAL_guest_get_place (gst);
767 }
768
769
770 static void
771 id_guest_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
772 {
773   GNUNET_assert (NULL != ego);
774   guest_ego = ego;
775
776   guest_slicer = GNUNET_SOCIAL_slicer_create ();
777   GNUNET_SOCIAL_slicer_add (guest_slicer, "",
778                             &guest_recv_method, &guest_recv_modifier,
779                             &guest_recv_data, &guest_recv_eom, NULL);
780   test = TEST_HOST_ANSWER_DOOR_ADMIT;
781   //host_announce ();
782   guest_enter ();
783 }
784
785
786 static void
787 id_guest_created (void *cls, const char *emsg)
788 {
789   if (NULL != emsg)
790   {
791     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
792                 "Could not create guest identity: %s\n", emsg);
793 #if ! DEBUG_TEST_SOCIAL
794     GNUNET_assert (0);
795 #endif
796   }
797
798  GNUNET_IDENTITY_ego_lookup (cfg, guest_name, &id_guest_ego_cb, NULL);
799 }
800
801
802 static void
803 host_entered (void *cls, int result, uint64_t max_message_id)
804 {
805   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Host entered to place.\n");
806
807   GNUNET_IDENTITY_create (id, guest_name, &id_guest_created, NULL);
808 }
809
810
811 static void
812 id_host_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
813 {
814   GNUNET_assert (NULL != ego);
815   host_ego = ego;
816
817   host_slicer = GNUNET_SOCIAL_slicer_create ();
818   GNUNET_SOCIAL_slicer_add (host_slicer, "",
819                             &host_recv_method, &host_recv_modifier,
820                             &host_recv_data, &host_recv_eom, NULL);
821
822   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Entering to place as host.\n");
823   hst = GNUNET_SOCIAL_host_enter (cfg, host_ego, place_key,
824                                   GNUNET_PSYC_CHANNEL_PRIVATE, host_slicer,
825                                   &host_entered, &host_answer_door,
826                                   &host_farewell, NULL);
827   hst_plc = GNUNET_SOCIAL_host_get_place (hst);
828 }
829
830
831 static void
832 id_host_created (void *cls, const char *emsg)
833 {
834   if (NULL != emsg)
835   {
836     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
837                 "Could not create host identity: %s\n", emsg);
838 #if ! DEBUG_TEST_SOCIAL
839     GNUNET_assert (0);
840 #endif
841   }
842
843   GNUNET_IDENTITY_ego_lookup (cfg, host_name, &id_host_ego_cb, NULL);
844 }
845
846
847 static void
848 identity_ego_cb (void *cls, struct GNUNET_IDENTITY_Ego *ego,
849                  void **ctx, const char *name)
850 {
851
852 }
853
854
855 static void
856 core_connected (void *cls, const struct GNUNET_PeerIdentity *my_identity)
857 {
858   this_peer = *my_identity;
859
860   id = GNUNET_IDENTITY_connect (cfg, &identity_ego_cb, NULL);
861   GNUNET_IDENTITY_create (id, host_name, &id_host_created, NULL);
862 }
863
864
865 /**
866  * Main function of the test, run from scheduler.
867  *
868  * @param cls NULL
869  * @param cfg configuration we use (also to connect to Social service)
870  * @param peer handle to access more of the peer (not used)
871  */
872 static void
873 #if DEBUG_TEST_SOCIAL
874 run (void *cls, char *const *args, const char *cfgfile,
875      const struct GNUNET_CONFIGURATION_Handle *c)
876 #else
877 run (void *cls,
878      const struct GNUNET_CONFIGURATION_Handle *c,
879      struct GNUNET_TESTING_Peer *peer)
880 #endif
881 {
882   cfg = c;
883   end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
884
885   place_key = GNUNET_CRYPTO_eddsa_key_create ();
886   guest_key = GNUNET_CRYPTO_ecdsa_key_create ();
887
888   GNUNET_CRYPTO_eddsa_key_get_public (place_key, &place_pub_key);
889   GNUNET_CRYPTO_ecdsa_key_get_public (guest_key, &guest_pub_key);
890
891   core = GNUNET_CORE_connect (cfg, NULL, &core_connected, NULL, NULL,
892                               NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
893 }
894
895
896 int
897 main (int argc, char *argv[])
898 {
899   res = 1;
900 #if DEBUG_TEST_SOCIAL
901   const struct GNUNET_GETOPT_CommandLineOption opts[] = {
902     GNUNET_GETOPT_OPTION_END
903   };
904   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-social",
905                                        "test-social [options]",
906                                        opts, &run, NULL))
907     return 1;
908 #else
909   if (0 != GNUNET_TESTING_peer_run ("test-social", "test_social.conf", &run, NULL))
910     return 1;
911 #endif
912   return res;
913 }
914
915 /* end of test_social.c */