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