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