2 This file is part of GNUnet.
3 Copyright (C) 2013, 2018x GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
16 * @file conversation/test_conversation_api_twocalls.c
17 * @brief testcase for conversation_api.c
19 * This test performs the operations of TWO calls made to a phone
20 * where the phone user picks up one, suspends it, picks up the
21 * second one; eventually, the initiator hangs up, the callee
22 * resumes the first call, and then the initiator hangs up the
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_gnsrecord_lib.h"
29 #include "gnunet_conversation_service.h"
30 #include "gnunet_identity_service.h"
31 #include "gnunet_namestore_service.h"
33 #define FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 25)
37 #define LOG(kind,...) \
38 GNUNET_log (kind, __VA_ARGS__)
40 #define LOG_DEBUG(...) \
41 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
43 static const struct GNUNET_CONFIGURATION_Handle *cfg;
45 static struct GNUNET_IDENTITY_Handle *id;
47 static struct GNUNET_IDENTITY_Operation *op;
49 static struct GNUNET_CONVERSATION_Phone *phone;
51 static struct GNUNET_NAMESTORE_Handle *ns;
53 static struct GNUNET_CONVERSATION_Call *call1;
55 static struct GNUNET_CONVERSATION_Call *call2;
57 static struct GNUNET_NAMESTORE_QueueEntry *qe;
59 static struct GNUNET_CONVERSATION_Caller *active_caller1;
61 static struct GNUNET_CONVERSATION_Caller *active_caller2;
63 static char *gns_name;
65 static char *gns_caller_id;
67 static GNUNET_MICROPHONE_RecordedDataCallback phone_rdc;
69 static void *phone_rdc_cls;
71 static struct GNUNET_SCHEDULER_Task *phone_task;
73 static struct GNUNET_SCHEDULER_Task *timeout_task;
76 * Variable for recognizing caller1
78 static const char *caller1 = "caller1";
81 * Variable for recognizing caller2
83 static const char *caller2 = "caller2";
86 * Variable for recognizing callee
88 static const char *phone0 = "phone";
91 #define CALLER1 &caller1
92 #define CALLER2 &caller2
93 #define PHONE0 &phone0
95 #define CLS_STR(caller) (*((char **)caller))
99 * Did caller1 call finish successfully
101 static int call1_finished;
104 * Did caller2 call finish successfully
106 static int call2_finished;
110 GNUNET_MICROPHONE_RecordedDataCallback rdc;
114 struct GNUNET_SCHEDULER_Task * call_task;
118 static struct MicContext call1_mic_ctx;
119 static struct MicContext call2_mic_ctx;
120 //static struct MicContext phone_mic_ctx;
124 phone_send (void *cls)
129 GNUNET_assert (NULL != phone_rdc);
130 GNUNET_snprintf (buf, sizeof (buf), "phone");
131 phone_rdc (phone_rdc_cls,
134 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ,
141 call_send (void *cls)
143 struct MicContext *mc = cls;
147 GNUNET_assert (NULL != mc->rdc);
148 GNUNET_snprintf (buf,
151 mc->rdc (mc->rdc_cls,
154 mc->call_task = GNUNET_SCHEDULER_add_delayed (FREQ,
161 enable_speaker (void *cls)
163 const char *origin = CLS_STR (cls);
166 LOG_DEBUG ("Speaker %s enabled\n",
173 disable_speaker (void *cls)
175 const char *origin = CLS_STR (cls);
178 LOG_DEBUG ("Speaker %s disabled\n",
188 static unsigned int phone_i;
189 static unsigned int call_i;
192 if (0 == strncmp ("call", data, data_size))
194 else if (0 == strncmp ("phone", data, data_size))
198 LOG_DEBUG ("Received %u bytes of unexpected data `%.*s'\n",
199 (unsigned int) data_size,
201 (const char *) data);
204 if ( (20 < call_i) &&
208 /* time to hang up ... */
209 GNUNET_CONVERSATION_call_stop (call2);
214 call2_finished = GNUNET_YES;
216 if ( (20 < call_i) &&
220 /* time to hang up ... */
221 GNUNET_CONVERSATION_call_stop (call1);
225 call1_finished = GNUNET_YES;
231 destroy_speaker (void *cls)
233 const char *origin = CLS_STR (cls);
235 LOG_DEBUG ("Speaker %s destroyed\n", origin);
239 static struct GNUNET_SPEAKER_Handle call1_speaker = {
248 static struct GNUNET_SPEAKER_Handle call2_speaker = {
257 static struct GNUNET_SPEAKER_Handle phone_speaker = {
267 enable_mic (void *cls,
268 GNUNET_MICROPHONE_RecordedDataCallback rdc,
271 const char *origin = CLS_STR (cls);
272 struct MicContext *mc;
274 LOG_DEBUG ("Mic %s enabled\n",
279 phone_rdc_cls = rdc_cls;
280 GNUNET_break (NULL == phone_task);
281 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
284 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
286 mc->rdc_cls = rdc_cls;
287 GNUNET_break (NULL == mc->call_task);
288 mc->call_task = GNUNET_SCHEDULER_add_now (&call_send, mc);
294 disable_mic (void *cls)
296 const char *origin = CLS_STR (cls);
297 struct MicContext *mc;
299 LOG_DEBUG ("Mic %s disabled\n",
304 phone_rdc_cls = NULL;
305 GNUNET_SCHEDULER_cancel (phone_task);
309 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
312 GNUNET_SCHEDULER_cancel (mc->call_task);
313 mc->call_task = NULL;
318 destroy_mic (void *cls)
320 const char *origin = CLS_STR (cls);
322 LOG_DEBUG ("Mic %s destroyed\n",
327 static struct GNUNET_MICROPHONE_Handle call1_mic = {
335 static struct GNUNET_MICROPHONE_Handle call2_mic = {
343 static struct GNUNET_MICROPHONE_Handle phone_mic = {
352 * Function run on timeout.
363 GNUNET_SCHEDULER_shutdown ();
368 * Function run on shutdown.
373 do_shutdown (void *cls)
376 if (NULL != timeout_task)
378 GNUNET_SCHEDULER_cancel (timeout_task);
383 GNUNET_IDENTITY_cancel (op);
388 GNUNET_CONVERSATION_call_stop (call1);
393 GNUNET_CONVERSATION_call_stop (call2);
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
399 GNUNET_CONVERSATION_phone_destroy (phone);
404 GNUNET_IDENTITY_disconnect (id);
409 GNUNET_NAMESTORE_cancel (qe);
414 GNUNET_NAMESTORE_disconnect (ns);
421 caller_event_handler (void *cls,
422 enum GNUNET_CONVERSATION_CallerEventCode code)
427 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
428 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
429 LOG (GNUNET_ERROR_TYPE_WARNING,
430 "Unexpected caller code: %d\n",
438 phone_event_handler (void *cls,
439 enum GNUNET_CONVERSATION_PhoneEventCode code,
440 struct GNUNET_CONVERSATION_Caller *caller,
441 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
449 case GNUNET_CONVERSATION_EC_PHONE_RING:
450 if (NULL == active_caller1)
452 active_caller1 = caller;
454 GNUNET_CONVERSATION_caller_pick_up (caller,
455 &caller_event_handler,
462 GNUNET_CONVERSATION_caller_suspend (active_caller1);
463 active_caller2 = caller;
465 GNUNET_CONVERSATION_caller_pick_up (caller,
466 &caller_event_handler,
472 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
473 if (caller == active_caller2)
475 active_caller2 = NULL;
476 GNUNET_CONVERSATION_caller_resume (active_caller1,
480 else if (caller == active_caller1)
482 active_caller1 = NULL;
483 GNUNET_break (NULL == active_caller2);
484 GNUNET_SCHEDULER_shutdown ();
488 LOG (GNUNET_ERROR_TYPE_WARNING,
489 "Unexpected phone code: %d\n",
497 call_event_handler (void *cls,
498 enum GNUNET_CONVERSATION_CallEventCode code)
500 const char *cid = cls;
504 case GNUNET_CONVERSATION_EC_CALL_RINGING:
506 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
507 LOG_DEBUG ("Call %s picked\n", cid);
509 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
510 LOG_DEBUG ("Call %s GNS lookup failed \n", cid);
512 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
513 LOG_DEBUG ("Call %s hungup\n", cid);
514 if (0 == strcmp (cid, "call1"))
519 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
520 LOG_DEBUG ("Call %s suspended\n", cid);
522 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
523 LOG_DEBUG ("Call %s resumed\n", cid);
525 case GNUNET_CONVERSATION_EC_CALL_ERROR:
527 if (0 == strcmp (cid, "call1"))
531 GNUNET_SCHEDULER_shutdown ();
538 caller_ego_create_cont (void *cls,
543 GNUNET_assert (NULL == emsg);
548 namestore_put_cont (void *cls,
554 GNUNET_assert (GNUNET_YES == success);
555 GNUNET_assert (NULL == emsg);
556 GNUNET_assert (NULL == op);
557 op = GNUNET_IDENTITY_create (id,
559 &caller_ego_create_cont,
565 identity_cb (void *cls,
566 struct GNUNET_IDENTITY_Ego *ego,
570 struct GNUNET_GNSRECORD_Data rd;
571 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
579 if (0 == strcmp (name, "phone-ego"))
581 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
582 GNUNET_asprintf (&gns_name,
584 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
585 phone = GNUNET_CONVERSATION_phone_create (cfg,
587 &phone_event_handler,
589 GNUNET_assert (NULL != phone);
590 memset (&rd, 0, sizeof (rd));
591 GNUNET_CONVERSATION_phone_get_record (phone,
593 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
594 rd.expiration_time = UINT64_MAX;
595 qe = GNUNET_NAMESTORE_records_store (ns,
596 GNUNET_IDENTITY_ego_get_private_key (ego),
597 "phone" /* GNS label */,
604 if (0 == strcmp (name, "caller-ego"))
606 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
607 GNUNET_asprintf (&gns_caller_id,
609 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
610 call1 = GNUNET_CONVERSATION_call_start (cfg,
617 call2 = GNUNET_CONVERSATION_call_start (cfg,
630 phone_ego_create_cont (void *cls,
635 GNUNET_assert (NULL == emsg);
641 const struct GNUNET_CONFIGURATION_Handle *c,
642 struct GNUNET_TESTING_Peer *peer)
647 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
650 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
652 id = GNUNET_IDENTITY_connect (cfg,
655 op = GNUNET_IDENTITY_create (id,
657 &phone_ego_create_cont,
659 ns = GNUNET_NAMESTORE_connect (cfg);
669 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api_twocalls",
670 "test_conversation.conf",
674 if (call1_finished && call2_finished)
679 /* end of test_conversation_api_twocalls.c */