2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2015, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file transport/test_transport_api_restart_reconnect.c
22 * @brief base test case for transport implementations
24 * This test case starts 2 peers, connects and exchanges a message.
25 * Then, 1 or 2 peers are restarted and it is tested if peers reconnect.
26 * How many peers are restarted is determined by the name of the binary.
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 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
40 static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
42 static int p1_connected;
44 static int p2_connected;
50 custom_shutdown (void *cls)
54 GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
61 restart_cb (void *cls)
63 static unsigned int c;
64 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
68 (NULL != strstr (ccc->test_name,
71 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
72 "Restarted peer %u (`%s'), issuing reconnect\n",
75 ats_sh = GNUNET_ATS_connectivity_suggest (p->ats,
82 restart (struct GNUNET_TRANSPORT_TESTING_PeerContext *p)
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 "Restarting peer %u (`%s')\n",
88 GNUNET_assert (GNUNET_OK ==
89 GNUNET_TRANSPORT_TESTING_restart_peer (p,
96 notify_receive (void *cls,
97 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
98 const struct GNUNET_PeerIdentity *sender,
99 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
102 char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
104 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
105 "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n",
108 ntohs (message->header.type),
109 ntohs (message->header.size),
110 GNUNET_i2s (sender));
113 if ( (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE == ntohs (message->header.type)) &&
114 (sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) == ntohs (message->header.size)) )
116 if (GNUNET_NO == restarted)
118 restarted = GNUNET_YES;
119 fprintf (stderr, "TN: %s\n", ccc->test_name);
121 if (NULL != strstr (ccc->test_name,
128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129 "Restarted peers connected and message was sent, stopping test...\n");
130 ccc->global_ret = GNUNET_OK;
131 GNUNET_SCHEDULER_shutdown ();
137 ccc->global_ret = GNUNET_SYSERR;
138 GNUNET_SCHEDULER_shutdown ();
144 notify_connect (void *cls,
145 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
146 const struct GNUNET_PeerIdentity *other)
148 static struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
153 GNUNET_TRANSPORT_TESTING_log_connect (cls,
157 p1_connected = GNUNET_YES;
159 p2_connected = GNUNET_YES;
161 if ( (GNUNET_YES == restarted) &&
162 (GNUNET_YES == p1_connected) &&
163 (GNUNET_YES == p2_connected) )
165 /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */
166 GNUNET_SCHEDULER_add_now (&GNUNET_TRANSPORT_TESTING_simple_send,
173 notify_disconnect (void *cls,
174 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
175 const struct GNUNET_PeerIdentity *other)
177 GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
181 p1_connected = GNUNET_NO;
183 p2_connected = GNUNET_NO;
191 struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
194 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
195 .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
196 .connect_continuation_cls = &sc,
197 .config_file = "test_transport_api_data.conf",
198 .rec = ¬ify_receive,
199 .nc = ¬ify_connect,
200 .nd = ¬ify_disconnect,
201 .shutdown_task = &custom_shutdown,
208 GNUNET_TRANSPORT_TESTING_main (2,
209 &GNUNET_TRANSPORT_TESTING_connect_check,
215 /* end of test_transport_api_restart_1peer.c */