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