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