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_transport_service.h"
30 #include "transport-testing.h"
33 * How long until we give up on transmitting the message?
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
38 * How long until we give up on transmitting the message?
40 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
44 static char *test_name;
48 static GNUNET_SCHEDULER_TaskIdentifier die_task;
50 static GNUNET_SCHEDULER_TaskIdentifier send_task;
52 static GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
54 static struct PeerContext *p1;
56 static int p1_connected;
58 static struct PeerContext *p2;
60 static int p2_connected;
62 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
64 static struct GNUNET_TRANSPORT_TransmitHandle *th;
66 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
68 static char *cfg_file_p1;
70 static char *cfg_file_p2;
78 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
80 if (send_task != GNUNET_SCHEDULER_NO_TASK)
81 GNUNET_SCHEDULER_cancel (send_task);
82 send_task = GNUNET_SCHEDULER_NO_TASK;
84 if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
85 GNUNET_SCHEDULER_cancel (reconnect_task);
86 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
88 if (die_task != GNUNET_SCHEDULER_NO_TASK)
89 GNUNET_SCHEDULER_cancel (die_task);
90 die_task = GNUNET_SCHEDULER_NO_TASK;
93 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
96 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
98 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
103 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
105 die_task = GNUNET_SCHEDULER_NO_TASK;
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
109 if (restarted == GNUNET_YES)
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was restarted\n");
112 if (restarted == GNUNET_NO)
113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NOT restarted\n");
115 if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
116 GNUNET_SCHEDULER_cancel (reconnect_task);
117 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
119 if (send_task != GNUNET_SCHEDULER_NO_TASK)
120 GNUNET_SCHEDULER_cancel (send_task);
121 send_task = GNUNET_SCHEDULER_NO_TASK;
125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
126 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
131 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
135 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
137 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
143 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
145 struct PeerContext *p = cls;
147 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
149 GNUNET_TRANSPORT_try_connect (p->th, &p2->id);
151 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect, p);
155 restart_cb (struct PeerContext *p, void *cls)
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Restarted peer %u (`%4s'), issuing reconnect\n", p->no,
160 GNUNET_i2s (&p->id));
162 reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, p);
166 restart (struct PeerContext *p, char *cfg_file)
168 GNUNET_assert (p != NULL);
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Restarting peer %u (`%4s')\n", p->no,
170 GNUNET_i2s (&p->id));
171 GNUNET_TRANSPORT_TESTING_restart_peer (tth, p, cfg_file, &restart_cb, p);
176 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
177 const struct GNUNET_MessageHeader *message,
178 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
180 struct PeerContext *p = cls;
181 struct PeerContext *t = NULL;
183 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
185 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
187 GNUNET_assert (t != NULL);
189 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
193 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
194 GNUNET_i2s (&t->id));
197 if ((MTYPE == ntohs (message->type)) &&
198 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
200 if (restarted == GNUNET_NO)
202 restarted = GNUNET_YES;
203 restart (p1, cfg_file_p1);
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
209 "Restarted peers connected and message was sent, stopping test...\n");
218 if (die_task != GNUNET_SCHEDULER_NO_TASK)
219 GNUNET_SCHEDULER_cancel (die_task);
220 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
226 notify_ready (void *cls, size_t size, void *buf)
228 struct PeerContext *p = cls;
229 struct GNUNET_MessageHeader *hdr;
235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236 "Timeout occurred while waiting for transmit_ready\n");
237 if (GNUNET_SCHEDULER_NO_TASK != die_task)
238 GNUNET_SCHEDULER_cancel (die_task);
239 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
244 GNUNET_assert (size >= 256);
249 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
250 hdr->type = htons (MTYPE);
252 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
256 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
257 GNUNET_i2s (&p->id));
259 return sizeof (struct GNUNET_MessageHeader);
264 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
266 send_task = GNUNET_SCHEDULER_NO_TASK;
268 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
270 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
274 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
275 GNUNET_free (receiver_s);
277 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 0,
278 TIMEOUT_TRANSMIT, ¬ify_ready,
284 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
285 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
290 struct PeerContext *p = cls;
291 struct PeerContext *t = NULL;
293 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
295 p1_connected = GNUNET_YES;
298 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
300 p2_connected = GNUNET_YES;
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) && ((p1_connected == GNUNET_YES) && (p2_connected == GNUNET_YES)))
314 /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */
315 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
321 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
323 struct PeerContext *p = cls;
326 (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity))))
328 p1_connected = GNUNET_NO;
331 (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity))))
333 p2_connected = GNUNET_NO;
335 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
343 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
345 if (GNUNET_SCHEDULER_NO_TASK != send_task)
346 GNUNET_SCHEDULER_cancel (send_task);
347 send_task = GNUNET_SCHEDULER_NO_TASK;
351 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
354 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
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));
360 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
366 start_cb (struct PeerContext *p, void *cls)
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
373 GNUNET_i2s (&p->id));
378 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
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);
385 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
391 run (void *cls, char *const *args, const char *cfgfile,
392 const struct GNUNET_CONFIGURATION_Handle *cfg)
394 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
395 p1_connected = GNUNET_NO;
396 p2_connected = GNUNET_NO;
397 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
398 ¬ify_receive, ¬ify_connect,
399 ¬ify_disconnect, &start_cb,
402 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
403 ¬ify_receive, ¬ify_connect,
404 ¬ify_disconnect, &start_cb,
407 if ((p1 == NULL) || (p2 == NULL))
409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
410 if (die_task != GNUNET_SCHEDULER_NO_TASK)
411 GNUNET_SCHEDULER_cancel (die_task);
412 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
421 static char *const argv[] = { "test-transport-api",
423 "test_transport_api_data.conf",
426 static struct GNUNET_GETOPT_CommandLineOption options[] = {
427 GNUNET_GETOPT_OPTION_END
430 send_task = GNUNET_SCHEDULER_NO_TASK;
433 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
434 "nohelp", options, &run, &ok);
440 main (int argc, char *argv[])
444 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
445 GNUNET_log_setup (test_name,
448 tth = GNUNET_TRANSPORT_TESTING_init ();
449 GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
450 GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
452 GNUNET_free (cfg_file_p1);
453 GNUNET_free (cfg_file_p2);
454 GNUNET_free (test_name);
455 GNUNET_TRANSPORT_TESTING_done (tth);
459 /* end of test_transport_api_restart_1peer.c */