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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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)
134 GNUNET_assert (NULL != phone_rdc);
135 GNUNET_snprintf (buf, sizeof (buf), "phone");
136 phone_rdc (phone_rdc_cls,
139 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ,
146 call_send (void *cls)
148 struct MicContext *mc = cls;
152 GNUNET_assert (NULL != mc->rdc);
153 GNUNET_snprintf (buf,
156 mc->rdc (mc->rdc_cls,
159 mc->call_task = GNUNET_SCHEDULER_add_delayed (FREQ,
166 enable_speaker (void *cls)
168 const char *origin = CLS_STR (cls);
171 LOG_DEBUG ("Speaker %s enabled\n",
178 disable_speaker (void *cls)
180 const char *origin = CLS_STR (cls);
183 LOG_DEBUG ("Speaker %s disabled\n",
193 static unsigned int phone_i;
194 static unsigned int call_i;
197 if (0 == strncmp ("call", data, data_size))
199 else if (0 == strncmp ("phone", data, data_size))
203 LOG_DEBUG ("Received %u bytes of unexpected data `%.*s'\n",
204 (unsigned int) data_size,
206 (const char *) data);
209 if ( (20 < call_i) &&
213 /* time to hang up ... */
214 GNUNET_CONVERSATION_call_stop (call2);
219 call2_finished = GNUNET_YES;
221 if ( (20 < call_i) &&
225 /* time to hang up ... */
226 GNUNET_CONVERSATION_call_stop (call1);
230 call1_finished = GNUNET_YES;
236 destroy_speaker (void *cls)
238 const char *origin = CLS_STR (cls);
240 LOG_DEBUG ("Speaker %s destroyed\n", origin);
244 static struct GNUNET_SPEAKER_Handle call1_speaker = {
253 static struct GNUNET_SPEAKER_Handle call2_speaker = {
262 static struct GNUNET_SPEAKER_Handle phone_speaker = {
272 enable_mic (void *cls,
273 GNUNET_MICROPHONE_RecordedDataCallback rdc,
276 const char *origin = CLS_STR (cls);
277 struct MicContext *mc;
279 LOG_DEBUG ("Mic %s enabled\n",
284 phone_rdc_cls = rdc_cls;
285 GNUNET_break (NULL == phone_task);
286 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
289 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
291 mc->rdc_cls = rdc_cls;
292 GNUNET_break (NULL == mc->call_task);
293 mc->call_task = GNUNET_SCHEDULER_add_now (&call_send, mc);
299 disable_mic (void *cls)
301 const char *origin = CLS_STR (cls);
302 struct MicContext *mc;
304 LOG_DEBUG ("Mic %s disabled\n",
309 phone_rdc_cls = NULL;
310 GNUNET_SCHEDULER_cancel (phone_task);
314 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
317 GNUNET_SCHEDULER_cancel (mc->call_task);
318 mc->call_task = NULL;
323 destroy_mic (void *cls)
325 const char *origin = CLS_STR (cls);
327 LOG_DEBUG ("Mic %s destroyed\n",
332 static struct GNUNET_MICROPHONE_Handle call1_mic = {
340 static struct GNUNET_MICROPHONE_Handle call2_mic = {
348 static struct GNUNET_MICROPHONE_Handle phone_mic = {
357 * Function run on timeout.
368 GNUNET_SCHEDULER_shutdown ();
373 * Function run on shutdown.
378 do_shutdown (void *cls)
381 if (NULL != timeout_task)
383 GNUNET_SCHEDULER_cancel (timeout_task);
388 GNUNET_IDENTITY_cancel (op);
393 GNUNET_CONVERSATION_call_stop (call1);
398 GNUNET_CONVERSATION_call_stop (call2);
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
404 GNUNET_CONVERSATION_phone_destroy (phone);
409 GNUNET_IDENTITY_disconnect (id);
414 GNUNET_NAMESTORE_cancel (qe);
419 GNUNET_NAMESTORE_disconnect (ns);
426 caller_event_handler (void *cls,
427 enum GNUNET_CONVERSATION_CallerEventCode code)
432 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
433 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
434 LOG (GNUNET_ERROR_TYPE_WARNING,
435 "Unexpected caller code: %d\n",
443 phone_event_handler (void *cls,
444 enum GNUNET_CONVERSATION_PhoneEventCode code,
445 struct GNUNET_CONVERSATION_Caller *caller,
446 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
454 case GNUNET_CONVERSATION_EC_PHONE_RING:
455 if (NULL == active_caller1)
457 active_caller1 = caller;
459 GNUNET_CONVERSATION_caller_pick_up (caller,
460 &caller_event_handler,
467 GNUNET_CONVERSATION_caller_suspend (active_caller1);
468 active_caller2 = caller;
470 GNUNET_CONVERSATION_caller_pick_up (caller,
471 &caller_event_handler,
477 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
478 if (caller == active_caller2)
480 active_caller2 = NULL;
481 GNUNET_CONVERSATION_caller_resume (active_caller1,
485 else if (caller == active_caller1)
487 active_caller1 = NULL;
488 GNUNET_break (NULL == active_caller2);
489 GNUNET_SCHEDULER_shutdown ();
493 LOG (GNUNET_ERROR_TYPE_WARNING,
494 "Unexpected phone code: %d\n",
502 call_event_handler (void *cls,
503 enum GNUNET_CONVERSATION_CallEventCode code)
505 const char *cid = cls;
509 case GNUNET_CONVERSATION_EC_CALL_RINGING:
511 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
512 LOG_DEBUG ("Call %s picked\n", cid);
514 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
515 LOG_DEBUG ("Call %s GNS lookup failed \n", cid);
517 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
518 LOG_DEBUG ("Call %s hungup\n", cid);
519 if (0 == strcmp (cid, "call1"))
524 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
525 LOG_DEBUG ("Call %s suspended\n", cid);
527 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
528 LOG_DEBUG ("Call %s resumed\n", cid);
530 case GNUNET_CONVERSATION_EC_CALL_ERROR:
532 if (0 == strcmp (cid, "call1"))
536 GNUNET_SCHEDULER_shutdown ();
543 caller_ego_create_cont (void *cls,
548 GNUNET_assert (NULL == emsg);
553 namestore_put_cont (void *cls,
559 GNUNET_assert (GNUNET_YES == success);
560 GNUNET_assert (NULL == emsg);
561 GNUNET_assert (NULL == op);
562 op = GNUNET_IDENTITY_create (id,
564 &caller_ego_create_cont,
570 identity_cb (void *cls,
571 struct GNUNET_IDENTITY_Ego *ego,
575 struct GNUNET_GNSRECORD_Data rd;
576 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
584 if (0 == strcmp (name, "phone-ego"))
586 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
587 GNUNET_asprintf (&gns_name,
589 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
590 phone = GNUNET_CONVERSATION_phone_create (cfg,
592 &phone_event_handler,
594 GNUNET_assert (NULL != phone);
595 memset (&rd, 0, sizeof (rd));
596 GNUNET_CONVERSATION_phone_get_record (phone,
598 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
599 rd.expiration_time = UINT64_MAX;
600 qe = GNUNET_NAMESTORE_records_store (ns,
601 GNUNET_IDENTITY_ego_get_private_key (ego),
602 "phone" /* GNS label */,
609 if (0 == strcmp (name, "caller-ego"))
611 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
612 GNUNET_asprintf (&gns_caller_id,
614 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
615 call1 = GNUNET_CONVERSATION_call_start (cfg,
622 call2 = GNUNET_CONVERSATION_call_start (cfg,
635 phone_ego_create_cont (void *cls,
640 GNUNET_assert (NULL == emsg);
646 const struct GNUNET_CONFIGURATION_Handle *c,
647 struct GNUNET_TESTING_Peer *peer)
652 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
655 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
657 id = GNUNET_IDENTITY_connect (cfg,
660 op = GNUNET_IDENTITY_create (id,
662 &phone_ego_create_cont,
664 ns = GNUNET_NAMESTORE_connect (cfg);
674 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api_twocalls",
675 "test_conversation.conf",
679 if (call1_finished && call2_finished)
684 /* end of test_conversation_api_twocalls.c */