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_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_ATS_ConnectivitySuggestHandle *ats_sh;
53 static struct PeerContext *p1;
55 static struct PeerContext *p2;
57 static struct 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 (NULL != send_task)
76 GNUNET_SCHEDULER_cancel (send_task);
81 GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
86 GNUNET_SCHEDULER_cancel (die_task);
91 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
96 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
101 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
108 end_badly (void *cls)
112 if (restarted == GNUNET_YES)
113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
114 "Peer was restarted, but communication did not resume\n");
116 if (restarted == GNUNET_NO)
117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118 "Peer was NOT (even) restarted\n");
121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
122 _("Fail! Could not connect peers\n"));
123 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
132 restart_cb (struct PeerContext *p,
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141 "Restarted peer %u (`%4s'), issuing reconnect\n",
143 GNUNET_i2s (&p->id));
144 ats_sh = GNUNET_ATS_connectivity_suggest (p->ats,
151 restart (struct PeerContext *p,
152 const char *cfg_file)
154 GNUNET_assert (NULL != p);
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156 "Restarting peer %u (`%4s')\n",
158 GNUNET_i2s (&p->id));
159 GNUNET_TRANSPORT_TESTING_restart_peer (p,
167 notify_receive (void *cls,
168 const struct GNUNET_PeerIdentity *peer,
169 const struct GNUNET_MessageHeader *message)
171 struct PeerContext *p = cls;
172 struct PeerContext *t = NULL;
174 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
176 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
178 GNUNET_assert (t != NULL);
181 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
184 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
187 ntohs (message->type),
188 ntohs (message->size),
190 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,
228 struct PeerContext *p = cls;
229 struct GNUNET_MessageHeader *hdr;
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235 "Timeout occurred while waiting for transmit_ready\n");
236 if (NULL != die_task)
237 GNUNET_SCHEDULER_cancel (die_task);
238 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
243 GNUNET_assert (size >= 256);
245 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
246 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",
258 GNUNET_i2s (&p->id));
262 return sizeof (struct GNUNET_MessageHeader);
269 const struct GNUNET_SCHEDULER_TaskContext *tc;
272 tc = GNUNET_SCHEDULER_get_task_context ();
273 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
277 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
282 GNUNET_i2s (&p2->id),
285 GNUNET_free (receiver_s);
288 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
298 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
301 struct PeerContext *p = cls;
302 struct PeerContext *t = NULL;
305 if (0 == memcmp (peer,
307 sizeof (struct GNUNET_PeerIdentity)))
309 if (0 == memcmp (peer,
311 sizeof (struct GNUNET_PeerIdentity)))
313 GNUNET_assert (t != NULL);
316 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
327 if ((restarted == GNUNET_YES) && (c == 4))
329 send_task = GNUNET_SCHEDULER_add_now (&sendtask,
336 notify_disconnect (void *cls,
337 const struct GNUNET_PeerIdentity *peer)
339 struct PeerContext *p = cls;
342 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
354 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
357 if (NULL != send_task)
359 GNUNET_SCHEDULER_cancel (send_task);
366 testing_connect_cb (struct PeerContext *p1,
367 struct PeerContext *p2,
373 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Peers connected: %u (%s) <-> %u (%s)\n",
380 GNUNET_i2s (&p2->id));
383 send_task = GNUNET_SCHEDULER_add_now (&sendtask,
389 start_cb (struct PeerContext *p, void *cls)
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395 "Peer %u (`%s') started\n",
397 GNUNET_i2s (&p->id));
402 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
409 GNUNET_i2s (&p2->id));
410 GNUNET_free (sender_c);
413 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth,
425 const struct GNUNET_CONFIGURATION_Handle *cfg)
427 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
430 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
439 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
448 if ((p1 == NULL) || (p2 == NULL))
450 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
451 if (die_task != NULL)
452 GNUNET_SCHEDULER_cancel (die_task);
453 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
462 static char *const argv[] = { "test-transport-api",
464 "test_transport_api_data.conf",
467 static struct GNUNET_GETOPT_CommandLineOption options[] = {
468 GNUNET_GETOPT_OPTION_END
474 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
475 "nohelp", options, &run, NULL);
482 main (int argc, char *argv[])
486 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
487 GNUNET_log_setup (test_name,
490 tth = GNUNET_TRANSPORT_TESTING_init ();
491 GNUNET_asprintf (&cfg_file_p1, "test_transport_api_tcp_peer1.conf");
492 GNUNET_asprintf (&cfg_file_p2, "test_transport_api_tcp_peer2.conf");
494 GNUNET_free (cfg_file_p1);
495 GNUNET_free (cfg_file_p2);
496 GNUNET_free (test_name);
497 GNUNET_TRANSPORT_TESTING_done (tth);
501 /* end of test_transport_api_restart_2peers.c */