-fix test
[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 call_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   "phone"
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     phone_task = GNUNET_SCHEDULER_NO_TASK;
227   }
228   else
229   {
230     call_rdc = NULL;
231     call_rdc_cls = NULL;
232     GNUNET_SCHEDULER_cancel (call_task);
233     call_task = GNUNET_SCHEDULER_NO_TASK;
234   }
235 }
236
237
238 static void
239 destroy_mic (void *cls)
240 {
241   const char *origin = cls;
242
243   fprintf (stderr,
244            "Mic %s destroyed\n",
245            origin);
246 }
247
248
249 static struct GNUNET_MICROPHONE_Handle call_mic = {
250   &enable_mic,
251   &disable_mic,
252   &destroy_mic,
253   "caller"
254 };
255
256
257 static struct GNUNET_MICROPHONE_Handle phone_mic = {
258   &enable_mic,
259   &disable_mic,
260   &destroy_mic,
261   "phone"
262 };
263
264
265 /**
266  * Signature of the main function of a task.
267  *
268  * @param cls closure
269  * @param tc context information (why was this task triggered now)
270  */
271 static void
272 end_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
273 {
274   GNUNET_SCHEDULER_shutdown ();
275   if (NULL != op)
276   {
277     GNUNET_IDENTITY_cancel (op);
278     op = NULL;
279   }
280   if (NULL != call)
281   {
282     GNUNET_CONVERSATION_call_stop (call);
283     call = NULL;
284   }
285   if (NULL != phone)
286   {
287     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
288     GNUNET_CONVERSATION_phone_destroy (phone);
289     phone = NULL;
290   }
291   if (NULL != id)
292   {
293     GNUNET_IDENTITY_disconnect (id);
294     id = NULL;
295   }
296   if (NULL != qe)
297   {
298     GNUNET_NAMESTORE_cancel (qe);
299     qe = NULL;
300   }
301   if (NULL != ns)
302   {
303     GNUNET_NAMESTORE_disconnect (ns);
304     ns = NULL;
305   }
306 }
307
308
309 static void
310 caller_event_handler (void *cls,
311                       enum GNUNET_CONVERSATION_CallerEventCode code)
312 {
313   switch (code)
314   {
315   case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
316   case GNUNET_CONVERSATION_EC_CALLER_RESUME:
317     fprintf (stderr, "Unexpected caller code: %d\n", code);
318     break;
319   }
320 }
321
322
323 static void
324 phone_event_handler (void *cls,
325                      enum GNUNET_CONVERSATION_PhoneEventCode code,
326                      struct GNUNET_CONVERSATION_Caller *caller,
327                      const char *caller_id)
328 {
329   static enum GNUNET_CONVERSATION_PhoneEventCode expect
330     = GNUNET_CONVERSATION_EC_PHONE_RING;
331
332   GNUNET_break (0 == strcmp (caller_id,
333                              gns_caller_id));
334   GNUNET_break (code == expect);
335   switch (code)
336   {
337   case GNUNET_CONVERSATION_EC_PHONE_RING:
338     active_caller = caller;
339     GNUNET_CONVERSATION_caller_pick_up (caller,
340                                         &caller_event_handler,
341                                         NULL,
342                                         &phone_speaker,
343                                         &phone_mic);
344     expect = GNUNET_CONVERSATION_EC_PHONE_HUNG_UP;
345     break;
346   case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
347     GNUNET_break (caller == active_caller);
348     active_caller = NULL;
349     ok = 0;
350     GNUNET_SCHEDULER_shutdown ();
351     break;
352   default:
353     fprintf (stderr, "Unexpected phone code: %d\n", code);
354     break;
355   }
356 }
357
358
359 static void
360 call_event_handler (void *cls,
361                     enum GNUNET_CONVERSATION_CallEventCode code)
362 {
363   static enum GNUNET_CONVERSATION_CallEventCode expect
364     = GNUNET_CONVERSATION_EC_CALL_RINGING;
365
366   GNUNET_break (code == expect);
367   switch (code)
368   {
369   case GNUNET_CONVERSATION_EC_CALL_RINGING:
370     expect = GNUNET_CONVERSATION_EC_CALL_PICKED_UP;
371     break;
372   case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
373     expect = -1;
374     break;
375   case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
376   case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
377   case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
378   case GNUNET_CONVERSATION_EC_CALL_RESUMED:
379     fprintf (stderr, "Unexpected call code: %d\n", code);
380     break;
381   }
382 }
383
384
385 static void
386 caller_ego_create_cont (void *cls,
387                         const char *emsg)
388 {
389   op = NULL;
390   GNUNET_assert (NULL == emsg);
391 }
392
393
394 static void
395 namestore_put_cont (void *cls,
396                     int32_t success,
397                     const char *emsg)
398 {
399   qe = NULL;
400   GNUNET_assert (GNUNET_YES == success);
401   GNUNET_assert (NULL == emsg);
402   GNUNET_assert (NULL == op);
403   op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
404 }
405
406
407 static void
408 identity_cb (void *cls,
409              struct GNUNET_IDENTITY_Ego *ego,
410              void **ctx,
411              const char *name)
412 {
413   struct GNUNET_GNSRECORD_Data rd;
414   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
415
416   if (NULL == name)
417     return;
418   if (NULL == ego)
419     return;
420   if (0 == strcmp (name, "phone-ego"))
421   {
422     GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
423     GNUNET_asprintf (&gns_name,
424                      "phone.%s",
425                      GNUNET_GNSRECORD_pkey_to_zkey (&pub));
426     phone = GNUNET_CONVERSATION_phone_create (cfg,
427                                               ego,
428                                               &phone_event_handler,
429                                               NULL);
430     GNUNET_assert (NULL != phone);
431     memset (&rd, 0, sizeof (rd));
432     GNUNET_CONVERSATION_phone_get_record (phone,
433                                           &rd);
434     GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
435     rd.expiration_time = UINT64_MAX;
436     qe = GNUNET_NAMESTORE_records_store (ns,
437                                          GNUNET_IDENTITY_ego_get_private_key (ego),
438                                          "phone" /* GNS label */,
439                                          1,
440                                          &rd,
441                                          &namestore_put_cont,
442                                          NULL);
443     return;
444   }
445   if (0 == strcmp (name, "caller-ego"))
446   {
447     GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
448     GNUNET_asprintf (&gns_caller_id,
449                      "%s",
450                      GNUNET_GNSRECORD_pkey_to_zkey (&pub));
451     call = GNUNET_CONVERSATION_call_start (cfg,
452                                            ego,
453                                            gns_name,
454                                            &call_speaker,
455                                            &call_mic,
456                                            &call_event_handler,
457                                            NULL);
458     return;
459   }
460 }
461
462
463 static void
464 phone_ego_create_cont (void *cls,
465                        const char *emsg)
466 {
467   op = NULL;
468   GNUNET_assert (NULL == emsg);
469 }
470
471
472 static void
473 run (void *cls,
474      const struct GNUNET_CONFIGURATION_Handle *c,
475      struct GNUNET_TESTING_Peer *peer)
476 {
477   cfg = c;
478   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
479                                 (GNUNET_TIME_UNIT_MINUTES, 1), &end_test,
480                                 NULL);
481   id = GNUNET_IDENTITY_connect (cfg,
482                                 &identity_cb,
483                                 NULL);
484   op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
485   ns = GNUNET_NAMESTORE_connect (cfg);
486 }
487
488
489 int
490 main (int argc, char *argv[])
491 {
492   if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
493                                     "test_conversation.conf",
494                                     &run, NULL))
495     return 1;
496   return ok;
497 }
498
499 /* end of test_conversation_api.c */