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