Use Suffix Extensions in Makefiles (doc, src/{arm,dht,integration,statistics}) for...
[oweals/gnunet.git] / src / conversation / test_conversation_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013, 2014 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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.
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   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 {
92   static unsigned int i;
93   char buf[32];
94
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, sizeof (buf), "call-%u", call_i++);
138   else
139     GNUNET_snprintf (buf, sizeof (buf), "phone-%u", phone_i++);
140   if ( (data_size != strlen (buf) + 1) ||
141        (0 != strncmp (buf, data, data_size)) )
142   {
143     fprintf (stderr,
144              "Expected %s, received %.*s\n",
145              buf,
146              (int) data_size,
147              (const char *) data);
148   }
149   else
150   {
151     fprintf (stderr, ".");
152   }
153   if ( (20 < call_i) &&
154        (20 < phone_i) &&
155        (NULL != call) )
156   {
157     /* time to hang up ... */
158     GNUNET_CONVERSATION_call_stop (call);
159     call = NULL;
160   }
161 }
162
163
164 static void
165 destroy_speaker (void *cls)
166 {
167   const char *origin = cls;
168
169   fprintf (stderr, "Speaker %s destroyed\n", origin);
170 }
171
172
173 static struct GNUNET_SPEAKER_Handle call_speaker = {
174   &enable_speaker,
175   &play,
176   &disable_speaker,
177   &destroy_speaker,
178   "caller"
179 };
180
181
182 static struct GNUNET_SPEAKER_Handle phone_speaker = {
183   &enable_speaker,
184   &play,
185   &disable_speaker,
186   &destroy_speaker,
187   "phone"
188 };
189
190
191 static int
192 enable_mic (void *cls,
193             GNUNET_MICROPHONE_RecordedDataCallback rdc,
194             void *rdc_cls)
195 {
196   const char *origin = cls;
197
198   fprintf (stderr,
199            "Mic %s enabled\n",
200            origin);
201   if (0 == strcmp (origin, "phone"))
202   {
203     phone_rdc = rdc;
204     phone_rdc_cls = rdc_cls;
205     phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
206   }
207   else
208   {
209     call_rdc = rdc;
210     call_rdc_cls = rdc_cls;
211     call_task = GNUNET_SCHEDULER_add_now (&call_send, NULL);
212   }
213   return GNUNET_OK;
214 }
215
216
217 static void
218 disable_mic (void *cls)
219 {
220   const char *origin = cls;
221
222   fprintf (stderr,
223            "Mic %s disabled\n",
224            origin);
225   if (0 == strcmp (origin, "phone"))
226   {
227     phone_rdc = NULL;
228     phone_rdc_cls = NULL;
229     GNUNET_SCHEDULER_cancel (phone_task);
230     phone_task = NULL;
231   }
232   else
233   {
234     call_rdc = NULL;
235     call_rdc_cls = NULL;
236     GNUNET_SCHEDULER_cancel (call_task);
237     call_task = NULL;
238   }
239 }
240
241
242 static void
243 destroy_mic (void *cls)
244 {
245   const char *origin = cls;
246
247   fprintf (stderr,
248            "Mic %s destroyed\n",
249            origin);
250 }
251
252
253 static struct GNUNET_MICROPHONE_Handle call_mic = {
254   &enable_mic,
255   &disable_mic,
256   &destroy_mic,
257   "caller"
258 };
259
260
261 static struct GNUNET_MICROPHONE_Handle phone_mic = {
262   &enable_mic,
263   &disable_mic,
264   &destroy_mic,
265   "phone"
266 };
267
268
269 /**
270  * Signature of the main function of a task.
271  *
272  * @param cls closure
273  */
274 static void
275 end_test (void *cls)
276 {
277   GNUNET_SCHEDULER_shutdown ();
278   if (NULL != op)
279   {
280     GNUNET_IDENTITY_cancel (op);
281     op = NULL;
282   }
283   if (NULL != call)
284   {
285     GNUNET_CONVERSATION_call_stop (call);
286     call = NULL;
287   }
288   if (NULL != phone)
289   {
290     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
291     GNUNET_CONVERSATION_phone_destroy (phone);
292     phone = NULL;
293   }
294   if (NULL != id)
295   {
296     GNUNET_IDENTITY_disconnect (id);
297     id = NULL;
298   }
299   if (NULL != qe)
300   {
301     GNUNET_NAMESTORE_cancel (qe);
302     qe = NULL;
303   }
304   if (NULL != ns)
305   {
306     GNUNET_NAMESTORE_disconnect (ns);
307     ns = NULL;
308   }
309 }
310
311
312 static void
313 caller_event_handler (void *cls,
314                       enum GNUNET_CONVERSATION_CallerEventCode code)
315 {
316   switch (code)
317   {
318   case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
319   case GNUNET_CONVERSATION_EC_CALLER_RESUME:
320     fprintf (stderr,
321              "Unexpected caller code: %d\n",
322              code);
323     break;
324   }
325 }
326
327
328 static void
329 phone_event_handler (void *cls,
330                      enum GNUNET_CONVERSATION_PhoneEventCode code,
331                      struct GNUNET_CONVERSATION_Caller *caller,
332                      const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
333 {
334   static enum GNUNET_CONVERSATION_PhoneEventCode expect
335     = GNUNET_CONVERSATION_EC_PHONE_RING;
336
337   GNUNET_break (code == expect);
338   switch (code)
339   {
340   case GNUNET_CONVERSATION_EC_PHONE_RING:
341     active_caller = caller;
342     GNUNET_CONVERSATION_caller_pick_up (caller,
343                                         &caller_event_handler,
344                                         NULL,
345                                         &phone_speaker,
346                                         &phone_mic);
347     expect = GNUNET_CONVERSATION_EC_PHONE_HUNG_UP;
348     break;
349   case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
350     GNUNET_break (caller == active_caller);
351     active_caller = NULL;
352     if (1 == ok)
353       ok = 0;
354     GNUNET_SCHEDULER_shutdown ();
355     break;
356   default:
357     fprintf (stderr,
358              "Unexpected phone code: %d\n",
359              code);
360     break;
361   }
362 }
363
364
365 static void
366 call_event_handler (void *cls,
367                     enum GNUNET_CONVERSATION_CallEventCode code)
368 {
369   static enum GNUNET_CONVERSATION_CallEventCode expect
370     = GNUNET_CONVERSATION_EC_CALL_RINGING;
371
372   GNUNET_break (code == expect);
373   switch (code)
374   {
375   case GNUNET_CONVERSATION_EC_CALL_RINGING:
376     expect = GNUNET_CONVERSATION_EC_CALL_PICKED_UP;
377     break;
378   case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
379     expect = -1;
380     break;
381   case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
382   case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
383     call = NULL;
384     ok = 2;
385     GNUNET_break (0);
386     fprintf (stderr, "Unexpected call code: %d\n", code);
387     break;
388   case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
389   case GNUNET_CONVERSATION_EC_CALL_RESUMED:
390     GNUNET_break (0);
391     fprintf (stderr, "Unexpected call code: %d\n", code);
392     ok = 2;
393     break;
394   case GNUNET_CONVERSATION_EC_CALL_ERROR:
395     GNUNET_break (0);
396     fprintf (stderr, "Unexpected call code: %d\n", code);
397     call = NULL;
398     ok = 2;
399     break;
400   }
401 }
402
403
404 static void
405 caller_ego_create_cont (void *cls,
406                         const char *emsg)
407 {
408   op = NULL;
409   GNUNET_assert (NULL == emsg);
410 }
411
412
413 static void
414 namestore_put_cont (void *cls,
415                     int32_t success,
416                     const char *emsg)
417 {
418   qe = NULL;
419   GNUNET_assert (GNUNET_YES == success);
420   GNUNET_assert (NULL == emsg);
421   GNUNET_assert (NULL == op);
422   op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
423 }
424
425
426 static void
427 identity_cb (void *cls,
428              struct GNUNET_IDENTITY_Ego *ego,
429              void **ctx,
430              const char *name)
431 {
432   struct GNUNET_GNSRECORD_Data rd;
433   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
434
435   if (NULL == name)
436     return;
437   if (NULL == ego)
438     return;
439   if (0 == strcmp (name, "phone-ego"))
440   {
441     GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
442     GNUNET_asprintf (&gns_name,
443                      "phone.%s",
444                      GNUNET_GNSRECORD_pkey_to_zkey (&pub));
445     phone = GNUNET_CONVERSATION_phone_create (cfg,
446                                               ego,
447                                               &phone_event_handler,
448                                               NULL);
449     GNUNET_assert (NULL != phone);
450     memset (&rd, 0, sizeof (rd));
451     GNUNET_CONVERSATION_phone_get_record (phone,
452                                           &rd);
453     GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
454     rd.expiration_time = UINT64_MAX;
455     qe = GNUNET_NAMESTORE_records_store (ns,
456                                          GNUNET_IDENTITY_ego_get_private_key (ego),
457                                          "phone" /* GNS label */,
458                                          1,
459                                          &rd,
460                                          &namestore_put_cont,
461                                          NULL);
462     return;
463   }
464   if (0 == strcmp (name, "caller-ego"))
465   {
466     GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
467     GNUNET_asprintf (&gns_caller_id,
468                      "%s",
469                      GNUNET_GNSRECORD_pkey_to_zkey (&pub));
470     call = GNUNET_CONVERSATION_call_start (cfg,
471                                            ego,
472                                            ego,
473                                            gns_name,
474                                            &call_speaker,
475                                            &call_mic,
476                                            &call_event_handler,
477                                            NULL);
478     return;
479   }
480 }
481
482
483 static void
484 phone_ego_create_cont (void *cls,
485                        const char *emsg)
486 {
487   op = NULL;
488   GNUNET_assert (NULL == emsg);
489 }
490
491
492 static void
493 run (void *cls,
494      const struct GNUNET_CONFIGURATION_Handle *c,
495      struct GNUNET_TESTING_Peer *peer)
496 {
497   cfg = c;
498   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test,
499                                 NULL);
500   id = GNUNET_IDENTITY_connect (cfg,
501                                 &identity_cb,
502                                 NULL);
503   op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
504   ns = GNUNET_NAMESTORE_connect (cfg);
505 }
506
507
508 int
509 main (int argc, char *argv[])
510 {
511   if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
512                                     "test_conversation.conf",
513                                     &run, NULL))
514     return 1;
515   return ok;
516 }
517
518 /* end of test_conversation_api.c */