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