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