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