- deduplicate
[oweals/gnunet.git] / src / chat / test_chat_private.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file chat/test_chat_private.c
23  * @brief testcase for private chatting
24  * @author Vitaly Minko
25  */
26
27 #include "platform.h"
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_chat_service.h"
32
33 #define VERBOSE GNUNET_NO
34
35 /**
36  * How long until we give up on passing the test?
37  */
38 #define KILL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
39
40 /**
41  * How long until we give up on receiving somebody else's private message?
42  */
43 #define PM_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
44
45 struct PeerContext
46 {
47   struct GNUNET_CONFIGURATION_Handle *cfg;
48   struct GNUNET_OS_Process *arm_proc;
49 };
50
51 struct Wanted
52 {
53   struct GNUNET_CONTAINER_MetaData *meta;
54
55   struct GNUNET_HashCode *sender;
56
57   /**
58    * Alternative meta/sender is used when we expect join/leave notification
59    * from two peers and don't know which one will come first.
60    */
61   struct GNUNET_CONTAINER_MetaData *meta2;
62
63   struct GNUNET_HashCode *sender2;
64
65   char *msg;
66
67   const char *me;
68
69   enum GNUNET_CHAT_MsgOptions opt;
70
71   struct GNUNET_TIME_Absolute timestamp;
72
73   GNUNET_SCHEDULER_Task next_task;
74
75   void *next_task_cls;
76
77 };
78
79 static struct PeerContext p1;
80
81 static struct PeerContext p2;
82
83 static struct PeerContext p3;
84
85 static struct GNUNET_HashCode alice;
86
87 static struct GNUNET_HashCode bob;
88
89 static struct GNUNET_HashCode carol;
90
91 static struct GNUNET_CHAT_Room *alice_room;
92
93 static struct GNUNET_CHAT_Room *bob_room;
94
95 static struct GNUNET_CHAT_Room *carol_room;
96
97 static struct GNUNET_CONTAINER_MetaData *alice_meta;
98
99 static struct GNUNET_CONTAINER_MetaData *bob_meta;
100
101 static struct GNUNET_CONTAINER_MetaData *carol_meta;
102
103 static struct Wanted alice_wanted;
104
105 static struct Wanted bob_wanted;
106
107 static struct Wanted carol_wanted;
108
109 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
110
111 static GNUNET_SCHEDULER_TaskIdentifier finish_task;
112
113 static GNUNET_SCHEDULER_TaskIdentifier wait_task;
114
115 static int err;
116
117 static int alice_ready;
118
119 static int bob_ready;
120
121 static int is_p2p;
122
123 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *bob_public_key;
124
125
126 static void
127 setup_peer (struct PeerContext *p, const char *cfgname)
128 {
129   char *binary;
130
131   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
132   p->cfg = GNUNET_CONFIGURATION_create ();
133   p->arm_proc =
134     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, binary,
135                                "gnunet-service-arm",
136                                "-c", cfgname, NULL);
137   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
138   GNUNET_free (binary);
139 }
140
141
142 static void
143 stop_arm (struct PeerContext *p)
144 {
145   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
146     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
147   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
148     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
149   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
150               GNUNET_OS_process_get_pid (p->arm_proc));
151   GNUNET_OS_process_destroy (p->arm_proc);
152   p->arm_proc = NULL;
153   GNUNET_CONFIGURATION_destroy (p->cfg);
154 }
155
156
157 static void
158 abort_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 {
160   if (alice_room != NULL)
161   {
162     GNUNET_CHAT_leave_room (alice_room);
163     alice_room = NULL;
164   }
165   if (bob_room != NULL)
166   {
167     GNUNET_CHAT_leave_room (bob_room);
168     bob_room = NULL;
169   }
170   if (carol_room != NULL)
171   {
172     GNUNET_CHAT_leave_room (carol_room);
173     carol_room = NULL;
174   }
175   err = 1;
176 }
177
178
179 static void
180 timeout_kill (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
181 {
182 #if VERBOSE
183   printf ("Timed out, stopping the test.\n");
184 #endif
185   kill_task = GNUNET_SCHEDULER_NO_TASK;
186   if (wait_task != GNUNET_SCHEDULER_NO_TASK)
187   {
188     GNUNET_SCHEDULER_cancel (wait_task);
189     wait_task = GNUNET_SCHEDULER_NO_TASK;
190   }
191   GNUNET_SCHEDULER_add_continuation (&abort_test, NULL,
192                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
193 }
194
195
196 static int
197 join_cb (void *cls)
198 {
199   struct Wanted *want = cls;
200
201 #if VERBOSE
202   printf ("%s has joined\n", want->me);
203 #endif
204   if (NULL != want->next_task)
205     GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
206   return GNUNET_OK;
207 }
208
209
210 static int
211 member_list_cb (void *cls, const struct GNUNET_CONTAINER_MetaData *member_info,
212                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *member_id,
213                 enum GNUNET_CHAT_MsgOptions options)
214 {
215   struct Wanted *want = cls;
216   struct GNUNET_HashCode sender;
217
218 #if VERBOSE
219   printf ("%s - told that %s has %s\n", want->me,
220           member_info ==
221           NULL ? NULL : GNUNET_CONTAINER_meta_data_get_by_type (member_info,
222                                                                 EXTRACTOR_METATYPE_TITLE),
223           member_info == NULL ? "left" : "joined");
224 #endif
225   GNUNET_CRYPTO_hash (member_id,
226                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
227                       &sender);
228   /* entertain both primary and an alternative sender/meta */
229   if (((0 == memcmp (&sender, want->sender, sizeof (struct GNUNET_HashCode))) ||
230        ((want->sender2 != NULL) &&
231         (0 == memcmp (&sender, want->sender2, sizeof (struct GNUNET_HashCode))))) &&
232       (((member_info == NULL) && (want->meta == NULL)) ||
233        ((member_info != NULL) &&
234         (((want->meta != NULL) &&
235           GNUNET_CONTAINER_meta_data_test_equal (member_info, want->meta)) ||
236          ((want->meta2 != NULL) &&
237           GNUNET_CONTAINER_meta_data_test_equal (member_info, want->meta2)))))
238       && (options == want->opt))
239   {
240     /* remember Bob's public key, we need it to send private message */
241     if (NULL == bob_public_key &&
242         (0 == memcmp (&bob, want->sender, sizeof (struct GNUNET_HashCode))))
243       bob_public_key =
244           GNUNET_memdup (member_id,
245                          sizeof (struct
246                                  GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
247     if (want->sender2 != NULL)
248     {
249       /* flush alternative sender */
250       if (0 == memcmp (&sender, want->sender, sizeof (struct GNUNET_HashCode)))
251       {
252         want->sender = want->sender2;
253         want->meta = want->meta2;
254       }
255       want->sender2 = NULL;
256       want->meta2 = NULL;
257     }
258     else if (NULL != want->next_task)
259       GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
260   }
261   else
262   {
263     GNUNET_SCHEDULER_cancel (kill_task);
264     kill_task = GNUNET_SCHEDULER_NO_TASK;
265     GNUNET_SCHEDULER_add_now (&abort_test, NULL);
266   }
267   return GNUNET_OK;
268 }
269
270
271 static int
272 receive_cb (void *cls, struct GNUNET_CHAT_Room *room,
273             const struct GNUNET_HashCode * sender,
274             const struct GNUNET_CONTAINER_MetaData *meta, const char *message,
275             struct GNUNET_TIME_Absolute timestamp,
276             enum GNUNET_CHAT_MsgOptions options)
277 {
278   struct Wanted *want = cls;
279
280 #if VERBOSE
281   printf ("%s - told that %s said '%s'\n", want->me,
282           meta == NULL ? NULL : GNUNET_CONTAINER_meta_data_get_by_type (meta,
283                                                                         EXTRACTOR_METATYPE_TITLE),
284           message);
285 #endif
286
287   if ((want->msg != NULL) && (0 == strcmp (message, want->msg)) &&
288       (((sender == NULL) && (want->sender == NULL)) ||
289        ((sender != NULL) && (want->sender != NULL) &&
290         (0 == memcmp (sender, want->sender, sizeof (struct GNUNET_HashCode))))) &&
291       (GNUNET_CONTAINER_meta_data_test_equal (meta, want->meta)) &&
292       (options == want->opt) &&
293       /* Not == since the library sets the actual timestamp, so it may be
294        * slightly greater
295        */
296       (timestamp.abs_value >= want->timestamp.abs_value))
297   {
298     if (NULL != want->next_task)
299       GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
300   }
301   else
302   {
303     GNUNET_SCHEDULER_cancel (kill_task);
304     kill_task = GNUNET_SCHEDULER_NO_TASK;
305     GNUNET_SCHEDULER_cancel (finish_task);
306     finish_task = GNUNET_SCHEDULER_NO_TASK;
307     GNUNET_SCHEDULER_add_now (&abort_test, NULL);
308   }
309   return GNUNET_OK;
310 }
311
312
313 static void
314 wait_until_all_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
315 {
316   GNUNET_SCHEDULER_Task task = cls;
317
318 #if VERBOSE
319   printf ("Waiting...\n");
320 #endif
321   if (alice_ready && bob_ready)
322   {
323     wait_task = GNUNET_SCHEDULER_NO_TASK;
324     GNUNET_SCHEDULER_add_now (task, NULL);
325   }
326   else
327     wait_task =
328         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
329                                       (GNUNET_TIME_UNIT_MILLISECONDS, 5000),
330                                       &wait_until_all_ready, task);
331 }
332
333
334 static void
335 set_alice_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
336 {
337   alice_ready = GNUNET_YES;
338 }
339
340
341 static void
342 set_bob_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
343 {
344   bob_ready = GNUNET_YES;
345 }
346
347
348 static void
349 disconnect_alice (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
350 {
351 #if VERBOSE
352   printf ("Alice is leaving.\n");
353 #endif
354   if (is_p2p)
355     stop_arm (&p2);
356   GNUNET_CHAT_leave_room (alice_room);
357   alice_room = NULL;
358   GNUNET_SCHEDULER_cancel (kill_task);
359   kill_task = GNUNET_SCHEDULER_NO_TASK;
360 }
361
362
363 static void
364 disconnect_bob (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
365 {
366 #if VERBOSE
367   printf ("Bod is leaving.\n");
368 #endif
369   if (is_p2p)
370     stop_arm (&p3);
371   alice_wanted.meta = NULL;
372   alice_wanted.sender = &bob;
373   alice_wanted.msg = NULL;
374   alice_wanted.opt = 0;
375   alice_wanted.next_task = &disconnect_alice;
376   alice_wanted.next_task_cls = NULL;
377   GNUNET_CHAT_leave_room (bob_room);
378   bob_room = NULL;
379 }
380
381
382 static void
383 disconnect_carol (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
384 {
385 #if VERBOSE
386   printf ("Carol is leaving.\n");
387 #endif
388   alice_wanted.meta = NULL;
389   alice_wanted.sender = &carol;
390   alice_wanted.msg = NULL;
391   alice_wanted.opt = 0;
392   alice_wanted.next_task = &set_alice_ready;
393   alice_wanted.next_task_cls = NULL;
394   alice_ready = GNUNET_NO;
395   bob_wanted.meta = NULL;
396   bob_wanted.sender = &carol;
397   bob_wanted.msg = NULL;
398   bob_wanted.opt = 0;
399   bob_wanted.next_task = &wait_until_all_ready;
400   bob_wanted.next_task_cls = &disconnect_bob;
401   bob_ready = GNUNET_YES;
402   GNUNET_CHAT_leave_room (carol_room);
403   carol_room = NULL;
404 }
405
406
407 static void
408 send_from_alice_to_bob (void *cls,
409                         const struct GNUNET_SCHEDULER_TaskContext *tc)
410 {
411   uint32_t seq;
412
413 #if VERBOSE
414   printf ("Alice says 'Hi!' to Bob\n");
415 #endif
416   alice_ready = GNUNET_YES;
417   bob_ready = GNUNET_NO;
418   bob_wanted.meta = alice_meta;
419   bob_wanted.sender = &alice;
420   bob_wanted.msg = "Hi Bob!";
421   bob_wanted.opt = GNUNET_CHAT_MSG_PRIVATE;
422   bob_wanted.next_task = &set_bob_ready;
423   bob_wanted.next_task_cls = NULL;
424   /* Carol should not receive this message */
425   carol_wanted.meta = NULL;
426   carol_wanted.sender = NULL;
427   carol_wanted.msg = NULL;
428   carol_wanted.opt = 0;
429   carol_wanted.next_task = NULL;
430   carol_wanted.next_task_cls = NULL;
431   GNUNET_CHAT_send_message (alice_room, "Hi Bob!", GNUNET_CHAT_MSG_PRIVATE,
432                             bob_public_key, &seq);
433   finish_task =
434       GNUNET_SCHEDULER_add_delayed (PM_TIMEOUT, &wait_until_all_ready,
435                                     &disconnect_carol);
436 }
437
438
439 static void
440 prepare_bob_for_alice_task (void *cls,
441                             const struct GNUNET_SCHEDULER_TaskContext *tc)
442 {
443   bob_wanted.meta = alice_meta;
444   bob_wanted.sender = &alice;
445   bob_wanted.msg = NULL;
446   bob_wanted.opt = -1;
447   bob_wanted.next_task = &set_bob_ready;
448   bob_wanted.next_task_cls = NULL;
449 }
450
451
452 static void
453 prepare_carol_for_alice_and_bob_task (void *cls,
454                                       const struct GNUNET_SCHEDULER_TaskContext
455                                       *tc)
456 {
457   carol_wanted.meta = alice_meta;
458   carol_wanted.sender = &alice;
459   /* set alternative meta/sender since we don't know from which peer
460    * notification will come first */
461   carol_wanted.meta2 = bob_meta;
462   carol_wanted.sender2 = &bob;
463   carol_wanted.msg = NULL;
464   carol_wanted.opt = -1;
465   carol_wanted.next_task = &wait_until_all_ready;
466   carol_wanted.next_task_cls = &send_from_alice_to_bob;
467 }
468
469
470 static void
471 join_carol_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
472 {
473 #if VERBOSE
474   printf ("Carol joining\n");
475 #endif
476   alice_wanted.meta = carol_meta;
477   alice_wanted.sender = &carol;
478   alice_wanted.msg = NULL;
479   alice_wanted.opt = -1;
480   alice_wanted.next_task = &set_alice_ready;
481   alice_wanted.next_task_cls = NULL;
482   alice_ready = GNUNET_NO;
483   bob_wanted.meta = carol_meta;
484   bob_wanted.sender = &carol;
485   bob_wanted.msg = NULL;
486   bob_wanted.opt = -1;
487   bob_wanted.next_task = &set_bob_ready;
488   bob_wanted.next_task_cls = NULL;
489   bob_ready = GNUNET_NO;
490   carol_wanted.next_task = &prepare_carol_for_alice_and_bob_task;
491   carol_wanted.next_task_cls = NULL;
492   carol_room =
493       GNUNET_CHAT_join_room (is_p2p ? p3.cfg : p1.cfg, "carol", carol_meta,
494                              "test", -1, &join_cb, &carol_wanted, &receive_cb,
495                              &carol_wanted, &member_list_cb, &carol_wanted,
496                              NULL, NULL, &carol);
497   if (NULL == carol_room)
498   {
499     GNUNET_SCHEDULER_cancel (kill_task);
500     kill_task = GNUNET_SCHEDULER_NO_TASK;
501     GNUNET_CHAT_leave_room (alice_room);
502     alice_room = NULL;
503     GNUNET_CHAT_leave_room (bob_room);
504     bob_room = NULL;
505     err = 1;
506   }
507 }
508
509
510 static void
511 join_bob_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
512 {
513 #if VERBOSE
514   printf ("Bob joining\n");
515 #endif
516   alice_wanted.meta = bob_meta;
517   alice_wanted.sender = &bob;
518   alice_wanted.msg = NULL;
519   alice_wanted.opt = -1;
520   alice_wanted.next_task = &wait_until_all_ready;
521   alice_wanted.next_task_cls = &join_carol_task;
522   alice_ready = GNUNET_YES;
523   bob_wanted.next_task = &prepare_bob_for_alice_task;
524   bob_wanted.next_task_cls = NULL;
525   bob_ready = GNUNET_NO;
526   bob_room =
527       GNUNET_CHAT_join_room (is_p2p ? p2.cfg : p1.cfg, "bob", bob_meta, "test",
528                              -1, &join_cb, &bob_wanted, &receive_cb,
529                              &bob_wanted, &member_list_cb, &bob_wanted, NULL,
530                              NULL, &bob);
531   if (NULL == bob_room)
532   {
533     GNUNET_SCHEDULER_cancel (kill_task);
534     kill_task = GNUNET_SCHEDULER_NO_TASK;
535     GNUNET_CHAT_leave_room (alice_room);
536     alice_room = NULL;
537     err = 1;
538   }
539 }
540
541
542 static void
543 join_alice_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
544 {
545 #if VERBOSE
546   printf ("Alice joining\n");
547 #endif
548   alice_wanted.next_task = &join_bob_task;
549   alice_wanted.next_task_cls = NULL;
550   alice_room =
551       GNUNET_CHAT_join_room (p1.cfg, "alice", alice_meta, "test", -1, &join_cb,
552                              &alice_wanted, &receive_cb, &alice_wanted,
553                              &member_list_cb, &alice_wanted, NULL, NULL,
554                              &alice);
555   if (NULL == alice_room)
556   {
557     GNUNET_SCHEDULER_cancel (kill_task);
558     kill_task = GNUNET_SCHEDULER_NO_TASK;
559     err = 1;
560   }
561 }
562
563
564 static void
565 run (void *cls, char *const *args, const char *cfgfile,
566      const struct GNUNET_CONFIGURATION_Handle *cfg)
567 {
568   if (is_p2p)
569   {
570     setup_peer (&p1, "test_chat_peer1.conf");
571     setup_peer (&p2, "test_chat_peer2.conf");
572     setup_peer (&p3, "test_chat_peer3.conf");
573   }
574   else
575     setup_peer (&p1, "test_chat_data.conf");
576
577   memset (&alice_wanted, 0, sizeof (struct Wanted));
578   memset (&bob_wanted, 0, sizeof (struct Wanted));
579   memset (&carol_wanted, 0, sizeof (struct Wanted));
580   alice_wanted.me = "Alice";
581   bob_wanted.me = "Bob";
582   carol_wanted.me = "Carol";
583   alice_meta = GNUNET_CONTAINER_meta_data_create ();
584   GNUNET_CONTAINER_meta_data_insert (alice_meta, "<gnunet>",
585                                      EXTRACTOR_METATYPE_TITLE,
586                                      EXTRACTOR_METAFORMAT_UTF8, "text/plain",
587                                      "Alice", strlen ("Alice") + 1);
588   bob_meta = GNUNET_CONTAINER_meta_data_create ();
589   GNUNET_CONTAINER_meta_data_insert (bob_meta, "<gnunet>",
590                                      EXTRACTOR_METATYPE_TITLE,
591                                      EXTRACTOR_METAFORMAT_UTF8, "text/plain",
592                                      "Bob", strlen ("Bob") + 1);
593   carol_meta = GNUNET_CONTAINER_meta_data_create ();
594   GNUNET_CONTAINER_meta_data_insert (carol_meta, "<gnunet>",
595                                      EXTRACTOR_METATYPE_TITLE,
596                                      EXTRACTOR_METAFORMAT_UTF8, "text/plain",
597                                      "Carol", strlen ("Carol") + 1);
598   kill_task = GNUNET_SCHEDULER_add_delayed (KILL_TIMEOUT, &timeout_kill, NULL);
599   GNUNET_SCHEDULER_add_now (&join_alice_task, NULL);
600 }
601
602
603 int
604 main (int argc, char *argv[])
605 {
606   char *const argvx[] = {
607     "test-chat",
608     "-c",
609     "test_chat_data.conf",
610     NULL
611   };
612   struct GNUNET_GETOPT_CommandLineOption options[] = {
613     GNUNET_GETOPT_OPTION_END
614   };
615
616   GNUNET_log_setup ("test_chat",
617                     "WARNING",
618                     NULL);
619   if (strstr (argv[0], "p2p") != NULL)
620   {
621     is_p2p = GNUNET_YES;
622   }
623   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
624                       "test-chat", "nohelp", options, &run, NULL);
625   stop_arm (&p1);
626   GNUNET_CONTAINER_meta_data_destroy (alice_meta);
627   GNUNET_CONTAINER_meta_data_destroy (bob_meta);
628   GNUNET_CONTAINER_meta_data_destroy (carol_meta);
629   if (is_p2p)
630   {
631     GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-1/");
632     GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-2/");
633     GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-3/");
634   }
635   else
636     GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat/");
637   return err;
638 }
639
640 /* end of test_chat_private.c */