2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 struct GNUNET_SCHEDULER_Task *die_task;
50 static struct GNUNET_SCHEDULER_Task *send_task;
52 static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
54 static struct PeerContext *p1;
56 static int p1_connected;
58 static struct PeerContext *p2;
60 static int p2_connected;
62 static struct 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,
81 if (NULL != send_task)
83 GNUNET_SCHEDULER_cancel (send_task);
88 GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
93 GNUNET_SCHEDULER_cancel (die_task);
98 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
103 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
108 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
115 end_badly (void *cls,
116 const struct GNUNET_SCHEDULER_TaskContext *tc)
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
121 "Fail! Stopping peers\n");
123 if (restarted == GNUNET_YES)
124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
125 "Peer was restarted\n");
127 if (restarted == GNUNET_NO)
128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
129 "Peer was NOT restarted\n");
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133 _("Fail! Could not connect peers\n"));
134 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
143 restart_cb (struct PeerContext *p, void *cls)
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "Restarted peer %u (`%4s'), issuing reconnect\n",
148 GNUNET_i2s (&p->id));
149 ats_sh = GNUNET_ATS_connectivity_suggest (p->ats,
156 restart (struct PeerContext *p,
157 const char *cfg_file)
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "Restarting peer %u (`%4s')\n",
162 GNUNET_i2s (&p->id));
163 GNUNET_TRANSPORT_TESTING_restart_peer (p,
171 notify_receive (void *cls,
172 const struct GNUNET_PeerIdentity *peer,
173 const struct GNUNET_MessageHeader *message)
175 struct PeerContext *p = cls;
176 struct PeerContext *t = NULL;
178 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
180 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
182 GNUNET_assert (t != NULL);
184 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
188 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
189 GNUNET_i2s (&t->id));
192 if ((MTYPE == ntohs (message->type)) &&
193 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
195 if (restarted == GNUNET_NO)
197 restarted = GNUNET_YES;
198 restart (p1, cfg_file_p1);
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "Restarted peers connected and message was sent, stopping test...\n");
213 if (die_task != NULL)
214 GNUNET_SCHEDULER_cancel (die_task);
215 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
221 notify_ready (void *cls, size_t size, void *buf)
223 struct PeerContext *p = cls;
224 struct GNUNET_MessageHeader *hdr;
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 "Timeout occurred while waiting for transmit_ready\n");
232 if (NULL != die_task)
233 GNUNET_SCHEDULER_cancel (die_task);
234 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
239 GNUNET_assert (size >= 256);
241 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
242 hdr->type = htons (MTYPE);
245 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
254 GNUNET_i2s (&p->id));
257 return sizeof (struct GNUNET_MessageHeader);
262 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
266 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
268 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
272 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
273 GNUNET_free (receiver_s);
275 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
285 notify_connect (void *cls,
286 const struct GNUNET_PeerIdentity *peer)
291 struct PeerContext *p = cls;
292 struct PeerContext *t = NULL;
294 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
296 p1_connected = GNUNET_YES;
299 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
301 p2_connected = GNUNET_YES;
304 GNUNET_assert (t != NULL);
306 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
310 t->no, GNUNET_i2s (peer));
313 if ((restarted == GNUNET_YES) && ((p1_connected == GNUNET_YES) && (p2_connected == GNUNET_YES)))
315 /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */
316 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
322 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
324 struct PeerContext *p = cls;
327 (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity))))
329 p1_connected = GNUNET_NO;
332 (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity))))
334 p2_connected = GNUNET_NO;
336 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
344 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
346 if (NULL != send_task)
347 GNUNET_SCHEDULER_cancel (send_task);
352 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
355 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
358 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
361 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
367 start_cb (struct PeerContext *p, void *cls)
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
374 GNUNET_i2s (&p->id));
379 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
383 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
384 GNUNET_free (sender_c);
386 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
392 run (void *cls, char *const *args, const char *cfgfile,
393 const struct GNUNET_CONFIGURATION_Handle *cfg)
395 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
396 p1_connected = GNUNET_NO;
397 p2_connected = GNUNET_NO;
398 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
399 ¬ify_receive, ¬ify_connect,
400 ¬ify_disconnect, &start_cb,
403 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
404 ¬ify_receive, ¬ify_connect,
405 ¬ify_disconnect, &start_cb,
408 if ((p1 == NULL) || (p2 == NULL))
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
411 if (die_task != NULL)
412 GNUNET_SCHEDULER_cancel (die_task);
413 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
422 static char *const argv[] = { "test-transport-api",
424 "test_transport_api_data.conf",
427 static struct GNUNET_GETOPT_CommandLineOption options[] = {
428 GNUNET_GETOPT_OPTION_END
434 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
435 "nohelp", options, &run, &ok);
441 main (int argc, char *argv[])
445 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
446 GNUNET_log_setup (test_name,
449 tth = GNUNET_TRANSPORT_TESTING_init ();
450 GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
451 GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
453 GNUNET_free (cfg_file_p1);
454 GNUNET_free (cfg_file_p2);
455 GNUNET_free (test_name);
456 GNUNET_TRANSPORT_TESTING_done (tth);
460 /* end of test_transport_api_restart_1peer.c */