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