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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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,
93 static unsigned int i;
97 GNUNET_assert (NULL != call_rdc);
98 GNUNET_snprintf (buf, sizeof (buf), "call-%u", i++);
99 call_rdc (call_rdc_cls, strlen (buf) + 1, buf);
100 call_task = GNUNET_SCHEDULER_add_delayed (FREQ,
106 enable_speaker (void *cls)
108 const char *origin = cls;
111 "Speaker %s enabled\n",
118 disable_speaker (void *cls)
120 const char *origin = cls;
123 "Speaker %s disabled\n",
133 const char *origin = cls;
134 static unsigned int phone_i = 1;
135 static unsigned int call_i;
138 if (0 == strcmp (origin, "phone"))
139 GNUNET_snprintf (buf,
144 GNUNET_snprintf (buf,
148 if ( (data_size != strlen (buf) + 1) ||
149 (0 != strncmp (buf, data, data_size)) )
152 "Expected %s, received %.*s\n",
155 (const char *) data);
159 fprintf (stderr, ".");
161 if ( (20 < call_i) &&
165 /* time to hang up ... */
166 GNUNET_CONVERSATION_call_stop (call);
173 destroy_speaker (void *cls)
175 const char *origin = cls;
177 fprintf (stderr, "Speaker %s destroyed\n", origin);
181 static struct GNUNET_SPEAKER_Handle call_speaker = {
190 static struct GNUNET_SPEAKER_Handle phone_speaker = {
200 enable_mic (void *cls,
201 GNUNET_MICROPHONE_RecordedDataCallback rdc,
204 const char *origin = cls;
209 if (0 == strcmp (origin, "phone"))
212 phone_rdc_cls = rdc_cls;
213 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
218 call_rdc_cls = rdc_cls;
219 call_task = GNUNET_SCHEDULER_add_now (&call_send, NULL);
226 disable_mic (void *cls)
228 const char *origin = cls;
233 if (0 == strcmp (origin, "phone"))
236 phone_rdc_cls = NULL;
237 GNUNET_SCHEDULER_cancel (phone_task);
244 GNUNET_SCHEDULER_cancel (call_task);
251 destroy_mic (void *cls)
253 const char *origin = cls;
256 "Mic %s destroyed\n",
261 static struct GNUNET_MICROPHONE_Handle call_mic = {
269 static struct GNUNET_MICROPHONE_Handle phone_mic = {
278 * Signature of the main function of a task.
286 GNUNET_SCHEDULER_shutdown ();
289 GNUNET_IDENTITY_cancel (op);
294 GNUNET_CONVERSATION_call_stop (call);
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
300 GNUNET_CONVERSATION_phone_destroy (phone);
305 GNUNET_IDENTITY_disconnect (id);
310 GNUNET_NAMESTORE_cancel (qe);
315 GNUNET_NAMESTORE_disconnect (ns);
322 caller_event_handler (void *cls,
323 enum GNUNET_CONVERSATION_CallerEventCode code)
328 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
329 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
331 "Unexpected caller code: %d\n",
339 phone_event_handler (void *cls,
340 enum GNUNET_CONVERSATION_PhoneEventCode code,
341 struct GNUNET_CONVERSATION_Caller *caller,
342 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
344 static enum GNUNET_CONVERSATION_PhoneEventCode expect
345 = GNUNET_CONVERSATION_EC_PHONE_RING;
349 GNUNET_break (code == expect);
352 case GNUNET_CONVERSATION_EC_PHONE_RING:
353 active_caller = caller;
354 GNUNET_CONVERSATION_caller_pick_up (caller,
355 &caller_event_handler,
359 expect = GNUNET_CONVERSATION_EC_PHONE_HUNG_UP;
361 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
362 GNUNET_break (caller == active_caller);
363 active_caller = NULL;
366 GNUNET_SCHEDULER_shutdown ();
370 "Unexpected phone code: %d\n",
378 call_event_handler (void *cls,
379 enum GNUNET_CONVERSATION_CallEventCode code)
381 static enum GNUNET_CONVERSATION_CallEventCode expect
382 = GNUNET_CONVERSATION_EC_CALL_RINGING;
385 GNUNET_break (code == expect);
388 case GNUNET_CONVERSATION_EC_CALL_RINGING:
389 expect = GNUNET_CONVERSATION_EC_CALL_PICKED_UP;
391 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
394 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
395 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
399 fprintf (stderr, "Unexpected call code: %d\n", code);
401 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
402 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
404 fprintf (stderr, "Unexpected call code: %d\n", code);
407 case GNUNET_CONVERSATION_EC_CALL_ERROR:
409 fprintf (stderr, "Unexpected call code: %d\n", code);
418 caller_ego_create_cont (void *cls,
423 GNUNET_assert (NULL == emsg);
428 namestore_put_cont (void *cls,
434 GNUNET_assert (GNUNET_YES == success);
435 GNUNET_assert (NULL == emsg);
436 GNUNET_assert (NULL == op);
437 op = GNUNET_IDENTITY_create (id,
439 &caller_ego_create_cont,
445 identity_cb (void *cls,
446 struct GNUNET_IDENTITY_Ego *ego,
450 struct GNUNET_GNSRECORD_Data rd;
451 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
459 if (0 == strcmp (name, "phone-ego"))
461 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
462 GNUNET_asprintf (&gns_name,
464 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
465 phone = GNUNET_CONVERSATION_phone_create (cfg,
467 &phone_event_handler,
469 GNUNET_assert (NULL != phone);
470 memset (&rd, 0, sizeof (rd));
471 GNUNET_CONVERSATION_phone_get_record (phone,
473 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
474 rd.expiration_time = UINT64_MAX;
475 qe = GNUNET_NAMESTORE_records_store (ns,
476 GNUNET_IDENTITY_ego_get_private_key (ego),
477 "phone" /* GNS label */,
484 if (0 == strcmp (name, "caller-ego"))
486 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
487 GNUNET_asprintf (&gns_caller_id,
489 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
490 call = GNUNET_CONVERSATION_call_start (cfg,
503 phone_ego_create_cont (void *cls,
508 GNUNET_assert (NULL == emsg);
514 const struct GNUNET_CONFIGURATION_Handle *c,
515 struct GNUNET_TESTING_Peer *peer)
520 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
523 id = GNUNET_IDENTITY_connect (cfg,
526 op = GNUNET_IDENTITY_create (id,
528 &phone_ego_create_cont,
530 ns = GNUNET_NAMESTORE_connect (cfg);
540 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
541 "test_conversation.conf",
548 /* end of test_conversation_api.c */