2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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. The
27 * actual transmission of voice data is not tested.
30 #include "gnunet_util_lib.h"
31 #include "gnunet_testing_lib.h"
32 #include "gnunet_gnsrecord_lib.h"
33 #include "gnunet_conversation_service.h"
34 #include "gnunet_identity_service.h"
35 #include "gnunet_namestore_service.h"
37 #define FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 25)
43 static const struct GNUNET_CONFIGURATION_Handle *cfg;
45 static struct GNUNET_IDENTITY_Handle *id;
47 static struct GNUNET_IDENTITY_Operation *op;
49 static struct GNUNET_CONVERSATION_Phone *phone;
51 static struct GNUNET_NAMESTORE_Handle *ns;
53 static struct GNUNET_CONVERSATION_Call *call;
55 static struct GNUNET_NAMESTORE_QueueEntry *qe;
57 static struct GNUNET_CONVERSATION_Caller *active_caller;
59 static char *gns_name;
61 static char *gns_caller_id;
63 static GNUNET_MICROPHONE_RecordedDataCallback phone_rdc;
65 static void *phone_rdc_cls;
67 static GNUNET_MICROPHONE_RecordedDataCallback call_rdc;
69 static void *call_rdc_cls;
71 static GNUNET_SCHEDULER_TaskIdentifier phone_task;
73 static GNUNET_SCHEDULER_TaskIdentifier call_task;
77 phone_send (void *cls,
78 const struct GNUNET_SCHEDULER_TaskContext *tc)
80 static unsigned int i;
83 GNUNET_assert (NULL != phone_rdc);
84 GNUNET_snprintf (buf, sizeof (buf), "phone-%u", i++);
85 phone_rdc (phone_rdc_cls, strlen (buf) + 1, buf);
86 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ,
93 const struct GNUNET_SCHEDULER_TaskContext *tc)
95 static unsigned int i;
98 GNUNET_assert (NULL != call_rdc);
99 GNUNET_snprintf (buf, sizeof (buf), "call-%u", i++);
100 call_rdc (call_rdc_cls, strlen (buf) + 1, buf);
101 call_task = GNUNET_SCHEDULER_add_delayed (FREQ,
107 enable_speaker (void *cls)
109 const char *origin = cls;
112 "Speaker %s enabled\n",
119 disable_speaker (void *cls)
121 const char *origin = cls;
124 "Speaker %s disabled\n",
134 const char *origin = cls;
135 static unsigned int phone_i;
136 static unsigned int call_i;
139 if (0 == strcmp (origin, "phone"))
140 GNUNET_snprintf (buf, sizeof (buf), "phone-%u", phone_i++);
142 GNUNET_snprintf (buf, sizeof (buf), "call-%u", call_i++);
143 if ( (data_size != strlen (buf) + 1) ||
144 (0 != strncmp (buf, data, data_size)) )
147 "Expected %s, received %.*s\n",
150 (const char *) data);
152 if ( (20 < call_i) &&
155 /* time to hang up ... */
156 GNUNET_CONVERSATION_call_stop (call);
163 destroy_speaker (void *cls)
165 const char *origin = cls;
167 fprintf (stderr, "Speaker %s destroyed\n", origin);
171 static struct GNUNET_SPEAKER_Handle call_speaker = {
180 static struct GNUNET_SPEAKER_Handle phone_speaker = {
190 enable_mic (void *cls,
191 GNUNET_MICROPHONE_RecordedDataCallback rdc,
194 const char *origin = cls;
199 if (0 == strcmp (origin, "phone"))
202 phone_rdc_cls = rdc_cls;
203 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
208 call_rdc_cls = rdc_cls;
209 call_task = GNUNET_SCHEDULER_add_now (&call_send, NULL);
216 disable_mic (void *cls)
218 const char *origin = cls;
223 if (0 == strcmp (origin, "phone"))
226 phone_rdc_cls = NULL;
227 GNUNET_SCHEDULER_cancel (phone_task);
228 phone_task = GNUNET_SCHEDULER_NO_TASK;
234 GNUNET_SCHEDULER_cancel (call_task);
235 call_task = GNUNET_SCHEDULER_NO_TASK;
241 destroy_mic (void *cls)
243 const char *origin = cls;
246 "Mic %s destroyed\n",
251 static struct GNUNET_MICROPHONE_Handle call_mic = {
259 static struct GNUNET_MICROPHONE_Handle phone_mic = {
268 * Signature of the main function of a task.
271 * @param tc context information (why was this task triggered now)
274 end_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
276 GNUNET_SCHEDULER_shutdown ();
279 GNUNET_IDENTITY_cancel (op);
284 GNUNET_CONVERSATION_call_stop (call);
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
290 GNUNET_CONVERSATION_phone_destroy (phone);
295 GNUNET_IDENTITY_disconnect (id);
300 GNUNET_NAMESTORE_cancel (qe);
305 GNUNET_NAMESTORE_disconnect (ns);
312 caller_event_handler (void *cls,
313 enum GNUNET_CONVERSATION_CallerEventCode code)
317 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
318 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
319 fprintf (stderr, "Unexpected caller code: %d\n", code);
326 phone_event_handler (void *cls,
327 enum GNUNET_CONVERSATION_PhoneEventCode code,
328 struct GNUNET_CONVERSATION_Caller *caller,
329 const char *caller_id)
331 static enum GNUNET_CONVERSATION_PhoneEventCode expect
332 = GNUNET_CONVERSATION_EC_PHONE_RING;
334 GNUNET_break (0 == strcmp (caller_id,
336 GNUNET_break (code == expect);
339 case GNUNET_CONVERSATION_EC_PHONE_RING:
340 active_caller = caller;
341 GNUNET_CONVERSATION_caller_pick_up (caller,
342 &caller_event_handler,
346 expect = GNUNET_CONVERSATION_EC_PHONE_HUNG_UP;
348 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
349 GNUNET_break (caller == active_caller);
350 active_caller = NULL;
352 GNUNET_SCHEDULER_shutdown ();
355 fprintf (stderr, "Unexpected phone code: %d\n", code);
362 call_event_handler (void *cls,
363 enum GNUNET_CONVERSATION_CallEventCode code)
365 static enum GNUNET_CONVERSATION_CallEventCode expect
366 = GNUNET_CONVERSATION_EC_CALL_RINGING;
368 GNUNET_break (code == expect);
371 case GNUNET_CONVERSATION_EC_CALL_RINGING:
372 expect = GNUNET_CONVERSATION_EC_CALL_PICKED_UP;
374 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
377 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
378 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
380 fprintf (stderr, "Unexpected call code: %d\n", code);
382 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
383 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
384 fprintf (stderr, "Unexpected call code: %d\n", code);
391 caller_ego_create_cont (void *cls,
395 GNUNET_assert (NULL == emsg);
400 namestore_put_cont (void *cls,
405 GNUNET_assert (GNUNET_YES == success);
406 GNUNET_assert (NULL == emsg);
407 GNUNET_assert (NULL == op);
408 op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
413 identity_cb (void *cls,
414 struct GNUNET_IDENTITY_Ego *ego,
418 struct GNUNET_GNSRECORD_Data rd;
419 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
425 if (0 == strcmp (name, "phone-ego"))
427 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
428 GNUNET_asprintf (&gns_name,
430 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
431 phone = GNUNET_CONVERSATION_phone_create (cfg,
433 &phone_event_handler,
435 GNUNET_assert (NULL != phone);
436 memset (&rd, 0, sizeof (rd));
437 GNUNET_CONVERSATION_phone_get_record (phone,
439 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
440 rd.expiration_time = UINT64_MAX;
441 qe = GNUNET_NAMESTORE_records_store (ns,
442 GNUNET_IDENTITY_ego_get_private_key (ego),
443 "phone" /* GNS label */,
450 if (0 == strcmp (name, "caller-ego"))
452 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
453 GNUNET_asprintf (&gns_caller_id,
455 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
456 call = GNUNET_CONVERSATION_call_start (cfg,
469 phone_ego_create_cont (void *cls,
473 GNUNET_assert (NULL == emsg);
479 const struct GNUNET_CONFIGURATION_Handle *c,
480 struct GNUNET_TESTING_Peer *peer)
483 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test,
485 id = GNUNET_IDENTITY_connect (cfg,
488 op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
489 ns = GNUNET_NAMESTORE_connect (cfg);
494 main (int argc, char *argv[])
496 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
497 "test_conversation.conf",
503 /* end of test_conversation_api.c */