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/>.
19 * @file conversation/test_conversation_api.c
20 * @brief testcase for conversation_api.c
22 * This test performs the operations of a call to a phone
23 * where the phone user picks up and then the call is
24 * terminated by the party that initiated the call.
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_gnsrecord_lib.h"
30 #include "gnunet_conversation_service.h"
31 #include "gnunet_identity_service.h"
32 #include "gnunet_namestore_service.h"
34 #define FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 25)
40 static const struct GNUNET_CONFIGURATION_Handle *cfg;
42 static struct GNUNET_IDENTITY_Handle *id;
44 static struct GNUNET_IDENTITY_Operation *op;
46 static struct GNUNET_CONVERSATION_Phone *phone;
48 static struct GNUNET_NAMESTORE_Handle *ns;
50 static struct GNUNET_CONVERSATION_Call *call;
52 static struct GNUNET_NAMESTORE_QueueEntry *qe;
54 static struct GNUNET_CONVERSATION_Caller *active_caller;
56 static char *gns_name;
58 static char *gns_caller_id;
60 static GNUNET_MICROPHONE_RecordedDataCallback phone_rdc;
62 static void *phone_rdc_cls;
64 static GNUNET_MICROPHONE_RecordedDataCallback call_rdc;
66 static void *call_rdc_cls;
68 static struct GNUNET_SCHEDULER_Task *phone_task;
70 static struct GNUNET_SCHEDULER_Task *call_task;
74 phone_send (void *cls)
76 static unsigned int i;
80 GNUNET_assert (NULL != phone_rdc);
81 GNUNET_snprintf (buf, sizeof (buf), "phone-%u", i++);
82 phone_rdc (phone_rdc_cls, strlen (buf) + 1, buf);
83 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ,
91 static unsigned int i;
95 GNUNET_assert (NULL != call_rdc);
96 GNUNET_snprintf (buf, sizeof (buf), "call-%u", i++);
97 call_rdc (call_rdc_cls, strlen (buf) + 1, buf);
98 call_task = GNUNET_SCHEDULER_add_delayed (FREQ,
104 enable_speaker (void *cls)
106 const char *origin = cls;
109 "Speaker %s enabled\n",
116 disable_speaker (void *cls)
118 const char *origin = cls;
121 "Speaker %s disabled\n",
131 const char *origin = cls;
132 static unsigned int phone_i = 1;
133 static unsigned int call_i;
136 if (0 == strcmp (origin, "phone"))
137 GNUNET_snprintf (buf,
142 GNUNET_snprintf (buf,
146 if ( (data_size != strlen (buf) + 1) ||
147 (0 != strncmp (buf, data, data_size)) )
150 "Expected %s, received %.*s\n",
153 (const char *) data);
157 fprintf (stderr, ".");
159 if ( (20 < call_i) &&
163 /* time to hang up ... */
164 GNUNET_CONVERSATION_call_stop (call);
171 destroy_speaker (void *cls)
173 const char *origin = cls;
175 fprintf (stderr, "Speaker %s destroyed\n", origin);
179 static struct GNUNET_SPEAKER_Handle call_speaker = {
188 static struct GNUNET_SPEAKER_Handle phone_speaker = {
198 enable_mic (void *cls,
199 GNUNET_MICROPHONE_RecordedDataCallback rdc,
202 const char *origin = cls;
207 if (0 == strcmp (origin, "phone"))
210 phone_rdc_cls = rdc_cls;
211 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
216 call_rdc_cls = rdc_cls;
217 call_task = GNUNET_SCHEDULER_add_now (&call_send, NULL);
224 disable_mic (void *cls)
226 const char *origin = cls;
231 if (0 == strcmp (origin, "phone"))
234 phone_rdc_cls = NULL;
235 GNUNET_SCHEDULER_cancel (phone_task);
242 GNUNET_SCHEDULER_cancel (call_task);
249 destroy_mic (void *cls)
251 const char *origin = cls;
254 "Mic %s destroyed\n",
259 static struct GNUNET_MICROPHONE_Handle call_mic = {
267 static struct GNUNET_MICROPHONE_Handle phone_mic = {
276 * Signature of the main function of a task.
284 GNUNET_SCHEDULER_shutdown ();
287 GNUNET_IDENTITY_cancel (op);
292 GNUNET_CONVERSATION_call_stop (call);
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
298 GNUNET_CONVERSATION_phone_destroy (phone);
303 GNUNET_IDENTITY_disconnect (id);
308 GNUNET_NAMESTORE_cancel (qe);
313 GNUNET_NAMESTORE_disconnect (ns);
320 caller_event_handler (void *cls,
321 enum GNUNET_CONVERSATION_CallerEventCode code)
326 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
327 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
329 "Unexpected caller code: %d\n",
337 phone_event_handler (void *cls,
338 enum GNUNET_CONVERSATION_PhoneEventCode code,
339 struct GNUNET_CONVERSATION_Caller *caller,
340 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
342 static enum GNUNET_CONVERSATION_PhoneEventCode expect
343 = GNUNET_CONVERSATION_EC_PHONE_RING;
347 GNUNET_break (code == expect);
350 case GNUNET_CONVERSATION_EC_PHONE_RING:
351 active_caller = caller;
352 GNUNET_CONVERSATION_caller_pick_up (caller,
353 &caller_event_handler,
357 expect = GNUNET_CONVERSATION_EC_PHONE_HUNG_UP;
359 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
360 GNUNET_break (caller == active_caller);
361 active_caller = NULL;
364 GNUNET_SCHEDULER_shutdown ();
368 "Unexpected phone code: %d\n",
376 call_event_handler (void *cls,
377 enum GNUNET_CONVERSATION_CallEventCode code)
379 static enum GNUNET_CONVERSATION_CallEventCode expect
380 = GNUNET_CONVERSATION_EC_CALL_RINGING;
383 GNUNET_break (code == expect);
386 case GNUNET_CONVERSATION_EC_CALL_RINGING:
387 expect = GNUNET_CONVERSATION_EC_CALL_PICKED_UP;
389 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
392 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
393 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
397 fprintf (stderr, "Unexpected call code: %d\n", code);
399 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
400 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
402 fprintf (stderr, "Unexpected call code: %d\n", code);
405 case GNUNET_CONVERSATION_EC_CALL_ERROR:
407 fprintf (stderr, "Unexpected call code: %d\n", code);
416 caller_ego_create_cont (void *cls,
421 GNUNET_assert (NULL == emsg);
426 namestore_put_cont (void *cls,
432 GNUNET_assert (GNUNET_YES == success);
433 GNUNET_assert (NULL == emsg);
434 GNUNET_assert (NULL == op);
435 op = GNUNET_IDENTITY_create (id,
437 &caller_ego_create_cont,
443 identity_cb (void *cls,
444 struct GNUNET_IDENTITY_Ego *ego,
448 struct GNUNET_GNSRECORD_Data rd;
449 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
457 if (0 == strcmp (name, "phone-ego"))
459 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
460 GNUNET_asprintf (&gns_name,
462 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
463 phone = GNUNET_CONVERSATION_phone_create (cfg,
465 &phone_event_handler,
467 GNUNET_assert (NULL != phone);
468 memset (&rd, 0, sizeof (rd));
469 GNUNET_CONVERSATION_phone_get_record (phone,
471 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
472 rd.expiration_time = UINT64_MAX;
473 qe = GNUNET_NAMESTORE_records_store (ns,
474 GNUNET_IDENTITY_ego_get_private_key (ego),
475 "phone" /* GNS label */,
482 if (0 == strcmp (name, "caller-ego"))
484 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
485 GNUNET_asprintf (&gns_caller_id,
487 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
488 call = GNUNET_CONVERSATION_call_start (cfg,
501 phone_ego_create_cont (void *cls,
506 GNUNET_assert (NULL == emsg);
512 const struct GNUNET_CONFIGURATION_Handle *c,
513 struct GNUNET_TESTING_Peer *peer)
518 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
521 id = GNUNET_IDENTITY_connect (cfg,
524 op = GNUNET_IDENTITY_create (id,
526 &phone_ego_create_cont,
528 ns = GNUNET_NAMESTORE_connect (cfg);
538 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
539 "test_conversation.conf",
546 /* end of test_conversation_api.c */