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"
39 static const struct GNUNET_CONFIGURATION_Handle *cfg;
41 static struct GNUNET_IDENTITY_Handle *id;
43 static struct GNUNET_IDENTITY_Operation *op;
45 static struct GNUNET_CONVERSATION_Phone *phone;
47 static struct GNUNET_NAMESTORE_Handle *ns;
49 static struct GNUNET_CONVERSATION_Call *call;
51 static struct GNUNET_NAMESTORE_QueueEntry *qe;
53 static struct GNUNET_CONVERSATION_Caller *active_caller;
55 static char *gns_name;
57 static char *gns_caller_id;
61 enable_speaker (void *cls)
63 const char *origin = cls;
66 "Speaker %s enabled\n",
73 disable_speaker (void *cls)
75 const char *origin = cls;
78 "Speaker %s disabled\n",
88 const char *origin = cls;
91 "Speaker %s plays %u bytes\n",
93 (unsigned int) data_size);
98 destroy_speaker (void *cls)
100 const char *origin = cls;
102 fprintf (stderr, "Speaker %s destroyed\n", origin);
106 static struct GNUNET_SPEAKER_Handle caller_speaker = {
115 static struct GNUNET_SPEAKER_Handle phone_speaker = {
125 enable_mic (void *cls,
126 GNUNET_MICROPHONE_RecordedDataCallback rdc,
129 const char *origin = cls;
139 disable_mic (void *cls)
141 const char *origin = cls;
150 destroy_mic (void *cls)
152 const char *origin = cls;
155 "Mic %s destroyed\n",
160 static struct GNUNET_MICROPHONE_Handle caller_mic = {
168 static struct GNUNET_MICROPHONE_Handle phone_mic = {
177 * Signature of the main function of a task.
180 * @param tc context information (why was this task triggered now)
183 end_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
185 GNUNET_SCHEDULER_shutdown ();
188 GNUNET_IDENTITY_cancel (op);
193 GNUNET_CONVERSATION_call_stop (call);
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
199 GNUNET_CONVERSATION_phone_destroy (phone);
204 GNUNET_IDENTITY_disconnect (id);
209 GNUNET_NAMESTORE_cancel (qe);
214 GNUNET_NAMESTORE_disconnect (ns);
221 caller_event_handler (void *cls,
222 enum GNUNET_CONVERSATION_CallerEventCode code)
226 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
227 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
228 fprintf (stderr, "Unexpected caller code: %d\n", code);
235 phone_event_handler (void *cls,
236 enum GNUNET_CONVERSATION_PhoneEventCode code,
237 struct GNUNET_CONVERSATION_Caller *caller,
238 const char *caller_id)
240 static enum GNUNET_CONVERSATION_PhoneEventCode expect
241 = GNUNET_CONVERSATION_EC_PHONE_RING;
243 GNUNET_break (0 == strcmp (caller_id,
245 GNUNET_break (code == expect);
248 case GNUNET_CONVERSATION_EC_PHONE_RING:
249 active_caller = caller;
250 GNUNET_CONVERSATION_caller_pick_up (caller,
251 &caller_event_handler,
255 expect = GNUNET_CONVERSATION_EC_PHONE_HUNG_UP;
257 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
258 GNUNET_break (caller == active_caller);
259 active_caller = NULL;
261 GNUNET_SCHEDULER_shutdown ();
264 fprintf (stderr, "Unexpected phone code: %d\n", code);
271 call_event_handler (void *cls,
272 enum GNUNET_CONVERSATION_CallEventCode code)
274 static enum GNUNET_CONVERSATION_CallEventCode expect
275 = GNUNET_CONVERSATION_EC_CALL_RINGING;
277 GNUNET_break (code == expect);
280 case GNUNET_CONVERSATION_EC_CALL_RINGING:
281 expect = GNUNET_CONVERSATION_EC_CALL_PICKED_UP;
283 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
285 GNUNET_CONVERSATION_call_stop (call);
288 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
289 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
290 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
291 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
292 fprintf (stderr, "Unexpected call code: %d\n", code);
299 caller_ego_create_cont (void *cls,
303 GNUNET_assert (NULL == emsg);
308 namestore_put_cont (void *cls,
313 GNUNET_assert (GNUNET_YES == success);
314 GNUNET_assert (NULL == emsg);
315 GNUNET_assert (NULL == op);
316 op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
321 identity_cb (void *cls,
322 struct GNUNET_IDENTITY_Ego *ego,
326 struct GNUNET_GNSRECORD_Data rd;
327 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
333 if (0 == strcmp (name, "phone-ego"))
335 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
336 GNUNET_asprintf (&gns_name,
338 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
339 phone = GNUNET_CONVERSATION_phone_create (cfg,
341 &phone_event_handler,
343 GNUNET_assert (NULL != phone);
344 memset (&rd, 0, sizeof (rd));
345 GNUNET_CONVERSATION_phone_get_record (phone,
347 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
348 rd.expiration_time = UINT64_MAX;
349 qe = GNUNET_NAMESTORE_records_store (ns,
350 GNUNET_IDENTITY_ego_get_private_key (ego),
351 "phone" /* GNS label */,
358 if (0 == strcmp (name, "caller-ego"))
360 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
361 GNUNET_asprintf (&gns_caller_id,
363 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
364 call = GNUNET_CONVERSATION_call_start (cfg,
377 phone_ego_create_cont (void *cls,
381 GNUNET_assert (NULL == emsg);
387 const struct GNUNET_CONFIGURATION_Handle *c,
388 struct GNUNET_TESTING_Peer *peer)
391 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
392 (GNUNET_TIME_UNIT_MINUTES, 1), &end_test,
394 id = GNUNET_IDENTITY_connect (cfg,
397 op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
398 ns = GNUNET_NAMESTORE_connect (cfg);
403 main (int argc, char *argv[])
405 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
406 "test_conversation.conf",
412 /* end of test_conversation_api.c */