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.
16 * @file conversation/test_conversation_api.c
17 * @brief testcase for conversation_api.c
19 * This test performs the operations of a call to a phone
20 * where the phone user picks up and then the call is
21 * terminated by the party that initiated the call.
24 #include "gnunet_util_lib.h"
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_gnsrecord_lib.h"
27 #include "gnunet_conversation_service.h"
28 #include "gnunet_identity_service.h"
29 #include "gnunet_namestore_service.h"
31 #define FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 25)
37 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39 static struct GNUNET_IDENTITY_Handle *id;
41 static struct GNUNET_IDENTITY_Operation *op;
43 static struct GNUNET_CONVERSATION_Phone *phone;
45 static struct GNUNET_NAMESTORE_Handle *ns;
47 static struct GNUNET_CONVERSATION_Call *call;
49 static struct GNUNET_NAMESTORE_QueueEntry *qe;
51 static struct GNUNET_CONVERSATION_Caller *active_caller;
53 static char *gns_name;
55 static char *gns_caller_id;
57 static GNUNET_MICROPHONE_RecordedDataCallback phone_rdc;
59 static void *phone_rdc_cls;
61 static GNUNET_MICROPHONE_RecordedDataCallback call_rdc;
63 static void *call_rdc_cls;
65 static struct GNUNET_SCHEDULER_Task *phone_task;
67 static struct GNUNET_SCHEDULER_Task *call_task;
71 phone_send (void *cls)
73 static unsigned int i;
77 GNUNET_assert (NULL != phone_rdc);
78 GNUNET_snprintf (buf, sizeof (buf), "phone-%u", i++);
79 phone_rdc (phone_rdc_cls, strlen (buf) + 1, buf);
80 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ,
88 static unsigned int i;
92 GNUNET_assert (NULL != call_rdc);
93 GNUNET_snprintf (buf, sizeof (buf), "call-%u", i++);
94 call_rdc (call_rdc_cls, strlen (buf) + 1, buf);
95 call_task = GNUNET_SCHEDULER_add_delayed (FREQ,
101 enable_speaker (void *cls)
103 const char *origin = cls;
106 "Speaker %s enabled\n",
113 disable_speaker (void *cls)
115 const char *origin = cls;
118 "Speaker %s disabled\n",
128 const char *origin = cls;
129 static unsigned int phone_i = 1;
130 static unsigned int call_i;
133 if (0 == strcmp (origin, "phone"))
134 GNUNET_snprintf (buf,
139 GNUNET_snprintf (buf,
143 if ( (data_size != strlen (buf) + 1) ||
144 (0 != strncmp (buf, data, data_size)) )
147 "Expected %s, received %.*s\n",
150 (const char *) data);
154 fprintf (stderr, ".");
156 if ( (20 < call_i) &&
160 /* time to hang up ... */
161 GNUNET_CONVERSATION_call_stop (call);
168 destroy_speaker (void *cls)
170 const char *origin = cls;
172 fprintf (stderr, "Speaker %s destroyed\n", origin);
176 static struct GNUNET_SPEAKER_Handle call_speaker = {
185 static struct GNUNET_SPEAKER_Handle phone_speaker = {
195 enable_mic (void *cls,
196 GNUNET_MICROPHONE_RecordedDataCallback rdc,
199 const char *origin = cls;
204 if (0 == strcmp (origin, "phone"))
207 phone_rdc_cls = rdc_cls;
208 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
213 call_rdc_cls = rdc_cls;
214 call_task = GNUNET_SCHEDULER_add_now (&call_send, NULL);
221 disable_mic (void *cls)
223 const char *origin = cls;
228 if (0 == strcmp (origin, "phone"))
231 phone_rdc_cls = NULL;
232 GNUNET_SCHEDULER_cancel (phone_task);
239 GNUNET_SCHEDULER_cancel (call_task);
246 destroy_mic (void *cls)
248 const char *origin = cls;
251 "Mic %s destroyed\n",
256 static struct GNUNET_MICROPHONE_Handle call_mic = {
264 static struct GNUNET_MICROPHONE_Handle phone_mic = {
273 * Signature of the main function of a task.
281 GNUNET_SCHEDULER_shutdown ();
284 GNUNET_IDENTITY_cancel (op);
289 GNUNET_CONVERSATION_call_stop (call);
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
295 GNUNET_CONVERSATION_phone_destroy (phone);
300 GNUNET_IDENTITY_disconnect (id);
305 GNUNET_NAMESTORE_cancel (qe);
310 GNUNET_NAMESTORE_disconnect (ns);
317 caller_event_handler (void *cls,
318 enum GNUNET_CONVERSATION_CallerEventCode code)
323 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
324 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
326 "Unexpected caller code: %d\n",
334 phone_event_handler (void *cls,
335 enum GNUNET_CONVERSATION_PhoneEventCode code,
336 struct GNUNET_CONVERSATION_Caller *caller,
337 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
339 static enum GNUNET_CONVERSATION_PhoneEventCode expect
340 = GNUNET_CONVERSATION_EC_PHONE_RING;
344 GNUNET_break (code == expect);
347 case GNUNET_CONVERSATION_EC_PHONE_RING:
348 active_caller = caller;
349 GNUNET_CONVERSATION_caller_pick_up (caller,
350 &caller_event_handler,
354 expect = GNUNET_CONVERSATION_EC_PHONE_HUNG_UP;
356 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
357 GNUNET_break (caller == active_caller);
358 active_caller = NULL;
361 GNUNET_SCHEDULER_shutdown ();
365 "Unexpected phone code: %d\n",
373 call_event_handler (void *cls,
374 enum GNUNET_CONVERSATION_CallEventCode code)
376 static enum GNUNET_CONVERSATION_CallEventCode expect
377 = GNUNET_CONVERSATION_EC_CALL_RINGING;
380 GNUNET_break (code == expect);
383 case GNUNET_CONVERSATION_EC_CALL_RINGING:
384 expect = GNUNET_CONVERSATION_EC_CALL_PICKED_UP;
386 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
389 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
390 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
394 fprintf (stderr, "Unexpected call code: %d\n", code);
396 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
397 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
399 fprintf (stderr, "Unexpected call code: %d\n", code);
402 case GNUNET_CONVERSATION_EC_CALL_ERROR:
404 fprintf (stderr, "Unexpected call code: %d\n", code);
413 caller_ego_create_cont (void *cls,
418 GNUNET_assert (NULL == emsg);
423 namestore_put_cont (void *cls,
429 GNUNET_assert (GNUNET_YES == success);
430 GNUNET_assert (NULL == emsg);
431 GNUNET_assert (NULL == op);
432 op = GNUNET_IDENTITY_create (id,
434 &caller_ego_create_cont,
440 identity_cb (void *cls,
441 struct GNUNET_IDENTITY_Ego *ego,
445 struct GNUNET_GNSRECORD_Data rd;
446 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
454 if (0 == strcmp (name, "phone-ego"))
456 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
457 GNUNET_asprintf (&gns_name,
459 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
460 phone = GNUNET_CONVERSATION_phone_create (cfg,
462 &phone_event_handler,
464 GNUNET_assert (NULL != phone);
465 memset (&rd, 0, sizeof (rd));
466 GNUNET_CONVERSATION_phone_get_record (phone,
468 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
469 rd.expiration_time = UINT64_MAX;
470 qe = GNUNET_NAMESTORE_records_store (ns,
471 GNUNET_IDENTITY_ego_get_private_key (ego),
472 "phone" /* GNS label */,
479 if (0 == strcmp (name, "caller-ego"))
481 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
482 GNUNET_asprintf (&gns_caller_id,
484 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
485 call = GNUNET_CONVERSATION_call_start (cfg,
498 phone_ego_create_cont (void *cls,
503 GNUNET_assert (NULL == emsg);
509 const struct GNUNET_CONFIGURATION_Handle *c,
510 struct GNUNET_TESTING_Peer *peer)
515 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
518 id = GNUNET_IDENTITY_connect (cfg,
521 op = GNUNET_IDENTITY_create (id,
523 &phone_ego_create_cont,
525 ns = GNUNET_NAMESTORE_connect (cfg);
535 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
536 "test_conversation.conf",
543 /* end of test_conversation_api.c */