error msg
[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                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
176 {
177   struct PeerContext *p = cls;
178   struct PeerContext *t = NULL;
179
180   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
181     t = p1;
182   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
183     t = p2;
184   GNUNET_assert (t != NULL);
185
186   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
187
188   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
190               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
191               GNUNET_i2s (&t->id));
192   GNUNET_free (ps);
193
194   if ((MTYPE == ntohs (message->type)) &&
195       (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
196   {
197     if (restarted == GNUNET_NO)
198     {
199       restarted = GNUNET_YES;
200       restart (p1, cfg_file_p1);
201       restart (p2, cfg_file_p2);
202       return;
203     }
204     else
205     {
206       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207                   "Restarted peers connected, stopping test...\n");
208       ok = 0;
209       end ();
210     }
211   }
212   else
213   {
214     GNUNET_break (0);
215     ok = 1;
216     if (die_task != GNUNET_SCHEDULER_NO_TASK)
217       GNUNET_SCHEDULER_cancel (die_task);
218     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
219   }
220 }
221
222
223 static size_t
224 notify_ready (void *cls, size_t size, void *buf)
225 {
226   struct PeerContext *p = cls;
227   struct GNUNET_MessageHeader *hdr;
228
229   th = NULL;
230   if (buf == NULL)
231   {
232     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233                 "Timeout occurred while waiting for transmit_ready\n");
234     if (GNUNET_SCHEDULER_NO_TASK != die_task)
235       GNUNET_SCHEDULER_cancel (die_task);
236     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
237     ok = 42;
238     return 0;
239   }
240
241   GNUNET_assert (size >= 256);
242
243   if (buf != NULL)
244   {
245     hdr = buf;
246     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
247     hdr->type = htons (MTYPE);
248   }
249   char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
250
251   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252               "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
253               p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
254               GNUNET_i2s (&p->id));
255   GNUNET_free (ps);
256   return sizeof (struct GNUNET_MessageHeader);
257 }
258
259
260 static void
261 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
262 {
263   send_task = GNUNET_SCHEDULER_NO_TASK;
264
265   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
266     return;
267   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
268
269   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
271               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
272   GNUNET_free (receiver_s);
273
274   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 0,
275                                                TIMEOUT_TRANSMIT, &notify_ready,
276                                                p1);
277 }
278
279
280 static void
281 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
282                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
283 {
284   static int c;
285
286   c++;
287   struct PeerContext *p = cls;
288   struct PeerContext *t = NULL;
289
290   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
291     t = p1;
292   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
293     t = p2;
294   GNUNET_assert (t != NULL);
295
296   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
297
298   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
300               t->no, GNUNET_i2s (peer));
301   GNUNET_free (ps);
302
303   if ((restarted == GNUNET_YES) && (c == 4))
304   {
305     send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
306   }
307 }
308
309
310 static void
311 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
312 {
313   struct PeerContext *p = cls;
314
315
316   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
317
318   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
320               GNUNET_i2s (peer));
321   GNUNET_free (ps);
322
323   if (th != NULL)
324     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
325   th = NULL;
326  if (GNUNET_SCHEDULER_NO_TASK != send_task)
327     GNUNET_SCHEDULER_cancel (send_task);
328   send_task = GNUNET_SCHEDULER_NO_TASK;
329 }
330
331
332 static void
333 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
334 {
335   cc = NULL;
336   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
337
338   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
339               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
340   GNUNET_free (p1_c);
341
342   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
343 }
344
345
346 static void
347 start_cb (struct PeerContext *p, void *cls)
348 {
349   static int started;
350
351   started++;
352
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
354               GNUNET_i2s (&p->id));
355
356   if (started != 2)
357     return;
358
359   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
360
361   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
363               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
364   GNUNET_free (sender_c);
365
366   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
367                                                NULL);
368
369 }
370
371
372 static void
373 run (void *cls, char *const *args, const char *cfgfile,
374      const struct GNUNET_CONFIGURATION_Handle *cfg)
375 {
376   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
377
378   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
379                                             &notify_receive, &notify_connect,
380                                             &notify_disconnect, &start_cb,
381                                             NULL);
382
383   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
384                                             &notify_receive, &notify_connect,
385                                             &notify_disconnect, &start_cb,
386                                             NULL);
387
388   if ((p1 == NULL) || (p2 == NULL))
389   {
390     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
391     if (die_task != GNUNET_SCHEDULER_NO_TASK)
392       GNUNET_SCHEDULER_cancel (die_task);
393     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
394     return;
395   }
396 }
397
398
399 static int
400 check ()
401 {
402   static char *const argv[] = { "test-transport-api",
403     "-c",
404     "test_transport_api_data.conf",
405     NULL
406   };
407   static struct GNUNET_GETOPT_CommandLineOption options[] = {
408     GNUNET_GETOPT_OPTION_END
409   };
410
411   send_task = GNUNET_SCHEDULER_NO_TASK;
412
413   ok = 1;
414   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
415                       "nohelp", options, &run, NULL);
416
417   return ok;
418 }
419
420
421 int
422 main (int argc, char *argv[])
423 {
424   int ret;
425
426   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
427   GNUNET_log_setup (test_name,
428                     "WARNING",
429                     NULL);
430   tth = GNUNET_TRANSPORT_TESTING_init ();
431   GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
432   GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
433   ret = check ();
434   GNUNET_free (cfg_file_p1);
435   GNUNET_free (cfg_file_p2);
436   GNUNET_free (test_name);
437   GNUNET_TRANSPORT_TESTING_done (tth);
438   return ret;
439 }
440
441 /* end of test_transport_api_restart_2peers.c */