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/>.
19 * @file topology/test_gnunet_daemon_topology.c
20 * @brief testcase for topology maintenance code
21 * @author Christian Grothoff
25 #include "gnunet_testbed_service.h"
26 #include "gnunet_statistics_service.h"
32 * The threshold defines the number of connection that are needed
33 * for one peer to pass the test. Be aware that setting NUM_PEERS
34 * too high can cause bandwidth problems for the testing peers.
35 * Normal should be 5KB/s per peer. See gnunet-config -s ats.
37 #define THRESHOLD NUM_PEERS/2
40 * How long until we give up on connecting the peers?
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
45 * Store manual connections.
47 static unsigned int connect_left;
50 * Result of the testcase.
55 * Peers that reached the threshold of connections.
57 static int checked_peers;
62 struct GNUNET_TESTBED_Operation *op[NUM_PEERS];
65 * Timeout for testcase.
67 static struct GNUNET_SCHEDULER_Task *timeout_tid;
70 * Peer context for every testbed peer.
75 struct GNUNET_STATISTICS_Handle *statistics;
77 int reported; /* GNUNET_NO | GNUNET_YES */
82 shutdown_task (void *cls)
86 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
87 "Shutting down testcase\n");
89 for (i=0;i<NUM_PEERS;i++) {
91 GNUNET_TESTBED_operation_done (op[i]);
94 if (NULL != timeout_tid)
95 GNUNET_SCHEDULER_cancel (timeout_tid);
99 timeout_task (void *cls)
102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
103 "Testcase timeout\n");
105 result = GNUNET_SYSERR;
106 GNUNET_SCHEDULER_shutdown();
110 * The function is called every time the topology of connected
111 * peers to a peer changes.
114 statistics_iterator (void *cls,
115 const char *subsystem,
120 struct peerctx *p_ctx = (struct peerctx*) cls;
122 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
123 "Peer %d: %s = %llu\n",
126 (unsigned long long) value);
128 if (p_ctx->connections < value)
129 p_ctx->connections = value;
131 if (THRESHOLD <= value && GNUNET_NO == p_ctx->reported) {
132 p_ctx->reported = GNUNET_YES;
134 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
135 "Peer %d successfully connected to at least %d peers once.\n",
139 if (checked_peers == NUM_PEERS) {
140 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
141 "Test OK: All peers have connected to %d peers once.\n",
144 GNUNET_SCHEDULER_shutdown();
152 ca_statistics (void *cls,
153 const struct GNUNET_CONFIGURATION_Handle *cfg)
155 return GNUNET_STATISTICS_create ("topology", cfg);
160 da_statistics (void *cls,
163 struct peerctx *p_ctx = (struct peerctx *) cls;
165 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
166 (p_ctx->statistics, "topology", "# peers connected",
167 statistics_iterator, p_ctx));
169 GNUNET_STATISTICS_destroy (p_ctx->statistics, GNUNET_NO);
170 p_ctx->statistics = NULL;
177 service_connect_complete (void *cls,
178 struct GNUNET_TESTBED_Operation *op,
183 struct peerctx *p_ctx = (struct peerctx*) cls;
185 if (NULL == ca_result)
186 GNUNET_SCHEDULER_shutdown();
188 p_ctx->statistics = ca_result;
190 ret = GNUNET_STATISTICS_watch (ca_result,
196 if (GNUNET_NO == ret)
197 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
198 "call to GNUNET_STATISTICS_watch() failed\n");
202 notify_connect_complete (void *cls,
203 struct GNUNET_TESTBED_Operation *op,
206 GNUNET_TESTBED_operation_done (op);
209 FPRINTF (stderr, "Failed to connect two peers: %s\n", emsg);
210 result = GNUNET_SYSERR;
211 GNUNET_SCHEDULER_shutdown ();
218 do_connect (void *cls,
219 struct GNUNET_TESTBED_RunHandle *h,
220 unsigned int num_peers,
221 struct GNUNET_TESTBED_Peer **peers,
222 unsigned int links_succeeded,
223 unsigned int links_failed)
226 struct peerctx *p_ctx;
228 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
229 "Threshold is set to %d.\n",
232 GNUNET_assert (NUM_PEERS == num_peers);
234 for (i=0;i<NUM_PEERS;i++)
236 p_ctx = GNUNET_new (struct peerctx);
238 p_ctx->connections = 0;
239 p_ctx->reported = GNUNET_NO;
243 GNUNET_TESTBED_overlay_connect (NULL,
244 ¬ify_connect_complete, NULL,
245 peers[i], peers[i+1]);
249 GNUNET_TESTBED_service_connect (cls,
252 service_connect_complete,
253 p_ctx, /* cls of completion cb */
254 ca_statistics, /* connect adapter */
255 da_statistics, /* disconnect adapter */
260 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
262 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
269 main (int argc, char *argv[])
271 result = GNUNET_SYSERR;
274 (void) GNUNET_TESTBED_test_run ("test-gnunet-daemon-topology",
275 "test_gnunet_daemon_topology_data.conf",
279 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-topology");
281 return (GNUNET_OK != result) ? 1 : 0;
284 /* end of test_gnunet_daemon_topology.c */