2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file transport/test_transport_api_restart_2peers.c
22 * @brief base test case for transport implementations
24 * This test case starts 2 peers, connects and exchanges a message
25 * boths peer are restarted and tested if peers reconnect
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"
39 #define START_ARM GNUNET_YES
42 * How long until we give up on transmitting the message?
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
47 * How long until we give up on transmitting the message?
49 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
53 static char *test_name;
57 static GNUNET_SCHEDULER_TaskIdentifier die_task;
59 static GNUNET_SCHEDULER_TaskIdentifier send_task;
61 static GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
63 static struct PeerContext *p1;
65 static struct PeerContext *p2;
67 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
69 static struct GNUNET_TRANSPORT_TransmitHandle *th;
71 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
73 static char *cfg_file_p1;
75 static char *cfg_file_p2;
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
84 if (send_task != GNUNET_SCHEDULER_NO_TASK)
85 GNUNET_SCHEDULER_cancel (send_task);
86 send_task = GNUNET_SCHEDULER_NO_TASK;
88 if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
89 GNUNET_SCHEDULER_cancel (reconnect_task);
90 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
92 if (die_task != GNUNET_SCHEDULER_NO_TASK)
93 GNUNET_SCHEDULER_cancel (die_task);
94 die_task = GNUNET_SCHEDULER_NO_TASK;
97 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
100 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
101 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
106 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
108 die_task = GNUNET_SCHEDULER_NO_TASK;
110 if (restarted == GNUNET_YES)
111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was restarted, but communication did not resume\n");
113 if (restarted == GNUNET_NO)
114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NOT (even) restarted\n");
116 if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
117 GNUNET_SCHEDULER_cancel (reconnect_task);
118 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
120 if (send_task != GNUNET_SCHEDULER_NO_TASK)
121 GNUNET_SCHEDULER_cancel (send_task);
122 send_task = GNUNET_SCHEDULER_NO_TASK;
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
127 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
132 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
136 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
138 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
145 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
147 struct PeerContext *p = cls;
149 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
150 GNUNET_TRANSPORT_try_connect (p1->th, &p2->id);
152 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect, p);
157 restart_cb (struct PeerContext *p, void *cls)
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "Restarted peer %u (`%4s'), issuing reconnect\n", p->no,
166 GNUNET_i2s (&p->id));
167 reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, p);
172 restart (struct PeerContext *p, char *cfg_file)
174 GNUNET_assert (p != NULL);
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Restarting peer %u (`%4s')\n", p->no,
176 GNUNET_i2s (&p->id));
177 GNUNET_TRANSPORT_TESTING_restart_peer (tth, p, cfg_file, &restart_cb, p);
182 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
183 const struct GNUNET_MessageHeader *message,
184 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
186 struct PeerContext *p = cls;
187 struct PeerContext *t = NULL;
189 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
191 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
193 GNUNET_assert (t != NULL);
195 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
199 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
200 GNUNET_i2s (&t->id));
203 if ((MTYPE == ntohs (message->type)) &&
204 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
206 if (restarted == GNUNET_NO)
208 restarted = GNUNET_YES;
209 restart (p1, cfg_file_p1);
210 restart (p2, cfg_file_p2);
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Restarted peers connected, stopping test...\n");
225 if (die_task != GNUNET_SCHEDULER_NO_TASK)
226 GNUNET_SCHEDULER_cancel (die_task);
227 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
233 notify_ready (void *cls, size_t size, void *buf)
235 struct PeerContext *p = cls;
236 struct GNUNET_MessageHeader *hdr;
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242 "Timeout occurred while waiting for transmit_ready\n");
243 if (GNUNET_SCHEDULER_NO_TASK != die_task)
244 GNUNET_SCHEDULER_cancel (die_task);
245 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
250 GNUNET_assert (size >= 256);
255 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
256 hdr->type = htons (MTYPE);
258 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
262 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
263 GNUNET_i2s (&p->id));
265 return sizeof (struct GNUNET_MessageHeader);
270 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
272 send_task = GNUNET_SCHEDULER_NO_TASK;
274 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
276 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
280 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
281 GNUNET_free (receiver_s);
283 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 0,
284 TIMEOUT_TRANSMIT, ¬ify_ready,
290 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
291 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
296 struct PeerContext *p = cls;
297 struct PeerContext *t = NULL;
299 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
301 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
303 GNUNET_assert (t != NULL);
305 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
309 t->no, GNUNET_i2s (peer));
312 if ((restarted == GNUNET_YES) && (c == 4))
314 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
320 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
322 struct PeerContext *p = cls;
325 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
328 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
333 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
335 if (GNUNET_SCHEDULER_NO_TASK != send_task)
336 GNUNET_SCHEDULER_cancel (send_task);
337 send_task = GNUNET_SCHEDULER_NO_TASK;
342 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
345 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
348 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
351 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
356 start_cb (struct PeerContext *p, void *cls)
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
363 GNUNET_i2s (&p->id));
368 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
372 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
373 GNUNET_free (sender_c);
375 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
382 run (void *cls, char *const *args, const char *cfgfile,
383 const struct GNUNET_CONFIGURATION_Handle *cfg)
385 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
387 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
388 ¬ify_receive, ¬ify_connect,
389 ¬ify_disconnect, &start_cb,
392 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
393 ¬ify_receive, ¬ify_connect,
394 ¬ify_disconnect, &start_cb,
397 if ((p1 == NULL) || (p2 == NULL))
399 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
400 if (die_task != GNUNET_SCHEDULER_NO_TASK)
401 GNUNET_SCHEDULER_cancel (die_task);
402 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
411 static char *const argv[] = { "test-transport-api",
413 "test_transport_api_data.conf",
416 static struct GNUNET_GETOPT_CommandLineOption options[] = {
417 GNUNET_GETOPT_OPTION_END
421 setTransportOptions ("test_transport_api_data.conf");
423 send_task = GNUNET_SCHEDULER_NO_TASK;
426 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
427 "nohelp", options, &run, NULL);
434 main (int argc, char *argv[])
438 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
439 GNUNET_log_setup (test_name,
442 tth = GNUNET_TRANSPORT_TESTING_init ();
443 GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
444 GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
446 GNUNET_free (cfg_file_p1);
447 GNUNET_free (cfg_file_p2);
448 GNUNET_free (test_name);
449 GNUNET_TRANSPORT_TESTING_done (tth);
453 /* end of test_transport_api_restart_2peers.c */