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/>.
19 * @file conversation/test_conversation_api_twocalls.c
20 * @brief testcase for conversation_api.c
22 * This test performs the operations of TWO calls made to a phone
23 * where the phone user picks up one, suspends it, picks up the
24 * second one; eventually, the initiator hangs up, the callee
25 * resumes the first call, and then the initiator hangs up the
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_gnsrecord_lib.h"
32 #include "gnunet_conversation_service.h"
33 #include "gnunet_identity_service.h"
34 #include "gnunet_namestore_service.h"
36 #define FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 25)
40 #define LOG(kind,...) \
41 GNUNET_log (kind, __VA_ARGS__)
43 #define LOG_DEBUG(...) \
44 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
46 static const struct GNUNET_CONFIGURATION_Handle *cfg;
48 static struct GNUNET_IDENTITY_Handle *id;
50 static struct GNUNET_IDENTITY_Operation *op;
52 static struct GNUNET_CONVERSATION_Phone *phone;
54 static struct GNUNET_NAMESTORE_Handle *ns;
56 static struct GNUNET_CONVERSATION_Call *call1;
58 static struct GNUNET_CONVERSATION_Call *call2;
60 static struct GNUNET_NAMESTORE_QueueEntry *qe;
62 static struct GNUNET_CONVERSATION_Caller *active_caller1;
64 static struct GNUNET_CONVERSATION_Caller *active_caller2;
66 static char *gns_name;
68 static char *gns_caller_id;
70 static GNUNET_MICROPHONE_RecordedDataCallback phone_rdc;
72 static void *phone_rdc_cls;
74 static struct GNUNET_SCHEDULER_Task *phone_task;
76 static struct GNUNET_SCHEDULER_Task *timeout_task;
79 * Variable for recognizing caller1
81 static const char *caller1 = "caller1";
84 * Variable for recognizing caller2
86 static const char *caller2 = "caller2";
89 * Variable for recognizing callee
91 static const char *phone0 = "phone";
94 #define CALLER1 &caller1
95 #define CALLER2 &caller2
96 #define PHONE0 &phone0
98 #define CLS_STR(caller) (*((char **)caller))
102 * Did caller1 call finish successfully
104 static int call1_finished;
107 * Did caller2 call finish successfully
109 static int call2_finished;
113 GNUNET_MICROPHONE_RecordedDataCallback rdc;
117 struct GNUNET_SCHEDULER_Task * call_task;
121 static struct MicContext call1_mic_ctx;
122 static struct MicContext call2_mic_ctx;
123 //static struct MicContext phone_mic_ctx;
127 phone_send (void *cls)
132 GNUNET_assert (NULL != phone_rdc);
133 GNUNET_snprintf (buf, sizeof (buf), "phone");
134 phone_rdc (phone_rdc_cls,
137 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ,
144 call_send (void *cls)
146 struct MicContext *mc = cls;
150 GNUNET_assert (NULL != mc->rdc);
151 GNUNET_snprintf (buf,
154 mc->rdc (mc->rdc_cls,
157 mc->call_task = GNUNET_SCHEDULER_add_delayed (FREQ,
164 enable_speaker (void *cls)
166 const char *origin = CLS_STR (cls);
169 LOG_DEBUG ("Speaker %s enabled\n",
176 disable_speaker (void *cls)
178 const char *origin = CLS_STR (cls);
181 LOG_DEBUG ("Speaker %s disabled\n",
191 static unsigned int phone_i;
192 static unsigned int call_i;
195 if (0 == strncmp ("call", data, data_size))
197 else if (0 == strncmp ("phone", data, data_size))
201 LOG_DEBUG ("Received %u bytes of unexpected data `%.*s'\n",
202 (unsigned int) data_size,
204 (const char *) data);
207 if ( (20 < call_i) &&
211 /* time to hang up ... */
212 GNUNET_CONVERSATION_call_stop (call2);
217 call2_finished = GNUNET_YES;
219 if ( (20 < call_i) &&
223 /* time to hang up ... */
224 GNUNET_CONVERSATION_call_stop (call1);
228 call1_finished = GNUNET_YES;
234 destroy_speaker (void *cls)
236 const char *origin = CLS_STR (cls);
238 LOG_DEBUG ("Speaker %s destroyed\n", origin);
242 static struct GNUNET_SPEAKER_Handle call1_speaker = {
251 static struct GNUNET_SPEAKER_Handle call2_speaker = {
260 static struct GNUNET_SPEAKER_Handle phone_speaker = {
270 enable_mic (void *cls,
271 GNUNET_MICROPHONE_RecordedDataCallback rdc,
274 const char *origin = CLS_STR (cls);
275 struct MicContext *mc;
277 LOG_DEBUG ("Mic %s enabled\n",
282 phone_rdc_cls = rdc_cls;
283 GNUNET_break (NULL == phone_task);
284 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
287 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
289 mc->rdc_cls = rdc_cls;
290 GNUNET_break (NULL == mc->call_task);
291 mc->call_task = GNUNET_SCHEDULER_add_now (&call_send, mc);
297 disable_mic (void *cls)
299 const char *origin = CLS_STR (cls);
300 struct MicContext *mc;
302 LOG_DEBUG ("Mic %s disabled\n",
307 phone_rdc_cls = NULL;
308 GNUNET_SCHEDULER_cancel (phone_task);
312 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
315 GNUNET_SCHEDULER_cancel (mc->call_task);
316 mc->call_task = NULL;
321 destroy_mic (void *cls)
323 const char *origin = CLS_STR (cls);
325 LOG_DEBUG ("Mic %s destroyed\n",
330 static struct GNUNET_MICROPHONE_Handle call1_mic = {
338 static struct GNUNET_MICROPHONE_Handle call2_mic = {
346 static struct GNUNET_MICROPHONE_Handle phone_mic = {
355 * Function run on timeout.
366 GNUNET_SCHEDULER_shutdown ();
371 * Function run on shutdown.
376 do_shutdown (void *cls)
379 if (NULL != timeout_task)
381 GNUNET_SCHEDULER_cancel (timeout_task);
386 GNUNET_IDENTITY_cancel (op);
391 GNUNET_CONVERSATION_call_stop (call1);
396 GNUNET_CONVERSATION_call_stop (call2);
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
402 GNUNET_CONVERSATION_phone_destroy (phone);
407 GNUNET_IDENTITY_disconnect (id);
412 GNUNET_NAMESTORE_cancel (qe);
417 GNUNET_NAMESTORE_disconnect (ns);
424 caller_event_handler (void *cls,
425 enum GNUNET_CONVERSATION_CallerEventCode code)
430 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
431 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
432 LOG (GNUNET_ERROR_TYPE_WARNING,
433 "Unexpected caller code: %d\n",
441 phone_event_handler (void *cls,
442 enum GNUNET_CONVERSATION_PhoneEventCode code,
443 struct GNUNET_CONVERSATION_Caller *caller,
444 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
452 case GNUNET_CONVERSATION_EC_PHONE_RING:
453 if (NULL == active_caller1)
455 active_caller1 = caller;
457 GNUNET_CONVERSATION_caller_pick_up (caller,
458 &caller_event_handler,
465 GNUNET_CONVERSATION_caller_suspend (active_caller1);
466 active_caller2 = caller;
468 GNUNET_CONVERSATION_caller_pick_up (caller,
469 &caller_event_handler,
475 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
476 if (caller == active_caller2)
478 active_caller2 = NULL;
479 GNUNET_CONVERSATION_caller_resume (active_caller1,
483 else if (caller == active_caller1)
485 active_caller1 = NULL;
486 GNUNET_break (NULL == active_caller2);
487 GNUNET_SCHEDULER_shutdown ();
491 LOG (GNUNET_ERROR_TYPE_WARNING,
492 "Unexpected phone code: %d\n",
500 call_event_handler (void *cls,
501 enum GNUNET_CONVERSATION_CallEventCode code)
503 const char *cid = cls;
507 case GNUNET_CONVERSATION_EC_CALL_RINGING:
509 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
510 LOG_DEBUG ("Call %s picked\n", cid);
512 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
513 LOG_DEBUG ("Call %s GNS lookup failed \n", cid);
515 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
516 LOG_DEBUG ("Call %s hungup\n", cid);
517 if (0 == strcmp (cid, "call1"))
522 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
523 LOG_DEBUG ("Call %s suspended\n", cid);
525 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
526 LOG_DEBUG ("Call %s resumed\n", cid);
528 case GNUNET_CONVERSATION_EC_CALL_ERROR:
530 if (0 == strcmp (cid, "call1"))
534 GNUNET_SCHEDULER_shutdown ();
541 caller_ego_create_cont (void *cls,
546 GNUNET_assert (NULL == emsg);
551 namestore_put_cont (void *cls,
557 GNUNET_assert (GNUNET_YES == success);
558 GNUNET_assert (NULL == emsg);
559 GNUNET_assert (NULL == op);
560 op = GNUNET_IDENTITY_create (id,
562 &caller_ego_create_cont,
568 identity_cb (void *cls,
569 struct GNUNET_IDENTITY_Ego *ego,
573 struct GNUNET_GNSRECORD_Data rd;
574 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
582 if (0 == strcmp (name, "phone-ego"))
584 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
585 GNUNET_asprintf (&gns_name,
587 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
588 phone = GNUNET_CONVERSATION_phone_create (cfg,
590 &phone_event_handler,
592 GNUNET_assert (NULL != phone);
593 memset (&rd, 0, sizeof (rd));
594 GNUNET_CONVERSATION_phone_get_record (phone,
596 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
597 rd.expiration_time = UINT64_MAX;
598 qe = GNUNET_NAMESTORE_records_store (ns,
599 GNUNET_IDENTITY_ego_get_private_key (ego),
600 "phone" /* GNS label */,
607 if (0 == strcmp (name, "caller-ego"))
609 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
610 GNUNET_asprintf (&gns_caller_id,
612 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
613 call1 = GNUNET_CONVERSATION_call_start (cfg,
620 call2 = GNUNET_CONVERSATION_call_start (cfg,
633 phone_ego_create_cont (void *cls,
638 GNUNET_assert (NULL == emsg);
644 const struct GNUNET_CONFIGURATION_Handle *c,
645 struct GNUNET_TESTING_Peer *peer)
650 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
653 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
655 id = GNUNET_IDENTITY_connect (cfg,
658 op = GNUNET_IDENTITY_create (id,
660 &phone_ego_create_cont,
662 ns = GNUNET_NAMESTORE_connect (cfg);
672 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api_twocalls",
673 "test_conversation.conf",
677 if (call1_finished && call2_finished)
682 /* end of test_conversation_api_twocalls.c */