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
28 #include "gnunet_transport_service.h"
29 #include "transport-testing.h"
32 * How long until we give up on transmitting the message?
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 900)
37 * How long until we give up on transmitting the message?
39 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
43 static char *test_name;
47 static struct GNUNET_SCHEDULER_Task * die_task;
49 static struct GNUNET_SCHEDULER_Task * send_task;
51 static struct GNUNET_SCHEDULER_Task * reconnect_task;
53 static struct PeerContext *p1;
55 static struct PeerContext *p2;
57 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
59 static struct GNUNET_TRANSPORT_TransmitHandle *th;
61 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
63 static char *cfg_file_p1;
65 static char *cfg_file_p2;
73 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
74 if (send_task != NULL)
75 GNUNET_SCHEDULER_cancel (send_task);
78 if (reconnect_task != NULL)
79 GNUNET_SCHEDULER_cancel (reconnect_task);
80 reconnect_task = NULL;
83 GNUNET_SCHEDULER_cancel (die_task);
87 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
90 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
91 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
96 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
100 if (restarted == GNUNET_YES)
101 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was restarted, but communication did not resume\n");
103 if (restarted == GNUNET_NO)
104 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NOT (even) restarted\n");
106 if (reconnect_task != NULL)
107 GNUNET_SCHEDULER_cancel (reconnect_task);
108 reconnect_task = NULL;
110 if (send_task != NULL)
111 GNUNET_SCHEDULER_cancel (send_task);
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
117 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
122 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
126 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
128 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
135 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
137 struct PeerContext *p = cls;
139 reconnect_task = NULL;
140 GNUNET_TRANSPORT_try_connect (p1->th, &p2->id, NULL, NULL); /*FIXME TRY_CONNECT change */
142 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect, p);
147 restart_cb (struct PeerContext *p, void *cls)
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Restarted peer %u (`%4s'), issuing reconnect\n", p->no,
156 GNUNET_i2s (&p->id));
157 reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, p);
162 restart (struct PeerContext *p, char *cfg_file)
164 GNUNET_assert (p != NULL);
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166 "Restarting peer %u (`%4s')\n",
168 GNUNET_i2s (&p->id));
169 GNUNET_TRANSPORT_TESTING_restart_peer (tth, p, cfg_file, &restart_cb, p);
174 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
175 const struct GNUNET_MessageHeader *message)
177 struct PeerContext *p = cls;
178 struct PeerContext *t = NULL;
180 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
182 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
184 GNUNET_assert (t != NULL);
186 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
190 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
191 GNUNET_i2s (&t->id));
194 if ((MTYPE == ntohs (message->type)) &&
195 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
197 if (restarted == GNUNET_NO)
199 restarted = GNUNET_YES;
200 restart (p1, cfg_file_p1);
201 restart (p2, cfg_file_p2);
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "Restarted peers connected, stopping test...\n");
216 if (die_task != NULL)
217 GNUNET_SCHEDULER_cancel (die_task);
218 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
224 notify_ready (void *cls, size_t size, void *buf)
226 struct PeerContext *p = cls;
227 struct GNUNET_MessageHeader *hdr;
232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233 "Timeout occurred while waiting for transmit_ready\n");
234 if (NULL != die_task)
235 GNUNET_SCHEDULER_cancel (die_task);
236 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
241 GNUNET_assert (size >= 256);
246 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
247 hdr->type = htons (MTYPE);
249 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
253 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
254 GNUNET_i2s (&p->id));
256 return sizeof (struct GNUNET_MessageHeader);
261 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
265 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
267 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
271 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
272 GNUNET_free (receiver_s);
274 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256,
275 TIMEOUT_TRANSMIT, ¬ify_ready,
281 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
286 struct PeerContext *p = cls;
287 struct PeerContext *t = NULL;
289 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
291 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
293 GNUNET_assert (t != NULL);
295 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
299 t->no, GNUNET_i2s (peer));
302 if ((restarted == GNUNET_YES) && (c == 4))
304 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
310 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
312 struct PeerContext *p = cls;
315 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
323 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
325 if (NULL != send_task)
326 GNUNET_SCHEDULER_cancel (send_task);
332 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
335 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
338 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
341 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
346 start_cb (struct PeerContext *p, void *cls)
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
353 GNUNET_i2s (&p->id));
358 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
362 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
363 GNUNET_free (sender_c);
365 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
372 run (void *cls, char *const *args, const char *cfgfile,
373 const struct GNUNET_CONFIGURATION_Handle *cfg)
375 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
377 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
378 ¬ify_receive, ¬ify_connect,
379 ¬ify_disconnect, &start_cb,
382 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
383 ¬ify_receive, ¬ify_connect,
384 ¬ify_disconnect, &start_cb,
387 if ((p1 == NULL) || (p2 == NULL))
389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
390 if (die_task != NULL)
391 GNUNET_SCHEDULER_cancel (die_task);
392 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
401 static char *const argv[] = { "test-transport-api",
403 "test_transport_api_data.conf",
406 static struct GNUNET_GETOPT_CommandLineOption options[] = {
407 GNUNET_GETOPT_OPTION_END
413 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
414 "nohelp", options, &run, NULL);
421 main (int argc, char *argv[])
425 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
426 GNUNET_log_setup (test_name,
429 tth = GNUNET_TRANSPORT_TESTING_init ();
430 GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
431 GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
433 GNUNET_free (cfg_file_p1);
434 GNUNET_free (cfg_file_p2);
435 GNUNET_free (test_name);
436 GNUNET_TRANSPORT_TESTING_done (tth);
440 /* end of test_transport_api_restart_2peers.c */