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