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 struct 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 struct GNUNET_HashCode alice;
85 static struct 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)
121 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
122 p->cfg = GNUNET_CONFIGURATION_create ();
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);
129 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
130 GNUNET_free (binary);
135 stop_arm (struct PeerContext *p)
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);
147 GNUNET_CONFIGURATION_destroy (p->cfg);
152 abort_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
154 if (alice_room != NULL)
156 GNUNET_CHAT_leave_room (alice_room);
159 if (bob_room != NULL)
161 GNUNET_CHAT_leave_room (bob_room);
169 timeout_kill (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
172 printf ("Timed out, stopping the test.\n");
174 kill_task = GNUNET_SCHEDULER_NO_TASK;
175 if (wait_task != GNUNET_SCHEDULER_NO_TASK)
177 GNUNET_SCHEDULER_cancel (wait_task);
178 wait_task = GNUNET_SCHEDULER_NO_TASK;
180 GNUNET_SCHEDULER_add_continuation (&abort_test, NULL,
181 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
188 struct Wanted *want = cls;
191 printf ("%s has joined\n", want->me);
193 if (NULL != want->next_task)
194 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
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)
204 struct Wanted *want = cls;
205 struct GNUNET_HashCode sender;
208 printf ("%s - told that %s has %s\n", want->me,
210 NULL ? NULL : GNUNET_CONTAINER_meta_data_get_by_type (member_info,
211 EXTRACTOR_METATYPE_TITLE),
212 member_info == NULL ? "left" : "joined");
214 GNUNET_CRYPTO_hash (member_id,
215 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
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))
223 if (NULL != want->next_task)
224 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
228 GNUNET_SCHEDULER_cancel (kill_task);
229 kill_task = GNUNET_SCHEDULER_NO_TASK;
230 GNUNET_SCHEDULER_add_now (&abort_test, NULL);
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)
243 struct Wanted *want = cls;
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),
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
260 (timestamp.abs_value >= want->timestamp.abs_value))
262 if (NULL != want->next_task)
263 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
267 GNUNET_SCHEDULER_cancel (kill_task);
268 kill_task = GNUNET_SCHEDULER_NO_TASK;
269 GNUNET_SCHEDULER_add_now (&abort_test, NULL);
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)
281 struct Wanted *want = cls;
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),
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))
293 if (NULL != want->next_task)
294 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
298 GNUNET_SCHEDULER_cancel (kill_task);
299 kill_task = GNUNET_SCHEDULER_NO_TASK;
300 GNUNET_SCHEDULER_add_now (&abort_test, NULL);
307 wait_until_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
309 GNUNET_SCHEDULER_Task task = cls;
312 printf ("Waiting...\n");
316 wait_task = GNUNET_SCHEDULER_NO_TASK;
317 GNUNET_SCHEDULER_add_now (task, NULL);
321 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
322 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
323 &wait_until_ready, task);
328 disconnect_alice (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
331 printf ("Alice is leaving.\n");
335 GNUNET_CHAT_leave_room (alice_room);
337 GNUNET_SCHEDULER_cancel (kill_task);
338 kill_task = GNUNET_SCHEDULER_NO_TASK;
343 disconnect_bob (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
346 printf ("Bod is leaving.\n");
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);
360 set_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
362 is_ready = GNUNET_YES;
367 send_to_alice (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
370 printf ("Bob says 'Hi!'\n");
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,
386 send_to_bob (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
388 enum GNUNET_CHAT_MsgOptions options;
389 uint32_t *seq = NULL;
392 printf ("Alice says 'Hi!'\n");
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);
409 options = GNUNET_CHAT_MSG_ANONYMOUS;
410 bob_wanted.meta = NULL;
411 bob_wanted.sender = NULL;
412 bob_wanted.next_task = &disconnect_bob;
416 options = GNUNET_CHAT_MSG_AUTHENTICATED;
417 bob_wanted.meta = alice_meta;
418 bob_wanted.sender = &alice;
419 bob_wanted.next_task = &disconnect_bob;
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;
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);
437 prepare_for_alice_task (void *cls,
438 const struct GNUNET_SCHEDULER_TaskContext *tc)
440 bob_wanted.meta = alice_meta;
441 bob_wanted.sender = &alice;
442 bob_wanted.msg = NULL;
444 bob_wanted.next_task = &set_ready;
445 bob_wanted.next_task_cls = NULL;
450 join_bob_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
453 printf ("Bob joining\n");
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;
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)
471 GNUNET_SCHEDULER_cancel (kill_task);
472 kill_task = GNUNET_SCHEDULER_NO_TASK;
473 GNUNET_CHAT_leave_room (alice_room);
481 join_alice_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
484 printf ("Alice joining\n");
486 alice_wanted.next_task = &join_bob_task;
487 alice_wanted.next_task_cls = NULL;
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)
495 GNUNET_SCHEDULER_cancel (kill_task);
496 kill_task = GNUNET_SCHEDULER_NO_TASK;
503 run (void *cls, char *const *args, const char *cfgfile,
504 const struct GNUNET_CONFIGURATION_Handle *cfg)
508 setup_peer (&p1, "test_chat_peer1.conf");
509 setup_peer (&p2, "test_chat_peer2.conf");
512 setup_peer (&p1, "test_chat_data.conf");
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);
534 main (int argc, char *argv[])
536 char *const argvx[] = {
539 "test_chat_data.conf",
545 struct GNUNET_GETOPT_CommandLineOption options[] = {
546 GNUNET_GETOPT_OPTION_END
549 GNUNET_log_setup ("test_chat",
556 if (strstr (argv[0], "p2p") != NULL)
560 if (strstr (argv[0], "acknowledgment") != NULL)
562 is_ackn = GNUNET_YES;
564 else if (strstr (argv[0], "anonymous") != NULL)
566 is_anon = GNUNET_YES;
568 else if (strstr (argv[0], "authentication") != NULL)
570 is_auth = GNUNET_YES;
572 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
573 "test-chat", "nohelp", options, &run, NULL);
575 GNUNET_CONTAINER_meta_data_destroy (alice_meta);
576 GNUNET_CONTAINER_meta_data_destroy (bob_meta);
579 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-1/");
580 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-2/");
583 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat/");
587 /* end of test_chat.c */