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/>.
19 * @file transport/test_transport_api_restart_reconnect.c
20 * @brief base test case for transport implementations
22 * This test case starts 2 peers, connects and exchanges a message.
23 * Then, 1 or 2 peers are restarted and it is tested if peers reconnect.
24 * How many peers are restarted is determined by the name of the binary.
27 #include "gnunet_transport_service.h"
28 #include "transport-testing.h"
31 * How long until we give up on transmitting the message?
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
36 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
38 static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
40 static int p1_connected;
42 static int p2_connected;
48 custom_shutdown (void *cls)
52 GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
59 restart_cb (void *cls)
61 static unsigned int c;
62 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
66 (NULL != strstr (ccc->test_name,
69 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
70 "Restarted peer %u (`%s'), issuing reconnect\n",
73 ats_sh = GNUNET_ATS_connectivity_suggest (p->ats,
80 restart (struct GNUNET_TRANSPORT_TESTING_PeerContext *p)
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
83 "Restarting peer %u (`%s')\n",
86 GNUNET_assert (GNUNET_OK ==
87 GNUNET_TRANSPORT_TESTING_restart_peer (p,
94 notify_receive (void *cls,
95 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
96 const struct GNUNET_PeerIdentity *sender,
97 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
100 char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
102 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
103 "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n",
106 ntohs (message->header.type),
107 ntohs (message->header.size),
108 GNUNET_i2s (sender));
111 if ( (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE == ntohs (message->header.type)) &&
112 (sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) == ntohs (message->header.size)) )
114 if (GNUNET_NO == restarted)
116 restarted = GNUNET_YES;
117 fprintf (stderr, "TN: %s\n", ccc->test_name);
119 if (NULL != strstr (ccc->test_name,
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "Restarted peers connected and message was sent, stopping test...\n");
128 ccc->global_ret = GNUNET_OK;
129 GNUNET_SCHEDULER_shutdown ();
135 ccc->global_ret = GNUNET_SYSERR;
136 GNUNET_SCHEDULER_shutdown ();
142 notify_connect (void *cls,
143 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
144 const struct GNUNET_PeerIdentity *other)
146 static struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
151 GNUNET_TRANSPORT_TESTING_log_connect (cls,
155 p1_connected = GNUNET_YES;
157 p2_connected = GNUNET_YES;
159 if ( (GNUNET_YES == restarted) &&
160 (GNUNET_YES == p1_connected) &&
161 (GNUNET_YES == p2_connected) )
163 /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */
164 GNUNET_SCHEDULER_add_now (&GNUNET_TRANSPORT_TESTING_simple_send,
171 notify_disconnect (void *cls,
172 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
173 const struct GNUNET_PeerIdentity *other)
175 GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
179 p1_connected = GNUNET_NO;
181 p2_connected = GNUNET_NO;
189 struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
192 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
193 .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
194 .connect_continuation_cls = &sc,
195 .config_file = "test_transport_api_data.conf",
196 .rec = ¬ify_receive,
197 .nc = ¬ify_connect,
198 .nd = ¬ify_disconnect,
199 .shutdown_task = &custom_shutdown,
206 GNUNET_TRANSPORT_TESTING_main (2,
207 &GNUNET_TRANSPORT_TESTING_connect_check,
213 /* end of test_transport_api_restart_1peer.c */