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