convert fs publish to MQ
[oweals/gnunet.git] / src / core / test_core_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2015 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  */
24 #include "platform.h"
25 #include "gnunet_arm_service.h"
26 #include "gnunet_core_service.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_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_Handle *th;
39   struct GNUNET_TRANSPORT_GetHelloHandle *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 process_hello (void *cls,
60                const struct GNUNET_MessageHeader *message)
61 {
62   struct PeerContext *p = cls;
63
64   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
65               "Received (my) `%s' from transport service\n", "HELLO");
66   GNUNET_assert (message != NULL);
67   if ((p == &p1) && (p2.th != NULL))
68     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
69   if ((p == &p2) && (p1.th != NULL))
70     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
71 }
72
73
74 static void
75 terminate_peer (struct PeerContext *p)
76 {
77   if (NULL != p->ch)
78   {
79     GNUNET_CORE_disconnect (p->ch);
80     p->ch = NULL;
81   }
82   if (NULL != p->th)
83   {
84     GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
85     GNUNET_TRANSPORT_disconnect (p->th);
86     p->th = NULL;
87   }
88   if (NULL != p->ats_sh)
89   {
90     GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
91     p->ats_sh = NULL;
92   }
93   if (NULL != p->ats)
94   {
95     GNUNET_ATS_connectivity_done (p->ats);
96     p->ats = NULL;
97   }
98 }
99
100
101 static void
102 terminate_task (void *cls)
103 {
104   GNUNET_assert (ok == 6);
105   terminate_peer (&p1);
106   terminate_peer (&p2);
107   ok = 0;
108 }
109
110
111 static void
112 terminate_task_error (void *cls)
113 {
114   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
115               "ENDING ANGRILY %u\n",
116               ok);
117   GNUNET_break (0);
118   terminate_peer (&p1);
119   terminate_peer (&p2);
120   ok = 42;
121 }
122
123
124 static size_t
125 transmit_ready (void *cls, size_t size, void *buf)
126 {
127   struct PeerContext *p = cls;
128   struct GNUNET_MessageHeader *m;
129
130   GNUNET_assert (ok == 4);
131   OKPP;
132   GNUNET_assert (p == &p1);
133   GNUNET_assert (NULL != buf);
134   m = (struct GNUNET_MessageHeader *) buf;
135   m->type = htons (MTYPE);
136   m->size = htons (sizeof (struct GNUNET_MessageHeader));
137   return sizeof (struct GNUNET_MessageHeader);
138 }
139
140
141 static void
142 connect_notify (void *cls,
143                 const struct GNUNET_PeerIdentity *peer)
144 {
145   struct PeerContext *pc = cls;
146
147   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
148     return;
149   GNUNET_assert (pc->connect_status == 0);
150   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151               "Encrypted connection established to peer `%4s'\n",
152               GNUNET_i2s (peer));
153   pc->connect_status = 1;
154   if (pc == &p1)
155   {
156     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157                 "Asking core (1) for transmission to peer `%4s'\n",
158                 GNUNET_i2s (&p2.id));
159     if (NULL ==
160         GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_YES,
161                                            GNUNET_CORE_PRIO_BEST_EFFORT,
162                                            GNUNET_TIME_relative_multiply
163                                            (GNUNET_TIME_UNIT_SECONDS, 145),
164                                            &p2.id,
165                                            sizeof (struct GNUNET_MessageHeader),
166                                            &transmit_ready, &p1))
167     {
168       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169                   "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
170                   GNUNET_i2s (&p2.id));
171     }
172   }
173 }
174
175
176 static void
177 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
178 {
179   struct PeerContext *pc = cls;
180
181   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
182     return;
183   pc->connect_status = 0;
184   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted connection to `%4s' cut\n",
185               GNUNET_i2s (peer));
186 }
187
188
189 static int
190 inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
191                 const struct GNUNET_MessageHeader *message)
192 {
193   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
195   return GNUNET_OK;
196 }
197
198
199 static int
200 outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
201                  const struct GNUNET_MessageHeader *message)
202 {
203   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204               "Core notifies about outbound data for `%4s'.\n",
205               GNUNET_i2s (other));
206   return GNUNET_OK;
207 }
208
209
210 static int
211 process_mtype (void *cls,
212                const struct GNUNET_PeerIdentity *peer,
213                const struct GNUNET_MessageHeader *message)
214 {
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216               "Receiving message from `%4s'.\n",
217               GNUNET_i2s (peer));
218   GNUNET_assert (ok == 5);
219   OKPP;
220   GNUNET_SCHEDULER_cancel (err_task);
221   err_task = GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
222   return GNUNET_OK;
223 }
224
225
226 static struct GNUNET_CORE_MessageHandler handlers[] = {
227   {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
228   {NULL, 0, 0}
229 };
230
231
232 static void
233 init_notify (void *cls,
234              const struct GNUNET_PeerIdentity *my_identity)
235 {
236   struct PeerContext *p = cls;
237
238   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239               "Core connection to `%4s' established\n",
240               GNUNET_i2s (my_identity));
241   p->id = *my_identity;
242   if (cls == &p1)
243   {
244     GNUNET_assert (ok == 2);
245     OKPP;
246     /* connect p2 */
247     p2.ch =
248         GNUNET_CORE_connect (p2.cfg, &p2, &init_notify, &connect_notify,
249                              &disconnect_notify, &inbound_notify, GNUNET_YES,
250                              &outbound_notify, GNUNET_YES, handlers);
251   }
252   else
253   {
254     GNUNET_assert (ok == 3);
255     OKPP;
256     GNUNET_assert (cls == &p2);
257     p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
258                                                  &p2.id,
259                                                  1);
260   }
261 }
262
263
264 static void
265 setup_peer (struct PeerContext *p,
266             const char *cfgname)
267 {
268   char *binary;
269
270   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
271   p->cfg = GNUNET_CONFIGURATION_create ();
272   p->arm_proc =
273     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
274                              NULL, NULL, NULL,
275                              binary,
276                              "gnunet-service-arm",
277                                "-c", cfgname, NULL);
278   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
279   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
280   GNUNET_assert (NULL != p->th);
281   p->ats = GNUNET_ATS_connectivity_init (p->cfg);
282   GNUNET_assert (NULL != p->ats);
283   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
284   GNUNET_free (binary);
285 }
286
287
288 static void
289 run (void *cls,
290      char *const *args,
291      const char *cfgfile,
292      const struct GNUNET_CONFIGURATION_Handle *cfg)
293 {
294   GNUNET_assert (ok == 1);
295   OKPP;
296   setup_peer (&p1, "test_core_api_peer1.conf");
297   setup_peer (&p2, "test_core_api_peer2.conf");
298   err_task =
299       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
300                                     (GNUNET_TIME_UNIT_SECONDS, 300),
301                                     &terminate_task_error, NULL);
302   p1.ch =
303       GNUNET_CORE_connect (p1.cfg, &p1,
304                            &init_notify,
305                            &connect_notify,
306                            &disconnect_notify,
307                            &inbound_notify, GNUNET_YES,
308                            &outbound_notify, GNUNET_YES,
309                            handlers);
310 }
311
312
313 static void
314 stop_arm (struct PeerContext *p)
315 {
316   if (0 != GNUNET_OS_process_kill (p->arm_proc, GNUNET_TERM_SIG))
317     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
318   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
319     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
320   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
321               GNUNET_OS_process_get_pid (p->arm_proc));
322   GNUNET_OS_process_destroy (p->arm_proc);
323   p->arm_proc = NULL;
324   GNUNET_CONFIGURATION_destroy (p->cfg);
325 }
326
327
328 int
329 main (int argc, char *argv1[])
330 {
331   char *const argv[] = { "test-core-api",
332     "-c",
333     "test_core_api_data.conf",
334     NULL
335   };
336   struct GNUNET_GETOPT_CommandLineOption options[] = {
337     GNUNET_GETOPT_OPTION_END
338   };
339   ok = 1;
340   GNUNET_log_setup ("test-core-api",
341                     "WARNING",
342                     NULL);
343   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
344                       "test-core-api", "nohelp", options, &run, &ok);
345   stop_arm (&p1);
346   stop_arm (&p2);
347   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
348   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
349
350   return ok;
351 }
352
353 /* end of test_core_api.c */