-fixing testcases, cleanup
[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_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 START_ARM GNUNET_YES
40
41 /**
42  * How long until we give up on transmitting the message?
43  */
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
45
46 /**
47  * How long until we give up on transmitting the message?
48  */
49 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
50
51 #define MTYPE 12345
52
53 static char *test_name;
54
55 static int ok;
56
57 static GNUNET_SCHEDULER_TaskIdentifier die_task;
58
59 static GNUNET_SCHEDULER_TaskIdentifier send_task;
60
61 static GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
62
63 static struct PeerContext *p1;
64
65 static struct PeerContext *p2;
66
67 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
68
69 static struct GNUNET_TRANSPORT_TransmitHandle *th;
70
71 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
72
73 static char *cfg_file_p1;
74
75 static char *cfg_file_p2;
76
77 static int restarted;
78
79
80 static void
81 end ()
82 {
83   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
84
85   if (send_task != GNUNET_SCHEDULER_NO_TASK)
86     GNUNET_SCHEDULER_cancel (send_task);
87   send_task = GNUNET_SCHEDULER_NO_TASK;
88
89   if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
90     GNUNET_SCHEDULER_cancel (reconnect_task);
91   reconnect_task = GNUNET_SCHEDULER_NO_TASK;
92
93   if (die_task != GNUNET_SCHEDULER_NO_TASK)
94     GNUNET_SCHEDULER_cancel (die_task);
95   die_task = GNUNET_SCHEDULER_NO_TASK;
96
97   if (th != NULL)
98     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
99   th = NULL;
100
101   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
102   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
103 }
104
105 static void
106 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
107 {
108   die_task = GNUNET_SCHEDULER_NO_TASK;
109
110   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
111
112   if (restarted == GNUNET_YES)
113     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was restarted\n");
114
115   if (restarted == GNUNET_NO)
116     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NOT restarted\n");
117
118   if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
119     GNUNET_SCHEDULER_cancel (reconnect_task);
120   reconnect_task = GNUNET_SCHEDULER_NO_TASK;
121
122   if (send_task != GNUNET_SCHEDULER_NO_TASK)
123     GNUNET_SCHEDULER_cancel (send_task);
124   send_task = GNUNET_SCHEDULER_NO_TASK;
125
126   if (cc != NULL)
127   {
128     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
129     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
130     cc = NULL;
131   }
132
133   if (th != NULL)
134     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
135   th = NULL;
136
137   if (p1 != NULL)
138     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
139   if (p2 != NULL)
140     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
141
142   ok = GNUNET_SYSERR;
143 }
144
145 static void
146 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
147 {
148   struct PeerContext *p = cls;
149
150   reconnect_task = GNUNET_SCHEDULER_NO_TASK;
151
152   GNUNET_TRANSPORT_try_connect (p1->th, &p2->id);
153   reconnect_task =
154       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect, p);
155 }
156
157 static void
158 restart_cb (struct PeerContext *p, void *cls)
159 {
160   static int c;
161
162   c++;
163
164   if (c != 2)
165     return;
166
167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168               "Restarted peer %u (`%4s'), issuing reconnect\n", p->no,
169               GNUNET_i2s (&p->id));
170
171   reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, p);
172 }
173
174 static void
175 restart (struct PeerContext *p, char *cfg_file)
176 {
177   GNUNET_assert (p != NULL);
178   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Restarting peer %u (`%4s')\n", p->no,
179               GNUNET_i2s (&p->id));
180   GNUNET_TRANSPORT_TESTING_restart_peer (tth, p, cfg_file, &restart_cb, p);
181   return;
182 }
183
184 static void
185 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
186                 const struct GNUNET_MessageHeader *message,
187                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
188 {
189   struct PeerContext *p = cls;
190   struct PeerContext *t = NULL;
191
192   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
193     t = p1;
194   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
195     t = p2;
196   GNUNET_assert (t != NULL);
197
198   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
199
200   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
202               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
203               GNUNET_i2s (&t->id));
204   GNUNET_free (ps);
205
206   if ((MTYPE == ntohs (message->type)) &&
207       (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
208   {
209     if (restarted == GNUNET_NO)
210     {
211       restarted = GNUNET_YES;
212       restart (p1, cfg_file_p1);
213       restart (p2, cfg_file_p2);
214       return;
215     }
216     else
217     {
218       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219                   "Restarted peers connected, 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     t = p1;
305   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
306     t = p2;
307   GNUNET_assert (t != NULL);
308
309   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
310
311   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
313               t->no, GNUNET_i2s (peer));
314   GNUNET_free (ps);
315
316   if ((restarted == GNUNET_YES) && (c == 4))
317   {
318     send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
319   }
320 }
321
322
323 static void
324 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
325 {
326   struct PeerContext *p = cls;
327
328
329   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
330
331   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
333               GNUNET_i2s (peer));
334   GNUNET_free (ps);
335
336   if (th != NULL)
337     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
338   th = NULL;
339  if (GNUNET_SCHEDULER_NO_TASK != send_task)
340     GNUNET_SCHEDULER_cancel (send_task);
341   send_task = GNUNET_SCHEDULER_NO_TASK;
342 }
343
344
345 static void
346 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
347 {
348   cc = NULL;
349   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
350
351   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
352               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
353   GNUNET_free (p1_c);
354
355   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
356 }
357
358
359
360 static void
361 start_cb (struct PeerContext *p, void *cls)
362 {
363   static int started;
364
365   started++;
366
367   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
368               GNUNET_i2s (&p->id));
369
370   if (started != 2)
371     return;
372
373   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
374
375   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
377               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
378   GNUNET_free (sender_c);
379
380   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
381                                                NULL);
382
383 }
384
385 static void
386 run (void *cls, char *const *args, const char *cfgfile,
387      const struct GNUNET_CONFIGURATION_Handle *cfg)
388 {
389   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
390
391   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
392                                             &notify_receive, &notify_connect,
393                                             &notify_disconnect, &start_cb,
394                                             NULL);
395
396   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
397                                             &notify_receive, &notify_connect,
398                                             &notify_disconnect, &start_cb,
399                                             NULL);
400
401   if ((p1 == NULL) || (p2 == NULL))
402   {
403     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
404     if (die_task != GNUNET_SCHEDULER_NO_TASK)
405       GNUNET_SCHEDULER_cancel (die_task);
406     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
407     return;
408   }
409 }
410
411
412 static int
413 check ()
414 {
415   static char *const argv[] = { "test-transport-api",
416     "-c",
417     "test_transport_api_data.conf",
418     NULL
419   };
420   static struct GNUNET_GETOPT_CommandLineOption options[] = {
421     GNUNET_GETOPT_OPTION_END
422   };
423
424 #if WRITECONFIG
425   setTransportOptions ("test_transport_api_data.conf");
426 #endif
427   send_task = GNUNET_SCHEDULER_NO_TASK;
428
429   ok = 1;
430   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
431                       "nohelp", options, &run, &ok);
432
433   return ok;
434 }
435
436 int
437 main (int argc, char *argv[])
438 {
439   int ret;
440
441   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
442   GNUNET_log_setup (test_name,
443                     "WARNING",
444                     NULL);
445   tth = GNUNET_TRANSPORT_TESTING_init ();
446   GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
447   GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
448   ret = check ();
449   GNUNET_free (cfg_file_p1);
450   GNUNET_free (cfg_file_p2);
451   GNUNET_free (test_name);
452   GNUNET_TRANSPORT_TESTING_done (tth);
453   return ret;
454 }
455
456 /* end of test_transport_api_restart_2peers.c */