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, ...) GNUNET_log (kind, __VA_ARGS__)
44 #define LOG_DEBUG(...) 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;
120 static struct MicContext call1_mic_ctx;
121 static struct MicContext call2_mic_ctx;
122 // static struct MicContext phone_mic_ctx;
126 phone_send (void *cls)
131 GNUNET_assert (NULL != phone_rdc);
132 GNUNET_snprintf (buf, sizeof(buf), "phone");
133 phone_rdc (phone_rdc_cls, strlen (buf) + 1, buf);
134 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ, &phone_send, NULL);
139 call_send (void *cls)
141 struct MicContext *mc = cls;
145 GNUNET_assert (NULL != mc->rdc);
146 GNUNET_snprintf (buf, sizeof(buf), "call");
147 mc->rdc (mc->rdc_cls, strlen (buf) + 1, buf);
148 mc->call_task = GNUNET_SCHEDULER_add_delayed (FREQ, &call_send, mc);
153 enable_speaker (void *cls)
155 const char *origin = CLS_STR (cls);
158 LOG_DEBUG ("Speaker %s enabled\n", origin);
164 disable_speaker (void *cls)
166 const char *origin = CLS_STR (cls);
169 LOG_DEBUG ("Speaker %s disabled\n", origin);
174 play (void *cls, size_t data_size, const void *data)
176 static unsigned int phone_i;
177 static unsigned int call_i;
180 if (0 == strncmp ("call", data, data_size))
182 else if (0 == strncmp ("phone", data, data_size))
186 LOG_DEBUG ("Received %u bytes of unexpected data `%.*s'\n",
187 (unsigned int) data_size,
189 (const char *) data);
192 if ((20 < call_i) && (20 < phone_i) && (CALLER2 == cls))
194 /* time to hang up ... */
195 GNUNET_CONVERSATION_call_stop (call2);
200 call2_finished = GNUNET_YES;
202 if ((20 < call_i) && (20 < phone_i) && (CALLER1 == cls))
204 /* time to hang up ... */
205 GNUNET_CONVERSATION_call_stop (call1);
209 call1_finished = GNUNET_YES;
215 destroy_speaker (void *cls)
217 const char *origin = CLS_STR (cls);
219 LOG_DEBUG ("Speaker %s destroyed\n", origin);
223 static struct GNUNET_SPEAKER_Handle call1_speaker = { &enable_speaker,
230 static struct GNUNET_SPEAKER_Handle call2_speaker = { &enable_speaker,
237 static struct GNUNET_SPEAKER_Handle phone_speaker = { &enable_speaker,
245 enable_mic (void *cls,
246 GNUNET_MICROPHONE_RecordedDataCallback rdc,
249 const char *origin = CLS_STR (cls);
250 struct MicContext *mc;
252 LOG_DEBUG ("Mic %s enabled\n", origin);
256 phone_rdc_cls = rdc_cls;
257 GNUNET_break (NULL == phone_task);
258 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
261 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
263 mc->rdc_cls = rdc_cls;
264 GNUNET_break (NULL == mc->call_task);
265 mc->call_task = GNUNET_SCHEDULER_add_now (&call_send, mc);
271 disable_mic (void *cls)
273 const char *origin = CLS_STR (cls);
274 struct MicContext *mc;
276 LOG_DEBUG ("Mic %s disabled\n", origin);
280 phone_rdc_cls = NULL;
281 GNUNET_SCHEDULER_cancel (phone_task);
285 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
288 GNUNET_SCHEDULER_cancel (mc->call_task);
289 mc->call_task = NULL;
294 destroy_mic (void *cls)
296 const char *origin = CLS_STR (cls);
298 LOG_DEBUG ("Mic %s destroyed\n", origin);
302 static struct GNUNET_MICROPHONE_Handle call1_mic = { &enable_mic,
308 static struct GNUNET_MICROPHONE_Handle call2_mic = { &enable_mic,
314 static struct GNUNET_MICROPHONE_Handle phone_mic = { &enable_mic,
321 * Function run on timeout.
330 fprintf (stderr, "Timeout!\n");
331 GNUNET_SCHEDULER_shutdown ();
336 * Function run on shutdown.
341 do_shutdown (void *cls)
344 if (NULL != timeout_task)
346 GNUNET_SCHEDULER_cancel (timeout_task);
351 GNUNET_IDENTITY_cancel (op);
356 GNUNET_CONVERSATION_call_stop (call1);
361 GNUNET_CONVERSATION_call_stop (call2);
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
367 GNUNET_CONVERSATION_phone_destroy (phone);
372 GNUNET_IDENTITY_disconnect (id);
377 GNUNET_NAMESTORE_cancel (qe);
382 GNUNET_NAMESTORE_disconnect (ns);
389 caller_event_handler (void *cls, enum GNUNET_CONVERSATION_CallerEventCode code)
394 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
395 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
396 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected caller code: %d\n", code);
403 phone_event_handler (void *cls,
404 enum GNUNET_CONVERSATION_PhoneEventCode code,
405 struct GNUNET_CONVERSATION_Caller *caller,
406 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
415 case GNUNET_CONVERSATION_EC_PHONE_RING:
416 if (NULL == active_caller1)
418 active_caller1 = caller;
420 GNUNET_CONVERSATION_caller_pick_up (caller,
421 &caller_event_handler,
428 GNUNET_CONVERSATION_caller_suspend (active_caller1);
429 active_caller2 = caller;
431 GNUNET_CONVERSATION_caller_pick_up (caller,
432 &caller_event_handler,
439 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
440 if (caller == active_caller2)
442 active_caller2 = NULL;
443 GNUNET_CONVERSATION_caller_resume (active_caller1,
447 else if (caller == active_caller1)
449 active_caller1 = NULL;
450 GNUNET_break (NULL == active_caller2);
451 GNUNET_SCHEDULER_shutdown ();
456 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected phone code: %d\n", code);
463 call_event_handler (void *cls, enum GNUNET_CONVERSATION_CallEventCode code)
465 const char *cid = cls;
469 case GNUNET_CONVERSATION_EC_CALL_RINGING:
472 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
473 LOG_DEBUG ("Call %s picked\n", cid);
476 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
477 LOG_DEBUG ("Call %s GNS lookup failed \n", cid);
480 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
481 LOG_DEBUG ("Call %s hungup\n", cid);
482 if (0 == strcmp (cid, "call1"))
488 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
489 LOG_DEBUG ("Call %s suspended\n", cid);
492 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
493 LOG_DEBUG ("Call %s resumed\n", cid);
496 case GNUNET_CONVERSATION_EC_CALL_ERROR:
498 if (0 == strcmp (cid, "call1"))
502 GNUNET_SCHEDULER_shutdown ();
509 caller_ego_create_cont (void *cls,
510 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk,
515 GNUNET_assert (NULL == emsg);
520 namestore_put_cont (void *cls, int32_t success, const char *emsg)
524 GNUNET_assert (GNUNET_YES == success);
525 GNUNET_assert (NULL == emsg);
526 GNUNET_assert (NULL == op);
527 op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
532 identity_cb (void *cls,
533 struct GNUNET_IDENTITY_Ego *ego,
537 struct GNUNET_GNSRECORD_Data rd;
538 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
546 if (0 == strcmp (name, "phone-ego"))
548 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
549 GNUNET_asprintf (&gns_name,
551 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
553 GNUNET_CONVERSATION_phone_create (cfg, ego, &phone_event_handler, NULL);
554 GNUNET_assert (NULL != phone);
555 memset (&rd, 0, sizeof(rd));
556 GNUNET_CONVERSATION_phone_get_record (phone, &rd);
557 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
558 rd.expiration_time = UINT64_MAX;
560 GNUNET_NAMESTORE_records_store (ns,
561 GNUNET_IDENTITY_ego_get_private_key (ego),
562 "phone" /* GNS label */,
569 if (0 == strcmp (name, "caller-ego"))
571 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
572 GNUNET_asprintf (&gns_caller_id,
574 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
575 call1 = GNUNET_CONVERSATION_call_start (cfg,
582 call2 = GNUNET_CONVERSATION_call_start (cfg,
595 phone_ego_create_cont (void *cls,
596 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk,
601 GNUNET_assert (NULL == emsg);
607 const struct GNUNET_CONFIGURATION_Handle *c,
608 struct GNUNET_TESTING_Peer *peer)
613 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test, NULL);
614 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
615 id = GNUNET_IDENTITY_connect (cfg, &identity_cb, NULL);
616 op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
617 ns = GNUNET_NAMESTORE_connect (cfg);
622 main (int argc, char *argv[])
626 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api_twocalls",
627 "test_conversation.conf",
631 if (call1_finished && call2_finished)
637 /* end of test_conversation_api_twocalls.c */