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