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