aboutsummaryrefslogtreecommitdiff
path: root/src/conversation/test_conversation_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/conversation/test_conversation_api.c')
-rw-r--r--src/conversation/test_conversation_api.c511
1 files changed, 0 insertions, 511 deletions
diff --git a/src/conversation/test_conversation_api.c b/src/conversation/test_conversation_api.c
deleted file mode 100644
index 41ef75821..000000000
--- a/src/conversation/test_conversation_api.c
+++ /dev/null
@@ -1,511 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013, 2014, 2018 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file conversation/test_conversation_api.c
22 * @brief testcase for conversation_api.c
23 *
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.
27 */
28#include "platform.h"
29#include "gnunet_util_lib.h"
30#include "gnunet_testing_lib.h"
31#include "gnunet_gnsrecord_lib.h"
32#include "gnunet_conversation_service.h"
33#include "gnunet_identity_service.h"
34#include "gnunet_namestore_service.h"
35
36#define FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
37
38#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 25)
39
40static int ok = 1;
41
42static const struct GNUNET_CONFIGURATION_Handle *cfg;
43
44static struct GNUNET_IDENTITY_Handle *id;
45
46static struct GNUNET_IDENTITY_Operation *op;
47
48static struct GNUNET_CONVERSATION_Phone *phone;
49
50static struct GNUNET_NAMESTORE_Handle *ns;
51
52static struct GNUNET_CONVERSATION_Call *call;
53
54static struct GNUNET_NAMESTORE_QueueEntry *qe;
55
56static struct GNUNET_CONVERSATION_Caller *active_caller;
57
58static char *gns_name;
59
60static char *gns_caller_id;
61
62static GNUNET_MICROPHONE_RecordedDataCallback phone_rdc;
63
64static void *phone_rdc_cls;
65
66static GNUNET_MICROPHONE_RecordedDataCallback call_rdc;
67
68static void *call_rdc_cls;
69
70static struct GNUNET_SCHEDULER_Task *phone_task;
71
72static struct GNUNET_SCHEDULER_Task *call_task;
73
74
75static void
76phone_send (void *cls)
77{
78 static unsigned int i;
79 char buf[32];
80
81 (void) cls;
82 GNUNET_assert (NULL != phone_rdc);
83 GNUNET_snprintf (buf, sizeof(buf), "phone-%u", i++);
84 phone_rdc (phone_rdc_cls, strlen (buf) + 1, buf);
85 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ, &phone_send, NULL);
86}
87
88
89static void
90call_send (void *cls)
91{
92 static unsigned int i;
93 char buf[32];
94
95 (void) cls;
96 GNUNET_assert (NULL != call_rdc);
97 GNUNET_snprintf (buf, sizeof(buf), "call-%u", i++);
98 call_rdc (call_rdc_cls, strlen (buf) + 1, buf);
99 call_task = GNUNET_SCHEDULER_add_delayed (FREQ, &call_send, NULL);
100}
101
102
103static int
104enable_speaker (void *cls)
105{
106 const char *origin = cls;
107
108 fprintf (stderr, "Speaker %s enabled\n", origin);
109 return GNUNET_OK;
110}
111
112
113static void
114disable_speaker (void *cls)
115{
116 const char *origin = cls;
117
118 fprintf (stderr, "Speaker %s disabled\n", origin);
119}
120
121
122static void
123play (void *cls, size_t data_size, const void *data)
124{
125 const char *origin = cls;
126 static unsigned int phone_i = 1;
127 static unsigned int call_i;
128 char buf[32];
129
130 if (0 == strcmp (origin, "phone"))
131 GNUNET_snprintf (buf, sizeof(buf), "call-%u", call_i++);
132 else
133 GNUNET_snprintf (buf, sizeof(buf), "phone-%u", phone_i++);
134 if ((data_size != strlen (buf) + 1) || (0 != strncmp (buf, data, data_size)))
135 {
136 fprintf (stderr,
137 "Expected %s, received %.*s\n",
138 buf,
139 (int) data_size,
140 (const char *) data);
141 }
142 else
143 {
144 fprintf (stderr, ".");
145 }
146 if ((20 < call_i) && (20 < phone_i) && (NULL != call))
147 {
148 /* time to hang up ... */
149 GNUNET_CONVERSATION_call_stop (call);
150 call = NULL;
151 }
152}
153
154
155static void
156destroy_speaker (void *cls)
157{
158 const char *origin = cls;
159
160 fprintf (stderr, "Speaker %s destroyed\n", origin);
161}
162
163
164static struct GNUNET_SPEAKER_Handle call_speaker = { &enable_speaker,
165 &play,
166 &disable_speaker,
167 &destroy_speaker,
168 "caller" };
169
170
171static struct GNUNET_SPEAKER_Handle phone_speaker = { &enable_speaker,
172 &play,
173 &disable_speaker,
174 &destroy_speaker,
175 "phone" };
176
177
178static int
179enable_mic (void *cls,
180 GNUNET_MICROPHONE_RecordedDataCallback rdc,
181 void *rdc_cls)
182{
183 const char *origin = cls;
184
185 fprintf (stderr, "Mic %s enabled\n", origin);
186 if (0 == strcmp (origin, "phone"))
187 {
188 phone_rdc = rdc;
189 phone_rdc_cls = rdc_cls;
190 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
191 }
192 else
193 {
194 call_rdc = rdc;
195 call_rdc_cls = rdc_cls;
196 call_task = GNUNET_SCHEDULER_add_now (&call_send, NULL);
197 }
198 return GNUNET_OK;
199}
200
201
202static void
203disable_mic (void *cls)
204{
205 const char *origin = cls;
206
207 fprintf (stderr, "Mic %s disabled\n", origin);
208 if (0 == strcmp (origin, "phone"))
209 {
210 phone_rdc = NULL;
211 phone_rdc_cls = NULL;
212 GNUNET_SCHEDULER_cancel (phone_task);
213 phone_task = NULL;
214 }
215 else
216 {
217 call_rdc = NULL;
218 call_rdc_cls = NULL;
219 GNUNET_SCHEDULER_cancel (call_task);
220 call_task = NULL;
221 }
222}
223
224
225static void
226destroy_mic (void *cls)
227{
228 const char *origin = cls;
229
230 fprintf (stderr, "Mic %s destroyed\n", origin);
231}
232
233
234static struct GNUNET_MICROPHONE_Handle call_mic = { &enable_mic,
235 &disable_mic,
236 &destroy_mic,
237 "caller" };
238
239
240static struct GNUNET_MICROPHONE_Handle phone_mic = { &enable_mic,
241 &disable_mic,
242 &destroy_mic,
243 "phone" };
244
245
246/**
247 * Signature of the main function of a task.
248 *
249 * @param cls closure
250 */
251static void
252end_test (void *cls)
253{
254 (void) cls;
255 GNUNET_SCHEDULER_shutdown ();
256 if (NULL != op)
257 {
258 GNUNET_IDENTITY_cancel (op);
259 op = NULL;
260 }
261 if (NULL != call)
262 {
263 GNUNET_CONVERSATION_call_stop (call);
264 call = NULL;
265 }
266 if (NULL != phone)
267 {
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
269 GNUNET_CONVERSATION_phone_destroy (phone);
270 phone = NULL;
271 }
272 if (NULL != id)
273 {
274 GNUNET_IDENTITY_disconnect (id);
275 id = NULL;
276 }
277 if (NULL != qe)
278 {
279 GNUNET_NAMESTORE_cancel (qe);
280 qe = NULL;
281 }
282 if (NULL != ns)
283 {
284 GNUNET_NAMESTORE_disconnect (ns);
285 ns = NULL;
286 }
287}
288
289
290static void
291caller_event_handler (void *cls, enum GNUNET_CONVERSATION_CallerEventCode code)
292{
293 (void) cls;
294 switch (code)
295 {
296 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
297 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
298 fprintf (stderr, "Unexpected caller code: %d\n", code);
299 break;
300 }
301}
302
303
304static void
305phone_event_handler (void *cls,
306 enum GNUNET_CONVERSATION_PhoneEventCode code,
307 struct GNUNET_CONVERSATION_Caller *caller,
308 const struct GNUNET_IDENTITY_PublicKey *caller_id)
309{
310 static enum GNUNET_CONVERSATION_PhoneEventCode expect =
311 GNUNET_CONVERSATION_EC_PHONE_RING;
312
313 (void) cls;
314 (void) caller_id;
315 GNUNET_break (code == expect);
316 switch (code)
317 {
318 case GNUNET_CONVERSATION_EC_PHONE_RING:
319 active_caller = caller;
320 GNUNET_CONVERSATION_caller_pick_up (caller,
321 &caller_event_handler,
322 NULL,
323 &phone_speaker,
324 &phone_mic);
325 expect = GNUNET_CONVERSATION_EC_PHONE_HUNG_UP;
326 break;
327
328 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
329 GNUNET_break (caller == active_caller);
330 active_caller = NULL;
331 if (1 == ok)
332 ok = 0;
333 GNUNET_SCHEDULER_shutdown ();
334 break;
335
336 default:
337 fprintf (stderr, "Unexpected phone code: %d\n", code);
338 break;
339 }
340}
341
342
343static void
344call_event_handler (void *cls, enum GNUNET_CONVERSATION_CallEventCode code)
345{
346 static enum GNUNET_CONVERSATION_CallEventCode expect =
347 GNUNET_CONVERSATION_EC_CALL_RINGING;
348
349 (void) cls;
350 GNUNET_break (code == expect);
351 switch (code)
352 {
353 case GNUNET_CONVERSATION_EC_CALL_RINGING:
354 expect = GNUNET_CONVERSATION_EC_CALL_PICKED_UP;
355 break;
356
357 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
358 expect = -1;
359 break;
360
361 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
362 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
363 call = NULL;
364 ok = 2;
365 GNUNET_break (0);
366 fprintf (stderr, "Unexpected call code: %d\n", code);
367 break;
368
369 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
370 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
371 GNUNET_break (0);
372 fprintf (stderr, "Unexpected call code: %d\n", code);
373 ok = 2;
374 break;
375
376 case GNUNET_CONVERSATION_EC_CALL_ERROR:
377 GNUNET_break (0);
378 fprintf (stderr, "Unexpected call code: %d\n", code);
379 call = NULL;
380 ok = 2;
381 break;
382 }
383}
384
385
386static void
387caller_ego_create_cont (void *cls,
388 const struct GNUNET_IDENTITY_PrivateKey *pk,
389 const char *emsg)
390{
391 (void) cls;
392 op = NULL;
393 GNUNET_assert (NULL == emsg);
394}
395
396
397static void
398namestore_put_cont (void *cls, int32_t success, const char *emsg)
399{
400 (void) cls;
401 qe = NULL;
402 GNUNET_assert (GNUNET_YES == success);
403 GNUNET_assert (NULL == emsg);
404 GNUNET_assert (NULL == op);
405 op = GNUNET_IDENTITY_create (id, "caller-ego", NULL,
406 GNUNET_IDENTITY_TYPE_ECDSA,
407 &caller_ego_create_cont,
408 NULL);
409}
410
411
412static void
413identity_cb (void *cls,
414 struct GNUNET_IDENTITY_Ego *ego,
415 void **ctx,
416 const char *name)
417{
418 struct GNUNET_GNSRECORD_Data rd;
419 struct GNUNET_IDENTITY_PublicKey pub;
420
421 (void) cls;
422 (void) ctx;
423 if (NULL == name)
424 return;
425 if (NULL == ego)
426 return;
427 if (0 == strcmp (name, "phone-ego"))
428 {
429 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
430 GNUNET_asprintf (&gns_name,
431 "phone.%s",
432 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
433 phone =
434 GNUNET_CONVERSATION_phone_create (cfg, ego, &phone_event_handler, NULL);
435 GNUNET_assert (NULL != phone);
436 memset (&rd, 0, sizeof(rd));
437 GNUNET_CONVERSATION_phone_get_record (phone, &rd);
438 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
439 rd.expiration_time = UINT64_MAX;
440 qe =
441 GNUNET_NAMESTORE_records_store (ns,
442 GNUNET_IDENTITY_ego_get_private_key (ego),
443 "phone" /* GNS label */,
444 1,
445 &rd,
446 &namestore_put_cont,
447 NULL);
448 return;
449 }
450 if (0 == strcmp (name, "caller-ego"))
451 {
452 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
453 GNUNET_asprintf (&gns_caller_id,
454 "%s",
455 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
456 call = GNUNET_CONVERSATION_call_start (cfg,
457 ego,
458 gns_name,
459 &call_speaker,
460 &call_mic,
461 &call_event_handler,
462 NULL);
463 return;
464 }
465}
466
467
468static void
469phone_ego_create_cont (void *cls,
470 const struct GNUNET_IDENTITY_PrivateKey *pk,
471 const char *emsg)
472{
473 (void) cls;
474 op = NULL;
475 GNUNET_assert (NULL == emsg);
476}
477
478
479static void
480run (void *cls,
481 const struct GNUNET_CONFIGURATION_Handle *c,
482 struct GNUNET_TESTING_Peer *peer)
483{
484 (void) cls;
485 (void) peer;
486 cfg = c;
487 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test, NULL);
488 id = GNUNET_IDENTITY_connect (cfg, &identity_cb, NULL);
489 op = GNUNET_IDENTITY_create (id, "phone-ego", NULL,
490 GNUNET_IDENTITY_TYPE_ECDSA,
491 &phone_ego_create_cont,
492 NULL);
493 ns = GNUNET_NAMESTORE_connect (cfg);
494}
495
496
497int
498main (int argc, char *argv[])
499{
500 (void) argc;
501 (void) argv;
502 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
503 "test_conversation.conf",
504 &run,
505 NULL))
506 return 1;
507 return ok;
508}
509
510
511/* end of test_conversation_api.c */