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"
41 * How long until we give up on passing the test?
43 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
47 struct GNUNET_CONFIGURATION_Handle *cfg;
48 struct GNUNET_OS_Process *arm_proc;
53 struct GNUNET_CONTAINER_MetaData *meta;
55 struct GNUNET_HashCode *sender;
61 enum GNUNET_CHAT_MsgOptions opt;
63 uint32_t sequence_number;
65 struct GNUNET_TIME_Absolute timestamp;
67 GNUNET_SCHEDULER_Task next_task;
73 static struct PeerContext p1;
75 static struct PeerContext p2;
77 static struct GNUNET_HashCode alice;
79 static struct GNUNET_HashCode bob;
81 static struct GNUNET_CHAT_Room *alice_room;
83 static struct GNUNET_CHAT_Room *bob_room;
85 static struct GNUNET_CONTAINER_MetaData *alice_meta;
87 static struct GNUNET_CONTAINER_MetaData *bob_meta;
89 static struct Wanted alice_wanted;
91 static struct Wanted bob_wanted;
93 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
95 static GNUNET_SCHEDULER_TaskIdentifier wait_task;
111 setup_peer (struct PeerContext *p, const char *cfgname)
115 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
116 p->cfg = GNUNET_CONFIGURATION_create ();
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);
127 stop_arm (struct PeerContext *p)
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);
137 GNUNET_CONFIGURATION_destroy (p->cfg);
142 abort_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 if (alice_room != NULL)
146 GNUNET_CHAT_leave_room (alice_room);
149 if (bob_room != NULL)
151 GNUNET_CHAT_leave_room (bob_room);
159 timeout_kill (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
162 "Timed out, stopping the test.\n");
163 kill_task = GNUNET_SCHEDULER_NO_TASK;
164 if (wait_task != GNUNET_SCHEDULER_NO_TASK)
166 GNUNET_SCHEDULER_cancel (wait_task);
167 wait_task = GNUNET_SCHEDULER_NO_TASK;
169 GNUNET_SCHEDULER_add_continuation (&abort_test, NULL,
170 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
177 struct Wanted *want = cls;
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);
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)
192 struct Wanted *want = cls;
193 struct GNUNET_HashCode sender;
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 "%s - told that %s has %s\n", want->me,
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),
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))
210 if (NULL != want->next_task)
211 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
215 GNUNET_SCHEDULER_cancel (kill_task);
216 kill_task = GNUNET_SCHEDULER_NO_TASK;
217 GNUNET_SCHEDULER_add_now (&abort_test, NULL);
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)
230 struct Wanted *want = cls;
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
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),
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
247 (timestamp.abs_value >= want->timestamp.abs_value))
249 if (NULL != want->next_task)
250 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
254 GNUNET_SCHEDULER_cancel (kill_task);
255 kill_task = GNUNET_SCHEDULER_NO_TASK;
256 GNUNET_SCHEDULER_add_now (&abort_test, NULL);
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)
268 struct Wanted *want = cls;
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),
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))
279 if (NULL != want->next_task)
280 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
284 GNUNET_SCHEDULER_cancel (kill_task);
285 kill_task = GNUNET_SCHEDULER_NO_TASK;
286 GNUNET_SCHEDULER_add_now (&abort_test, NULL);
293 wait_until_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
295 GNUNET_SCHEDULER_Task task = cls;
299 wait_task = GNUNET_SCHEDULER_NO_TASK;
300 GNUNET_SCHEDULER_add_now (task, NULL);
304 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
305 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
306 &wait_until_ready, task);
311 disconnect_alice (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Alice is leaving.\n");
317 GNUNET_CHAT_leave_room (alice_room);
319 GNUNET_SCHEDULER_cancel (kill_task);
320 kill_task = GNUNET_SCHEDULER_NO_TASK;
325 disconnect_bob (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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);
341 set_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
343 is_ready = GNUNET_YES;
348 send_to_alice (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
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,
365 send_to_bob (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
367 enum GNUNET_CHAT_MsgOptions options;
368 uint32_t *seq = NULL;
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Alice says 'Hi!'\n");
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);
387 options = GNUNET_CHAT_MSG_ANONYMOUS;
388 bob_wanted.meta = NULL;
389 bob_wanted.sender = NULL;
390 bob_wanted.next_task = &disconnect_bob;
394 options = GNUNET_CHAT_MSG_AUTHENTICATED;
395 bob_wanted.meta = alice_meta;
396 bob_wanted.sender = &alice;
397 bob_wanted.next_task = &disconnect_bob;
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;
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);
415 prepare_for_alice_task (void *cls,
416 const struct GNUNET_SCHEDULER_TaskContext *tc)
418 bob_wanted.meta = alice_meta;
419 bob_wanted.sender = &alice;
420 bob_wanted.msg = NULL;
422 bob_wanted.next_task = &set_ready;
423 bob_wanted.next_task_cls = NULL;
428 join_bob_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
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;
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)
448 GNUNET_SCHEDULER_cancel (kill_task);
449 kill_task = GNUNET_SCHEDULER_NO_TASK;
450 GNUNET_CHAT_leave_room (alice_room);
458 join_alice_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 alice_wanted.next_task = &join_bob_task;
463 alice_wanted.next_task_cls = NULL;
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)
471 GNUNET_SCHEDULER_cancel (kill_task);
472 kill_task = GNUNET_SCHEDULER_NO_TASK;
479 run (void *cls, char *const *args, const char *cfgfile,
480 const struct GNUNET_CONFIGURATION_Handle *cfg)
484 setup_peer (&p1, "test_chat_peer1.conf");
485 setup_peer (&p2, "test_chat_peer2.conf");
488 setup_peer (&p1, "test_chat_data.conf");
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);
510 main (int argc, char *argv[])
512 char *const argvx[] = {
515 "test_chat_data.conf",
518 struct GNUNET_GETOPT_CommandLineOption options[] = {
519 GNUNET_GETOPT_OPTION_END
522 GNUNET_log_setup ("test_chat",
525 if (strstr (argv[0], "p2p") != NULL)
529 if (strstr (argv[0], "acknowledgment") != NULL)
531 is_ackn = GNUNET_YES;
533 else if (strstr (argv[0], "anonymous") != NULL)
535 is_anon = GNUNET_YES;
537 else if (strstr (argv[0], "authentication") != NULL)
539 is_auth = GNUNET_YES;
541 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
542 "test-chat", "nohelp", options, &run, NULL);
544 GNUNET_CONTAINER_meta_data_destroy (alice_meta);
545 GNUNET_CONTAINER_meta_data_destroy (bob_meta);
548 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-1/");
549 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-2/");
552 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat/");
556 /* end of test_chat.c */