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