maybe this will fix buildbot errors...
[oweals/gnunet.git] / src / transport / test_transport_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file transport/test_transport_api.c
22  * @brief testcase for transport_api.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_os_lib.h"
29 #include "gnunet_program_lib.h"
30 #include "gnunet_scheduler_lib.h"
31 #include "gnunet_transport_service.h"
32 #include "transport.h"
33
34 #define VERBOSE GNUNET_NO
35
36 #define VERBOSE_ARM GNUNET_NO
37
38 #define START_ARM GNUNET_YES
39
40 #define VERBOSE_TRANSPORT GNUNET_NO
41
42 /**
43  * How long until we give up on transmitting the message?
44  */
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 50)
46
47 #define MTYPE 12345
48
49 struct PeerContext
50 {
51   struct GNUNET_CONFIGURATION_Handle *cfg;
52   struct GNUNET_TRANSPORT_Handle *th;
53   struct GNUNET_PeerIdentity id;
54 #if START_ARM
55   pid_t arm_pid;
56 #endif
57 };
58
59 static struct PeerContext p1;
60
61 static struct PeerContext p2;
62
63 static struct GNUNET_SCHEDULER_Handle *sched;
64
65 static int ok;
66
67 static int is_tcp;
68
69 static int is_udp;
70
71 GNUNET_SCHEDULER_TaskIdentifier die_task;
72
73 #if VERBOSE
74 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
75 #else
76 #define OKPP do { ok++; } while (0)
77 #endif
78
79
80 static void
81 end ()
82 {
83   /* do work here */
84   GNUNET_assert (ok == 8);
85   GNUNET_SCHEDULER_cancel (sched, die_task);
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
87   GNUNET_TRANSPORT_disconnect (p1.th);
88   GNUNET_TRANSPORT_disconnect (p2.th);
89
90   die_task = GNUNET_SCHEDULER_NO_TASK;
91   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
92   ok = 0;
93 }
94
95 static void
96 stop_arm (struct PeerContext *p)
97 {
98 #if START_ARM
99   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
100     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
101   GNUNET_OS_process_wait (p->arm_pid);
102 #endif
103   GNUNET_CONFIGURATION_destroy (p->cfg);
104 }
105
106 static void
107 end_badly ()
108 {
109   /* do work here */
110   fprintf(stderr, "Ending on an unhappy note.\n");
111
112   GNUNET_TRANSPORT_disconnect (p1.th);
113   GNUNET_TRANSPORT_disconnect (p2.th);
114
115   ok = 1;
116   return;
117 }
118
119 static void
120 notify_receive (void *cls,
121                 const struct GNUNET_PeerIdentity *peer,
122                 const struct GNUNET_MessageHeader *message,
123                 struct GNUNET_TIME_Relative latency,
124                 uint32_t distance)
125 {
126   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok is (%d)!\n",
127               ok);
128   GNUNET_assert (ok == 7);
129   OKPP;
130   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
131                 ntohs(message->type), cls);
132
133   GNUNET_assert (MTYPE == ntohs (message->type));
134   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
135                  ntohs (message->size));
136   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
137               cls);
138   end ();
139 }
140
141
142 static void
143 notify_connect (void *cls,
144                 const struct GNUNET_PeerIdentity *peer,
145                 struct GNUNET_TIME_Relative latency,
146                 uint32_t distance)
147 {
148   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149               "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
150   GNUNET_assert ((ok >= 1) && (ok <= 6));
151   OKPP;
152 }
153
154
155 static void
156 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
157 {
158   ok--;
159 }
160
161
162 static void
163 setup_peer (struct PeerContext *p, const char *cfgname)
164 {
165   p->cfg = GNUNET_CONFIGURATION_create ();
166 #if START_ARM
167   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
168                                         "gnunet-service-arm",
169 #if VERBOSE_ARM
170                                         "-L", "DEBUG",
171 #endif
172                                         "-c", cfgname, NULL);
173 #endif
174   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
175
176   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
177                                     p,
178                                     &notify_receive,
179                                     &notify_connect, &notify_disconnect);
180   GNUNET_assert (p->th != NULL);
181 }
182
183
184 static size_t
185 notify_ready (void *cls, size_t size, void *buf)
186 {
187   struct GNUNET_MessageHeader *hdr;
188
189   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190               "Transmitting message to peer (%p) - %u!\n", cls, size);
191   GNUNET_assert (size >= 256);
192   GNUNET_assert ((ok >= 5) && (ok <= 6));
193   OKPP;
194   if (buf != NULL)
195   {
196     hdr = buf;
197     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
198     hdr->type = htons (MTYPE);
199   }
200
201   return sizeof (struct GNUNET_MessageHeader);
202 }
203
204
205 static void
206 exchange_hello_last (void *cls,
207                      const struct GNUNET_MessageHeader *message)
208 {
209   struct PeerContext *me = cls;
210
211   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
212
213   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214               "Exchanging HELLO with peer (%p)!\n", cls);
215   GNUNET_assert (ok >= 3);
216   OKPP;
217   GNUNET_assert (message != NULL);
218   GNUNET_assert (GNUNET_OK ==
219                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
220                                       message, &me->id));
221
222   GNUNET_TRANSPORT_offer_hello (p1.th, message);
223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224               "Finished exchanging HELLOs, now waiting for transmission!\n");
225
226   /* both HELLOs exchanged, get ready to test transmission! */
227   GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
228                                           &p2.id,
229                                           256, 0, TIMEOUT, &notify_ready,
230                                           &p1);
231 }
232
233
234 static void
235 exchange_hello (void *cls,
236                 const struct GNUNET_MessageHeader *message)
237 {
238   struct PeerContext *me = cls;
239
240   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
241   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242               "Exchanging HELLO with peer (%p)!\n", cls);
243   GNUNET_assert (ok >= 2);
244   OKPP;
245   GNUNET_assert (message != NULL);
246   GNUNET_assert (GNUNET_OK ==
247                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
248                                       message, &me->id));
249
250   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251               "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
252
253   GNUNET_TRANSPORT_offer_hello (p2.th, message);
254
255   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
256 }
257
258 static void
259 setTransportOptions(char * filename)
260 {
261   struct GNUNET_CONFIGURATION_Handle * tempcfg;
262
263   tempcfg = GNUNET_CONFIGURATION_create();
264   GNUNET_CONFIGURATION_load (tempcfg, filename);
265
266   unsigned long long curr_port;
267   GNUNET_CONFIGURATION_get_value_number(tempcfg, "transport", "port", &curr_port);
268
269   if (is_udp)
270     {
271       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "plugins", "udp");
272       GNUNET_CONFIGURATION_set_value_number(tempcfg, "transport-udp", "PORT", curr_port + 3);
273     }
274   else if (is_tcp)
275     {
276       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "plugins", "tcp");
277       GNUNET_CONFIGURATION_set_value_number(tempcfg, "transport-tcp", "port", curr_port + 3);
278     }
279
280 #if VERBOSE_TRANSPORT
281       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "DEBUG", "YES");
282       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "PREFIX", "xterm -e xterm -T transport -e gdb --args");
283 #else
284       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "DEBUG", "NO");
285       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "PREFIX", "");
286 #endif
287
288   GNUNET_CONFIGURATION_write(tempcfg, filename);
289
290   GNUNET_CONFIGURATION_destroy(tempcfg);
291   return;
292 }
293
294 static void
295 run (void *cls,
296      struct GNUNET_SCHEDULER_Handle *s,
297      char *const *args,
298      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
299 {
300   GNUNET_assert (ok == 1);
301   OKPP;
302   sched = s;
303
304   setTransportOptions("test_transport_api_peer1.conf");
305   setTransportOptions("test_transport_api_peer2.conf");
306
307   die_task = GNUNET_SCHEDULER_add_delayed (sched,
308       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, NULL);
309
310   setup_peer (&p1, "test_transport_api_peer1.conf");
311   setup_peer (&p2, "test_transport_api_peer2.conf");
312
313   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
314 }
315
316 static int
317 check ()
318 {
319
320   char *const argv[] = { "test-transport-api",
321     "-c",
322     "test_transport_api_data.conf",
323 #if VERBOSE
324     "-L", "DEBUG",
325 #endif
326     NULL
327   };
328
329   setTransportOptions("test_transport_api_data.conf");
330
331   struct GNUNET_GETOPT_CommandLineOption options[] = {
332     GNUNET_GETOPT_OPTION_END
333   };
334
335   ok = 1;
336   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
337                       argv, "test-transport-api", "nohelp",
338                       options, &run, &ok);
339   stop_arm (&p1);
340   stop_arm (&p2);
341   return ok;
342 }
343
344 int
345 main (int argc, char *argv[])
346 {
347   int ret;
348
349   if (strstr(argv[0], "tcp") != NULL)
350     {
351       is_tcp = GNUNET_YES;
352     }
353   else if (strstr(argv[0], "udp") != NULL)
354     {
355       is_udp = GNUNET_YES;
356     }
357
358
359   GNUNET_log_setup ("test-transport-api",
360 #if VERBOSE
361                     "DEBUG",
362 #else
363                     "WARNING",
364 #endif
365                     NULL);
366   ret = check ();
367   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
368   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
369   return ret;
370 }
371
372 /* end of test_transport_api.c */