-makefile for new test_stream_local (commented)
[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 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 struct PeerContext *p2;
69
70 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
71
72 struct GNUNET_TRANSPORT_TransmitHandle *th;
73
74 struct GNUNET_TRANSPORT_TESTING_handle *tth;
75
76 char *cfg_file_p1;
77
78 char *cfg_file_p2;
79
80 static int restarted;
81
82 #if VERBOSE
83 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
84 #else
85 #define OKPP do { ok++; } while (0)
86 #endif
87
88
89 static void
90 end ()
91 {
92   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
93
94   if (send_task != GNUNET_SCHEDULER_NO_TASK)
95     GNUNET_SCHEDULER_cancel (send_task);
96   send_task = GNUNET_SCHEDULER_NO_TASK;
97
98   if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
99     GNUNET_SCHEDULER_cancel (reconnect_task);
100   reconnect_task = GNUNET_SCHEDULER_NO_TASK;
101
102   if (die_task != GNUNET_SCHEDULER_NO_TASK)
103     GNUNET_SCHEDULER_cancel (die_task);
104   die_task = GNUNET_SCHEDULER_NO_TASK;
105
106   if (th != NULL)
107     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
108   th = NULL;
109
110   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
111   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
112 }
113
114 static void
115 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117   die_task = GNUNET_SCHEDULER_NO_TASK;
118
119   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
120
121   if (restarted == GNUNET_YES)
122     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was restarted\n");
123
124   if (restarted == GNUNET_NO)
125     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NOT restarted\n");
126
127   if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
128     GNUNET_SCHEDULER_cancel (reconnect_task);
129   reconnect_task = GNUNET_SCHEDULER_NO_TASK;
130
131   if (send_task != GNUNET_SCHEDULER_NO_TASK)
132     GNUNET_SCHEDULER_cancel (send_task);
133   send_task = GNUNET_SCHEDULER_NO_TASK;
134
135   if (cc != NULL)
136   {
137     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
138     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
139     cc = NULL;
140   }
141
142   if (th != NULL)
143     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
144   th = NULL;
145
146   if (p1 != NULL)
147     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
148   if (p2 != NULL)
149     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
150
151   ok = GNUNET_SYSERR;
152 }
153
154 static void
155 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
156 {
157   struct PeerContext *p = cls;
158
159   reconnect_task = GNUNET_SCHEDULER_NO_TASK;
160
161   GNUNET_TRANSPORT_try_connect (p1->th, &p2->id);
162   reconnect_task =
163       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect, p);
164 }
165
166 static void
167 restart_cb (struct PeerContext *p, void *cls)
168 {
169   static int c;
170
171   c++;
172
173   if (c != 2)
174     return;
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       restart (p2, cfg_file_p2);
223       return;
224     }
225     else
226     {
227       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228                   "Restarted peers connected, stopping test...\n");
229       ok = 0;
230       end ();
231     }
232   }
233   else
234   {
235     GNUNET_break (0);
236     ok = 1;
237     if (die_task != GNUNET_SCHEDULER_NO_TASK)
238       GNUNET_SCHEDULER_cancel (die_task);
239     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
240   }
241 }
242
243
244 static size_t
245 notify_ready (void *cls, size_t size, void *buf)
246 {
247   struct PeerContext *p = cls;
248   struct GNUNET_MessageHeader *hdr;
249
250   th = NULL;
251
252   if (buf == NULL)
253   {
254     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
255                 "Timeout occurred while waiting for transmit_ready\n");
256     if (GNUNET_SCHEDULER_NO_TASK != die_task)
257       GNUNET_SCHEDULER_cancel (die_task);
258     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
259     ok = 42;
260     return 0;
261   }
262
263   GNUNET_assert (size >= 256);
264
265   if (buf != NULL)
266   {
267     hdr = buf;
268     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
269     hdr->type = htons (MTYPE);
270   }
271   char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
272
273   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274               "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
275               p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
276               GNUNET_i2s (&p->id));
277   GNUNET_free (ps);
278   return sizeof (struct GNUNET_MessageHeader);
279 }
280
281
282 static void
283 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
284 {
285   send_task = GNUNET_SCHEDULER_NO_TASK;
286
287   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
288     return;
289   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
290
291   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
293               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
294   GNUNET_free (receiver_s);
295
296   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 0,
297                                                TIMEOUT_TRANSMIT, &notify_ready,
298                                                p1);
299 }
300
301
302 static void
303 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
304                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
305 {
306   static int c;
307
308   c++;
309   struct PeerContext *p = cls;
310   struct PeerContext *t = NULL;
311
312   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
313     t = p1;
314   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
315     t = p2;
316   GNUNET_assert (t != NULL);
317
318   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
319
320   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
322               t->no, GNUNET_i2s (peer));
323   GNUNET_free (ps);
324
325   if ((restarted == GNUNET_YES) && (c == 4))
326   {
327     send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
328   }
329 }
330
331
332 static void
333 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
334 {
335   struct PeerContext *p = cls;
336
337
338   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
339
340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
342               GNUNET_i2s (peer));
343   GNUNET_free (ps);
344
345   if (th != NULL)
346     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
347   th = NULL;
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 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
396   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
397                                             &notify_receive, &notify_connect,
398                                             &notify_disconnect, &start_cb,
399                                             NULL);
400
401   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
402                                             &notify_receive, &notify_connect,
403                                             &notify_disconnect, &start_cb,
404                                             NULL);
405
406   if ((p1 == NULL) || (p2 == NULL))
407   {
408     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
409     if (die_task != GNUNET_SCHEDULER_NO_TASK)
410       GNUNET_SCHEDULER_cancel (die_task);
411     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
412     return;
413   }
414 }
415
416
417 static int
418 check ()
419 {
420   static char *const argv[] = { "test-transport-api",
421     "-c",
422     "test_transport_api_data.conf",
423 #if VERBOSE
424     "-L", "DEBUG",
425 #endif
426     NULL
427   };
428   static struct GNUNET_GETOPT_CommandLineOption options[] = {
429     GNUNET_GETOPT_OPTION_END
430   };
431
432 #if WRITECONFIG
433   setTransportOptions ("test_transport_api_data.conf");
434 #endif
435   send_task = GNUNET_SCHEDULER_NO_TASK;
436
437   ok = 1;
438   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
439                       "nohelp", options, &run, &ok);
440
441   return ok;
442 }
443
444 int
445 main (int argc, char *argv[])
446 {
447   int ret;
448
449   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
450
451
452   GNUNET_log_setup (test_name,
453 #if VERBOSE
454                     "DEBUG",
455 #else
456                     "WARNING",
457 #endif
458                     NULL);
459
460   tth = GNUNET_TRANSPORT_TESTING_init ();
461
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
465   ret = check ();
466
467   GNUNET_free (cfg_file_p1);
468   GNUNET_free (cfg_file_p2);
469
470   GNUNET_free (test_name);
471
472   GNUNET_TRANSPORT_TESTING_done (tth);
473
474   return ret;
475 }
476
477 /* end of test_transport_api_restart_2peers.c */