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