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