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