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