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