2 This file is part of GNUnet.
3 Copyright (C) 2013, 2014, 2018 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.c
22 * @brief testcase for conversation_api.c
24 * This test performs the operations of a call to a phone
25 * where the phone user picks up and then the call is
26 * terminated by the party that initiated the call.
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)
42 static const struct GNUNET_CONFIGURATION_Handle *cfg;
44 static struct GNUNET_IDENTITY_Handle *id;
46 static struct GNUNET_IDENTITY_Operation *op;
48 static struct GNUNET_CONVERSATION_Phone *phone;
50 static struct GNUNET_NAMESTORE_Handle *ns;
52 static struct GNUNET_CONVERSATION_Call *call;
54 static struct GNUNET_NAMESTORE_QueueEntry *qe;
56 static struct GNUNET_CONVERSATION_Caller *active_caller;
58 static char *gns_name;
60 static char *gns_caller_id;
62 static GNUNET_MICROPHONE_RecordedDataCallback phone_rdc;
64 static void *phone_rdc_cls;
66 static GNUNET_MICROPHONE_RecordedDataCallback call_rdc;
68 static void *call_rdc_cls;
70 static struct GNUNET_SCHEDULER_Task *phone_task;
72 static struct GNUNET_SCHEDULER_Task *call_task;
76 phone_send (void *cls)
78 static unsigned int i;
82 GNUNET_assert (NULL != phone_rdc);
83 GNUNET_snprintf (buf, sizeof (buf), "phone-%u", i++);
84 phone_rdc (phone_rdc_cls, strlen (buf) + 1, buf);
85 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ, &phone_send, NULL);
92 static unsigned int i;
96 GNUNET_assert (NULL != call_rdc);
97 GNUNET_snprintf (buf, sizeof (buf), "call-%u", i++);
98 call_rdc (call_rdc_cls, strlen (buf) + 1, buf);
99 call_task = GNUNET_SCHEDULER_add_delayed (FREQ, &call_send, NULL);
104 enable_speaker (void *cls)
106 const char *origin = cls;
108 fprintf (stderr, "Speaker %s enabled\n", origin);
114 disable_speaker (void *cls)
116 const char *origin = cls;
118 fprintf (stderr, "Speaker %s disabled\n", origin);
123 play (void *cls, size_t data_size, const void *data)
125 const char *origin = cls;
126 static unsigned int phone_i = 1;
127 static unsigned int call_i;
130 if (0 == strcmp (origin, "phone"))
131 GNUNET_snprintf (buf, sizeof (buf), "call-%u", call_i++);
133 GNUNET_snprintf (buf, sizeof (buf), "phone-%u", phone_i++);
134 if ((data_size != strlen (buf) + 1) || (0 != strncmp (buf, data, data_size)))
137 "Expected %s, received %.*s\n",
140 (const char *) data);
144 fprintf (stderr, ".");
146 if ((20 < call_i) && (20 < phone_i) && (NULL != call))
148 /* time to hang up ... */
149 GNUNET_CONVERSATION_call_stop (call);
156 destroy_speaker (void *cls)
158 const char *origin = cls;
160 fprintf (stderr, "Speaker %s destroyed\n", origin);
164 static struct GNUNET_SPEAKER_Handle call_speaker = {&enable_speaker,
171 static struct GNUNET_SPEAKER_Handle phone_speaker = {&enable_speaker,
179 enable_mic (void *cls,
180 GNUNET_MICROPHONE_RecordedDataCallback rdc,
183 const char *origin = cls;
185 fprintf (stderr, "Mic %s enabled\n", origin);
186 if (0 == strcmp (origin, "phone"))
189 phone_rdc_cls = rdc_cls;
190 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
195 call_rdc_cls = rdc_cls;
196 call_task = GNUNET_SCHEDULER_add_now (&call_send, NULL);
203 disable_mic (void *cls)
205 const char *origin = cls;
207 fprintf (stderr, "Mic %s disabled\n", origin);
208 if (0 == strcmp (origin, "phone"))
211 phone_rdc_cls = NULL;
212 GNUNET_SCHEDULER_cancel (phone_task);
219 GNUNET_SCHEDULER_cancel (call_task);
226 destroy_mic (void *cls)
228 const char *origin = cls;
230 fprintf (stderr, "Mic %s destroyed\n", origin);
234 static struct GNUNET_MICROPHONE_Handle call_mic = {&enable_mic,
240 static struct GNUNET_MICROPHONE_Handle phone_mic = {&enable_mic,
247 * Signature of the main function of a task.
255 GNUNET_SCHEDULER_shutdown ();
258 GNUNET_IDENTITY_cancel (op);
263 GNUNET_CONVERSATION_call_stop (call);
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
269 GNUNET_CONVERSATION_phone_destroy (phone);
274 GNUNET_IDENTITY_disconnect (id);
279 GNUNET_NAMESTORE_cancel (qe);
284 GNUNET_NAMESTORE_disconnect (ns);
291 caller_event_handler (void *cls, enum GNUNET_CONVERSATION_CallerEventCode code)
296 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
297 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
298 fprintf (stderr, "Unexpected caller code: %d\n", code);
305 phone_event_handler (void *cls,
306 enum GNUNET_CONVERSATION_PhoneEventCode code,
307 struct GNUNET_CONVERSATION_Caller *caller,
308 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
310 static enum GNUNET_CONVERSATION_PhoneEventCode expect =
311 GNUNET_CONVERSATION_EC_PHONE_RING;
315 GNUNET_break (code == expect);
318 case GNUNET_CONVERSATION_EC_PHONE_RING:
319 active_caller = caller;
320 GNUNET_CONVERSATION_caller_pick_up (caller,
321 &caller_event_handler,
325 expect = GNUNET_CONVERSATION_EC_PHONE_HUNG_UP;
327 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
328 GNUNET_break (caller == active_caller);
329 active_caller = NULL;
332 GNUNET_SCHEDULER_shutdown ();
335 fprintf (stderr, "Unexpected phone code: %d\n", code);
342 call_event_handler (void *cls, enum GNUNET_CONVERSATION_CallEventCode code)
344 static enum GNUNET_CONVERSATION_CallEventCode expect =
345 GNUNET_CONVERSATION_EC_CALL_RINGING;
348 GNUNET_break (code == expect);
351 case GNUNET_CONVERSATION_EC_CALL_RINGING:
352 expect = GNUNET_CONVERSATION_EC_CALL_PICKED_UP;
354 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
357 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
358 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
362 fprintf (stderr, "Unexpected call code: %d\n", code);
364 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
365 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
367 fprintf (stderr, "Unexpected call code: %d\n", code);
370 case GNUNET_CONVERSATION_EC_CALL_ERROR:
372 fprintf (stderr, "Unexpected call code: %d\n", code);
381 caller_ego_create_cont (void *cls,
382 const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk,
387 GNUNET_assert (NULL == emsg);
392 namestore_put_cont (void *cls, int32_t success, const char *emsg)
396 GNUNET_assert (GNUNET_YES == success);
397 GNUNET_assert (NULL == emsg);
398 GNUNET_assert (NULL == op);
399 op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
404 identity_cb (void *cls,
405 struct GNUNET_IDENTITY_Ego *ego,
409 struct GNUNET_GNSRECORD_Data rd;
410 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
418 if (0 == strcmp (name, "phone-ego"))
420 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
421 GNUNET_asprintf (&gns_name,
423 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
425 GNUNET_CONVERSATION_phone_create (cfg, ego, &phone_event_handler, NULL);
426 GNUNET_assert (NULL != phone);
427 memset (&rd, 0, sizeof (rd));
428 GNUNET_CONVERSATION_phone_get_record (phone, &rd);
429 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
430 rd.expiration_time = UINT64_MAX;
432 GNUNET_NAMESTORE_records_store (ns,
433 GNUNET_IDENTITY_ego_get_private_key (ego),
434 "phone" /* GNS label */,
441 if (0 == strcmp (name, "caller-ego"))
443 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
444 GNUNET_asprintf (&gns_caller_id,
446 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
447 call = GNUNET_CONVERSATION_call_start (cfg,
460 phone_ego_create_cont (void *cls, const char *emsg)
464 GNUNET_assert (NULL == emsg);
470 const struct GNUNET_CONFIGURATION_Handle *c,
471 struct GNUNET_TESTING_Peer *peer)
476 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test, NULL);
477 id = GNUNET_IDENTITY_connect (cfg, &identity_cb, NULL);
478 op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
479 ns = GNUNET_NAMESTORE_connect (cfg);
484 main (int argc, char *argv[])
488 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
489 "test_conversation.conf",
496 /* end of test_conversation_api.c */