2 This file is part of GNUnet.
3 (C) 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_private.c
23 * @brief testcase for private chatting
24 * @author Vitaly Minko
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_chat_service.h"
33 #define VERBOSE GNUNET_NO
35 #define START_ARM GNUNET_YES
38 * How long until we give up on passing the test?
40 #define KILL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
43 * How long until we give up on receiving somebody else's private message?
45 #define PM_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
49 struct GNUNET_CONFIGURATION_Handle *cfg;
51 struct GNUNET_OS_Process *arm_proc;
57 struct GNUNET_CONTAINER_MetaData *meta;
59 struct GNUNET_HashCode *sender;
62 * Alternative meta/sender is used when we expect join/leave notification
63 * from two peers and don't know which one will come first.
65 struct GNUNET_CONTAINER_MetaData *meta2;
67 struct GNUNET_HashCode *sender2;
73 enum GNUNET_CHAT_MsgOptions opt;
75 struct GNUNET_TIME_Absolute timestamp;
77 GNUNET_SCHEDULER_Task next_task;
83 static struct PeerContext p1;
85 static struct PeerContext p2;
87 static struct PeerContext p3;
89 static struct GNUNET_HashCode alice;
91 static struct GNUNET_HashCode bob;
93 static struct GNUNET_HashCode carol;
95 static struct GNUNET_CHAT_Room *alice_room;
97 static struct GNUNET_CHAT_Room *bob_room;
99 static struct GNUNET_CHAT_Room *carol_room;
101 static struct GNUNET_CONTAINER_MetaData *alice_meta;
103 static struct GNUNET_CONTAINER_MetaData *bob_meta;
105 static struct GNUNET_CONTAINER_MetaData *carol_meta;
107 static struct Wanted alice_wanted;
109 static struct Wanted bob_wanted;
111 static struct Wanted carol_wanted;
113 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
115 static GNUNET_SCHEDULER_TaskIdentifier finish_task;
117 static GNUNET_SCHEDULER_TaskIdentifier wait_task;
121 static int alice_ready;
123 static int bob_ready;
127 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *bob_public_key;
131 setup_peer (struct PeerContext *p, const char *cfgname)
135 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
136 p->cfg = GNUNET_CONFIGURATION_create ();
139 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, binary,
140 "gnunet-service-arm",
141 "-c", cfgname, NULL);
143 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
144 GNUNET_free (binary);
149 stop_arm (struct PeerContext *p)
152 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
153 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
154 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
155 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
157 GNUNET_OS_process_get_pid (p->arm_proc));
158 GNUNET_OS_process_destroy (p->arm_proc);
161 GNUNET_CONFIGURATION_destroy (p->cfg);
166 abort_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
168 if (alice_room != NULL)
170 GNUNET_CHAT_leave_room (alice_room);
173 if (bob_room != NULL)
175 GNUNET_CHAT_leave_room (bob_room);
178 if (carol_room != NULL)
180 GNUNET_CHAT_leave_room (carol_room);
188 timeout_kill (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
191 printf ("Timed out, stopping the test.\n");
193 kill_task = GNUNET_SCHEDULER_NO_TASK;
194 if (wait_task != GNUNET_SCHEDULER_NO_TASK)
196 GNUNET_SCHEDULER_cancel (wait_task);
197 wait_task = GNUNET_SCHEDULER_NO_TASK;
199 GNUNET_SCHEDULER_add_continuation (&abort_test, NULL,
200 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
207 struct Wanted *want = cls;
210 printf ("%s has joined\n", want->me);
212 if (NULL != want->next_task)
213 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
219 member_list_cb (void *cls, const struct GNUNET_CONTAINER_MetaData *member_info,
220 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *member_id,
221 enum GNUNET_CHAT_MsgOptions options)
223 struct Wanted *want = cls;
224 struct GNUNET_HashCode sender;
227 printf ("%s - told that %s has %s\n", want->me,
229 NULL ? NULL : GNUNET_CONTAINER_meta_data_get_by_type (member_info,
230 EXTRACTOR_METATYPE_TITLE),
231 member_info == NULL ? "left" : "joined");
233 GNUNET_CRYPTO_hash (member_id,
234 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
236 /* entertain both primary and an alternative sender/meta */
237 if (((0 == memcmp (&sender, want->sender, sizeof (struct GNUNET_HashCode))) ||
238 ((want->sender2 != NULL) &&
239 (0 == memcmp (&sender, want->sender2, sizeof (struct GNUNET_HashCode))))) &&
240 (((member_info == NULL) && (want->meta == NULL)) ||
241 ((member_info != NULL) &&
242 (((want->meta != NULL) &&
243 GNUNET_CONTAINER_meta_data_test_equal (member_info, want->meta)) ||
244 ((want->meta2 != NULL) &&
245 GNUNET_CONTAINER_meta_data_test_equal (member_info, want->meta2)))))
246 && (options == want->opt))
248 /* remember Bob's public key, we need it to send private message */
249 if (NULL == bob_public_key &&
250 (0 == memcmp (&bob, want->sender, sizeof (struct GNUNET_HashCode))))
252 GNUNET_memdup (member_id,
254 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
255 if (want->sender2 != NULL)
257 /* flush alternative sender */
258 if (0 == memcmp (&sender, want->sender, sizeof (struct GNUNET_HashCode)))
260 want->sender = want->sender2;
261 want->meta = want->meta2;
263 want->sender2 = NULL;
266 else if (NULL != want->next_task)
267 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
271 GNUNET_SCHEDULER_cancel (kill_task);
272 kill_task = GNUNET_SCHEDULER_NO_TASK;
273 GNUNET_SCHEDULER_add_now (&abort_test, NULL);
280 receive_cb (void *cls, struct GNUNET_CHAT_Room *room,
281 const struct GNUNET_HashCode * sender,
282 const struct GNUNET_CONTAINER_MetaData *meta, const char *message,
283 struct GNUNET_TIME_Absolute timestamp,
284 enum GNUNET_CHAT_MsgOptions options)
286 struct Wanted *want = cls;
289 printf ("%s - told that %s said '%s'\n", want->me,
290 meta == NULL ? NULL : GNUNET_CONTAINER_meta_data_get_by_type (meta,
291 EXTRACTOR_METATYPE_TITLE),
295 if ((want->msg != NULL) && (0 == strcmp (message, want->msg)) &&
296 (((sender == NULL) && (want->sender == NULL)) ||
297 ((sender != NULL) && (want->sender != NULL) &&
298 (0 == memcmp (sender, want->sender, sizeof (struct GNUNET_HashCode))))) &&
299 (GNUNET_CONTAINER_meta_data_test_equal (meta, want->meta)) &&
300 (options == want->opt) &&
301 /* Not == since the library sets the actual timestamp, so it may be
304 (timestamp.abs_value >= want->timestamp.abs_value))
306 if (NULL != want->next_task)
307 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
311 GNUNET_SCHEDULER_cancel (kill_task);
312 kill_task = GNUNET_SCHEDULER_NO_TASK;
313 GNUNET_SCHEDULER_cancel (finish_task);
314 finish_task = GNUNET_SCHEDULER_NO_TASK;
315 GNUNET_SCHEDULER_add_now (&abort_test, NULL);
322 wait_until_all_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
324 GNUNET_SCHEDULER_Task task = cls;
327 printf ("Waiting...\n");
329 if (alice_ready && bob_ready)
331 wait_task = GNUNET_SCHEDULER_NO_TASK;
332 GNUNET_SCHEDULER_add_now (task, NULL);
336 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
337 (GNUNET_TIME_UNIT_MILLISECONDS, 5000),
338 &wait_until_all_ready, task);
343 set_alice_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
345 alice_ready = GNUNET_YES;
350 set_bob_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
352 bob_ready = GNUNET_YES;
357 disconnect_alice (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
360 printf ("Alice is leaving.\n");
364 GNUNET_CHAT_leave_room (alice_room);
366 GNUNET_SCHEDULER_cancel (kill_task);
367 kill_task = GNUNET_SCHEDULER_NO_TASK;
372 disconnect_bob (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
375 printf ("Bod is leaving.\n");
379 alice_wanted.meta = NULL;
380 alice_wanted.sender = &bob;
381 alice_wanted.msg = NULL;
382 alice_wanted.opt = 0;
383 alice_wanted.next_task = &disconnect_alice;
384 alice_wanted.next_task_cls = NULL;
385 GNUNET_CHAT_leave_room (bob_room);
391 disconnect_carol (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
394 printf ("Carol is leaving.\n");
396 alice_wanted.meta = NULL;
397 alice_wanted.sender = &carol;
398 alice_wanted.msg = NULL;
399 alice_wanted.opt = 0;
400 alice_wanted.next_task = &set_alice_ready;
401 alice_wanted.next_task_cls = NULL;
402 alice_ready = GNUNET_NO;
403 bob_wanted.meta = NULL;
404 bob_wanted.sender = &carol;
405 bob_wanted.msg = NULL;
407 bob_wanted.next_task = &wait_until_all_ready;
408 bob_wanted.next_task_cls = &disconnect_bob;
409 bob_ready = GNUNET_YES;
410 GNUNET_CHAT_leave_room (carol_room);
416 send_from_alice_to_bob (void *cls,
417 const struct GNUNET_SCHEDULER_TaskContext *tc)
422 printf ("Alice says 'Hi!' to Bob\n");
424 alice_ready = GNUNET_YES;
425 bob_ready = GNUNET_NO;
426 bob_wanted.meta = alice_meta;
427 bob_wanted.sender = &alice;
428 bob_wanted.msg = "Hi Bob!";
429 bob_wanted.opt = GNUNET_CHAT_MSG_PRIVATE;
430 bob_wanted.next_task = &set_bob_ready;
431 bob_wanted.next_task_cls = NULL;
432 /* Carol should not receive this message */
433 carol_wanted.meta = NULL;
434 carol_wanted.sender = NULL;
435 carol_wanted.msg = NULL;
436 carol_wanted.opt = 0;
437 carol_wanted.next_task = NULL;
438 carol_wanted.next_task_cls = NULL;
439 GNUNET_CHAT_send_message (alice_room, "Hi Bob!", GNUNET_CHAT_MSG_PRIVATE,
440 bob_public_key, &seq);
442 GNUNET_SCHEDULER_add_delayed (PM_TIMEOUT, &wait_until_all_ready,
448 prepare_bob_for_alice_task (void *cls,
449 const struct GNUNET_SCHEDULER_TaskContext *tc)
451 bob_wanted.meta = alice_meta;
452 bob_wanted.sender = &alice;
453 bob_wanted.msg = NULL;
455 bob_wanted.next_task = &set_bob_ready;
456 bob_wanted.next_task_cls = NULL;
461 prepare_carol_for_alice_and_bob_task (void *cls,
462 const struct GNUNET_SCHEDULER_TaskContext
465 carol_wanted.meta = alice_meta;
466 carol_wanted.sender = &alice;
467 /* set alternative meta/sender since we don't know from which peer
468 * notification will come first */
469 carol_wanted.meta2 = bob_meta;
470 carol_wanted.sender2 = &bob;
471 carol_wanted.msg = NULL;
472 carol_wanted.opt = -1;
473 carol_wanted.next_task = &wait_until_all_ready;
474 carol_wanted.next_task_cls = &send_from_alice_to_bob;
479 join_carol_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
482 printf ("Carol joining\n");
484 alice_wanted.meta = carol_meta;
485 alice_wanted.sender = &carol;
486 alice_wanted.msg = NULL;
487 alice_wanted.opt = -1;
488 alice_wanted.next_task = &set_alice_ready;
489 alice_wanted.next_task_cls = NULL;
490 alice_ready = GNUNET_NO;
491 bob_wanted.meta = carol_meta;
492 bob_wanted.sender = &carol;
493 bob_wanted.msg = NULL;
495 bob_wanted.next_task = &set_bob_ready;
496 bob_wanted.next_task_cls = NULL;
497 bob_ready = GNUNET_NO;
498 carol_wanted.next_task = &prepare_carol_for_alice_and_bob_task;
499 carol_wanted.next_task_cls = NULL;
501 GNUNET_CHAT_join_room (is_p2p ? p3.cfg : p1.cfg, "carol", carol_meta,
502 "test", -1, &join_cb, &carol_wanted, &receive_cb,
503 &carol_wanted, &member_list_cb, &carol_wanted,
505 if (NULL == carol_room)
507 GNUNET_SCHEDULER_cancel (kill_task);
508 kill_task = GNUNET_SCHEDULER_NO_TASK;
509 GNUNET_CHAT_leave_room (alice_room);
511 GNUNET_CHAT_leave_room (bob_room);
519 join_bob_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
522 printf ("Bob joining\n");
524 alice_wanted.meta = bob_meta;
525 alice_wanted.sender = &bob;
526 alice_wanted.msg = NULL;
527 alice_wanted.opt = -1;
528 alice_wanted.next_task = &wait_until_all_ready;
529 alice_wanted.next_task_cls = &join_carol_task;
530 alice_ready = GNUNET_YES;
531 bob_wanted.next_task = &prepare_bob_for_alice_task;
532 bob_wanted.next_task_cls = NULL;
533 bob_ready = GNUNET_NO;
535 GNUNET_CHAT_join_room (is_p2p ? p2.cfg : p1.cfg, "bob", bob_meta, "test",
536 -1, &join_cb, &bob_wanted, &receive_cb,
537 &bob_wanted, &member_list_cb, &bob_wanted, NULL,
539 if (NULL == bob_room)
541 GNUNET_SCHEDULER_cancel (kill_task);
542 kill_task = GNUNET_SCHEDULER_NO_TASK;
543 GNUNET_CHAT_leave_room (alice_room);
551 join_alice_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
554 printf ("Alice joining\n");
556 alice_wanted.next_task = &join_bob_task;
557 alice_wanted.next_task_cls = NULL;
559 GNUNET_CHAT_join_room (p1.cfg, "alice", alice_meta, "test", -1, &join_cb,
560 &alice_wanted, &receive_cb, &alice_wanted,
561 &member_list_cb, &alice_wanted, NULL, NULL,
563 if (NULL == alice_room)
565 GNUNET_SCHEDULER_cancel (kill_task);
566 kill_task = GNUNET_SCHEDULER_NO_TASK;
573 run (void *cls, char *const *args, const char *cfgfile,
574 const struct GNUNET_CONFIGURATION_Handle *cfg)
578 setup_peer (&p1, "test_chat_peer1.conf");
579 setup_peer (&p2, "test_chat_peer2.conf");
580 setup_peer (&p3, "test_chat_peer3.conf");
583 setup_peer (&p1, "test_chat_data.conf");
585 memset (&alice_wanted, 0, sizeof (struct Wanted));
586 memset (&bob_wanted, 0, sizeof (struct Wanted));
587 memset (&carol_wanted, 0, sizeof (struct Wanted));
588 alice_wanted.me = "Alice";
589 bob_wanted.me = "Bob";
590 carol_wanted.me = "Carol";
591 alice_meta = GNUNET_CONTAINER_meta_data_create ();
592 GNUNET_CONTAINER_meta_data_insert (alice_meta, "<gnunet>",
593 EXTRACTOR_METATYPE_TITLE,
594 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
595 "Alice", strlen ("Alice") + 1);
596 bob_meta = GNUNET_CONTAINER_meta_data_create ();
597 GNUNET_CONTAINER_meta_data_insert (bob_meta, "<gnunet>",
598 EXTRACTOR_METATYPE_TITLE,
599 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
600 "Bob", strlen ("Bob") + 1);
601 carol_meta = GNUNET_CONTAINER_meta_data_create ();
602 GNUNET_CONTAINER_meta_data_insert (carol_meta, "<gnunet>",
603 EXTRACTOR_METATYPE_TITLE,
604 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
605 "Carol", strlen ("Carol") + 1);
606 kill_task = GNUNET_SCHEDULER_add_delayed (KILL_TIMEOUT, &timeout_kill, NULL);
607 GNUNET_SCHEDULER_add_now (&join_alice_task, NULL);
612 main (int argc, char *argv[])
614 char *const argvx[] = {
617 "test_chat_data.conf",
620 struct GNUNET_GETOPT_CommandLineOption options[] = {
621 GNUNET_GETOPT_OPTION_END
624 GNUNET_log_setup ("test_chat",
627 if (strstr (argv[0], "p2p") != NULL)
631 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
632 "test-chat", "nohelp", options, &run, NULL);
634 GNUNET_CONTAINER_meta_data_destroy (alice_meta);
635 GNUNET_CONTAINER_meta_data_destroy (bob_meta);
636 GNUNET_CONTAINER_meta_data_destroy (carol_meta);
639 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-1/");
640 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-2/");
641 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-3/");
644 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat/");
648 /* end of test_chat_private.c */