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