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 3, 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 gns/test_gns_twopeer.c
22 * @brief base testcase for testing DHT service with
25 * This testcase starts peers using the GNUNET_TESTING_daemons_start
26 * function call. On peer start, connects to the peers DHT service
27 * by calling GNUNET_DHT_connected. Once notified about all peers
28 * being started (by the peers_started_callback function), calls
29 * GNUNET_TESTING_connect_topology, which connects the peers in a
30 * "straight line" topology. On notification that all peers have
31 * been properly connected, calls the do_get function which initiates
32 * a GNUNET_DHT_get from the *second* peer. Once the GNUNET_DHT_get
33 * function starts, runs the do_put function to insert data at the first peer.
34 * If the GET is successful, schedules finish_testing
35 * to stop the test and shut down peers. If GET is unsuccessful
36 * after GET_TIMEOUT seconds, prints an error message and shuts down
40 #include "gnunet_testing_lib.h"
41 #include "gnunet_core_service.h"
42 #include "gnunet_dht_service.h"
43 #include "block_dns.h"
44 #include "gnunet_signatures.h"
47 #define VERBOSE GNUNET_YES
49 /* Timeout for entire testcase */
50 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 40)
52 /* If number of peers not in config file, use this number */
53 #define DEFAULT_NUM_PEERS 2
58 * Directory to store temp data in, defined in config file
60 static char *test_directory;
63 * Variable used to store the number of connections we should wait for.
65 static unsigned int expected_connections;
68 * Variable used to keep track of how many peers aren't yet started.
70 static unsigned long long peers_left;
72 struct GNUNET_TESTING_Daemon *d1;
73 struct GNUNET_TESTING_Daemon *d2;
77 * Total number of peers to run, set based on config file.
79 static unsigned long long num_peers;
82 * Global used to count how many connections we have currently
83 * been notified about (how many times has topology_callback been called
86 static unsigned int total_connections;
89 * Global used to count how many failed connections we have
90 * been notified about (how many times has topology_callback
91 * been called with failure?)
93 static unsigned int failed_connections;
95 /* Task handle to use to schedule test failure */
96 GNUNET_SCHEDULER_TaskIdentifier die_task;
98 GNUNET_SCHEDULER_TaskIdentifier bob_task;
100 /* Global return value (0 for success, anything else for failure) */
103 int bob_online, alice_online;
106 * Check whether peers successfully shut down.
109 shutdown_callback (void *cls, const char *emsg)
119 * Function scheduled to be run on the successful completion of this
120 * testcase. Specifically, called when our get request completes.
123 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
126 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
127 GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
128 GNUNET_YES, GNUNET_NO);
129 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer2!\n");
130 GNUNET_TESTING_daemon_stop (d2, TIMEOUT, &shutdown_callback, NULL,
131 GNUNET_YES, GNUNET_NO);
132 GNUNET_SCHEDULER_cancel(bob_task);
133 GNUNET_SCHEDULER_cancel(die_task);
137 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
138 * down the peers without freeing memory associated with GET request.
141 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
145 GNUNET_YES, GNUNET_NO);
147 GNUNET_TESTING_daemon_stop (d2, TIMEOUT, &shutdown_callback, NULL,
148 GNUNET_YES, GNUNET_NO);
152 * Check if the get_handle is being used, if so stop the request. Either
153 * way, schedule the end_badly_cont function which actually shuts down the
157 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failing test with error: `%s'!\n",
161 GNUNET_SCHEDULER_cancel(bob_task);
162 GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
167 do_lookup(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
171 (GNUNET_TIME_UNIT_SECONDS, 30),
172 &finish_testing, NULL);
176 gns_started(void *cls, const struct GNUNET_PeerIdentity *id,
177 const struct GNUNET_CONFIGURATION_Handle *cfg,
178 struct GNUNET_TESTING_Daemon *d, const char *emsg)
183 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "GNS failed to start alice\n");
185 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "GNS failed to start bob\n");
190 /* start gns for bob */
191 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "GNS started on alice\n");
192 GNUNET_TESTING_daemon_start_service (d2, "gns", TIMEOUT, &gns_started,
197 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "GNS started on bob\n");
199 /* start the lookup tests */
200 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
201 (GNUNET_TIME_UNIT_SECONDS, 1),
206 * This function is called whenever a connection attempt is finished between two of
207 * the started peers (started with GNUNET_TESTING_daemons_start). The total
208 * number of times this function is called should equal the number returned
209 * from the GNUNET_TESTING_connect_topology call.
211 * The emsg variable is NULL on success (peers connected), and non-NULL on
212 * failure (peers failed to connect).
215 notify_connect (void *cls, const struct GNUNET_PeerIdentity *first,
216 const struct GNUNET_PeerIdentity *second, uint32_t distance,
217 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
218 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
219 struct GNUNET_TESTING_Daemon *first_daemon,
220 struct GNUNET_TESTING_Daemon *second_daemon,
227 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
228 "connected peer %s to peer %s, distance %u\n",
229 first_daemon->shortname, second_daemon->shortname, distance);
235 failed_connections++;
236 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
237 "Failed to connect peer %s to peer %s with error :\n%s\n",
238 first_daemon->shortname, second_daemon->shortname, emsg);
242 if (total_connections == expected_connections)
245 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
246 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
249 GNUNET_SCHEDULER_cancel (die_task);
251 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from test lookup");
253 /* start gns for alice */
254 GNUNET_TESTING_daemon_start_service (d1, "gns", TIMEOUT, &gns_started, NULL);
257 else if (total_connections + failed_connections == expected_connections)
259 GNUNET_SCHEDULER_cancel (die_task);
261 GNUNET_SCHEDULER_add_now (&end_badly,
262 "from topology_callback (too many failed connections)");
267 * Set up some data, and call API PUT function
270 alice_idle (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
276 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
277 (GNUNET_TIME_UNIT_SECONDS, 2),
282 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting peers\n");
283 GNUNET_TESTING_daemons_connect (d1, d2, TIMEOUT, 5, 1,
284 ¬ify_connect, NULL);
288 bob_idle (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
290 /* he's lazy FIXME forever */
292 bob_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
293 (GNUNET_TIME_UNIT_SECONDS, 20),
301 * Callback which is called whenever a peer is started (as a result of the
302 * GNUNET_TESTING_daemons_start call.
304 * @param cls closure argument given to GNUNET_TESTING_daemons_start
305 * @param id the GNUNET_PeerIdentity of the started peer
306 * @param cfg the configuration for this specific peer (needed to connect
308 * @param d the handle to the daemon started
309 * @param emsg NULL if peer started, non-NULL on error
312 alice_started (void *cls, const struct GNUNET_PeerIdentity *id,
313 const struct GNUNET_CONFIGURATION_Handle *cfg,
314 struct GNUNET_TESTING_Daemon *d, const char *emsg)
318 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
319 "Failed to start daemon with error: `%s'\n", emsg);
322 GNUNET_assert (id != NULL);
324 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
325 (GNUNET_TIME_UNIT_SECONDS, 2),
330 bob_started (void *cls, const struct GNUNET_PeerIdentity *id,
331 const struct GNUNET_CONFIGURATION_Handle *cfg,
332 struct GNUNET_TESTING_Daemon *d, const char *emsg)
336 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
337 "Failed to start daemon with error: `%s'\n", emsg);
340 GNUNET_assert (id != NULL);
342 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
343 (GNUNET_TIME_UNIT_SECONDS, 2),
348 run (void *cls, char *const *args, const char *cfgfile,
349 const struct GNUNET_CONFIGURATION_Handle *cfg)
352 /* Get path from configuration file */
354 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
361 /* Get number of peers to start from configuration (should be two) */
363 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
365 num_peers = DEFAULT_NUM_PEERS;
367 /* Set peers_left so we know when all peers started */
368 peers_left = num_peers;
370 /* Set up a task to end testing if peer start fails */
372 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
373 "didn't start all daemons in reasonable amount of time!!!");
377 expected_connections = 1;
380 d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
381 NULL, NULL, NULL, &alice_started, NULL);
383 /* Somebody care to explain? */
384 uint16_t port = 6000;
390 * Modify some config options for bob
391 * namely swap keys and disable dns hijacking
393 struct GNUNET_CONFIGURATION_Handle *cfg2 = GNUNET_TESTING_create_cfg(cfg,
397 GNUNET_CONFIGURATION_set_value_string (cfg2, "paths", "servicehome",
398 "/tmp/test-gnunetd-gns-peer-2/");
399 GNUNET_CONFIGURATION_set_value_string (cfg2, "gns", "HIJACK_DNS",
401 GNUNET_CONFIGURATION_set_value_string (cfg2, "gns", "ZONEKEY",
403 GNUNET_CONFIGURATION_set_value_string (cfg2, "gns", "TRUSTED",
404 "alice:/tmp/alicekey");
407 d2 = GNUNET_TESTING_daemon_start(cfg2, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
408 NULL, NULL, NULL, &bob_started, NULL);
418 /* Arguments for GNUNET_PROGRAM_run */
419 char *const argv[] = { "test-gns-twopeer", /* Name to give running binary */
421 "test_gns_twopeer.conf", /* Config file to use */
427 struct GNUNET_GETOPT_CommandLineOption options[] = {
428 GNUNET_GETOPT_OPTION_END
430 /* Run the run function as a new program */
432 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
433 "test-gns-twopeer", "nohelp", options, &run,
435 if (ret != GNUNET_OK)
437 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
438 "`test-gns-twopeer': Failed with error code %d\n", ret);
444 main (int argc, char *argv[])
448 GNUNET_log_setup ("test-gns-twopeer",
457 * Need to remove base directory, subdirectories taken care
458 * of by the testing framework.
463 /* end of test_gns_twopeer.c */