2 This file is part of GNUnet.
3 (C) 2005, 2006, 2007, 2008, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
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.
35 #include "gnunet_crypto_lib.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_arm_service.h"
38 #include "gnunet_chat_service.h"
40 #define VERBOSE GNUNET_NO
42 #define START_ARM GNUNET_YES
45 * How long until we give up on passing the test?
47 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
51 struct GNUNET_CONFIGURATION_Handle *cfg;
53 struct GNUNET_OS_Process *arm_proc;
59 struct GNUNET_CONTAINER_MetaData *meta;
61 GNUNET_HashCode *sender;
67 enum GNUNET_CHAT_MsgOptions opt;
69 uint32_t sequence_number;
71 struct GNUNET_TIME_Absolute timestamp;
73 GNUNET_SCHEDULER_Task next_task;
79 static struct PeerContext p1;
81 static struct PeerContext p2;
83 static GNUNET_HashCode alice;
85 static GNUNET_HashCode bob;
87 static struct GNUNET_CHAT_Room *alice_room;
89 static struct GNUNET_CHAT_Room *bob_room;
91 static struct GNUNET_CONTAINER_MetaData *alice_meta;
93 static struct GNUNET_CONTAINER_MetaData *bob_meta;
95 static struct Wanted alice_wanted;
97 static struct Wanted bob_wanted;
99 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
101 static GNUNET_SCHEDULER_TaskIdentifier wait_task;
117 setup_peer (struct PeerContext *p, const char *cfgname)
119 p->cfg = GNUNET_CONFIGURATION_create ();
122 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
123 "gnunet-service-arm",
127 "-c", cfgname, NULL);
129 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
134 stop_arm (struct PeerContext *p)
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_close (p->arm_proc);
146 GNUNET_CONFIGURATION_destroy (p->cfg);
151 abort_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
153 if (alice_room != NULL)
155 GNUNET_CHAT_leave_room (alice_room);
158 if (bob_room != NULL)
160 GNUNET_CHAT_leave_room (bob_room);
168 timeout_kill (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 printf ("Timed out, stopping the test.\n");
173 kill_task = GNUNET_SCHEDULER_NO_TASK;
174 if (wait_task != GNUNET_SCHEDULER_NO_TASK)
176 GNUNET_SCHEDULER_cancel (wait_task);
177 wait_task = GNUNET_SCHEDULER_NO_TASK;
179 GNUNET_SCHEDULER_add_continuation (&abort_test, NULL,
180 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
187 struct Wanted *want = cls;
190 printf ("%s has joined\n", want->me);
192 if (NULL != want->next_task)
193 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
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)
203 struct Wanted *want = cls;
204 GNUNET_HashCode sender;
207 printf ("%s - told that %s has %s\n", want->me,
209 NULL ? NULL : GNUNET_CONTAINER_meta_data_get_by_type (member_info,
210 EXTRACTOR_METATYPE_TITLE),
211 member_info == NULL ? "left" : "joined");
213 GNUNET_CRYPTO_hash (member_id,
214 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
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))
222 if (NULL != want->next_task)
223 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
227 GNUNET_SCHEDULER_cancel (kill_task);
228 kill_task = GNUNET_SCHEDULER_NO_TASK;
229 GNUNET_SCHEDULER_add_now (&abort_test, NULL);
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)
242 struct Wanted *want = cls;
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),
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
259 (timestamp.abs_value >= want->timestamp.abs_value))
261 if (NULL != want->next_task)
262 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
266 GNUNET_SCHEDULER_cancel (kill_task);
267 kill_task = GNUNET_SCHEDULER_NO_TASK;
268 GNUNET_SCHEDULER_add_now (&abort_test, NULL);
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)
280 struct Wanted *want = cls;
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),
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))
292 if (NULL != want->next_task)
293 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
297 GNUNET_SCHEDULER_cancel (kill_task);
298 kill_task = GNUNET_SCHEDULER_NO_TASK;
299 GNUNET_SCHEDULER_add_now (&abort_test, NULL);
306 wait_until_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
308 GNUNET_SCHEDULER_Task task = cls;
311 printf ("Waiting...\n");
315 wait_task = GNUNET_SCHEDULER_NO_TASK;
316 GNUNET_SCHEDULER_add_now (task, NULL);
320 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
321 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
322 &wait_until_ready, task);
327 disconnect_alice (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
330 printf ("Alice is leaving.\n");
334 GNUNET_CHAT_leave_room (alice_room);
336 GNUNET_SCHEDULER_cancel (kill_task);
337 kill_task = GNUNET_SCHEDULER_NO_TASK;
342 disconnect_bob (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
345 printf ("Bod is leaving.\n");
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);
359 set_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
361 is_ready = GNUNET_YES;
366 send_to_alice (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
369 printf ("Bob says 'Hi!'\n");
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,
385 send_to_bob (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
387 enum GNUNET_CHAT_MsgOptions options;
388 uint32_t *seq = NULL;
391 printf ("Alice says 'Hi!'\n");
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);
408 options = GNUNET_CHAT_MSG_ANONYMOUS;
409 bob_wanted.meta = NULL;
410 bob_wanted.sender = NULL;
411 bob_wanted.next_task = &disconnect_bob;
415 options = GNUNET_CHAT_MSG_AUTHENTICATED;
416 bob_wanted.meta = alice_meta;
417 bob_wanted.sender = &alice;
418 bob_wanted.next_task = &disconnect_bob;
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;
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);
436 prepare_for_alice_task (void *cls,
437 const struct GNUNET_SCHEDULER_TaskContext *tc)
439 bob_wanted.meta = alice_meta;
440 bob_wanted.sender = &alice;
441 bob_wanted.msg = NULL;
443 bob_wanted.next_task = &set_ready;
444 bob_wanted.next_task_cls = NULL;
449 join_bob_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
452 printf ("Bob joining\n");
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;
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)
470 GNUNET_SCHEDULER_cancel (kill_task);
471 kill_task = GNUNET_SCHEDULER_NO_TASK;
472 GNUNET_CHAT_leave_room (alice_room);
480 join_alice_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
483 printf ("Alice joining\n");
485 alice_wanted.next_task = &join_bob_task;
486 alice_wanted.next_task_cls = NULL;
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)
494 GNUNET_SCHEDULER_cancel (kill_task);
495 kill_task = GNUNET_SCHEDULER_NO_TASK;
502 run (void *cls, char *const *args, const char *cfgfile,
503 const struct GNUNET_CONFIGURATION_Handle *cfg)
507 setup_peer (&p1, "test_chat_peer1.conf");
508 setup_peer (&p2, "test_chat_peer2.conf");
511 setup_peer (&p1, "test_chat_data.conf");
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);
533 main (int argc, char *argv[])
535 char *const argvx[] = {
538 "test_chat_data.conf",
544 struct GNUNET_GETOPT_CommandLineOption options[] = {
545 GNUNET_GETOPT_OPTION_END
548 GNUNET_log_setup ("test_chat",
555 if (strstr (argv[0], "p2p") != NULL)
559 if (strstr (argv[0], "acknowledgment") != NULL)
561 is_ackn = GNUNET_YES;
563 else if (strstr (argv[0], "anonymous") != NULL)
565 is_anon = GNUNET_YES;
567 else if (strstr (argv[0], "authentication") != NULL)
569 is_auth = GNUNET_YES;
571 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
572 "test-chat", "nohelp", options, &run, NULL);
574 GNUNET_CONTAINER_meta_data_destroy (alice_meta);
575 GNUNET_CONTAINER_meta_data_destroy (bob_meta);
578 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-1/");
579 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-2/");
582 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat/");
586 /* end of test_chat.c */