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