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 dht/test_dht_twopeer_path_tracking.c
22 * @brief testcase for testing DHT service with
23 * two running peers, logging the path of the dht requests.
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_dht_service.h"
31 #define VERBOSE GNUNET_NO
33 /* Timeout for entire testcase */
34 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
36 /* Timeout for waiting for replies to get requests */
37 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
39 /* If number of peers not in config file, use this number */
40 #define DEFAULT_NUM_PEERS 2
45 * Directory to store temp data in, defined in config file
47 static char *test_directory;
50 * Variable used to store the number of connections we should wait for.
52 static unsigned int expected_connections;
55 * Variable used to keep track of how many peers aren't yet started.
57 static unsigned long long peers_left;
60 * Handle to the set of all peers run for this test.
62 static struct GNUNET_TESTING_PeerGroup *pg;
65 * Global handle we will use for GET requests.
67 struct GNUNET_DHT_GetHandle *global_get_handle;
71 * Total number of peers to run, set based on config file.
73 static unsigned long long num_peers;
76 * Global used to count how many connections we have currently
77 * been notified about (how many times has topology_callback been called
80 static unsigned int total_connections;
83 * Global used to count how many failed connections we have
84 * been notified about (how many times has topology_callback
85 * been called with failure?)
87 static unsigned int failed_connections;
89 /* Task handle to use to schedule test failure */
90 GNUNET_SCHEDULER_TaskIdentifier die_task;
92 /* Global return value (0 for success, anything else for failure) */
96 * Peer identity of the first peer started.
98 static struct GNUNET_PeerIdentity peer1id;
101 * Peer identity of the second peer started.
103 static struct GNUNET_PeerIdentity peer2id;
106 * Handle to the first peers DHT service (via the API)
108 static struct GNUNET_DHT_Handle *peer1dht;
111 * Handle to the second peers DHT service (via the API)
113 static struct GNUNET_DHT_Handle *peer2dht;
116 * Check whether peers successfully shut down.
119 shutdown_callback (void *cls, const char *emsg)
129 * Function scheduled to be run on the successful completion of this
130 * testcase. Specifically, called when our get request completes.
133 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 GNUNET_assert (pg != NULL);
136 GNUNET_assert (peer1dht != NULL);
137 GNUNET_assert (peer2dht != NULL);
138 GNUNET_DHT_disconnect (peer1dht);
139 GNUNET_DHT_disconnect (peer2dht);
140 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
145 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
146 * down the peers without freeing memory associated with GET request.
149 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
151 if (peer1dht != NULL)
152 GNUNET_DHT_disconnect (peer1dht);
154 if (peer2dht != NULL)
155 GNUNET_DHT_disconnect (peer2dht);
158 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
162 * Check if the get_handle is being used, if so stop the request. Either
163 * way, schedule the end_badly_cont function which actually shuts down the
167 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failing test with error: `%s'!\n",
171 if (global_get_handle != NULL)
173 GNUNET_DHT_get_stop (global_get_handle);
174 global_get_handle = NULL;
176 GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
181 * Iterator called if the GET request initiated returns a response.
184 * @param exp when will this value expire
185 * @param key key of the result
186 * @param type type of the result
187 * @param size number of bytes in data
188 * @param data pointer to the result data
191 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
192 const GNUNET_HashCode * key,
193 const struct GNUNET_PeerIdentity *const *get_path,
194 const struct GNUNET_PeerIdentity *const *put_path,
195 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
197 GNUNET_HashCode original_key; /* Key data was stored data under */
198 char original_data[4]; /* Made up data that was stored */
200 memset (&original_key, 42, sizeof (GNUNET_HashCode)); /* Set the key to what it was set to previously */
201 memset (original_data, 43, sizeof (original_data));
206 if ((0 != memcmp (&original_key, key, sizeof (GNUNET_HashCode))) ||
207 (0 != memcmp (original_data, data, sizeof (original_data))))
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210 "Key or data is not the same as was inserted!\n");
211 GNUNET_SCHEDULER_cancel (die_task);
212 GNUNET_SCHEDULER_add_now (&end_badly,
213 "key or data mismatch in get response!\n");
218 if (put_path != NULL)
220 fprintf (stderr, "PUT Path: ");
221 for (i = 0; put_path[i] != NULL; i++)
222 fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (put_path[i]));
223 fprintf (stderr, "\n");
225 if (get_path != NULL)
227 fprintf (stderr, "GET Path: ");
228 for (i = 0; get_path[i] != NULL; i++)
229 fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (get_path[i]));
230 fprintf (stderr, "\n");
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
235 GNUNET_SCHEDULER_cancel (die_task);
236 GNUNET_DHT_get_stop (global_get_handle);
237 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
241 * Start the GET request for the same key/data that was inserted.
244 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
246 GNUNET_HashCode key; /* Key for data lookup */
248 memset (&key, 42, sizeof (GNUNET_HashCode)); /* Set the key to the same thing as when data was inserted */
250 GNUNET_DHT_get_start (peer2dht, GNUNET_TIME_relative_get_forever (),
251 GNUNET_BLOCK_TYPE_TEST, &key,
252 DEFAULT_GET_REPLICATION, GNUNET_DHT_RO_RECORD_ROUTE,
253 NULL, 0, NULL, 0, &get_result_iterator, NULL);
257 * Called when the PUT request has been transmitted to the DHT service.
258 * Schedule the GET request for some time in the future.
261 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
263 GNUNET_SCHEDULER_cancel (die_task);
265 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &end_badly,
266 "waiting for get response (data not found)");
267 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
268 (GNUNET_TIME_UNIT_SECONDS, 10), &do_get, NULL);
272 * Set up some data, and call API PUT function
275 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
277 GNUNET_HashCode key; /* Made up key to store data under */
278 char data[4]; /* Made up data to store */
280 memset (&key, 42, sizeof (GNUNET_HashCode)); /* Set the key to something simple so we can issue GET request */
281 memset (data, 43, sizeof (data));
283 /* Insert the data at the first peer */
284 GNUNET_DHT_put (peer1dht, &key, DEFAULT_PUT_REPLICATION,
285 GNUNET_DHT_RO_RECORD_ROUTE, GNUNET_BLOCK_TYPE_TEST,
286 sizeof (data), data, GNUNET_TIME_UNIT_FOREVER_ABS,
287 GNUNET_TIME_UNIT_FOREVER_REL, &put_finished, NULL);
291 * This function is called whenever a connection attempt is finished between two of
292 * the started peers (started with GNUNET_TESTING_daemons_start). The total
293 * number of times this function is called should equal the number returned
294 * from the GNUNET_TESTING_connect_topology call.
296 * The emsg variable is NULL on success (peers connected), and non-NULL on
297 * failure (peers failed to connect).
300 topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
301 const struct GNUNET_PeerIdentity *second, uint32_t distance,
302 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
303 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
304 struct GNUNET_TESTING_Daemon *first_daemon,
305 struct GNUNET_TESTING_Daemon *second_daemon,
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 "connected peer %s to peer %s, distance %u\n",
314 first_daemon->shortname, second_daemon->shortname, distance);
320 failed_connections++;
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "Failed to connect peer %s to peer %s with error :\n%s\n",
323 first_daemon->shortname, second_daemon->shortname, emsg);
327 if (total_connections == expected_connections)
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
334 GNUNET_SCHEDULER_cancel (die_task);
336 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from test gets");
338 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
339 (GNUNET_TIME_UNIT_SECONDS, 2), &do_put, NULL);
341 else if (total_connections + failed_connections == expected_connections)
343 GNUNET_SCHEDULER_cancel (die_task);
345 GNUNET_SCHEDULER_add_now (&end_badly,
346 "from topology_callback (too many failed connections)");
352 * Callback which is called whenever a peer is started (as a result of the
353 * GNUNET_TESTING_daemons_start call.
355 * @param cls closure argument given to GNUNET_TESTING_daemons_start
356 * @param id the GNUNET_PeerIdentity of the started peer
357 * @param cfg the configuration for this specific peer (needed to connect
359 * @param d the handle to the daemon started
360 * @param emsg NULL if peer started, non-NULL on error
363 peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
364 const struct GNUNET_CONFIGURATION_Handle *cfg,
365 struct GNUNET_TESTING_Daemon *d, const char *emsg)
369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
370 "Failed to start daemon with error: `%s'\n", emsg);
373 GNUNET_assert (id != NULL);
375 /* This is the first peer started */
376 if (peers_left == num_peers)
378 memcpy (&peer1id, id, sizeof (struct GNUNET_PeerIdentity)); /* Save the peer id */
379 peer1dht = GNUNET_DHT_connect (cfg, 100); /* Connect to the first peers DHT service */
380 if (peer1dht == NULL) /* If DHT connect failed */
382 GNUNET_SCHEDULER_cancel (die_task);
383 GNUNET_SCHEDULER_add_now (&end_badly, "Failed to get dht handle!\n");
386 else /* This is the second peer started */
388 memcpy (&peer2id, id, sizeof (struct GNUNET_PeerIdentity)); /* Same as for first peer... */
389 peer2dht = GNUNET_DHT_connect (cfg, 100);
390 if (peer2dht == NULL)
392 GNUNET_SCHEDULER_cancel (die_task);
393 GNUNET_SCHEDULER_add_now (&end_badly, "Failed to get dht handle!\n");
397 /* Decrement number of peers left to start */
400 if (peers_left == 0) /* Indicates all peers started */
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "All %d daemons started, now connecting peers!\n", num_peers);
406 expected_connections = -1;
407 if ((pg != NULL)) /* Sanity check */
409 /* Connect peers in a "straight line" topology, return the number of expected connections */
410 expected_connections =
411 GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_LINE,
412 GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
413 0.0, TIMEOUT, 2, NULL, NULL);
416 /* Cancel current timeout fail task */
417 GNUNET_SCHEDULER_cancel (die_task);
418 if (expected_connections == GNUNET_SYSERR) /* Some error happened */
420 GNUNET_SCHEDULER_add_now (&end_badly,
421 "from connect topology (bad return)");
423 /* Schedule timeout on failure task */
425 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
426 "from connect topology (timeout)");
432 run (void *cls, char *const *args, const char *cfgfile,
433 const struct GNUNET_CONFIGURATION_Handle *cfg)
436 /* Get path from configuration file */
438 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
445 /* Get number of peers to start from configuration (should be two) */
447 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
449 num_peers = DEFAULT_NUM_PEERS;
451 /* Set peers_left so we know when all peers started */
452 peers_left = num_peers;
454 /* Set up a task to end testing if peer start fails */
456 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
457 "didn't start all daemons in reasonable amount of time!!!");
459 /* Start num_peers peers, call peers_started_callback on peer start, topology_callback on peer connect */
460 /* Read the API documentation for other parameters! */
461 pg = GNUNET_TESTING_daemons_start (cfg, peers_left, /* Total number of peers */
462 peers_left, /* Number of outstanding connections */
463 peers_left, /* Number of parallel ssh connections, or peers being started at once */
465 &peers_started_callback, NULL,
466 &topology_callback, NULL, NULL);
475 /* Arguments for GNUNET_PROGRAM_run */
476 char *const argv[] = { "test-dht-twopeer-put-get", /* Name to give running binary */
478 "test_dht_twopeer_data.conf", /* Config file to use */
484 struct GNUNET_GETOPT_CommandLineOption options[] = {
485 GNUNET_GETOPT_OPTION_END
487 /* Run the run function as a new program */
489 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
490 "test-dht-twopeer-put-get", "nohelp", options, &run,
492 if (ret != GNUNET_OK)
494 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
495 "`test-dht-twopeer': Failed with error code %d\n", ret);
501 main (int argc, char *argv[])
505 GNUNET_log_setup ("test-dht-twopeer",
514 * Need to remove base directory, subdirectories taken care
515 * of by the testing framework.
517 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
519 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
520 "Failed to remove testing directory %s\n", test_directory);
525 /* end of test_dht_twopeer_put_get.c */