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_1peer.c
22 * @brief base test case for transport implementations
24 * This test case starts 2 peers, connects and exchanges a message
25 * 1 peer is 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"
40 #define START_ARM GNUNET_YES
43 * How long until we give up on transmitting the message?
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
48 * How long until we give up on transmitting the message?
50 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
54 static char *test_name;
58 static GNUNET_SCHEDULER_TaskIdentifier die_task;
60 static GNUNET_SCHEDULER_TaskIdentifier send_task;
62 static GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
64 static struct PeerContext *p1;
66 static int p1_connected;
68 static struct PeerContext *p2;
70 static int p2_connected;
72 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
74 static struct GNUNET_TRANSPORT_TransmitHandle *th;
76 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
78 static char *cfg_file_p1;
80 static char *cfg_file_p2;
88 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
90 if (send_task != GNUNET_SCHEDULER_NO_TASK)
91 GNUNET_SCHEDULER_cancel (send_task);
92 send_task = GNUNET_SCHEDULER_NO_TASK;
94 if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
95 GNUNET_SCHEDULER_cancel (reconnect_task);
96 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
98 if (die_task != GNUNET_SCHEDULER_NO_TASK)
99 GNUNET_SCHEDULER_cancel (die_task);
100 die_task = GNUNET_SCHEDULER_NO_TASK;
103 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
106 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
108 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
113 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 die_task = GNUNET_SCHEDULER_NO_TASK;
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
119 if (restarted == GNUNET_YES)
120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was restarted\n");
122 if (restarted == GNUNET_NO)
123 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NOT restarted\n");
125 if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
126 GNUNET_SCHEDULER_cancel (reconnect_task);
127 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
129 if (send_task != GNUNET_SCHEDULER_NO_TASK)
130 GNUNET_SCHEDULER_cancel (send_task);
131 send_task = GNUNET_SCHEDULER_NO_TASK;
135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
136 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
141 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
145 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
147 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
153 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 struct PeerContext *p = cls;
157 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
159 GNUNET_TRANSPORT_try_connect (p->th, &p2->id);
161 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect, p);
165 restart_cb (struct PeerContext *p, void *cls)
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 "Restarted peer %u (`%4s'), issuing reconnect\n", p->no,
170 GNUNET_i2s (&p->id));
172 reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, p);
176 restart (struct PeerContext *p, char *cfg_file)
178 GNUNET_assert (p != NULL);
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Restarting peer %u (`%4s')\n", p->no,
180 GNUNET_i2s (&p->id));
181 GNUNET_TRANSPORT_TESTING_restart_peer (tth, p, cfg_file, &restart_cb, p);
186 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
187 const struct GNUNET_MessageHeader *message,
188 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
190 struct PeerContext *p = cls;
191 struct PeerContext *t = NULL;
193 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
195 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
197 GNUNET_assert (t != NULL);
199 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
203 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
204 GNUNET_i2s (&t->id));
207 if ((MTYPE == ntohs (message->type)) &&
208 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
210 if (restarted == GNUNET_NO)
212 restarted = GNUNET_YES;
213 restart (p1, cfg_file_p1);
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "Restarted peers connected and message was sent, stopping test...\n");
228 if (die_task != GNUNET_SCHEDULER_NO_TASK)
229 GNUNET_SCHEDULER_cancel (die_task);
230 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
236 notify_ready (void *cls, size_t size, void *buf)
238 struct PeerContext *p = cls;
239 struct GNUNET_MessageHeader *hdr;
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);
254 GNUNET_assert (size >= 256);
259 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
260 hdr->type = htons (MTYPE);
262 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
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));
269 return sizeof (struct GNUNET_MessageHeader);
274 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
276 send_task = GNUNET_SCHEDULER_NO_TASK;
278 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
280 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
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);
287 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 0,
288 TIMEOUT_TRANSMIT, ¬ify_ready,
294 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
295 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
300 struct PeerContext *p = cls;
301 struct PeerContext *t = NULL;
303 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
305 p1_connected = GNUNET_YES;
308 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
310 p2_connected = GNUNET_YES;
313 GNUNET_assert (t != NULL);
315 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
319 t->no, GNUNET_i2s (peer));
322 if ((restarted == GNUNET_YES) && ((p1_connected == GNUNET_YES) && (p2_connected == GNUNET_YES)))
324 /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */
325 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
331 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
333 struct PeerContext *p = cls;
336 (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity))))
338 p1_connected = GNUNET_NO;
341 (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity))))
343 p2_connected = GNUNET_NO;
345 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
353 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
355 if (GNUNET_SCHEDULER_NO_TASK != send_task)
356 GNUNET_SCHEDULER_cancel (send_task);
357 send_task = GNUNET_SCHEDULER_NO_TASK;
361 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
364 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
367 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
370 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
376 start_cb (struct PeerContext *p, void *cls)
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
383 GNUNET_i2s (&p->id));
388 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
392 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
393 GNUNET_free (sender_c);
395 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
401 run (void *cls, char *const *args, const char *cfgfile,
402 const struct GNUNET_CONFIGURATION_Handle *cfg)
404 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
405 p1_connected = GNUNET_NO;
406 p2_connected = GNUNET_NO;
407 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
408 ¬ify_receive, ¬ify_connect,
409 ¬ify_disconnect, &start_cb,
412 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
413 ¬ify_receive, ¬ify_connect,
414 ¬ify_disconnect, &start_cb,
417 if ((p1 == NULL) || (p2 == NULL))
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
420 if (die_task != GNUNET_SCHEDULER_NO_TASK)
421 GNUNET_SCHEDULER_cancel (die_task);
422 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
431 static char *const argv[] = { "test-transport-api",
433 "test_transport_api_data.conf",
436 static struct GNUNET_GETOPT_CommandLineOption options[] = {
437 GNUNET_GETOPT_OPTION_END
441 setTransportOptions ("test_transport_api_data.conf");
443 send_task = GNUNET_SCHEDULER_NO_TASK;
446 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
447 "nohelp", options, &run, &ok);
453 main (int argc, char *argv[])
457 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
458 GNUNET_log_setup (test_name,
461 tth = GNUNET_TRANSPORT_TESTING_init ();
462 GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
463 GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
465 GNUNET_free (cfg_file_p1);
466 GNUNET_free (cfg_file_p2);
467 GNUNET_free (test_name);
468 GNUNET_TRANSPORT_TESTING_done (tth);
472 /* end of test_transport_api_restart_1peer.c */