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
36 * How long until we give up on passing the test?
38 #define KILL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
41 * How long until we give up on receiving somebody else's private message?
43 #define PM_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
47 struct GNUNET_CONFIGURATION_Handle *cfg;
48 struct GNUNET_OS_Process *arm_proc;
53 struct GNUNET_CONTAINER_MetaData *meta;
55 struct GNUNET_HashCode *sender;
58 * Alternative meta/sender is used when we expect join/leave notification
59 * from two peers and don't know which one will come first.
61 struct GNUNET_CONTAINER_MetaData *meta2;
63 struct GNUNET_HashCode *sender2;
69 enum GNUNET_CHAT_MsgOptions opt;
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 PeerContext p3;
85 static struct GNUNET_HashCode alice;
87 static struct GNUNET_HashCode bob;
89 static struct GNUNET_HashCode carol;
91 static struct GNUNET_CHAT_Room *alice_room;
93 static struct GNUNET_CHAT_Room *bob_room;
95 static struct GNUNET_CHAT_Room *carol_room;
97 static struct GNUNET_CONTAINER_MetaData *alice_meta;
99 static struct GNUNET_CONTAINER_MetaData *bob_meta;
101 static struct GNUNET_CONTAINER_MetaData *carol_meta;
103 static struct Wanted alice_wanted;
105 static struct Wanted bob_wanted;
107 static struct Wanted carol_wanted;
109 static GNUNET_SCHEDULER_TaskIdentifier kill_task;
111 static GNUNET_SCHEDULER_TaskIdentifier finish_task;
113 static GNUNET_SCHEDULER_TaskIdentifier wait_task;
117 static int alice_ready;
119 static int bob_ready;
123 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *bob_public_key;
127 setup_peer (struct PeerContext *p, const char *cfgname)
131 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
132 p->cfg = GNUNET_CONFIGURATION_create ();
134 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, binary,
135 "gnunet-service-arm",
136 "-c", cfgname, NULL);
137 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
138 GNUNET_free (binary);
143 stop_arm (struct PeerContext *p)
145 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
146 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
147 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
148 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
150 GNUNET_OS_process_get_pid (p->arm_proc));
151 GNUNET_OS_process_destroy (p->arm_proc);
153 GNUNET_CONFIGURATION_destroy (p->cfg);
158 abort_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 if (alice_room != NULL)
162 GNUNET_CHAT_leave_room (alice_room);
165 if (bob_room != NULL)
167 GNUNET_CHAT_leave_room (bob_room);
170 if (carol_room != NULL)
172 GNUNET_CHAT_leave_room (carol_room);
180 timeout_kill (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
183 printf ("Timed out, stopping the test.\n");
185 kill_task = GNUNET_SCHEDULER_NO_TASK;
186 if (wait_task != GNUNET_SCHEDULER_NO_TASK)
188 GNUNET_SCHEDULER_cancel (wait_task);
189 wait_task = GNUNET_SCHEDULER_NO_TASK;
191 GNUNET_SCHEDULER_add_continuation (&abort_test, NULL,
192 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
199 struct Wanted *want = cls;
202 printf ("%s has joined\n", want->me);
204 if (NULL != want->next_task)
205 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
211 member_list_cb (void *cls, const struct GNUNET_CONTAINER_MetaData *member_info,
212 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *member_id,
213 enum GNUNET_CHAT_MsgOptions options)
215 struct Wanted *want = cls;
216 struct GNUNET_HashCode sender;
219 printf ("%s - told that %s has %s\n", want->me,
221 NULL ? NULL : GNUNET_CONTAINER_meta_data_get_by_type (member_info,
222 EXTRACTOR_METATYPE_TITLE),
223 member_info == NULL ? "left" : "joined");
225 GNUNET_CRYPTO_hash (member_id,
226 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
228 /* entertain both primary and an alternative sender/meta */
229 if (((0 == memcmp (&sender, want->sender, sizeof (struct GNUNET_HashCode))) ||
230 ((want->sender2 != NULL) &&
231 (0 == memcmp (&sender, want->sender2, sizeof (struct GNUNET_HashCode))))) &&
232 (((member_info == NULL) && (want->meta == NULL)) ||
233 ((member_info != NULL) &&
234 (((want->meta != NULL) &&
235 GNUNET_CONTAINER_meta_data_test_equal (member_info, want->meta)) ||
236 ((want->meta2 != NULL) &&
237 GNUNET_CONTAINER_meta_data_test_equal (member_info, want->meta2)))))
238 && (options == want->opt))
240 /* remember Bob's public key, we need it to send private message */
241 if (NULL == bob_public_key &&
242 (0 == memcmp (&bob, want->sender, sizeof (struct GNUNET_HashCode))))
244 GNUNET_memdup (member_id,
246 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
247 if (want->sender2 != NULL)
249 /* flush alternative sender */
250 if (0 == memcmp (&sender, want->sender, sizeof (struct GNUNET_HashCode)))
252 want->sender = want->sender2;
253 want->meta = want->meta2;
255 want->sender2 = NULL;
258 else if (NULL != want->next_task)
259 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
263 GNUNET_SCHEDULER_cancel (kill_task);
264 kill_task = GNUNET_SCHEDULER_NO_TASK;
265 GNUNET_SCHEDULER_add_now (&abort_test, NULL);
272 receive_cb (void *cls, struct GNUNET_CHAT_Room *room,
273 const struct GNUNET_HashCode * sender,
274 const struct GNUNET_CONTAINER_MetaData *meta, const char *message,
275 struct GNUNET_TIME_Absolute timestamp,
276 enum GNUNET_CHAT_MsgOptions options)
278 struct Wanted *want = cls;
281 printf ("%s - told that %s said '%s'\n", want->me,
282 meta == NULL ? NULL : GNUNET_CONTAINER_meta_data_get_by_type (meta,
283 EXTRACTOR_METATYPE_TITLE),
287 if ((want->msg != NULL) && (0 == strcmp (message, want->msg)) &&
288 (((sender == NULL) && (want->sender == NULL)) ||
289 ((sender != NULL) && (want->sender != NULL) &&
290 (0 == memcmp (sender, want->sender, sizeof (struct GNUNET_HashCode))))) &&
291 (GNUNET_CONTAINER_meta_data_test_equal (meta, want->meta)) &&
292 (options == want->opt) &&
293 /* Not == since the library sets the actual timestamp, so it may be
296 (timestamp.abs_value >= want->timestamp.abs_value))
298 if (NULL != want->next_task)
299 GNUNET_SCHEDULER_add_now (want->next_task, want->next_task_cls);
303 GNUNET_SCHEDULER_cancel (kill_task);
304 kill_task = GNUNET_SCHEDULER_NO_TASK;
305 GNUNET_SCHEDULER_cancel (finish_task);
306 finish_task = GNUNET_SCHEDULER_NO_TASK;
307 GNUNET_SCHEDULER_add_now (&abort_test, NULL);
314 wait_until_all_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
316 GNUNET_SCHEDULER_Task task = cls;
319 printf ("Waiting...\n");
321 if (alice_ready && bob_ready)
323 wait_task = GNUNET_SCHEDULER_NO_TASK;
324 GNUNET_SCHEDULER_add_now (task, NULL);
328 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
329 (GNUNET_TIME_UNIT_MILLISECONDS, 5000),
330 &wait_until_all_ready, task);
335 set_alice_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
337 alice_ready = GNUNET_YES;
342 set_bob_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
344 bob_ready = GNUNET_YES;
349 disconnect_alice (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
352 printf ("Alice is leaving.\n");
356 GNUNET_CHAT_leave_room (alice_room);
358 GNUNET_SCHEDULER_cancel (kill_task);
359 kill_task = GNUNET_SCHEDULER_NO_TASK;
364 disconnect_bob (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
367 printf ("Bod is leaving.\n");
371 alice_wanted.meta = NULL;
372 alice_wanted.sender = &bob;
373 alice_wanted.msg = NULL;
374 alice_wanted.opt = 0;
375 alice_wanted.next_task = &disconnect_alice;
376 alice_wanted.next_task_cls = NULL;
377 GNUNET_CHAT_leave_room (bob_room);
383 disconnect_carol (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
386 printf ("Carol is leaving.\n");
388 alice_wanted.meta = NULL;
389 alice_wanted.sender = &carol;
390 alice_wanted.msg = NULL;
391 alice_wanted.opt = 0;
392 alice_wanted.next_task = &set_alice_ready;
393 alice_wanted.next_task_cls = NULL;
394 alice_ready = GNUNET_NO;
395 bob_wanted.meta = NULL;
396 bob_wanted.sender = &carol;
397 bob_wanted.msg = NULL;
399 bob_wanted.next_task = &wait_until_all_ready;
400 bob_wanted.next_task_cls = &disconnect_bob;
401 bob_ready = GNUNET_YES;
402 GNUNET_CHAT_leave_room (carol_room);
408 send_from_alice_to_bob (void *cls,
409 const struct GNUNET_SCHEDULER_TaskContext *tc)
414 printf ("Alice says 'Hi!' to Bob\n");
416 alice_ready = GNUNET_YES;
417 bob_ready = GNUNET_NO;
418 bob_wanted.meta = alice_meta;
419 bob_wanted.sender = &alice;
420 bob_wanted.msg = "Hi Bob!";
421 bob_wanted.opt = GNUNET_CHAT_MSG_PRIVATE;
422 bob_wanted.next_task = &set_bob_ready;
423 bob_wanted.next_task_cls = NULL;
424 /* Carol should not receive this message */
425 carol_wanted.meta = NULL;
426 carol_wanted.sender = NULL;
427 carol_wanted.msg = NULL;
428 carol_wanted.opt = 0;
429 carol_wanted.next_task = NULL;
430 carol_wanted.next_task_cls = NULL;
431 GNUNET_CHAT_send_message (alice_room, "Hi Bob!", GNUNET_CHAT_MSG_PRIVATE,
432 bob_public_key, &seq);
434 GNUNET_SCHEDULER_add_delayed (PM_TIMEOUT, &wait_until_all_ready,
440 prepare_bob_for_alice_task (void *cls,
441 const struct GNUNET_SCHEDULER_TaskContext *tc)
443 bob_wanted.meta = alice_meta;
444 bob_wanted.sender = &alice;
445 bob_wanted.msg = NULL;
447 bob_wanted.next_task = &set_bob_ready;
448 bob_wanted.next_task_cls = NULL;
453 prepare_carol_for_alice_and_bob_task (void *cls,
454 const struct GNUNET_SCHEDULER_TaskContext
457 carol_wanted.meta = alice_meta;
458 carol_wanted.sender = &alice;
459 /* set alternative meta/sender since we don't know from which peer
460 * notification will come first */
461 carol_wanted.meta2 = bob_meta;
462 carol_wanted.sender2 = &bob;
463 carol_wanted.msg = NULL;
464 carol_wanted.opt = -1;
465 carol_wanted.next_task = &wait_until_all_ready;
466 carol_wanted.next_task_cls = &send_from_alice_to_bob;
471 join_carol_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
474 printf ("Carol joining\n");
476 alice_wanted.meta = carol_meta;
477 alice_wanted.sender = &carol;
478 alice_wanted.msg = NULL;
479 alice_wanted.opt = -1;
480 alice_wanted.next_task = &set_alice_ready;
481 alice_wanted.next_task_cls = NULL;
482 alice_ready = GNUNET_NO;
483 bob_wanted.meta = carol_meta;
484 bob_wanted.sender = &carol;
485 bob_wanted.msg = NULL;
487 bob_wanted.next_task = &set_bob_ready;
488 bob_wanted.next_task_cls = NULL;
489 bob_ready = GNUNET_NO;
490 carol_wanted.next_task = &prepare_carol_for_alice_and_bob_task;
491 carol_wanted.next_task_cls = NULL;
493 GNUNET_CHAT_join_room (is_p2p ? p3.cfg : p1.cfg, "carol", carol_meta,
494 "test", -1, &join_cb, &carol_wanted, &receive_cb,
495 &carol_wanted, &member_list_cb, &carol_wanted,
497 if (NULL == carol_room)
499 GNUNET_SCHEDULER_cancel (kill_task);
500 kill_task = GNUNET_SCHEDULER_NO_TASK;
501 GNUNET_CHAT_leave_room (alice_room);
503 GNUNET_CHAT_leave_room (bob_room);
511 join_bob_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
514 printf ("Bob joining\n");
516 alice_wanted.meta = bob_meta;
517 alice_wanted.sender = &bob;
518 alice_wanted.msg = NULL;
519 alice_wanted.opt = -1;
520 alice_wanted.next_task = &wait_until_all_ready;
521 alice_wanted.next_task_cls = &join_carol_task;
522 alice_ready = GNUNET_YES;
523 bob_wanted.next_task = &prepare_bob_for_alice_task;
524 bob_wanted.next_task_cls = NULL;
525 bob_ready = GNUNET_NO;
527 GNUNET_CHAT_join_room (is_p2p ? p2.cfg : p1.cfg, "bob", bob_meta, "test",
528 -1, &join_cb, &bob_wanted, &receive_cb,
529 &bob_wanted, &member_list_cb, &bob_wanted, NULL,
531 if (NULL == bob_room)
533 GNUNET_SCHEDULER_cancel (kill_task);
534 kill_task = GNUNET_SCHEDULER_NO_TASK;
535 GNUNET_CHAT_leave_room (alice_room);
543 join_alice_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
546 printf ("Alice joining\n");
548 alice_wanted.next_task = &join_bob_task;
549 alice_wanted.next_task_cls = NULL;
551 GNUNET_CHAT_join_room (p1.cfg, "alice", alice_meta, "test", -1, &join_cb,
552 &alice_wanted, &receive_cb, &alice_wanted,
553 &member_list_cb, &alice_wanted, NULL, NULL,
555 if (NULL == alice_room)
557 GNUNET_SCHEDULER_cancel (kill_task);
558 kill_task = GNUNET_SCHEDULER_NO_TASK;
565 run (void *cls, char *const *args, const char *cfgfile,
566 const struct GNUNET_CONFIGURATION_Handle *cfg)
570 setup_peer (&p1, "test_chat_peer1.conf");
571 setup_peer (&p2, "test_chat_peer2.conf");
572 setup_peer (&p3, "test_chat_peer3.conf");
575 setup_peer (&p1, "test_chat_data.conf");
577 memset (&alice_wanted, 0, sizeof (struct Wanted));
578 memset (&bob_wanted, 0, sizeof (struct Wanted));
579 memset (&carol_wanted, 0, sizeof (struct Wanted));
580 alice_wanted.me = "Alice";
581 bob_wanted.me = "Bob";
582 carol_wanted.me = "Carol";
583 alice_meta = GNUNET_CONTAINER_meta_data_create ();
584 GNUNET_CONTAINER_meta_data_insert (alice_meta, "<gnunet>",
585 EXTRACTOR_METATYPE_TITLE,
586 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
587 "Alice", strlen ("Alice") + 1);
588 bob_meta = GNUNET_CONTAINER_meta_data_create ();
589 GNUNET_CONTAINER_meta_data_insert (bob_meta, "<gnunet>",
590 EXTRACTOR_METATYPE_TITLE,
591 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
592 "Bob", strlen ("Bob") + 1);
593 carol_meta = GNUNET_CONTAINER_meta_data_create ();
594 GNUNET_CONTAINER_meta_data_insert (carol_meta, "<gnunet>",
595 EXTRACTOR_METATYPE_TITLE,
596 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
597 "Carol", strlen ("Carol") + 1);
598 kill_task = GNUNET_SCHEDULER_add_delayed (KILL_TIMEOUT, &timeout_kill, NULL);
599 GNUNET_SCHEDULER_add_now (&join_alice_task, NULL);
604 main (int argc, char *argv[])
606 char *const argvx[] = {
609 "test_chat_data.conf",
612 struct GNUNET_GETOPT_CommandLineOption options[] = {
613 GNUNET_GETOPT_OPTION_END
616 GNUNET_log_setup ("test_chat",
619 if (strstr (argv[0], "p2p") != NULL)
623 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
624 "test-chat", "nohelp", options, &run, NULL);
626 GNUNET_CONTAINER_meta_data_destroy (alice_meta);
627 GNUNET_CONTAINER_meta_data_destroy (bob_meta);
628 GNUNET_CONTAINER_meta_data_destroy (carol_meta);
631 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-1/");
632 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-2/");
633 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat-peer-3/");
636 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-chat/");
640 /* end of test_chat_private.c */