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