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