2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file conversation/test_conversation_api_twocalls.c
22 * @brief testcase for conversation_api.c
24 * This test performs the operations of TWO calls made to a phone
25 * where the phone user picks up one, suspends it, picks up the
26 * second one; eventually, the initiator hangs up, the callee
27 * resumes the first call, and then the initiator hangs up the
31 #include "gnunet_util_lib.h"
32 #include "gnunet_testing_lib.h"
33 #include "gnunet_gnsrecord_lib.h"
34 #include "gnunet_conversation_service.h"
35 #include "gnunet_identity_service.h"
36 #include "gnunet_namestore_service.h"
38 #define FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 25)
42 #define LOG(kind,...) \
43 GNUNET_log (kind, __VA_ARGS__)
45 #define LOG_DEBUG(...) \
46 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
48 static const struct GNUNET_CONFIGURATION_Handle *cfg;
50 static struct GNUNET_IDENTITY_Handle *id;
52 static struct GNUNET_IDENTITY_Operation *op;
54 static struct GNUNET_CONVERSATION_Phone *phone;
56 static struct GNUNET_NAMESTORE_Handle *ns;
58 static struct GNUNET_CONVERSATION_Call *call1;
60 static struct GNUNET_CONVERSATION_Call *call2;
62 static struct GNUNET_NAMESTORE_QueueEntry *qe;
64 static struct GNUNET_CONVERSATION_Caller *active_caller1;
66 static struct GNUNET_CONVERSATION_Caller *active_caller2;
68 static char *gns_name;
70 static char *gns_caller_id;
72 static GNUNET_MICROPHONE_RecordedDataCallback phone_rdc;
74 static void *phone_rdc_cls;
76 static struct GNUNET_SCHEDULER_Task *phone_task;
78 static struct GNUNET_SCHEDULER_Task *timeout_task;
81 * Variable for recognizing caller1
83 static const char *caller1 = "caller1";
86 * Variable for recognizing caller2
88 static const char *caller2 = "caller2";
91 * Variable for recognizing callee
93 static const char *phone0 = "phone";
96 #define CALLER1 &caller1
97 #define CALLER2 &caller2
98 #define PHONE0 &phone0
100 #define CLS_STR(caller) (*((char **)caller))
104 * Did caller1 call finish successfully
106 static int call1_finished;
109 * Did caller2 call finish successfully
111 static int call2_finished;
115 GNUNET_MICROPHONE_RecordedDataCallback rdc;
119 struct GNUNET_SCHEDULER_Task * call_task;
123 static struct MicContext call1_mic_ctx;
124 static struct MicContext call2_mic_ctx;
125 //static struct MicContext phone_mic_ctx;
129 phone_send (void *cls)
133 GNUNET_assert (NULL != phone_rdc);
134 GNUNET_snprintf (buf, sizeof (buf), "phone");
135 phone_rdc (phone_rdc_cls, strlen (buf) + 1, buf);
136 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ,
142 call_send (void *cls)
144 struct MicContext *mc = cls;
147 GNUNET_assert (NULL != mc->rdc);
148 GNUNET_snprintf (buf, sizeof (buf), "call");
149 mc->rdc (mc->rdc_cls, strlen (buf) + 1, buf);
150 mc->call_task = GNUNET_SCHEDULER_add_delayed (FREQ,
156 enable_speaker (void *cls)
158 const char *origin = CLS_STR (cls);
160 LOG_DEBUG ("Speaker %s enabled\n",
167 disable_speaker (void *cls)
169 const char *origin = CLS_STR (cls);
171 LOG_DEBUG ("Speaker %s disabled\n",
181 static unsigned int phone_i;
182 static unsigned int call_i;
184 if (0 == strncmp ("call", data, data_size))
186 else if (0 == strncmp ("phone", data, data_size))
190 LOG_DEBUG ("Received %u bytes of unexpected data `%.*s'\n",
191 (unsigned int) data_size,
193 (const char *) data);
196 if ( (20 < call_i) &&
200 /* time to hang up ... */
201 GNUNET_CONVERSATION_call_stop (call2);
206 call2_finished = GNUNET_YES;
208 if ( (20 < call_i) &&
212 /* time to hang up ... */
213 GNUNET_CONVERSATION_call_stop (call1);
217 call1_finished = GNUNET_YES;
223 destroy_speaker (void *cls)
225 const char *origin = CLS_STR (cls);
227 LOG_DEBUG ("Speaker %s destroyed\n", origin);
231 static struct GNUNET_SPEAKER_Handle call1_speaker = {
240 static struct GNUNET_SPEAKER_Handle call2_speaker = {
249 static struct GNUNET_SPEAKER_Handle phone_speaker = {
259 enable_mic (void *cls,
260 GNUNET_MICROPHONE_RecordedDataCallback rdc,
263 const char *origin = CLS_STR (cls);
264 struct MicContext *mc;
266 LOG_DEBUG ("Mic %s enabled\n",
271 phone_rdc_cls = rdc_cls;
272 GNUNET_break (NULL == phone_task);
273 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
276 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
278 mc->rdc_cls = rdc_cls;
279 GNUNET_break (NULL == mc->call_task);
280 mc->call_task = GNUNET_SCHEDULER_add_now (&call_send, mc);
286 disable_mic (void *cls)
288 const char *origin = CLS_STR (cls);
289 struct MicContext *mc;
291 LOG_DEBUG ("Mic %s disabled\n",
296 phone_rdc_cls = NULL;
297 GNUNET_SCHEDULER_cancel (phone_task);
301 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
304 GNUNET_SCHEDULER_cancel (mc->call_task);
305 mc->call_task = NULL;
310 destroy_mic (void *cls)
312 const char *origin = CLS_STR (cls);
314 LOG_DEBUG ("Mic %s destroyed\n",
319 static struct GNUNET_MICROPHONE_Handle call1_mic = {
327 static struct GNUNET_MICROPHONE_Handle call2_mic = {
335 static struct GNUNET_MICROPHONE_Handle phone_mic = {
344 * Function run on timeout.
354 GNUNET_SCHEDULER_shutdown ();
359 * Function run on shutdown.
364 do_shutdown (void *cls)
366 if (NULL != timeout_task)
368 GNUNET_SCHEDULER_cancel (timeout_task);
373 GNUNET_IDENTITY_cancel (op);
378 GNUNET_CONVERSATION_call_stop (call1);
383 GNUNET_CONVERSATION_call_stop (call2);
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
389 GNUNET_CONVERSATION_phone_destroy (phone);
394 GNUNET_IDENTITY_disconnect (id);
399 GNUNET_NAMESTORE_cancel (qe);
404 GNUNET_NAMESTORE_disconnect (ns);
411 caller_event_handler (void *cls,
412 enum GNUNET_CONVERSATION_CallerEventCode code)
416 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
417 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
418 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected caller code: %d\n", code);
425 phone_event_handler (void *cls,
426 enum GNUNET_CONVERSATION_PhoneEventCode code,
427 struct GNUNET_CONVERSATION_Caller *caller,
428 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
434 case GNUNET_CONVERSATION_EC_PHONE_RING:
435 if (NULL == active_caller1)
437 active_caller1 = caller;
439 GNUNET_CONVERSATION_caller_pick_up (caller,
440 &caller_event_handler,
447 GNUNET_CONVERSATION_caller_suspend (active_caller1);
448 active_caller2 = caller;
450 GNUNET_CONVERSATION_caller_pick_up (caller,
451 &caller_event_handler,
457 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
458 if (caller == active_caller2)
460 active_caller2 = NULL;
461 GNUNET_CONVERSATION_caller_resume (active_caller1,
465 else if (caller == active_caller1)
467 active_caller1 = NULL;
468 GNUNET_break (NULL == active_caller2);
469 GNUNET_SCHEDULER_shutdown ();
473 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected phone code: %d\n", code);
480 call_event_handler (void *cls,
481 enum GNUNET_CONVERSATION_CallEventCode code)
483 const char *cid = cls;
487 case GNUNET_CONVERSATION_EC_CALL_RINGING:
489 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
490 LOG_DEBUG ("Call %s picked\n", cid);
492 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
493 LOG_DEBUG ("Call %s GNS lookup failed \n", cid);
495 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
496 LOG_DEBUG ("Call %s hungup\n", cid);
497 if (0 == strcmp (cid, "call1"))
502 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
503 LOG_DEBUG ("Call %s suspended\n", cid);
505 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
506 LOG_DEBUG ("Call %s resumed\n", cid);
508 case GNUNET_CONVERSATION_EC_CALL_ERROR:
510 if (0 == strcmp (cid, "call1"))
514 GNUNET_SCHEDULER_shutdown ();
521 caller_ego_create_cont (void *cls,
525 GNUNET_assert (NULL == emsg);
530 namestore_put_cont (void *cls,
535 GNUNET_assert (GNUNET_YES == success);
536 GNUNET_assert (NULL == emsg);
537 GNUNET_assert (NULL == op);
538 op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
543 identity_cb (void *cls,
544 struct GNUNET_IDENTITY_Ego *ego,
548 struct GNUNET_GNSRECORD_Data rd;
549 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
555 if (0 == strcmp (name, "phone-ego"))
557 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
558 GNUNET_asprintf (&gns_name,
560 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
561 phone = GNUNET_CONVERSATION_phone_create (cfg,
563 &phone_event_handler,
565 GNUNET_assert (NULL != phone);
566 memset (&rd, 0, sizeof (rd));
567 GNUNET_CONVERSATION_phone_get_record (phone,
569 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
570 rd.expiration_time = UINT64_MAX;
571 qe = GNUNET_NAMESTORE_records_store (ns,
572 GNUNET_IDENTITY_ego_get_private_key (ego),
573 "phone" /* GNS label */,
580 if (0 == strcmp (name, "caller-ego"))
582 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
583 GNUNET_asprintf (&gns_caller_id,
585 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
586 call1 = GNUNET_CONVERSATION_call_start (cfg,
594 call2 = GNUNET_CONVERSATION_call_start (cfg,
608 phone_ego_create_cont (void *cls,
612 GNUNET_assert (NULL == emsg);
618 const struct GNUNET_CONFIGURATION_Handle *c,
619 struct GNUNET_TESTING_Peer *peer)
622 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
625 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
627 id = GNUNET_IDENTITY_connect (cfg,
630 op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
631 ns = GNUNET_NAMESTORE_connect (cfg);
636 main (int argc, char *argv[])
639 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api_twocalls",
640 "test_conversation.conf",
643 if (call1_finished && call2_finished)
648 /* end of test_conversation_api_twocalls.c */