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"
39 #define VERBOSE GNUNET_EXTRA_LOGGING
40 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
42 #define START_ARM GNUNET_YES
45 * How long until we give up on transmitting the message?
47 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
50 * How long until we give up on transmitting the message?
52 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
56 static char *test_name;
60 static GNUNET_SCHEDULER_TaskIdentifier die_task;
62 static GNUNET_SCHEDULER_TaskIdentifier send_task;
64 static GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
66 struct PeerContext *p1;
68 struct PeerContext *p2;
70 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
72 struct GNUNET_TRANSPORT_TransmitHandle *th;
74 struct GNUNET_TRANSPORT_TESTING_handle *tth;
83 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
85 #define OKPP do { ok++; } while (0)
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
94 if (send_task != GNUNET_SCHEDULER_NO_TASK)
95 GNUNET_SCHEDULER_cancel (send_task);
96 send_task = GNUNET_SCHEDULER_NO_TASK;
98 if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
99 GNUNET_SCHEDULER_cancel (reconnect_task);
100 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
102 if (die_task != GNUNET_SCHEDULER_NO_TASK)
103 GNUNET_SCHEDULER_cancel (die_task);
104 die_task = GNUNET_SCHEDULER_NO_TASK;
107 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
110 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
111 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
115 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 die_task = GNUNET_SCHEDULER_NO_TASK;
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
121 if (restarted == GNUNET_YES)
122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was restarted\n");
124 if (restarted == GNUNET_NO)
125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NOT restarted\n");
127 if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
128 GNUNET_SCHEDULER_cancel (reconnect_task);
129 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
131 if (send_task != GNUNET_SCHEDULER_NO_TASK)
132 GNUNET_SCHEDULER_cancel (send_task);
133 send_task = GNUNET_SCHEDULER_NO_TASK;
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
138 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
143 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
147 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
149 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
155 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 struct PeerContext *p = cls;
158 reconnect_task = GNUNET_SCHEDULER_NO_TASK;
160 GNUNET_TRANSPORT_try_connect (p->th, &p2->id);
161 reconnect_task = 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",
171 GNUNET_i2s (&p->id));
173 reconnect_task = GNUNET_SCHEDULER_add_now(&reconnect, p);
177 void restart (struct PeerContext *p , char * cfg_file)
179 GNUNET_assert (p != NULL);
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 "Restarting peer %u (`%4s')\n",
183 GNUNET_i2s (&p->id));
184 GNUNET_TRANSPORT_TESTING_restart_peer (tth, p, cfg_file, &restart_cb, p);
189 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
190 const struct GNUNET_MessageHeader *message,
191 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
193 struct PeerContext *p = cls;
194 struct PeerContext *t = NULL;
196 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
198 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
200 GNUNET_assert (t != NULL);
202 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
205 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
206 GNUNET_i2s (&t->id));
209 if ((MTYPE == ntohs (message->type)) &&
210 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
212 if (restarted == GNUNET_NO)
214 restarted = GNUNET_YES;
215 restart (p1, cfg_file_p1);
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "Restarted peers connected, stopping test...\n");
230 if (die_task != GNUNET_SCHEDULER_NO_TASK)
231 GNUNET_SCHEDULER_cancel (die_task);
232 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
238 notify_ready (void *cls, size_t size, void *buf)
240 struct PeerContext *p = cls;
241 struct GNUNET_MessageHeader *hdr;
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
248 "Timeout occurred while waiting for transmit_ready\n");
249 if (GNUNET_SCHEDULER_NO_TASK != die_task)
250 GNUNET_SCHEDULER_cancel (die_task);
251 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
256 GNUNET_assert (size >= 256);
261 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
262 hdr->type = htons (MTYPE);
264 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
268 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
269 GNUNET_i2s (&p->id));
271 return sizeof (struct GNUNET_MessageHeader);
276 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
278 send_task = GNUNET_SCHEDULER_NO_TASK;
280 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
282 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
286 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
287 GNUNET_free (receiver_s);
289 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 0,
290 TIMEOUT_TRANSMIT, ¬ify_ready,
296 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
297 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
302 struct PeerContext *p = cls;
303 struct PeerContext *t = NULL;
305 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
307 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
309 GNUNET_assert (t != NULL);
311 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
315 t->no, GNUNET_i2s (peer));
318 if ((restarted == GNUNET_YES) && (c == 4))
320 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
326 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
328 struct PeerContext *p = cls;
331 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
338 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
343 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
346 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
349 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
352 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
358 start_cb (struct PeerContext *p, void *cls)
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
365 GNUNET_i2s (&p->id));
370 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
373 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
374 GNUNET_free (sender_c);
376 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",
419 static struct GNUNET_GETOPT_CommandLineOption options[] = {
420 GNUNET_GETOPT_OPTION_END
424 setTransportOptions ("test_transport_api_data.conf");
426 send_task = GNUNET_SCHEDULER_NO_TASK;
429 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
430 "nohelp", options, &run, &ok);
436 main (int argc, char *argv[])
440 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
443 GNUNET_log_setup (test_name,
451 tth = GNUNET_TRANSPORT_TESTING_init ();
454 GNUNET_asprintf(&cfg_file_p1,"test_transport_api_tcp_peer1.conf");
455 GNUNET_asprintf(&cfg_file_p2,"test_transport_api_tcp_peer2.conf");
460 GNUNET_free (cfg_file_p1);
461 GNUNET_free (cfg_file_p2);
463 GNUNET_free (test_name);
465 GNUNET_TRANSPORT_TESTING_done (tth);
470 /* end of test_transport_api_restart_1peer.c */