2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 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 topology/test_gnunet_daemon_topology.c
22 * @brief testcase for topology maintenance code
23 * @author Christian Grothoff
27 #include "gnunet_testbed_service.h"
28 #include "gnunet_statistics_service.h"
34 * The threshold defines the number of connection that are needed
35 * for one peer to pass the test. Be aware that setting NUM_PEERS
36 * too high can cause bandwidth problems for the testing peers.
37 * Normal should be 5KB/s per peer. See gnunet-config -s ats.
38 * schanzen 12/2019: This _only_ makes sense if we connect to the
39 * actual network as in the test we do not connect to more than 1 peer.
40 * => reducing to 1 for now, was NUM_PEERS / 2
45 * How long until we give up on connecting the peers?
47 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
50 * Store manual connections.
52 static unsigned int connect_left;
55 * Result of the testcase.
60 * Peers that reached the threshold of connections.
62 static int checked_peers;
67 struct GNUNET_TESTBED_Operation *op[NUM_PEERS];
70 * Timeout for testcase.
72 static struct GNUNET_SCHEDULER_Task *timeout_tid;
75 * Peer context for every testbed peer.
80 struct GNUNET_STATISTICS_Handle *statistics;
82 int reported; /* GNUNET_NO | GNUNET_YES */
87 shutdown_task (void *cls)
91 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
92 "Shutting down testcase\n");
94 for (i = 0; i < NUM_PEERS; i++)
97 GNUNET_TESTBED_operation_done (op[i]);
100 if (NULL != timeout_tid)
101 GNUNET_SCHEDULER_cancel (timeout_tid);
106 timeout_task (void *cls)
109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
110 "Testcase timeout\n");
112 result = GNUNET_SYSERR;
113 GNUNET_SCHEDULER_shutdown ();
118 * The function is called every time the topology of connected
119 * peers to a peer changes.
122 statistics_iterator (void *cls,
123 const char *subsystem,
128 struct peerctx *p_ctx = (struct peerctx*) cls;
130 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
131 "Peer %d: %s = %llu\n",
134 (unsigned long long) value);
136 if (p_ctx->connections < value)
137 p_ctx->connections = value;
139 if ((THRESHOLD <= value) && (GNUNET_NO == p_ctx->reported))
141 p_ctx->reported = GNUNET_YES;
143 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
144 "Peer %d successfully connected to at least %d peers once.\n",
148 if (checked_peers == NUM_PEERS)
150 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
151 "Test OK: All peers have connected to %d peers once.\n",
154 GNUNET_SCHEDULER_shutdown ();
163 ca_statistics (void *cls,
164 const struct GNUNET_CONFIGURATION_Handle *cfg)
166 return GNUNET_STATISTICS_create ("topology", cfg);
171 da_statistics (void *cls,
174 struct peerctx *p_ctx = (struct peerctx *) cls;
176 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
177 (p_ctx->statistics, "topology", "# peers connected",
178 statistics_iterator, p_ctx));
180 GNUNET_STATISTICS_destroy (p_ctx->statistics, GNUNET_NO);
181 p_ctx->statistics = NULL;
188 service_connect_complete (void *cls,
189 struct GNUNET_TESTBED_Operation *op,
194 struct peerctx *p_ctx = (struct peerctx*) cls;
196 if (NULL == ca_result)
197 GNUNET_SCHEDULER_shutdown ();
199 p_ctx->statistics = ca_result;
201 ret = GNUNET_STATISTICS_watch (ca_result,
207 if (GNUNET_NO == ret)
208 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
209 "call to GNUNET_STATISTICS_watch() failed\n");
214 notify_connect_complete (void *cls,
215 struct GNUNET_TESTBED_Operation *op,
218 GNUNET_TESTBED_operation_done (op);
221 fprintf (stderr, "Failed to connect two peers: %s\n", emsg);
222 result = GNUNET_SYSERR;
223 GNUNET_SCHEDULER_shutdown ();
231 do_connect (void *cls,
232 struct GNUNET_TESTBED_RunHandle *h,
233 unsigned int num_peers,
234 struct GNUNET_TESTBED_Peer **peers,
235 unsigned int links_succeeded,
236 unsigned int links_failed)
239 struct peerctx *p_ctx;
241 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
242 "Threshold is set to %d.\n",
245 GNUNET_assert (NUM_PEERS == num_peers);
247 for (i = 0; i < NUM_PEERS; i++)
249 p_ctx = GNUNET_new (struct peerctx);
251 p_ctx->connections = 0;
252 p_ctx->reported = GNUNET_NO;
254 if (i < NUM_PEERS - 1)
257 GNUNET_TESTBED_overlay_connect (NULL,
258 ¬ify_connect_complete, NULL,
259 peers[i], peers[i + 1]);
263 GNUNET_TESTBED_service_connect (cls,
266 service_connect_complete,
267 p_ctx, /* cls of completion cb */
268 ca_statistics, /* connect adapter */
269 da_statistics, /* disconnect adapter */
273 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
275 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
282 main (int argc, char *argv[])
284 result = GNUNET_SYSERR;
287 (void) GNUNET_TESTBED_test_run ("test-gnunet-daemon-topology",
288 "test_gnunet_daemon_topology_data.conf",
292 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-topology");
294 return (GNUNET_OK != result) ? 1 : 0;
298 /* end of test_gnunet_daemon_topology.c */