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