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