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