2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file dht/test_dht_twopeer.c
22 * @brief base testcase for testing DHT service with
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_dht_service.h"
31 #define VERBOSE GNUNET_YES
33 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
35 #define DEFAULT_NUM_PEERS 2
41 static char *test_directory;
43 static unsigned int expected_connections;
45 static unsigned long long peers_left;
47 static struct GNUNET_TESTING_PeerGroup *pg;
49 static struct GNUNET_SCHEDULER_Handle *sched;
51 static unsigned long long num_peers;
53 static unsigned int total_connections;
55 static unsigned int failed_connections;
57 GNUNET_SCHEDULER_TaskIdentifier die_task;
62 * Check whether peers successfully shut down.
64 void shutdown_callback (void *cls,
75 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
77 GNUNET_assert (pg != NULL);
78 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
83 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
86 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
91 topology_callback (void *cls,
92 const struct GNUNET_PeerIdentity *first,
93 const struct GNUNET_PeerIdentity *second,
95 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
96 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
97 struct GNUNET_TESTING_Daemon *first_daemon,
98 struct GNUNET_TESTING_Daemon *second_daemon,
105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
106 first_daemon->shortname,
107 second_daemon->shortname,
114 failed_connections++;
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
116 first_daemon->shortname,
117 second_daemon->shortname, emsg);
121 if (total_connections == expected_connections)
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
128 GNUNET_SCHEDULER_cancel (sched, die_task);
129 die_task = GNUNET_SCHEDULER_NO_TASK;
130 //GNUNET_SCHEDULER_add_now (sched, &next_phase, NULL);
131 GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
133 else if (total_connections + failed_connections == expected_connections)
135 GNUNET_SCHEDULER_cancel (sched, die_task);
136 die_task = GNUNET_SCHEDULER_add_now (sched,
137 &end_badly, "from topology_callback (too many failed connections)");
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143 "Have %d total connections, %d failed connections, Want %d\n",
144 total_connections, failed_connections, expected_connections);
150 connect_topology (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
152 expected_connections = -1;
153 if ((pg != NULL) && (peers_left == 0))
155 expected_connections = GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_CLIQUE, GNUNET_TESTING_TOPOLOGY_OPTION_ALL, 0.0);
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Have %d expected connections\n", expected_connections);
162 GNUNET_SCHEDULER_cancel (sched, die_task);
163 if (expected_connections == GNUNET_SYSERR)
165 die_task = GNUNET_SCHEDULER_add_now (sched,
166 &end_badly, "from connect topology (bad return)");
169 die_task = GNUNET_SCHEDULER_add_delayed (sched,
171 &end_badly, "from connect topology (timeout)");
175 peers_started_callback (void *cls,
176 const struct GNUNET_PeerIdentity *id,
177 const struct GNUNET_CONFIGURATION_Handle *cfg,
178 struct GNUNET_TESTING_Daemon *d, const char *emsg)
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start daemon with error: `%s'\n",
186 GNUNET_assert (id != NULL);
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
189 (num_peers - peers_left) + 1, num_peers);
192 //GNUNET_DHT_connect();
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199 "All %d daemons started, now creating topology!\n",
202 GNUNET_SCHEDULER_cancel (sched, die_task);
203 /* Set up task in case topology creation doesn't finish
204 * within a reasonable amount of time */
205 die_task = GNUNET_SCHEDULER_add_delayed (sched,
206 GNUNET_TIME_relative_multiply
207 (GNUNET_TIME_UNIT_MINUTES, 5),
208 &end_badly, "from peers_started_callback");
210 GNUNET_SCHEDULER_add_now(sched, &connect_topology, NULL);
217 struct GNUNET_SCHEDULER_Handle *s,
219 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
224 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
231 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
233 num_peers = DEFAULT_NUM_PEERS;
235 peers_left = num_peers;
237 /* Set up a task to end testing if peer start fails */
238 die_task = GNUNET_SCHEDULER_add_delayed (sched,
240 &end_badly, "didn't start all daemons in reasonable amount of time!!!");
242 pg = GNUNET_TESTING_daemons_start (sched, cfg,
243 num_peers, TIMEOUT, NULL, NULL, &peers_started_callback, NULL,
244 &topology_callback, NULL, NULL);
252 char *const argv[] = {"test-dht-twopeer",
254 "test_dht_twopeer_data.conf",
260 struct GNUNET_GETOPT_CommandLineOption options[] = {
261 GNUNET_GETOPT_OPTION_END
263 ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
264 argv, "test-dht-twopeer", "nohelp",
266 if (ret != GNUNET_OK)
268 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`test-dht-twopeer': Failed with error code %d\n", ret);
274 main (int argc, char *argv[])
278 GNUNET_log_setup ("test-dht-twopeer",
287 * Need to remove base directory, subdirectories taken care
288 * of by the testing framework.
290 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
292 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
297 /* end of test_dht_twopeer.c */