add args for GNUNET_OS_start_process's
[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
107 static void
108 end_badly ()
109 {
110   /* do work here */
111 #if VERBOSE
112   fprintf(stderr, "Ending on an unhappy note.\n");
113 #endif
114
115   GNUNET_TRANSPORT_disconnect (p1.th);
116   GNUNET_TRANSPORT_disconnect (p2.th);
117
118   ok = 1;
119   return;
120 }
121
122 static void
123 notify_receive (void *cls,
124                 const struct GNUNET_PeerIdentity *peer,
125                 const struct GNUNET_MessageHeader *message,
126                 struct GNUNET_TIME_Relative latency,
127                 uint32_t distance)
128 {
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok is (%d)!\n",
130               ok);
131
132   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
133                 ntohs(message->type), cls);
134
135   GNUNET_assert (ok == 7);
136   OKPP;
137
138   GNUNET_assert (MTYPE == ntohs (message->type));
139   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
140                  ntohs (message->size));
141   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
142               cls);
143   end ();
144 }
145
146
147 static void
148 notify_connect (void *cls,
149                 const struct GNUNET_PeerIdentity *peer,
150                 struct GNUNET_TIME_Relative latency,
151                 uint32_t distance)
152 {
153   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154               "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
155   GNUNET_assert ((ok >= 1) && (ok <= 6));
156   OKPP;
157 }
158
159
160 static void
161 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
162 {
163   ok--;
164 }
165
166
167 static void
168 setup_peer (struct PeerContext *p, const char *cfgname)
169 {
170   p->cfg = GNUNET_CONFIGURATION_create ();
171 #if START_ARM
172   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
173                                         "gnunet-service-arm",
174 #if VERBOSE_ARM
175                                         "-L", "DEBUG",
176 #endif
177                                         "-c", cfgname, NULL);
178 #endif
179   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
180
181   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
182                                     p,
183                                     &notify_receive,
184                                     &notify_connect, &notify_disconnect);
185   GNUNET_assert (p->th != NULL);
186 }
187
188
189 static size_t
190 notify_ready (void *cls, size_t size, void *buf)
191 {
192   struct GNUNET_MessageHeader *hdr;
193
194   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195               "Transmitting message to peer (%p) - %u!\n", cls, size);
196   GNUNET_assert (size >= 256);
197   GNUNET_assert ((ok >= 5) && (ok <= 6));
198   OKPP;
199   if (buf != NULL)
200   {
201     hdr = buf;
202     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
203     hdr->type = htons (MTYPE);
204   }
205
206   return sizeof (struct GNUNET_MessageHeader);
207 }
208
209
210 static void
211 exchange_hello_last (void *cls,
212                      const struct GNUNET_MessageHeader *message)
213 {
214   struct PeerContext *me = cls;
215
216   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
217
218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219               "Exchanging HELLO with peer (%p)!\n", cls);
220   GNUNET_assert (ok >= 3);
221   OKPP;
222   GNUNET_assert (message != NULL);
223   GNUNET_assert (GNUNET_OK ==
224                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
225                                       message, &me->id));
226
227   GNUNET_TRANSPORT_offer_hello (p1.th, message);
228
229   /*sleep(1);*/ /* Make sure we are not falling prey to the "favorable timing" bug... */
230
231   /* both HELLOs exchanged, get ready to test transmission! */
232   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233               "Finished exchanging HELLOs, now waiting for transmission!\n");
234
235   GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
236                                             &p2.id,
237                                             256, 0, TIMEOUT, &notify_ready,
238                                             &p1);
239 }
240
241 static void
242 exchange_hello (void *cls,
243                 const struct GNUNET_MessageHeader *message)
244 {
245   struct PeerContext *me = cls;
246
247   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
248   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249               "Exchanging HELLO with peer (%p)!\n", cls);
250   GNUNET_assert (ok >= 2);
251   OKPP;
252   GNUNET_assert (message != NULL);
253   GNUNET_assert (GNUNET_OK ==
254                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
255                                       message, &me->id));
256
257   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258               "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
259
260   GNUNET_TRANSPORT_offer_hello (p2.th, message);
261
262   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
263 }
264
265 #if WRITECONFIG
266 static void
267 setTransportOptions(char * filename)
268 {
269   struct GNUNET_CONFIGURATION_Handle * tempcfg;
270
271   tempcfg = GNUNET_CONFIGURATION_create();
272   GNUNET_CONFIGURATION_load (tempcfg, filename);
273
274   unsigned long long curr_port;
275   GNUNET_CONFIGURATION_get_value_number(tempcfg, "transport", "port", &curr_port);
276
277   if (is_udp)
278     {
279       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "plugins", "udp");
280       GNUNET_CONFIGURATION_set_value_number(tempcfg, "transport-udp", "PORT", curr_port + 3);
281     }
282   else if (is_tcp)
283     {
284       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "plugins", "tcp");
285       GNUNET_CONFIGURATION_set_value_number(tempcfg, "transport-tcp", "port", curr_port + 3);
286     }
287
288 #if VERBOSE_TRANSPORT
289       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "DEBUG", "YES");
290       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "PREFIX", "xterm -e xterm -T transport -e gdb --args");
291 #else
292       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "DEBUG", "NO");
293       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "PREFIX", "");
294 #endif
295
296   GNUNET_CONFIGURATION_write(tempcfg, filename);
297
298   GNUNET_CONFIGURATION_destroy(tempcfg);
299   return;
300 }
301 #endif
302
303 static void
304 run (void *cls,
305      struct GNUNET_SCHEDULER_Handle *s,
306      char *const *args,
307      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
308 {
309   GNUNET_assert (ok == 1);
310   OKPP;
311   sched = s;
312
313   die_task = GNUNET_SCHEDULER_add_delayed (sched,
314       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, NULL);
315
316   if (is_udp)
317     {
318       setup_peer (&p1, "test_transport_api_udp_peer1.conf");
319       setup_peer (&p2, "test_transport_api_udp_peer2.conf");
320     }
321   else if (is_tcp)
322     {
323       setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
324       setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
325     }
326
327   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
328 }
329
330 static int
331 check ()
332 {
333
334   char *const argv[] = { "test-transport-api",
335     "-c",
336     "test_transport_api_data.conf",
337 #if VERBOSE
338     "-L", "DEBUG",
339 #endif
340     NULL
341   };
342
343 #if WRITECONFIG
344   setTransportOptions("test_transport_api_data.conf");
345 #endif
346
347   struct GNUNET_GETOPT_CommandLineOption options[] = {
348     GNUNET_GETOPT_OPTION_END
349   };
350
351   ok = 1;
352   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
353                       argv, "test-transport-api", "nohelp",
354                       options, &run, &ok);
355   stop_arm (&p1);
356   stop_arm (&p2);
357   return ok;
358 }
359
360 int
361 main (int argc, char *argv[])
362 {
363   int ret;
364
365   if (strstr(argv[0], "tcp") != NULL)
366     {
367       is_tcp = GNUNET_YES;
368     }
369   else if (strstr(argv[0], "udp") != NULL)
370     {
371       is_udp = GNUNET_YES;
372     }
373
374
375   GNUNET_log_setup ("test-transport-api",
376 #if VERBOSE
377                     "DEBUG",
378 #else
379                     "WARNING",
380 #endif
381                     NULL);
382   ret = check ();
383   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
384   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
385   return ret;
386 }
387
388 /* end of test_transport_api.c */