9702d945d136bd58e1b82ddedcbdd3ace648acd4
[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, 30)
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 #if VERBOSE
72 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
73 #else
74 #define OKPP do { ok++; } while (0)
75 #endif
76
77
78 static void
79 end ()
80 {
81   /* do work here */
82   //GNUNET_assert (ok == 8);
83   GNUNET_TRANSPORT_disconnect (p1.th);
84   GNUNET_TRANSPORT_disconnect (p2.th);
85   ok = 0;
86 }
87
88
89 static void
90 notify_receive (void *cls,
91                 const struct GNUNET_PeerIdentity *peer,
92                 const struct GNUNET_MessageHeader *message,
93                 struct GNUNET_TIME_Relative latency,
94                 uint32_t distance)
95 {
96   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok is (%d)!\n",
97               ok);
98   //GNUNET_assert (ok == 7);
99   OKPP;
100   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
101                 ntohs(message->type), cls);
102
103   //GNUNET_assert (MTYPE == ntohs (message->type));
104   //GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
105   //               ntohs (message->size));
106   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
107               cls);
108   end ();
109 }
110
111
112 static void
113 notify_connect (void *cls,
114                 const struct GNUNET_PeerIdentity *peer,
115                 struct GNUNET_TIME_Relative latency,
116                 uint32_t distance)
117 {
118   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119               "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
120   GNUNET_assert ((ok >= 1) && (ok <= 6));
121   OKPP;
122 }
123
124
125 static void
126 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
127 {
128   ok--;
129 }
130
131
132 static void
133 setup_peer (struct PeerContext *p, const char *cfgname)
134 {
135   p->cfg = GNUNET_CONFIGURATION_create ();
136 #if START_ARM
137   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
138                                         "gnunet-service-arm",
139 #if VERBOSE_ARM
140                                         "-L", "DEBUG",
141 #endif
142                                         "-c", cfgname, NULL);
143 #endif
144   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
145
146   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
147                                     p,
148                                     &notify_receive,
149                                     &notify_connect, &notify_disconnect);
150   GNUNET_assert (p->th != NULL);
151 }
152
153
154 static size_t
155 notify_ready (void *cls, size_t size, void *buf)
156 {
157   struct GNUNET_MessageHeader *hdr;
158
159   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160               "Transmitting message to peer (%p) - %u!\n", cls, size);
161   //GNUNET_assert (size >= 256);
162   //GNUNET_assert ((ok >= 5) && (ok <= 6));
163   OKPP;
164   if (buf != NULL)
165   {
166     hdr = buf;
167     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
168     hdr->type = htons (MTYPE);
169   }
170
171   return 0;
172   //return sizeof (struct GNUNET_MessageHeader);
173 }
174
175
176 static void
177 exchange_hello_last (void *cls,
178                      const struct GNUNET_MessageHeader *message)
179 {
180   struct PeerContext *me = cls;
181
182   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
183
184   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185               "Exchanging HELLO with peer (%p)!\n", cls);
186   GNUNET_assert (ok >= 3);
187   OKPP;
188   GNUNET_assert (message != NULL);
189   GNUNET_assert (GNUNET_OK ==
190                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
191                                       message, &me->id));
192   GNUNET_TRANSPORT_offer_hello (p1.th, message);
193   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194               "Finished exchanging HELLOs, now waiting for transmission!\n");
195
196   /* both HELLOs exchanged, get ready to test transmission! */
197   GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
198                                           &p2.id,
199                                           256, 0, TIMEOUT, &notify_ready,
200                                           &p1);
201 }
202
203
204 static void
205 exchange_hello (void *cls,
206                 const struct GNUNET_MessageHeader *message)
207 {
208   struct PeerContext *me = cls;
209
210   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212               "Exchanging HELLO with peer (%p)!\n", cls);
213   GNUNET_assert (ok >= 2);
214   OKPP;
215   GNUNET_assert (message != NULL);
216   GNUNET_assert (GNUNET_OK ==
217                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
218                                       message, &me->id));
219
220   GNUNET_TRANSPORT_offer_hello (p2.th, message);
221
222   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
223 }
224
225 static void
226 setTransportOptions(char * filename)
227 {
228   struct GNUNET_CONFIGURATION_Handle * tempcfg;
229
230   tempcfg = GNUNET_CONFIGURATION_create();
231   GNUNET_CONFIGURATION_load (tempcfg, filename);
232
233   unsigned long long curr_port;
234   GNUNET_CONFIGURATION_get_value_number(tempcfg, "transport", "port", &curr_port);
235
236   if (is_udp)
237     {
238       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "plugins", "udp");
239       GNUNET_CONFIGURATION_set_value_number(tempcfg, "transport-udp", "PORT", curr_port + 3);
240     }
241   else if (is_tcp)
242     {
243       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "plugins", "tcp");
244       GNUNET_CONFIGURATION_set_value_number(tempcfg, "transport-tcp", "port", curr_port + 3);
245     }
246
247 #if VERBOSE_TRANSPORT
248       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "DEBUG", "YES");
249       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "PREFIX", "xterm -e xterm -T transport -e gdb --args");
250 #else
251       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "DEBUG", "NO");
252       GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "PREFIX", "");
253 #endif
254
255   GNUNET_CONFIGURATION_write(tempcfg, filename);
256   return;
257 }
258
259 static void
260 run (void *cls,
261      struct GNUNET_SCHEDULER_Handle *s,
262      char *const *args,
263      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
264 {
265   GNUNET_assert (ok == 1);
266   OKPP;
267   sched = s;
268
269   setTransportOptions("test_transport_api_peer1.conf");
270   setTransportOptions("test_transport_api_peer2.conf");
271
272   setup_peer (&p1, "test_transport_api_peer1.conf");
273   setup_peer (&p2, "test_transport_api_peer2.conf");
274   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
275 }
276
277
278 static void
279 stop_arm (struct PeerContext *p)
280 {
281 #if START_ARM
282   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
283     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
284   GNUNET_OS_process_wait (p->arm_pid);
285 #endif
286   GNUNET_CONFIGURATION_destroy (p->cfg);
287 }
288
289 static int
290 check ()
291 {
292
293   char *const argv[] = { "test-transport-api",
294     "-c",
295     "test_transport_api_data.conf",
296 #if VERBOSE
297     "-L", "DEBUG",
298 #endif
299     NULL
300   };
301
302   setTransportOptions("test_transport_api_data.conf");
303
304   struct GNUNET_GETOPT_CommandLineOption options[] = {
305     GNUNET_GETOPT_OPTION_END
306   };
307
308   ok = 1;
309   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
310                       argv, "test-transport-api", "nohelp",
311                       options, &run, &ok);
312   stop_arm (&p1);
313   stop_arm (&p2);
314   return ok;
315 }
316
317 int
318 main (int argc, char *argv[])
319 {
320   int ret;
321
322   if (strstr(argv[0], "tcp") != NULL)
323     {
324       is_tcp = GNUNET_YES;
325     }
326   else if (strstr(argv[0], "udp") != NULL)
327     {
328       is_udp = GNUNET_YES;
329     }
330
331
332   GNUNET_log_setup ("test-transport-api",
333 #if VERBOSE
334                     "DEBUG",
335 #else
336                     "WARNING",
337 #endif
338                     NULL);
339   ret = check ();
340   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
341   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
342   return ret;
343 }
344
345 /* end of test_transport_api.c */