glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / conversation / test_conversation_api_reject.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013, 2014, 2018 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @file conversation/test_conversation_api_reject.c
17  * @brief testcase for conversation_api.c
18  *
19  * This test performs the operations of a call to a phone
20  * where the phone user immediately hangs up (rejecting the
21  * call).
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_gnsrecord_lib.h"
27 #include "gnunet_conversation_service.h"
28 #include "gnunet_identity_service.h"
29 #include "gnunet_namestore_service.h"
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 25)
32
33 static int ok = 1;
34
35 static const struct GNUNET_CONFIGURATION_Handle *cfg;
36
37 static struct GNUNET_IDENTITY_Handle *id;
38
39 static struct GNUNET_IDENTITY_Operation *op;
40
41 static struct GNUNET_CONVERSATION_Phone *phone;
42
43 static struct GNUNET_NAMESTORE_Handle *ns;
44
45 static struct GNUNET_CONVERSATION_Call *call;
46
47 static struct GNUNET_NAMESTORE_QueueEntry *qe;
48
49 static char *gns_name;
50
51 static char *gns_caller_id;
52
53
54 static int
55 enable_speaker (void *cls)
56 {
57   (void) cls;
58   GNUNET_break (0);
59   return GNUNET_SYSERR;
60 }
61
62
63 static void
64 disable_speaker (void *cls)
65 {
66   (void) cls;
67   GNUNET_break (0);
68 }
69
70
71 static void
72 play (void *cls,
73       size_t data_size,
74       const void *data)
75 {
76   (void) cls;
77   (void) data_size;
78   (void) data;
79   GNUNET_break (0);
80 }
81
82
83 static void
84 destroy_speaker (void *cls)
85 {
86   (void) cls;
87 }
88
89
90 static struct GNUNET_SPEAKER_Handle call_speaker = {
91   &enable_speaker,
92   &play,
93   &disable_speaker,
94   &destroy_speaker,
95   "caller"
96 };
97
98
99 static int
100 enable_mic (void *cls,
101             GNUNET_MICROPHONE_RecordedDataCallback rdc,
102             void *rdc_cls)
103 {
104   (void) cls;
105   (void) rdc;
106   (void) rdc_cls;
107   GNUNET_break (0);
108   return GNUNET_SYSERR;
109 }
110
111
112 static void
113 disable_mic (void *cls)
114 {
115   (void) cls;
116   GNUNET_break (0);
117 }
118
119
120 static void
121 destroy_mic (void *cls)
122 {
123   (void) cls;
124 }
125
126
127 static struct GNUNET_MICROPHONE_Handle call_mic = {
128   &enable_mic,
129   &disable_mic,
130   &destroy_mic,
131   "caller"
132 };
133
134
135 /**
136  * Signature of the main function of a task.
137  *
138  * @param cls closure
139  */
140 static void
141 end_test (void *cls)
142 {
143   (void) cls;
144   GNUNET_SCHEDULER_shutdown ();
145   if (NULL != op)
146   {
147     GNUNET_IDENTITY_cancel (op);
148     op = NULL;
149   }
150   if (NULL != call)
151   {
152     GNUNET_CONVERSATION_call_stop (call);
153     call = NULL;
154   }
155   if (NULL != phone)
156   {
157     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158                 "Disconnecting from PHONE service.\n");
159     GNUNET_CONVERSATION_phone_destroy (phone);
160     phone = NULL;
161   }
162   if (NULL != id)
163   {
164     GNUNET_IDENTITY_disconnect (id);
165     id = NULL;
166   }
167   if (NULL != qe)
168   {
169     GNUNET_NAMESTORE_cancel (qe);
170     qe = NULL;
171   }
172   if (NULL != ns)
173   {
174     GNUNET_NAMESTORE_disconnect (ns);
175     ns = NULL;
176   }
177 }
178
179
180 static void
181 phone_event_handler (void *cls,
182                      enum GNUNET_CONVERSATION_PhoneEventCode code,
183                      struct GNUNET_CONVERSATION_Caller *caller,
184                      const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
185 {
186   static enum GNUNET_CONVERSATION_PhoneEventCode expect
187     = GNUNET_CONVERSATION_EC_PHONE_RING;
188
189   (void) cls;
190   (void) caller_id;
191   GNUNET_break (code == expect);
192   switch (code)
193   {
194   case GNUNET_CONVERSATION_EC_PHONE_RING:
195     GNUNET_CONVERSATION_caller_hang_up (caller);
196     break;
197   default:
198     fprintf (stderr, "Unexpected phone code: %d\n", code);
199     break;
200   }
201 }
202
203
204 static void
205 call_event_handler (void *cls,
206                     enum GNUNET_CONVERSATION_CallEventCode code)
207 {
208   static enum GNUNET_CONVERSATION_CallEventCode expect
209     = GNUNET_CONVERSATION_EC_CALL_RINGING;
210
211   (void) cls;
212   GNUNET_break (code == expect);
213   switch (code)
214   {
215   case GNUNET_CONVERSATION_EC_CALL_RINGING:
216     expect = GNUNET_CONVERSATION_EC_CALL_HUNG_UP;
217     break;
218   case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
219     call = NULL;
220     ok = 0;
221     GNUNET_SCHEDULER_shutdown ();
222     expect = -1;
223     break;
224   case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
225   case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
226   case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
227   case GNUNET_CONVERSATION_EC_CALL_RESUMED:
228     fprintf (stderr, "Unexpected call code: %d\n", code);
229     break;
230   case GNUNET_CONVERSATION_EC_CALL_ERROR:
231     fprintf (stderr, "Unexpected call code: %d\n", code);
232     call = NULL;
233     break;
234   }
235 }
236
237
238 static void
239 caller_ego_create_cont (void *cls,
240                         const char *emsg)
241 {
242   (void) cls;
243   op = NULL;
244   GNUNET_assert (NULL == emsg);
245 }
246
247
248 static void
249 namestore_put_cont (void *cls,
250                     int32_t success,
251                     const char *emsg)
252 {
253   (void) cls;
254   qe = NULL;
255   GNUNET_assert (GNUNET_YES == success);
256   GNUNET_assert (NULL == emsg);
257   GNUNET_assert (NULL == op);
258   op = GNUNET_IDENTITY_create (id,
259                                "caller-ego",
260                                &caller_ego_create_cont,
261                                NULL);
262 }
263
264
265 static void
266 identity_cb (void *cls,
267              struct GNUNET_IDENTITY_Ego *ego,
268              void **ctx,
269              const char *name)
270 {
271   struct GNUNET_GNSRECORD_Data rd;
272   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
273
274   (void) cls;
275   (void) ctx;
276   if (NULL == name)
277     return;
278   if (NULL == ego)
279     return;
280   if (0 == strcmp (name, "phone-ego"))
281   {
282     GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
283     GNUNET_asprintf (&gns_name,
284                      "phone.%s",
285                      GNUNET_GNSRECORD_pkey_to_zkey (&pub));
286     phone = GNUNET_CONVERSATION_phone_create (cfg,
287                                               ego,
288                                               &phone_event_handler,
289                                               NULL);
290     GNUNET_assert (NULL != phone);
291     memset (&rd, 0, sizeof (rd));
292     GNUNET_CONVERSATION_phone_get_record (phone,
293                                           &rd);
294     GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
295     rd.expiration_time = UINT64_MAX;
296     qe = GNUNET_NAMESTORE_records_store (ns,
297                                          GNUNET_IDENTITY_ego_get_private_key (ego),
298                                          "phone" /* GNS label */,
299                                          1,
300                                          &rd,
301                                          &namestore_put_cont,
302                                          NULL);
303     return;
304   }
305   if (0 == strcmp (name, "caller-ego"))
306   {
307     GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
308     GNUNET_asprintf (&gns_caller_id,
309                      "%s",
310                      GNUNET_GNSRECORD_pkey_to_zkey (&pub));
311     call = GNUNET_CONVERSATION_call_start (cfg,
312                                            ego,
313                                            gns_name,
314                                            &call_speaker,
315                                            &call_mic,
316                                            &call_event_handler,
317                                            NULL);
318     return;
319   }
320 }
321
322
323 static void
324 phone_ego_create_cont (void *cls,
325                        const char *emsg)
326 {
327   (void) cls;
328   op = NULL;
329   GNUNET_assert (NULL == emsg);
330 }
331
332
333 static void
334 run (void *cls,
335      const struct GNUNET_CONFIGURATION_Handle *c,
336      struct GNUNET_TESTING_Peer *peer)
337 {
338   (void) cls;
339   (void) peer;
340   cfg = c;
341   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test,
342                                 NULL);
343   id = GNUNET_IDENTITY_connect (cfg,
344                                 &identity_cb,
345                                 NULL);
346   op = GNUNET_IDENTITY_create (id,
347                                "phone-ego",
348                                &phone_ego_create_cont,
349                                NULL);
350   ns = GNUNET_NAMESTORE_connect (cfg);
351 }
352
353
354 int
355 main (int argc, char *argv[])
356 {
357   (void) argc;
358   (void) argv;
359   if (0 != GNUNET_TESTING_peer_run ("test_conversation_api",
360                                     "test_conversation.conf",
361                                     &run, NULL))
362     return 1;
363   return ok;
364 }
365
366 /* end of test_conversation_api_reject.c */