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
25 #include "gnunet_util_lib.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_gnsrecord_lib.h"
28 #include "gnunet_conversation_service.h"
29 #include "gnunet_identity_service.h"
30 #include "gnunet_namestore_service.h"
34 static const struct GNUNET_CONFIGURATION_Handle *cfg;
36 static struct GNUNET_IDENTITY_Handle *id;
38 static struct GNUNET_IDENTITY_Operation *op;
40 static struct GNUNET_CONVERSATION_Phone *phone;
42 static struct GNUNET_NAMESTORE_Handle *ns;
44 static struct GNUNET_CONVERSATION_Call *call;
46 static struct GNUNET_NAMESTORE_QueueEntry *qe;
48 static struct GNUNET_CONVERSATION_Caller *active_caller;
50 static char *gns_name;
52 static char *gns_caller_id;
56 enable_speaker (void *cls)
58 const char *origin = cls;
61 "Speaker %s enabled\n",
68 disable_speaker (void *cls)
70 const char *origin = cls;
73 "Speaker %s disabled\n",
83 const char *origin = cls;
86 "Speaker %s plays %u bytes\n",
88 (unsigned int) data_size);
93 destroy_speaker (void *cls)
95 const char *origin = cls;
97 fprintf (stderr, "Speaker %s destroyed\n", origin);
101 static struct GNUNET_SPEAKER_Handle caller_speaker = {
110 static struct GNUNET_SPEAKER_Handle phone_speaker = {
120 enable_mic (void *cls,
121 GNUNET_MICROPHONE_RecordedDataCallback rdc,
124 const char *origin = cls;
134 disable_mic (void *cls)
136 const char *origin = cls;
145 destroy_mic (void *cls)
147 const char *origin = cls;
150 "Mic %s destroyed\n",
155 static struct GNUNET_MICROPHONE_Handle caller_mic = {
163 static struct GNUNET_MICROPHONE_Handle phone_mic = {
172 * Signature of the main function of a task.
175 * @param tc context information (why was this task triggered now)
178 end_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
180 GNUNET_SCHEDULER_shutdown ();
183 GNUNET_IDENTITY_cancel (op);
188 GNUNET_CONVERSATION_call_stop (call);
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
194 GNUNET_CONVERSATION_phone_destroy (phone);
199 GNUNET_IDENTITY_disconnect (id);
204 GNUNET_NAMESTORE_cancel (qe);
209 GNUNET_NAMESTORE_disconnect (ns);
216 caller_event_handler (void *cls,
217 enum GNUNET_CONVERSATION_CallerEventCode code)
221 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
222 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
223 fprintf (stderr, "Unexpected caller code: %d\n", code);
230 phone_event_handler (void *cls,
231 enum GNUNET_CONVERSATION_PhoneEventCode code,
232 struct GNUNET_CONVERSATION_Caller *caller,
233 const char *caller_id)
235 static enum GNUNET_CONVERSATION_PhoneEventCode expect
236 = GNUNET_CONVERSATION_EC_PHONE_RING;
238 GNUNET_break (0 == strcmp (caller_id,
240 GNUNET_break (code == expect);
243 case GNUNET_CONVERSATION_EC_PHONE_RING:
244 active_caller = caller;
245 GNUNET_CONVERSATION_caller_pick_up (caller,
246 &caller_event_handler,
250 expect = GNUNET_CONVERSATION_EC_PHONE_HUNG_UP;
252 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
253 GNUNET_break (caller == active_caller);
254 active_caller = NULL;
256 GNUNET_SCHEDULER_shutdown ();
259 fprintf (stderr, "Unexpected phone code: %d\n", code);
266 call_event_handler (void *cls,
267 enum GNUNET_CONVERSATION_CallEventCode code)
269 static enum GNUNET_CONVERSATION_CallEventCode expect
270 = GNUNET_CONVERSATION_EC_CALL_RINGING;
272 GNUNET_break (code == expect);
275 case GNUNET_CONVERSATION_EC_CALL_RINGING:
276 expect = GNUNET_CONVERSATION_EC_CALL_PICKED_UP;
278 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
280 GNUNET_CONVERSATION_call_stop (call);
283 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
284 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
285 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
286 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
287 fprintf (stderr, "Unexpected call code: %d\n", code);
294 caller_ego_create_cont (void *cls,
298 GNUNET_assert (NULL == emsg);
303 namestore_put_cont (void *cls,
308 GNUNET_assert (GNUNET_YES == success);
309 GNUNET_assert (NULL == emsg);
310 GNUNET_assert (NULL == op);
311 op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
316 identity_cb (void *cls,
317 struct GNUNET_IDENTITY_Ego *ego,
321 struct GNUNET_GNSRECORD_Data rd;
322 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
328 if (0 == strcmp (name, "phone-ego"))
330 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
331 GNUNET_asprintf (&gns_name,
333 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
334 phone = GNUNET_CONVERSATION_phone_create (cfg,
336 &phone_event_handler,
338 GNUNET_assert (NULL != phone);
339 memset (&rd, 0, sizeof (rd));
340 GNUNET_CONVERSATION_phone_get_record (phone,
342 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
343 rd.expiration_time = UINT64_MAX;
344 qe = GNUNET_NAMESTORE_records_store (ns,
345 GNUNET_IDENTITY_ego_get_private_key (ego),
346 "phone" /* GNS label */,
353 if (0 == strcmp (name, "caller-ego"))
355 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
356 GNUNET_asprintf (&gns_caller_id,
358 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
359 call = GNUNET_CONVERSATION_call_start (cfg,
372 phone_ego_create_cont (void *cls,
376 GNUNET_assert (NULL == emsg);
382 const struct GNUNET_CONFIGURATION_Handle *c,
383 struct GNUNET_TESTING_Peer *peer)
386 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
387 (GNUNET_TIME_UNIT_MINUTES, 1), &end_test,
389 id = GNUNET_IDENTITY_connect (cfg,
392 op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
393 ns = GNUNET_NAMESTORE_connect (cfg);
398 main (int argc, char *argv[])
400 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
401 "test_conversation.conf",
407 /* end of test_conversation_api.c */