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