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.
39 #define THRESHOLD NUM_PEERS/2
42 * How long until we give up on connecting the peers?
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
47 * Store manual connections.
49 static unsigned int connect_left;
52 * Result of the testcase.
57 * Peers that reached the threshold of connections.
59 static int checked_peers;
64 struct GNUNET_TESTBED_Operation *op[NUM_PEERS];
67 * Timeout for testcase.
69 static struct GNUNET_SCHEDULER_Task *timeout_tid;
72 * Peer context for every testbed peer.
77 struct GNUNET_STATISTICS_Handle *statistics;
79 int reported; /* GNUNET_NO | GNUNET_YES */
84 shutdown_task (void *cls)
88 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
89 "Shutting down testcase\n");
91 for (i=0;i<NUM_PEERS;i++) {
93 GNUNET_TESTBED_operation_done (op[i]);
96 if (NULL != timeout_tid)
97 GNUNET_SCHEDULER_cancel (timeout_tid);
101 timeout_task (void *cls)
104 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
105 "Testcase timeout\n");
107 result = GNUNET_SYSERR;
108 GNUNET_SCHEDULER_shutdown();
112 * The function is called every time the topology of connected
113 * peers to a peer changes.
116 statistics_iterator (void *cls,
117 const char *subsystem,
122 struct peerctx *p_ctx = (struct peerctx*) cls;
124 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
125 "Peer %d: %s = %llu\n",
128 (unsigned long long) value);
130 if (p_ctx->connections < value)
131 p_ctx->connections = value;
133 if (THRESHOLD <= value && GNUNET_NO == p_ctx->reported) {
134 p_ctx->reported = GNUNET_YES;
136 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
137 "Peer %d successfully connected to at least %d peers once.\n",
141 if (checked_peers == NUM_PEERS) {
142 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
143 "Test OK: All peers have connected to %d peers once.\n",
146 GNUNET_SCHEDULER_shutdown();
154 ca_statistics (void *cls,
155 const struct GNUNET_CONFIGURATION_Handle *cfg)
157 return GNUNET_STATISTICS_create ("topology", cfg);
162 da_statistics (void *cls,
165 struct peerctx *p_ctx = (struct peerctx *) cls;
167 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
168 (p_ctx->statistics, "topology", "# peers connected",
169 statistics_iterator, p_ctx));
171 GNUNET_STATISTICS_destroy (p_ctx->statistics, GNUNET_NO);
172 p_ctx->statistics = NULL;
179 service_connect_complete (void *cls,
180 struct GNUNET_TESTBED_Operation *op,
185 struct peerctx *p_ctx = (struct peerctx*) cls;
187 if (NULL == ca_result)
188 GNUNET_SCHEDULER_shutdown();
190 p_ctx->statistics = ca_result;
192 ret = GNUNET_STATISTICS_watch (ca_result,
198 if (GNUNET_NO == ret)
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
200 "call to GNUNET_STATISTICS_watch() failed\n");
204 notify_connect_complete (void *cls,
205 struct GNUNET_TESTBED_Operation *op,
208 GNUNET_TESTBED_operation_done (op);
211 FPRINTF (stderr, "Failed to connect two peers: %s\n", emsg);
212 result = GNUNET_SYSERR;
213 GNUNET_SCHEDULER_shutdown ();
220 do_connect (void *cls,
221 struct GNUNET_TESTBED_RunHandle *h,
222 unsigned int num_peers,
223 struct GNUNET_TESTBED_Peer **peers,
224 unsigned int links_succeeded,
225 unsigned int links_failed)
228 struct peerctx *p_ctx;
230 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
231 "Threshold is set to %d.\n",
234 GNUNET_assert (NUM_PEERS == num_peers);
236 for (i=0;i<NUM_PEERS;i++)
238 p_ctx = GNUNET_new (struct peerctx);
240 p_ctx->connections = 0;
241 p_ctx->reported = GNUNET_NO;
245 GNUNET_TESTBED_overlay_connect (NULL,
246 ¬ify_connect_complete, NULL,
247 peers[i], peers[i+1]);
251 GNUNET_TESTBED_service_connect (cls,
254 service_connect_complete,
255 p_ctx, /* cls of completion cb */
256 ca_statistics, /* connect adapter */
257 da_statistics, /* disconnect adapter */
262 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
264 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
271 main (int argc, char *argv[])
273 result = GNUNET_SYSERR;
276 (void) GNUNET_TESTBED_test_run ("test-gnunet-daemon-topology",
277 "test_gnunet_daemon_topology_data.conf",
281 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-topology");
283 return (GNUNET_OK != result) ? 1 : 0;
286 /* end of test_gnunet_daemon_topology.c */