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