-fix
[oweals/gnunet.git] / src / transport / test_transport_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 3, 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 base test case for transport implementations
23  *
24  * This test case serves as a base for tcp, udp, and udp-nat
25  * transport test cases.  Based on the executable being run
26  * the correct test case will be performed.  Conservation of
27  * C code apparently.
28  */
29 #include "platform.h"
30 #include "gnunet_common.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_transport_service.h"
37 #include "transport.h"
38 #include "transport-testing.h"
39
40 #define VERBOSE GNUNET_EXTRA_LOGGING
41 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
42
43 #define START_ARM GNUNET_YES
44
45 /**
46  * How long until we give up on transmitting the message?
47  */
48 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
49
50 /**
51  * How long until we give up on transmitting the message?
52  */
53 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
54
55 #define MSIZE 2600
56
57 #define MTYPE 12345
58
59 static char *test_source;
60
61 static char *test_plugin;
62
63 static char *test_name;
64
65 static int ok;
66
67 static GNUNET_SCHEDULER_TaskIdentifier die_task;
68
69 static GNUNET_SCHEDULER_TaskIdentifier send_task;
70
71 struct PeerContext *p1;
72
73 struct PeerContext *p2;
74
75 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
76
77 struct GNUNET_TRANSPORT_TransmitHandle *th;
78
79 struct GNUNET_TRANSPORT_TESTING_handle *tth;
80
81 char *cfg_file_p1;
82
83 char *cfg_file_p2;
84
85 #if VERBOSE
86 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
87 #else
88 #define OKPP do { ok++; } while (0)
89 #endif
90
91 static void
92 end ()
93 {
94   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
95
96   if (send_task != GNUNET_SCHEDULER_NO_TASK)
97     GNUNET_SCHEDULER_cancel (send_task);
98
99   if (die_task != GNUNET_SCHEDULER_NO_TASK)
100     GNUNET_SCHEDULER_cancel (die_task);
101
102   if (th != NULL)
103     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
104   th = NULL;
105
106   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
107   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
108 }
109
110 static void
111 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
112 {
113   die_task = GNUNET_SCHEDULER_NO_TASK;
114
115   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
116
117
118   if (send_task != GNUNET_SCHEDULER_NO_TASK)
119     GNUNET_SCHEDULER_cancel (send_task);
120
121   if (cc != NULL)
122   {
123     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
124     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
125     cc = NULL;
126   }
127
128   if (th != NULL)
129     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
130   th = NULL;
131
132   if (p1 != NULL)
133     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
134   if (p2 != NULL)
135     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
136
137   ok = GNUNET_SYSERR;
138 }
139
140
141 static void
142 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
143                 const struct GNUNET_MessageHeader *message,
144                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
145 {
146   struct PeerContext *p = cls;
147   struct PeerContext *t = NULL;
148
149   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
150     t = p1;
151   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
152     t = p2;
153   GNUNET_assert (t != NULL);
154
155   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
156
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
159               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
160               GNUNET_i2s (&t->id));
161   GNUNET_free (ps);
162
163   if ((MTYPE == ntohs (message->type)) &&
164       (MSIZE == ntohs (message->size)))
165   {
166     ok = 0;
167     end ();
168   }
169   else
170   {
171     GNUNET_break (0);
172     ok = 1;
173     end ();
174   }
175 }
176
177
178 static size_t
179 notify_ready (void *cls, size_t size, void *buf)
180 {
181   struct PeerContext *p = cls;
182   struct GNUNET_MessageHeader *hdr;
183
184   th = NULL;
185
186   if (buf == NULL)
187   {
188     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189                 "Timeout occurred while waiting for transmit_ready\n");
190     if (GNUNET_SCHEDULER_NO_TASK != die_task)
191       GNUNET_SCHEDULER_cancel (die_task);
192     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
193     ok = 42;
194     return 0;
195   }
196
197   GNUNET_assert (size >= MSIZE);
198
199   if (buf != NULL)
200   {
201     hdr = buf;
202     hdr->size = htons (MSIZE);
203     hdr->type = htons (MTYPE);
204   }
205
206   char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
207   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208               "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
209               p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
210               GNUNET_i2s (&p->id));
211   GNUNET_free (ps);
212
213   return MSIZE;
214 }
215
216
217 static void
218 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
219 {
220   send_task = GNUNET_SCHEDULER_NO_TASK;
221
222   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
223     return;
224   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
225
226   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
228               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
229   GNUNET_free (receiver_s);
230
231   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, MSIZE, 0,
232                                                TIMEOUT_TRANSMIT, &notify_ready,
233                                                p1);
234 }
235
236
237 static void
238 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
239                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
240 {
241   static int c;
242
243   c++;
244   struct PeerContext *p = cls;
245   struct PeerContext *t = NULL;
246
247   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
248     t = p1;
249   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
250     t = p2;
251   GNUNET_assert (t != NULL);
252
253   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
254
255   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
257               t->no, GNUNET_i2s (peer));
258   GNUNET_free (ps);
259 }
260
261
262 static void
263 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
264 {
265   struct PeerContext *p = cls;
266   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
267
268   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
270               GNUNET_i2s (peer));
271
272   GNUNET_free (ps);
273
274   if (th != NULL)
275     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
276   th = NULL;
277 }
278
279 static void
280 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
281 {
282   cc = NULL;
283   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
284
285   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
286               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
287   GNUNET_free (p1_c);
288
289   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
290 }
291
292
293
294 void
295 start_cb (struct PeerContext *p, void *cls)
296 {
297   static int started;
298
299   started++;
300
301   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
302               GNUNET_i2s (&p->id));
303
304   if (started != 2)
305     return;
306
307   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
308
309   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
311               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
312   GNUNET_free (sender_c);
313
314   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
315                                                NULL);
316
317 }
318
319 static void
320 run (void *cls, char *const *args, const char *cfgfile,
321      const struct GNUNET_CONFIGURATION_Handle *cfg)
322 {
323   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
324
325   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
326                                             &notify_receive, &notify_connect,
327                                             &notify_disconnect, &start_cb,
328                                             NULL);
329
330   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
331                                             &notify_receive, &notify_connect,
332                                             &notify_disconnect, &start_cb,
333                                             NULL);
334
335   if ((p1 == NULL) || (p2 == NULL))
336   {
337     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
338     if (die_task != GNUNET_SCHEDULER_NO_TASK)
339       GNUNET_SCHEDULER_cancel (die_task);
340     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
341     return;
342   }
343 }
344
345
346 static int
347 check ()
348 {
349   static char *const argv[] = { "test-transport-api",
350     "-c",
351     "test_transport_api_data.conf",
352 #if VERBOSE
353     "-L", "DEBUG",
354 #endif
355     NULL
356   };
357   static struct GNUNET_GETOPT_CommandLineOption options[] = {
358     GNUNET_GETOPT_OPTION_END
359   };
360
361 #if WRITECONFIG
362   setTransportOptions ("test_transport_api_data.conf");
363 #endif
364   send_task = GNUNET_SCHEDULER_NO_TASK;
365
366   ok = 1;
367   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
368                       "nohelp", options, &run, &ok);
369
370   return ok;
371 }
372
373 int
374 main (int argc, char *argv[])
375 {
376   int ret;
377   int nat_res;
378
379   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
380   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
381   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
382                                                  &test_plugin);
383
384   GNUNET_log_setup (test_name,
385 #if VERBOSE
386                     "DEBUG",
387 #else
388                     "WARNING",
389 #endif
390                     NULL);
391
392   tth = GNUNET_TRANSPORT_TESTING_init ();
393
394   if ((strcmp (test_plugin, "tcp_nat") == 0) ||
395       (strcmp (test_plugin, "udp_nat") == 0))
396   {
397     nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
398     if (GNUNET_NO == nat_res)
399     {
400       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
401                   "gnunet-nat-server", "SUID not set");
402       return 0;
403     }
404     if (GNUNET_SYSERR == nat_res)
405     {
406       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
407                   "gnunet-nat-server", "file not found");
408       return 0;
409     }
410   }
411
412   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
413   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
414
415   ret = check ();
416
417   GNUNET_free (cfg_file_p1);
418   GNUNET_free (cfg_file_p2);
419
420   GNUNET_free (test_source);
421   GNUNET_free (test_plugin);
422   GNUNET_free (test_name);
423
424   GNUNET_TRANSPORT_TESTING_done (tth);
425
426   return ret;
427 }
428
429 /* end of test_transport_api.c */