-simplifying core API (#2400)
[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 =
122       GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
123                                "gnunet-service-arm",
124 #if VERBOSE
125                                "-L", "DEBUG",
126 #endif
127                                "-c", cfgname, NULL);
128 #endif
129   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
130 }
131
132
133 static void
134 stop_arm (struct PeerContext *p)
135 {
136 #if START_ARM
137   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
138     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
139   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
140     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
141   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
142               GNUNET_OS_process_get_pid (p->arm_proc));
143   GNUNET_OS_process_destroy (p->arm_proc);
144   p->arm_proc = NULL;
145 #endif
146   GNUNET_CONFIGURATION_destroy (p->cfg);
147 }
148
149
150 static void
151 abort_test (void *cls, 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, const struct GNUNET_SCHEDULER_TaskContext *tc)
169 {
170 #if VERBOSE
171   printf ("Timed out, stopping the test.\n");
172 #endif
173   kill_task = GNUNET_SCHEDULER_NO_TASK;
174   if (wait_task != GNUNET_SCHEDULER_NO_TASK)
175   {
176     GNUNET_SCHEDULER_cancel (wait_task);
177     wait_task = GNUNET_SCHEDULER_NO_TASK;
178   }
179   GNUNET_SCHEDULER_add_continuation (&abort_test, NULL,
180                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
181 }
182
183
184 static int
185 join_cb (void *cls)
186 {
187   struct Wanted *want = cls;
188
189 #if VERBOSE
190   printf ("%s has joined\n", want->me);
191 #endif
192   if (NULL != want->next_task)
193     GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
194   return GNUNET_OK;
195 }
196
197
198 static int
199 member_list_cb (void *cls, const struct GNUNET_CONTAINER_MetaData *member_info,
200                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *member_id,
201                 enum GNUNET_CHAT_MsgOptions options)
202 {
203   struct Wanted *want = cls;
204   GNUNET_HashCode sender;
205
206 #if VERBOSE
207   printf ("%s - told that %s has %s\n", want->me,
208           member_info ==
209           NULL ? NULL : GNUNET_CONTAINER_meta_data_get_by_type (member_info,
210                                                                 EXTRACTOR_METATYPE_TITLE),
211           member_info == NULL ? "left" : "joined");
212 #endif
213   GNUNET_CRYPTO_hash (member_id,
214                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
215                       &sender);
216   if ((0 == memcmp (&sender, want->sender, sizeof (GNUNET_HashCode))) &&
217       (((member_info == NULL) && (want->meta == NULL)) ||
218        ((member_info != NULL) && (want->meta != NULL) &&
219         (GNUNET_CONTAINER_meta_data_test_equal (member_info, want->meta)))) &&
220       (options == want->opt))
221   {
222     if (NULL != want->next_task)
223       GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
224   }
225   else
226   {
227     GNUNET_SCHEDULER_cancel (kill_task);
228     kill_task = GNUNET_SCHEDULER_NO_TASK;
229     GNUNET_SCHEDULER_add_now (&abort_test, NULL);
230   }
231   return GNUNET_OK;
232 }
233
234
235 static int
236 receive_cb (void *cls, struct GNUNET_CHAT_Room *room,
237             const GNUNET_HashCode * sender,
238             const struct GNUNET_CONTAINER_MetaData *meta, const char *message,
239             struct GNUNET_TIME_Absolute timestamp,
240             enum GNUNET_CHAT_MsgOptions options)
241 {
242   struct Wanted *want = cls;
243
244 #if VERBOSE
245   printf ("%s - told that %s said %s\n", want->me,
246           meta == NULL ? NULL : GNUNET_CONTAINER_meta_data_get_by_type (meta,
247                                                                         EXTRACTOR_METATYPE_TITLE),
248           message);
249 #endif
250   if ((0 == strcmp (message, want->msg)) &&
251       (((sender == NULL) && (want->sender == NULL)) ||
252        ((sender != NULL) && (want->sender != NULL) &&
253         (0 == memcmp (sender, want->sender, sizeof (GNUNET_HashCode))))) &&
254       (GNUNET_CONTAINER_meta_data_test_equal (meta, want->meta)) &&
255       (options == want->opt) &&
256       /* Not == since the library sets the actual timestamp, so it may be
257        * slightly greater
258        */
259       (timestamp.abs_value >= want->timestamp.abs_value))
260   {
261     if (NULL != want->next_task)
262       GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
263   }
264   else
265   {
266     GNUNET_SCHEDULER_cancel (kill_task);
267     kill_task = GNUNET_SCHEDULER_NO_TASK;
268     GNUNET_SCHEDULER_add_now (&abort_test, NULL);
269   }
270   return GNUNET_OK;
271 }
272
273
274 static int
275 confirmation_cb (void *cls, struct GNUNET_CHAT_Room *room,
276                  uint32_t orig_seq_number,
277                  struct GNUNET_TIME_Absolute timestamp,
278                  const GNUNET_HashCode * receiver)
279 {
280   struct Wanted *want = cls;
281
282 #if VERBOSE
283   printf ("%s - told that %s acknowledged message #%d\n", want->me,
284           GNUNET_CONTAINER_meta_data_get_by_type (want->meta,
285                                                   EXTRACTOR_METATYPE_TITLE),
286           orig_seq_number);
287 #endif
288   if ((0 == memcmp (receiver, want->sender, sizeof (GNUNET_HashCode))) &&
289       (orig_seq_number == want->sequence_number) &&
290       (timestamp.abs_value >= want->timestamp.abs_value))
291   {
292     if (NULL != want->next_task)
293       GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
294   }
295   else
296   {
297     GNUNET_SCHEDULER_cancel (kill_task);
298     kill_task = GNUNET_SCHEDULER_NO_TASK;
299     GNUNET_SCHEDULER_add_now (&abort_test, NULL);
300   }
301   return GNUNET_OK;
302 }
303
304
305 static void
306 wait_until_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
307 {
308   GNUNET_SCHEDULER_Task task = cls;
309
310 #if VERBOSE
311   printf ("Waiting...\n");
312 #endif
313   if (is_ready)
314   {
315     wait_task = GNUNET_SCHEDULER_NO_TASK;
316     GNUNET_SCHEDULER_add_now (task, NULL);
317   }
318   else
319     wait_task =
320         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
321                                       (GNUNET_TIME_UNIT_MILLISECONDS, 50),
322                                       &wait_until_ready, task);
323 }
324
325
326 static void
327 disconnect_alice (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
328 {
329 #if VERBOSE
330   printf ("Alice is leaving.\n");
331 #endif
332   if (is_p2p)
333     stop_arm (&p2);
334   GNUNET_CHAT_leave_room (alice_room);
335   alice_room = NULL;
336   GNUNET_SCHEDULER_cancel (kill_task);
337   kill_task = GNUNET_SCHEDULER_NO_TASK;
338 }
339
340
341 static void
342 disconnect_bob (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
343 {
344 #if VERBOSE
345   printf ("Bod is leaving.\n");
346 #endif
347   alice_wanted.meta = NULL;
348   alice_wanted.sender = &bob;
349   alice_wanted.msg = NULL;
350   alice_wanted.opt = 0;
351   alice_wanted.next_task = &disconnect_alice;
352   alice_wanted.next_task_cls = NULL;
353   GNUNET_CHAT_leave_room (bob_room);
354   bob_room = NULL;
355 }
356
357
358 static void
359 set_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
360 {
361   is_ready = GNUNET_YES;
362 }
363
364
365 static void
366 send_to_alice (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
367 {
368 #if VERBOSE
369   printf ("Bob says 'Hi!'\n");
370 #endif
371
372   alice_wanted.meta = bob_meta;
373   alice_wanted.sender = &bob;
374   alice_wanted.msg = "Hi Alice!";
375   alice_wanted.opt = GNUNET_CHAT_MSG_OPTION_NONE;
376   alice_wanted.timestamp = GNUNET_TIME_absolute_get ();
377   alice_wanted.next_task = &disconnect_bob;
378   alice_wanted.next_task_cls = NULL;
379   GNUNET_CHAT_send_message (bob_room, "Hi Alice!", GNUNET_CHAT_MSG_OPTION_NONE,
380                             NULL, NULL);
381 }
382
383
384 static void
385 send_to_bob (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
386 {
387   enum GNUNET_CHAT_MsgOptions options;
388   uint32_t *seq = NULL;
389
390 #if VERBOSE
391   printf ("Alice says 'Hi!'\n");
392 #endif
393   if (is_ackn)
394   {
395     options = GNUNET_CHAT_MSG_ACKNOWLEDGED;
396     alice_wanted.meta = bob_meta;
397     alice_wanted.sender = &bob;
398     alice_wanted.timestamp = GNUNET_TIME_absolute_get ();
399     alice_wanted.next_task = &disconnect_bob;
400     alice_wanted.next_task_cls = NULL;
401     bob_wanted.meta = alice_meta;
402     bob_wanted.sender = &alice;
403     bob_wanted.next_task = NULL;
404     seq = &(alice_wanted.sequence_number);
405   }
406   else if (is_anon)
407   {
408     options = GNUNET_CHAT_MSG_ANONYMOUS;
409     bob_wanted.meta = NULL;
410     bob_wanted.sender = NULL;
411     bob_wanted.next_task = &disconnect_bob;
412   }
413   else if (is_auth)
414   {
415     options = GNUNET_CHAT_MSG_AUTHENTICATED;
416     bob_wanted.meta = alice_meta;
417     bob_wanted.sender = &alice;
418     bob_wanted.next_task = &disconnect_bob;
419   }
420   else
421   {
422     options = GNUNET_CHAT_MSG_OPTION_NONE;
423     bob_wanted.meta = alice_meta;
424     bob_wanted.sender = &alice;
425     bob_wanted.next_task = &send_to_alice;
426   }
427   bob_wanted.msg = "Hi Bob!";
428   bob_wanted.opt = options;
429   bob_wanted.timestamp = GNUNET_TIME_absolute_get ();
430   bob_wanted.next_task_cls = NULL;
431   GNUNET_CHAT_send_message (alice_room, "Hi Bob!", options, NULL, seq);
432 }
433
434
435 static void
436 prepare_for_alice_task (void *cls,
437                         const struct GNUNET_SCHEDULER_TaskContext *tc)
438 {
439   bob_wanted.meta = alice_meta;
440   bob_wanted.sender = &alice;
441   bob_wanted.msg = NULL;
442   bob_wanted.opt = -1;
443   bob_wanted.next_task = &set_ready;
444   bob_wanted.next_task_cls = NULL;
445 }
446
447
448 static void
449 join_bob_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
450 {
451 #if VERBOSE
452   printf ("Bob joining\n");
453 #endif
454   alice_wanted.meta = bob_meta;
455   alice_wanted.sender = &bob;
456   alice_wanted.msg = NULL;
457   alice_wanted.opt = -1;
458   alice_wanted.next_task = &wait_until_ready;
459   alice_wanted.next_task_cls = &send_to_bob;
460   bob_wanted.next_task = &prepare_for_alice_task;
461   bob_wanted.next_task_cls = NULL;
462   is_ready = GNUNET_NO;
463   bob_room =
464       GNUNET_CHAT_join_room (is_p2p ? p2.cfg : p1.cfg, "bob", bob_meta, "test",
465                              -1, &join_cb, &bob_wanted, &receive_cb,
466                              &bob_wanted, &member_list_cb, &bob_wanted,
467                              &confirmation_cb, &bob_wanted, &bob);
468   if (NULL == bob_room)
469   {
470     GNUNET_SCHEDULER_cancel (kill_task);
471     kill_task = GNUNET_SCHEDULER_NO_TASK;
472     GNUNET_CHAT_leave_room (alice_room);
473     alice_room = NULL;
474     err = 1;
475   }
476 }
477
478
479 static void
480 join_alice_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
481 {
482 #if VERBOSE
483   printf ("Alice joining\n");
484 #endif
485   alice_wanted.next_task = &join_bob_task;
486   alice_wanted.next_task_cls = NULL;
487   alice_room =
488       GNUNET_CHAT_join_room (p1.cfg, "alice", alice_meta, "test", -1, &join_cb,
489                              &alice_wanted, &receive_cb, &alice_wanted,
490                              &member_list_cb, &alice_wanted, &confirmation_cb,
491                              &alice_wanted, &alice);
492   if (NULL == alice_room)
493   {
494     GNUNET_SCHEDULER_cancel (kill_task);
495     kill_task = GNUNET_SCHEDULER_NO_TASK;
496     err = 1;
497   }
498 }
499
500
501 static void
502 run (void *cls, char *const *args, const char *cfgfile,
503      const struct GNUNET_CONFIGURATION_Handle *cfg)
504 {
505   if (is_p2p)
506   {
507     setup_peer (&p1, "test_chat_peer1.conf");
508     setup_peer (&p2, "test_chat_peer2.conf");
509   }
510   else
511     setup_peer (&p1, "test_chat_data.conf");
512
513   memset (&alice_wanted, 0, sizeof (struct Wanted));
514   memset (&bob_wanted, 0, sizeof (struct Wanted));
515   alice_wanted.me = "Alice";
516   bob_wanted.me = "Bob";
517   alice_meta = GNUNET_CONTAINER_meta_data_create ();
518   GNUNET_CONTAINER_meta_data_insert (alice_meta, "<gnunet>",
519                                      EXTRACTOR_METATYPE_TITLE,
520                                      EXTRACTOR_METAFORMAT_UTF8, "text/plain",
521                                      "Alice", strlen ("Alice") + 1);
522   bob_meta = GNUNET_CONTAINER_meta_data_create ();
523   GNUNET_CONTAINER_meta_data_insert (bob_meta, "<gnunet>",
524                                      EXTRACTOR_METATYPE_TITLE,
525                                      EXTRACTOR_METAFORMAT_UTF8, "text/plain",
526                                      "Bob", strlen ("Bob") + 1);
527   kill_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill, NULL);
528   GNUNET_SCHEDULER_add_now (&join_alice_task, NULL);
529 }
530
531
532 int
533 main (int argc, char *argv[])
534 {
535   char *const argvx[] = {
536     "test-chat",
537     "-c",
538     "test_chat_data.conf",
539 #if VERBOSE
540     "-L", "DEBUG",
541 #endif
542     NULL
543   };
544   struct GNUNET_GETOPT_CommandLineOption options[] = {
545     GNUNET_GETOPT_OPTION_END
546   };
547
548   GNUNET_log_setup ("test_chat",
549 #if VERBOSE
550                     "DEBUG",
551 #else
552                     "WARNING",
553 #endif
554                     NULL);
555   if (strstr (argv[0], "p2p") != NULL)
556   {
557     is_p2p = GNUNET_YES;
558   }
559   if (strstr (argv[0], "acknowledgment") != NULL)
560   {
561     is_ackn = GNUNET_YES;
562   }
563   else if (strstr (argv[0], "anonymous") != NULL)
564   {
565     is_anon = GNUNET_YES;
566   }
567   else if (strstr (argv[0], "authentication") != NULL)
568   {
569     is_auth = GNUNET_YES;
570   }
571   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
572                       "test-chat", "nohelp", options, &run, NULL);
573   stop_arm (&p1);
574   GNUNET_CONTAINER_meta_data_destroy (alice_meta);
575   GNUNET_CONTAINER_meta_data_destroy (bob_meta);
576   if (is_p2p)
577   {
578     GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-1/");
579     GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-2/");
580   }
581   else
582     GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat/");
583   return err;
584 }
585
586 /* end of test_chat.c */