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