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