small API change: do no longer pass rarely needed GNUNET_SCHEDULER_TaskContext to...
[oweals/gnunet.git] / src / transport / test_transport_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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_transport_service.h"
31 #include "transport-testing.h"
32
33 /**
34  * How long until we give up on transmitting the message?
35  */
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
37
38 /**
39  * How long until we give up on transmitting the message?
40  */
41 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
42
43 #define TEST_MESSAGE_SIZE 2600
44
45 #define TEST_MESSAGE_TYPE 12345
46
47 static char *test_source;
48
49 static char *test_plugin;
50
51 static char *test_name;
52
53 static int ok;
54
55 static int s_started;
56
57 static int s_connected;
58
59 static int s_sending;
60
61 static struct GNUNET_SCHEDULER_Task * die_task;
62
63 static struct GNUNET_SCHEDULER_Task * send_task;
64
65 static struct PeerContext *p1;
66
67 static struct PeerContext *p2;
68
69 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
70
71 static struct GNUNET_TRANSPORT_TransmitHandle *th;
72
73 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
74
75 static char *cfg_file_p1;
76
77 static char *cfg_file_p2;
78
79
80 static void
81 end ()
82 {
83   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping peers\n");
84
85   if (send_task != NULL)
86     GNUNET_SCHEDULER_cancel (send_task);
87
88   if (die_task != NULL)
89     GNUNET_SCHEDULER_cancel (die_task);
90
91   if (th != NULL)
92     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
93   th = NULL;
94
95   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
96   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
97 }
98
99
100 static void
101 end_badly (void *cls)
102 {
103   die_task = NULL;
104
105   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Fail! Stopping peers\n");
106
107
108   if (send_task != NULL)
109     GNUNET_SCHEDULER_cancel (send_task);
110
111   if (cc != NULL)
112   {
113     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
114     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
115     cc = NULL;
116   }
117
118   if (th != NULL)
119     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
120   else
121     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
122
123   if (s_started == GNUNET_NO)
124     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
125   else
126     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
127
128   if (s_connected == GNUNET_NO)
129     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
130   else
131     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
132
133   if (s_sending == GNUNET_NO)
134     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
135   else
136     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
137
138   th = NULL;
139
140   if (p1 != NULL)
141     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
142   else
143     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
144   if (p2 != NULL)
145     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
146   else
147     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
148
149   ok = GNUNET_SYSERR;
150 }
151
152
153 static void
154 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
155                 const struct GNUNET_MessageHeader *message)
156 {
157   struct PeerContext *p = cls;
158   struct PeerContext *t = NULL;
159
160   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
161     t = p1;
162   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
163     t = p2;
164   GNUNET_assert (t != NULL);
165
166   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
167
168   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
169               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
170               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
171               GNUNET_i2s (&t->id));
172   GNUNET_free (ps);
173
174   if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
175       (TEST_MESSAGE_SIZE == ntohs (message->size)))
176   {
177     ok = 0;
178     end ();
179   }
180   else
181   {
182     GNUNET_break (0);
183     ok = 1;
184     end ();
185   }
186 }
187
188
189 static size_t
190 notify_ready (void *cls, size_t size, void *buf)
191 {
192   struct PeerContext *p = cls;
193   struct GNUNET_MessageHeader *hdr;
194
195   th = NULL;
196
197   if (buf == NULL)
198   {
199     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200                 "Timeout occurred while waiting for transmit_ready\n");
201     if (NULL != die_task)
202       GNUNET_SCHEDULER_cancel (die_task);
203     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
204     ok = 42;
205     return 0;
206   }
207
208   GNUNET_assert (size >= TEST_MESSAGE_SIZE);
209   if (buf != NULL)
210   {
211     memset (buf, '\0', TEST_MESSAGE_SIZE);
212     hdr = buf;
213     hdr->size = htons (TEST_MESSAGE_SIZE);
214     hdr->type = htons (TEST_MESSAGE_TYPE);
215   }
216
217   char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
218   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
219               "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
220               p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
221               GNUNET_i2s (&p->id));
222   GNUNET_free (ps);
223
224   return TEST_MESSAGE_SIZE;
225 }
226
227
228 static void
229 sendtask (void *cls)
230 {
231   const struct GNUNET_SCHEDULER_TaskContext *tc;
232
233   send_task = NULL;
234   tc = GNUNET_SCHEDULER_get_task_context ();
235   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
236     return;
237   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
238
239   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
240               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
241               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
242   GNUNET_free (receiver_s);
243   s_sending = GNUNET_YES;
244   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE,
245                                                TIMEOUT_TRANSMIT, &notify_ready,
246                                                p1);
247 }
248
249
250 static void
251 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
252 {
253   static int c;
254
255   c++;
256   struct PeerContext *p = cls;
257   struct PeerContext *t = NULL;
258
259   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
260     t = p1;
261   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
262     t = p2;
263   GNUNET_assert (t != NULL);
264
265   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
266
267   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
268               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
269               t->no, GNUNET_i2s (peer));
270   GNUNET_free (ps);
271 }
272
273
274 static void
275 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
276 {
277   struct PeerContext *p = cls;
278   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
279
280   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
281               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
282               GNUNET_i2s (peer));
283
284   GNUNET_free (ps);
285
286   if (th != NULL)
287     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
288   th = NULL;
289 }
290
291
292 static void
293 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
294 {
295   cc = NULL;
296   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
297
298   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
299               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
300   GNUNET_free (p1_c);
301
302   s_connected = GNUNET_YES;
303   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
304 }
305
306
307 static void
308 start_cb (struct PeerContext *p, void *cls)
309 {
310   static int started;
311
312   started++;
313
314   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer %u (`%s') started\n", p->no,
315               GNUNET_i2s (&p->id));
316
317   if (started != 2)
318     return;
319   else
320     s_started = GNUNET_YES;
321   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
322
323   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
324               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
325               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
326   GNUNET_free (sender_c);
327
328   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
329                                                NULL);
330
331 }
332
333
334 static void
335 run (void *cls, char *const *args, const char *cfgfile,
336      const struct GNUNET_CONFIGURATION_Handle *cfg)
337 {
338   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
339
340   s_started = GNUNET_NO;
341   s_connected = GNUNET_NO;
342   s_sending = GNUNET_NO;
343
344   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
345                                             &notify_receive, &notify_connect,
346                                             &notify_disconnect, &start_cb,
347                                             NULL);
348
349   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
350                                             &notify_receive, &notify_connect,
351                                             &notify_disconnect, &start_cb,
352                                             NULL);
353
354   if ((p1 == NULL) || (p2 == NULL))
355   {
356     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
357     if (die_task != NULL)
358       GNUNET_SCHEDULER_cancel (die_task);
359     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
360     return;
361   }
362 }
363
364
365 static int
366 check ()
367 {
368   static char *const argv[] = { "test-transport-api",
369     "-c",
370     "test_transport_api_data.conf",
371     NULL
372   };
373   static struct GNUNET_GETOPT_CommandLineOption options[] = {
374     GNUNET_GETOPT_OPTION_END
375   };
376
377   send_task = NULL;
378
379   ok = 1;
380   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
381                       "nohelp", options, &run, &ok);
382
383   return ok;
384 }
385
386 int
387 main (int argc, char *argv[])
388 {
389   int ret;
390
391   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
392   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
393   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
394                                                  &test_plugin);
395
396   GNUNET_log_setup (test_name,
397                     "WARNING",
398                     NULL);
399   tth = GNUNET_TRANSPORT_TESTING_init ();
400
401   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
402   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
403
404   ret = check ();
405
406   GNUNET_free (cfg_file_p1);
407   GNUNET_free (cfg_file_p2);
408
409   GNUNET_free (test_source);
410   GNUNET_free (test_plugin);
411   GNUNET_free (test_name);
412
413   GNUNET_TRANSPORT_TESTING_done (tth);
414
415   return ret;
416 }
417
418 /* end of test_transport_api.c */