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_put_get.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, runs the do_put function to insert data
32 * at the first peer. Once the GNUNET_DHT_put function completes,
33 * calls the do_get function which initiates a GNUNET_DHT_get from
34 * the *second* peer. 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_EXTRA_LOGGING
49 /* Timeout for entire testcase */
50 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
52 /* Timeout for waiting for replies to get requests */
53 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
55 /* If number of peers not in config file, use this number */
56 #define DEFAULT_NUM_PEERS 2
61 * Directory to store temp data in, defined in config file
63 static char *test_directory;
66 * Variable used to store the number of connections we should wait for.
68 static unsigned int expected_connections;
71 * Variable used to keep track of how many peers aren't yet started.
73 static unsigned long long peers_left;
76 * Handle to the set of all peers run for this test.
78 static struct GNUNET_TESTING_PeerGroup *pg;
81 * Global handle we will use for GET requests.
83 struct GNUNET_DHT_GetHandle *global_get_handle;
87 * Total number of peers to run, set based on config file.
89 static unsigned long long num_peers;
92 * Global used to count how many connections we have currently
93 * been notified about (how many times has topology_callback been called
96 static unsigned int total_connections;
99 * Global used to count how many failed connections we have
100 * been notified about (how many times has topology_callback
101 * been called with failure?)
103 static unsigned int failed_connections;
105 /* Task handle to use to schedule test failure */
106 static GNUNET_SCHEDULER_TaskIdentifier die_task;
108 /* Global return value (0 for success, anything else for failure) */
112 * Peer identity of the first peer started.
114 static struct GNUNET_PeerIdentity peer1id;
117 * Peer identity of the second peer started.
119 static struct GNUNET_PeerIdentity peer2id;
122 * Handle to the first peers DHT service (via the API)
124 static struct GNUNET_DHT_Handle *peer1dht;
127 * Handle to the second peers DHT service (via the API)
129 static struct GNUNET_DHT_Handle *peer2dht;
132 * Check whether peers successfully shut down.
135 shutdown_callback (void *cls, const char *emsg)
145 * Function scheduled to be run on the successful completion of this
146 * testcase. Specifically, called when our get request completes.
149 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
151 GNUNET_assert (pg != NULL);
152 GNUNET_assert (peer1dht != NULL);
153 GNUNET_assert (peer2dht != NULL);
154 GNUNET_DHT_disconnect (peer1dht);
155 GNUNET_DHT_disconnect (peer2dht);
156 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
161 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
162 * down the peers without freeing memory associated with GET request.
165 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
167 if (peer1dht != NULL)
168 GNUNET_DHT_disconnect (peer1dht);
170 if (peer2dht != NULL)
171 GNUNET_DHT_disconnect (peer2dht);
174 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
178 * Check if the get_handle is being used, if so stop the request. Either
179 * way, schedule the end_badly_cont function which actually shuts down the
183 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failing test with error: `%s'!\n",
187 if (global_get_handle != NULL)
189 GNUNET_DHT_get_stop (global_get_handle);
190 global_get_handle = NULL;
192 GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
197 * Iterator called if the GET request initiated returns a response.
200 * @param exp when will this value expire
201 * @param key key of the result
202 * @param type type of the result
203 * @param size number of bytes in data
204 * @param data pointer to the result data
207 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
208 const GNUNET_HashCode * key,
209 const struct GNUNET_PeerIdentity *get_path,
210 unsigned int get_path_size,
211 const struct GNUNET_PeerIdentity *put_path,
212 unsigned int put_path_size,
213 enum GNUNET_BLOCK_Type type, size_t size,
214 const void *result_data)
216 GNUNET_HashCode original_key; /* Key data was stored data under */
217 char original_data[4]; /* Made up data that was stored */
219 memset (&original_key, 42, sizeof (GNUNET_HashCode)); /* Set the key to what it was set to previously */
220 memset (original_data, 43, sizeof (original_data));
222 if ((sizeof (original_data) != size) ||
223 (0 != memcmp (&original_key, key, sizeof (GNUNET_HashCode))) ||
224 (0 != memcmp (original_data, result_data, sizeof (original_data))))
226 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
227 "Key or data is not the same as was inserted!\n");
228 GNUNET_SCHEDULER_cancel (die_task);
229 GNUNET_SCHEDULER_add_now (&end_badly,
230 "key or data mismatch in get response!\n");
234 GNUNET_SCHEDULER_cancel (die_task);
235 GNUNET_DHT_get_stop (global_get_handle);
236 global_get_handle = NULL;
237 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
242 * Called when the PUT request has been transmitted to the DHT service.
243 * Schedule the GET request for some time in the future.
246 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
248 GNUNET_HashCode key; /* Key for data lookup */
250 GNUNET_SCHEDULER_cancel (die_task);
252 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &end_badly,
253 "waiting for get response (data not found)");
255 memset (&key, 42, sizeof (GNUNET_HashCode)); /* Set the key to the same thing as when data was inserted */
257 GNUNET_DHT_get_start (peer2dht, GNUNET_TIME_UNIT_FOREVER_REL,
258 GNUNET_BLOCK_TYPE_TEST,
259 &key, 1, GNUNET_DHT_RO_NONE,
260 NULL, 0, &get_result_iterator, NULL);
265 * Set up some data, and call API PUT function
268 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
270 GNUNET_HashCode key; /* Made up key to store data under */
271 char data[4]; /* Made up data to store */
273 memset (&key, 42, sizeof (GNUNET_HashCode)); /* Set the key to something simple so we can issue GET request */
274 memset (data, 43, sizeof (data));
276 /* Insert the data at the first peer */
277 GNUNET_DHT_put (peer1dht, &key, 1, GNUNET_DHT_RO_NONE,
278 GNUNET_BLOCK_TYPE_TEST, sizeof (data), data,
279 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
280 &put_finished, NULL);
285 * This function is called whenever a connection attempt is finished between two of
286 * the started peers (started with GNUNET_TESTING_daemons_start). The total
287 * number of times this function is called should equal the number returned
288 * from the GNUNET_TESTING_connect_topology call.
290 * The emsg variable is NULL on success (peers connected), and non-NULL on
291 * failure (peers failed to connect).
294 topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
295 const struct GNUNET_PeerIdentity *second, uint32_t distance,
296 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
297 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
298 struct GNUNET_TESTING_Daemon *first_daemon,
299 struct GNUNET_TESTING_Daemon *second_daemon,
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "connected peer %s to peer %s, distance %u\n",
308 first_daemon->shortname, second_daemon->shortname, distance);
314 failed_connections++;
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Failed to connect peer %s to peer %s with error :\n%s\n",
317 first_daemon->shortname, second_daemon->shortname, emsg);
321 if (total_connections == expected_connections)
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
328 GNUNET_SCHEDULER_cancel (die_task);
330 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from test gets");
332 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_put, NULL);
334 else if (total_connections + failed_connections == expected_connections)
336 GNUNET_SCHEDULER_cancel (die_task);
338 GNUNET_SCHEDULER_add_now (&end_badly,
339 "from topology_callback (too many failed connections)");
345 * Callback which is called whenever a peer is started (as a result of the
346 * GNUNET_TESTING_daemons_start call.
348 * @param cls closure argument given to GNUNET_TESTING_daemons_start
349 * @param id the GNUNET_PeerIdentity of the started peer
350 * @param cfg the configuration for this specific peer (needed to connect
352 * @param d the handle to the daemon started
353 * @param emsg NULL if peer started, non-NULL on error
356 peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
357 const struct GNUNET_CONFIGURATION_Handle *cfg,
358 struct GNUNET_TESTING_Daemon *d, const char *emsg)
362 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
363 "Failed to start daemon with error: `%s'\n", emsg);
366 GNUNET_assert (id != NULL);
368 /* This is the first peer started */
369 if (peers_left == num_peers)
371 memcpy (&peer1id, id, sizeof (struct GNUNET_PeerIdentity)); /* Save the peer id */
372 peer1dht = GNUNET_DHT_connect (cfg, 100); /* Connect to the first peers DHT service */
373 if (peer1dht == NULL) /* If DHT connect failed */
375 GNUNET_SCHEDULER_cancel (die_task);
376 GNUNET_SCHEDULER_add_now (&end_badly, "Failed to get dht handle!\n");
379 else /* This is the second peer started */
381 memcpy (&peer2id, id, sizeof (struct GNUNET_PeerIdentity)); /* Same as for first peer... */
382 peer2dht = GNUNET_DHT_connect (cfg, 100);
383 if (peer2dht == NULL)
385 GNUNET_SCHEDULER_cancel (die_task);
386 GNUNET_SCHEDULER_add_now (&end_badly, "Failed to get dht handle!\n");
390 /* Decrement number of peers left to start */
393 if (peers_left == 0) /* Indicates all peers started */
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "All %d daemons started, now connecting peers!\n", num_peers);
399 expected_connections = -1;
400 if ((pg != NULL)) /* Sanity check */
402 /* Connect peers in a "straight line" topology, return the number of expected connections */
403 expected_connections =
404 GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_LINE,
405 GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
406 0.0, TIMEOUT, 12, NULL, NULL);
409 /* Cancel current timeout fail task */
410 GNUNET_SCHEDULER_cancel (die_task);
411 if (expected_connections == GNUNET_SYSERR) /* Some error happened */
413 GNUNET_SCHEDULER_add_now (&end_badly,
414 "from connect topology (bad return)");
416 /* Schedule timeout on failure task */
418 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
419 "from connect topology (timeout)");
425 run (void *cls, char *const *args, const char *cfgfile,
426 const struct GNUNET_CONFIGURATION_Handle *cfg)
429 /* Get path from configuration file */
431 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
438 /* Get number of peers to start from configuration (should be two) */
440 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
442 num_peers = DEFAULT_NUM_PEERS;
444 /* Set peers_left so we know when all peers started */
445 peers_left = num_peers;
447 /* Set up a task to end testing if peer start fails */
449 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
450 "didn't start all daemons in reasonable amount of time!!!");
452 /* Start num_peers peers, call peers_started_callback on peer start, topology_callback on peer connect */
453 /* Read the API documentation for other parameters! */
454 pg = GNUNET_TESTING_daemons_start (cfg, num_peers, 2, 2, TIMEOUT, NULL, NULL,
455 &peers_started_callback, NULL,
456 &topology_callback, NULL, NULL);
465 /* Arguments for GNUNET_PROGRAM_run */
466 char *const argv[] = { "test-dht-twopeer-put-get", /* Name to give running binary */
468 "test_dht_twopeer_data.conf", /* Config file to use */
474 struct GNUNET_GETOPT_CommandLineOption options[] = {
475 GNUNET_GETOPT_OPTION_END
477 /* Run the run function as a new program */
479 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
480 "test-dht-twopeer-put-get", "nohelp", options, &run,
482 if (ret != GNUNET_OK)
484 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
485 "`test-dht-twopeer': Failed with error code %d\n", ret);
491 main (int argc, char *argv[])
495 GNUNET_log_setup ("test-dht-twopeer",
504 * Need to remove base directory, subdirectories taken care
505 * of by the testing framework.
507 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
509 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
510 "Failed to remove testing directory %s\n", test_directory);
515 /* end of test_dht_twopeer_put_get.c */