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