-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / core / test_core_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2015, 2016 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 /**
19  * @file core/test_core_api.c
20  * @brief testcase for core_api.c
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_arm_service.h"
25 #include "gnunet_core_service.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_transport_service.h"
28 #include "gnunet_transport_hello_service.h"
29 #include "gnunet_ats_service.h"
30
31 #define MTYPE 12345
32
33 struct PeerContext
34 {
35   struct GNUNET_CONFIGURATION_Handle *cfg;
36   struct GNUNET_CORE_Handle *ch;
37   struct GNUNET_PeerIdentity id;
38   struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
39   struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
40   struct GNUNET_ATS_ConnectivityHandle *ats;
41   struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
42   struct GNUNET_MessageHeader *hello;
43   int connect_status;
44   struct GNUNET_OS_Process *arm_proc;
45 };
46
47 static struct PeerContext p1;
48
49 static struct PeerContext p2;
50
51 static struct GNUNET_SCHEDULER_Task *err_task;
52
53 static int ok;
54
55 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
56
57
58 static void
59 offer_hello_done (void *cls)
60 {
61   struct PeerContext *p = cls;
62
63   p->oh = NULL;
64 }
65
66
67 static void
68 process_hello (void *cls,
69                const struct GNUNET_MessageHeader *message)
70 {
71   struct PeerContext *p = cls;
72
73   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
74               "Received (my) HELLO from transport service\n");
75   GNUNET_assert (message != NULL);
76   if ((p == &p1) && (NULL == p2.oh))
77     p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg, message,
78                                           &offer_hello_done,
79                                           &p2);
80   if ((p == &p2) && (NULL == p1.oh))
81     p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
82                                           message,
83                                           &offer_hello_done,
84                                           &p1);
85 }
86
87
88 static void
89 terminate_peer (struct PeerContext *p)
90 {
91   if (NULL != p->ch)
92   {
93     GNUNET_CORE_disconnect (p->ch);
94     p->ch = NULL;
95   }
96   if (NULL != p->ghh)
97   {
98     GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
99     p->ghh = NULL;
100   }
101   if (NULL != p->oh)
102   {
103     GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
104     p->oh = NULL;
105   }
106   if (NULL != p->ats_sh)
107   {
108     GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
109     p->ats_sh = NULL;
110   }
111   if (NULL != p->ats)
112   {
113     GNUNET_ATS_connectivity_done (p->ats);
114     p->ats = NULL;
115   }
116 }
117
118
119 static void
120 terminate_task (void *cls)
121 {
122   GNUNET_assert (ok == 6);
123   terminate_peer (&p1);
124   terminate_peer (&p2);
125   ok = 0;
126 }
127
128
129 static void
130 terminate_task_error (void *cls)
131 {
132   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133               "ENDING ANGRILY %u\n",
134               ok);
135   GNUNET_break (0);
136   terminate_peer (&p1);
137   terminate_peer (&p2);
138   ok = 42;
139 }
140
141
142 static void *
143 connect_notify (void *cls,
144                 const struct GNUNET_PeerIdentity *peer,
145                 struct GNUNET_MQ_Handle *mq)
146 {
147   struct PeerContext *pc = cls;
148   struct GNUNET_MQ_Envelope *env;
149   struct GNUNET_MessageHeader *msg;
150
151   if (0 == memcmp (&pc->id,
152                    peer,
153                    sizeof (struct GNUNET_PeerIdentity)))
154     return (void *) peer;
155   GNUNET_assert (pc->connect_status == 0);
156   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157               "Encrypted connection established to peer `%s'\n",
158               GNUNET_i2s (peer));
159   pc->connect_status = 1;
160   if (pc == &p1)
161   {
162     uint64_t flags;
163     const void *extra;
164
165     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166                 "Asking core (1) for transmission to peer `%s'\n",
167                 GNUNET_i2s (&p2.id));
168     env = GNUNET_MQ_msg (msg,
169                          MTYPE);
170     /* enable corking for this test */
171     extra = GNUNET_CORE_get_mq_options (GNUNET_YES,
172                                         GNUNET_CORE_PRIO_BEST_EFFORT,
173                                         &flags);
174     GNUNET_MQ_env_set_options (env,
175                                flags,
176                                extra);
177     /* now actually transmit message */
178     GNUNET_assert (ok == 4);
179     OKPP;
180     GNUNET_MQ_send (mq,
181                     env);
182   }
183   return (void *) peer;
184 }
185
186
187 static void
188 disconnect_notify (void *cls,
189                    const struct GNUNET_PeerIdentity *peer,
190                    void *internal_cls)
191 {
192   struct PeerContext *pc = cls;
193
194   if (0 == memcmp (&pc->id,
195                    peer,
196                    sizeof (struct GNUNET_PeerIdentity)))
197     return;
198   pc->connect_status = 0;
199   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200               "Encrypted connection to `%s' cut\n",
201               GNUNET_i2s (peer));
202 }
203
204
205 static void
206 handle_test (void *cls,
207              const struct GNUNET_MessageHeader *message)
208 {
209   const struct GNUNET_PeerIdentity *peer = cls;
210
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212               "Receiving message from `%s'.\n",
213               GNUNET_i2s (peer));
214   GNUNET_assert (ok == 5);
215   OKPP;
216   GNUNET_SCHEDULER_cancel (err_task);
217   err_task = GNUNET_SCHEDULER_add_now (&terminate_task,
218                                        NULL);
219 }
220
221
222 static void
223 init_notify (void *cls,
224              const struct GNUNET_PeerIdentity *my_identity)
225 {
226   struct PeerContext *p = cls;
227   struct GNUNET_MQ_MessageHandler handlers[] = {
228     GNUNET_MQ_hd_fixed_size (test,
229                              MTYPE,
230                              struct GNUNET_MessageHeader,
231                              NULL),
232     GNUNET_MQ_handler_end ()
233   };
234
235   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236               "Core connection to `%s' established\n",
237               GNUNET_i2s (my_identity));
238   p->id = *my_identity;
239   if (cls == &p1)
240   {
241     GNUNET_assert (ok == 2);
242     OKPP;
243     /* connect p2 */
244     p2.ch = GNUNET_CORE_connect (p2.cfg,
245                                  &p2,
246                                  &init_notify,
247                                  &connect_notify,
248                                  &disconnect_notify,
249                                  handlers);
250   }
251   else
252   {
253     GNUNET_assert (ok == 3);
254     OKPP;
255     GNUNET_assert (cls == &p2);
256     p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
257                                                  &p2.id,
258                                                  1);
259   }
260 }
261
262
263 static void
264 setup_peer (struct PeerContext *p,
265             const char *cfgname)
266 {
267   char *binary;
268
269   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
270   p->cfg = GNUNET_CONFIGURATION_create ();
271   p->arm_proc =
272     GNUNET_OS_start_process (GNUNET_YES,
273                              GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
274                              NULL, NULL, NULL,
275                              binary,
276                              "gnunet-service-arm",
277                              "-c",
278                              cfgname,
279                              NULL);
280   GNUNET_assert (GNUNET_OK ==
281                  GNUNET_CONFIGURATION_load (p->cfg,
282                                             cfgname));
283   p->ats = GNUNET_ATS_connectivity_init (p->cfg);
284   GNUNET_assert (NULL != p->ats);
285   p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
286                                        GNUNET_TRANSPORT_AC_ANY,
287                                        &process_hello,
288                                        p);
289   GNUNET_free (binary);
290 }
291
292
293 static void
294 run (void *cls,
295      char *const *args,
296      const char *cfgfile,
297      const struct GNUNET_CONFIGURATION_Handle *cfg)
298 {
299   struct GNUNET_MQ_MessageHandler handlers[] = {
300     GNUNET_MQ_hd_fixed_size (test,
301                              MTYPE,
302                              struct GNUNET_MessageHeader,
303                              NULL),
304     GNUNET_MQ_handler_end ()
305   };
306
307   GNUNET_assert (ok == 1);
308   OKPP;
309   setup_peer (&p1,
310               "test_core_api_peer1.conf");
311   setup_peer (&p2,
312               "test_core_api_peer2.conf");
313   err_task =
314       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
315                                     (GNUNET_TIME_UNIT_SECONDS, 300),
316                                     &terminate_task_error, NULL);
317   p1.ch =
318       GNUNET_CORE_connect (p1.cfg,
319                            &p1,
320                            &init_notify,
321                            &connect_notify,
322                            &disconnect_notify,
323                            handlers);
324 }
325
326
327 static void
328 stop_arm (struct PeerContext *p)
329 {
330   if (0 != GNUNET_OS_process_kill (p->arm_proc,
331                                    GNUNET_TERM_SIG))
332     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
333                          "kill");
334   if (GNUNET_OK !=
335       GNUNET_OS_process_wait (p->arm_proc))
336     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
337                          "waitpid");
338   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339               "ARM process %u stopped\n",
340               GNUNET_OS_process_get_pid (p->arm_proc));
341   GNUNET_OS_process_destroy (p->arm_proc);
342   p->arm_proc = NULL;
343   GNUNET_CONFIGURATION_destroy (p->cfg);
344 }
345
346
347 int
348 main (int argc,
349       char *argv1[])
350 {
351   char *const argv[] = {
352     "test-core-api",
353     "-c",
354     "test_core_api_data.conf",
355     NULL
356   };
357   struct GNUNET_GETOPT_CommandLineOption options[] = {
358     GNUNET_GETOPT_OPTION_END
359   };
360   ok = 1;
361   GNUNET_log_setup ("test-core-api",
362                     "WARNING",
363                     NULL);
364   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
365                       argv,
366                       "test-core-api",
367                       "nohelp",
368                       options,
369                       &run,
370                       &ok);
371   stop_arm (&p1);
372   stop_arm (&p2);
373   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
374   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
375
376   return ok;
377 }
378
379 /* end of test_core_api.c */