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