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"
45 #define VERBOSE GNUNET_NO
47 /* Timeout for entire testcase */
48 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
50 /* Timeout for waiting for replies to get requests */
51 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
53 /* If number of peers not in config file, use this number */
54 #define DEFAULT_NUM_PEERS 2
59 * Directory to store temp data in, defined in config file
61 static char *test_directory;
64 * Variable used to store the number of connections we should wait for.
66 static unsigned int expected_connections;
69 * Variable used to keep track of how many peers aren't yet started.
71 static unsigned long long peers_left;
74 * Handle to the set of all peers run for this test.
76 static struct GNUNET_TESTING_PeerGroup *pg;
79 * Global handle we will use for GET requests.
81 struct GNUNET_DHT_GetHandle *global_get_handle;
85 * Total number of peers to run, set based on config file.
87 static unsigned long long num_peers;
90 * Global used to count how many connections we have currently
91 * been notified about (how many times has topology_callback been called
94 static unsigned int total_connections;
97 * Global used to count how many failed connections we have
98 * been notified about (how many times has topology_callback
99 * been called with failure?)
101 static unsigned int failed_connections;
103 /* Task handle to use to schedule test failure */
104 GNUNET_SCHEDULER_TaskIdentifier die_task;
106 /* Global return value (0 for success, anything else for failure) */
110 * Peer identity of the first peer started.
112 static struct GNUNET_PeerIdentity peer1id;
115 * Peer identity of the second peer started.
117 static struct GNUNET_PeerIdentity peer2id;
120 * Handle to the first peers DHT service (via the API)
122 static struct GNUNET_DHT_Handle *peer1dht;
125 * Handle to the second peers DHT service (via the API)
127 static struct GNUNET_DHT_Handle *peer2dht;
130 * Check whether peers successfully shut down.
132 void shutdown_callback (void *cls,
143 * Function scheduled to be run on the successful completion of this
144 * testcase. Specifically, called when our get request completes.
147 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
149 GNUNET_assert (pg != NULL);
150 GNUNET_assert (peer1dht != NULL);
151 GNUNET_assert (peer2dht != NULL);
152 GNUNET_DHT_disconnect(peer1dht);
153 GNUNET_DHT_disconnect(peer2dht);
154 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
159 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
160 * down the peers without freeing memory associated with GET request.
163 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
165 if (peer1dht != NULL)
166 GNUNET_DHT_disconnect(peer1dht);
168 if (peer2dht != NULL)
169 GNUNET_DHT_disconnect(peer2dht);
172 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
176 * Check if the get_handle is being used, if so stop the request. Either
177 * way, schedule the end_badly_cont function which actually shuts down the
181 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failing test with error: `%s'!\n", (char *)cls);
184 if (global_get_handle != NULL)
186 GNUNET_DHT_get_stop(global_get_handle);
187 global_get_handle = NULL;
189 GNUNET_SCHEDULER_add_now(&end_badly_cont, NULL);
194 * Iterator called if the GET request initiated returns a response.
197 * @param exp when will this value expire
198 * @param key key of the result
199 * @param type type of the result
200 * @param size number of bytes in data
201 * @param data pointer to the result data
203 void get_result_iterator (void *cls,
204 struct GNUNET_TIME_Absolute exp,
205 const GNUNET_HashCode * key,
206 const struct GNUNET_PeerIdentity * const *get_path,
207 const struct GNUNET_PeerIdentity * const *put_path,
208 enum GNUNET_BLOCK_Type type,
212 GNUNET_HashCode original_key; /* Key data was stored data under */
213 char original_data[4]; /* Made up data that was stored */
214 memset(&original_key, 42, sizeof(GNUNET_HashCode)); /* Set the key to what it was set to previously */
215 memset(original_data, 43, sizeof(original_data));
217 if ((0 != memcmp(&original_key, key, sizeof (GNUNET_HashCode))) || (0 != memcmp(original_data, data, sizeof(original_data))))
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Key or data is not the same as was inserted!\n");
220 GNUNET_SCHEDULER_cancel(die_task);
221 GNUNET_SCHEDULER_add_now(&end_badly, "key or data mismatch in get response!\n");
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
226 GNUNET_SCHEDULER_cancel(die_task);
227 GNUNET_DHT_get_stop(global_get_handle);
228 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
232 * Start the GET request for the same key/data that was inserted.
235 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
237 GNUNET_HashCode key; /* Key for data lookup */
238 memset(&key, 42, sizeof(GNUNET_HashCode)); /* Set the key to the same thing as when data was inserted */
239 global_get_handle = GNUNET_DHT_get_start(peer2dht, GNUNET_TIME_relative_get_forever(),
240 GNUNET_BLOCK_TYPE_TEST,
242 DEFAULT_GET_REPLICATION,
246 &get_result_iterator, NULL);
250 * Called when the PUT request has been transmitted to the DHT service.
251 * Schedule the GET request for some time in the future.
254 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
256 GNUNET_SCHEDULER_cancel (die_task);
257 die_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
258 &end_badly, "waiting for get response (data not found)");
259 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10), &do_get, NULL);
263 * Set up some data, and call API PUT function
266 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
268 GNUNET_HashCode key; /* Made up key to store data under */
269 char data[4]; /* Made up data to store */
270 memset(&key, 42, sizeof(GNUNET_HashCode)); /* Set the key to something simple so we can issue GET request */
271 memset(data, 43, sizeof(data));
273 /* Insert the data at the first peer */
274 GNUNET_DHT_put(peer1dht,
276 DEFAULT_PUT_REPLICATION,
278 GNUNET_BLOCK_TYPE_TEST,
280 GNUNET_TIME_UNIT_FOREVER_ABS,
281 GNUNET_TIME_UNIT_FOREVER_REL,
282 &put_finished, NULL);
286 * This function is called whenever a connection attempt is finished between two of
287 * the started peers (started with GNUNET_TESTING_daemons_start). The total
288 * number of times this function is called should equal the number returned
289 * from the GNUNET_TESTING_connect_topology call.
291 * The emsg variable is NULL on success (peers connected), and non-NULL on
292 * failure (peers failed to connect).
295 topology_callback (void *cls,
296 const struct GNUNET_PeerIdentity *first,
297 const struct GNUNET_PeerIdentity *second,
299 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
300 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
301 struct GNUNET_TESTING_Daemon *first_daemon,
302 struct GNUNET_TESTING_Daemon *second_daemon,
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
310 first_daemon->shortname,
311 second_daemon->shortname,
318 failed_connections++;
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
320 first_daemon->shortname,
321 second_daemon->shortname, emsg);
325 if (total_connections == expected_connections)
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
332 GNUNET_SCHEDULER_cancel (die_task);
333 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
334 &end_badly, "from test gets");
336 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2), &do_put, NULL);
338 else if (total_connections + failed_connections == expected_connections)
340 GNUNET_SCHEDULER_cancel (die_task);
341 die_task = GNUNET_SCHEDULER_add_now (&end_badly, "from topology_callback (too many failed connections)");
347 * Callback which is called whenever a peer is started (as a result of the
348 * GNUNET_TESTING_daemons_start call.
350 * @param cls closure argument given to GNUNET_TESTING_daemons_start
351 * @param id the GNUNET_PeerIdentity of the started peer
352 * @param cfg the configuration for this specific peer (needed to connect
354 * @param d the handle to the daemon started
355 * @param emsg NULL if peer started, non-NULL on error
358 peers_started_callback (void *cls,
359 const struct GNUNET_PeerIdentity *id,
360 const struct GNUNET_CONFIGURATION_Handle *cfg,
361 struct GNUNET_TESTING_Daemon *d, const char *emsg)
365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to start daemon with error: `%s'\n",
369 GNUNET_assert (id != NULL);
371 /* This is the first peer started */
372 if (peers_left == num_peers)
374 memcpy(&peer1id, id, sizeof(struct GNUNET_PeerIdentity)); /* Save the peer id */
375 peer1dht = GNUNET_DHT_connect(cfg, 100); /* Connect to the first peers DHT service */
376 if (peer1dht == NULL) /* If DHT connect failed */
378 GNUNET_SCHEDULER_cancel (die_task);
379 GNUNET_SCHEDULER_add_now(&end_badly, "Failed to get dht handle!\n");
382 else /* This is the second peer started */
384 memcpy(&peer2id, id, sizeof(struct GNUNET_PeerIdentity)); /* Same as for first peer... */
385 peer2dht = GNUNET_DHT_connect(cfg, 100);
386 if (peer2dht == NULL)
388 GNUNET_SCHEDULER_cancel (die_task);
389 GNUNET_SCHEDULER_add_now(&end_badly, "Failed to get dht handle!\n");
393 /* Decrement number of peers left to start */
396 if (peers_left == 0) /* Indicates all peers started */
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "All %d daemons started, now connecting peers!\n",
403 expected_connections = -1;
404 if ((pg != NULL)) /* Sanity check */
406 /* Connect peers in a "straight line" topology, return the number of expected connections */
407 expected_connections = GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_LINE, GNUNET_TESTING_TOPOLOGY_OPTION_ALL, 0.0, NULL, NULL);
410 /* Cancel current timeout fail task */
411 GNUNET_SCHEDULER_cancel (die_task);
412 if (expected_connections == GNUNET_SYSERR) /* Some error happened */
413 die_task = GNUNET_SCHEDULER_add_now (&end_badly, "from connect topology (bad return)");
415 /* Schedule timeout on failure task */
416 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
417 &end_badly, "from connect topology (timeout)");
425 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
428 /* Get path from configuration file */
429 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
435 /* Get number of peers to start from configuration (should be two) */
437 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
439 num_peers = DEFAULT_NUM_PEERS;
441 /* Set peers_left so we know when all peers started */
442 peers_left = num_peers;
444 /* Set up a task to end testing if peer start fails */
445 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
446 &end_badly, "didn't start all daemons in reasonable amount of time!!!");
448 /* Start num_peers peers, call peers_started_callback on peer start, topology_callback on peer connect */
449 /* Read the API documentation for other parameters! */
450 pg = GNUNET_TESTING_daemons_start (cfg,
451 num_peers, TIMEOUT, NULL, NULL, &peers_started_callback, NULL,
452 &topology_callback, NULL, NULL);
460 /* Arguments for GNUNET_PROGRAM_run */
461 char *const argv[] = {"test-dht-twopeer-put-get", /* Name to give running binary */
463 "test_dht_twopeer_data.conf", /* Config file to use */
469 struct GNUNET_GETOPT_CommandLineOption options[] = {
470 GNUNET_GETOPT_OPTION_END
472 /* Run the run function as a new program */
473 ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
474 argv, "test-dht-twopeer-put-get", "nohelp",
476 if (ret != GNUNET_OK)
478 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`test-dht-twopeer': Failed with error code %d\n", ret);
484 main (int argc, char *argv[])
488 GNUNET_log_setup ("test-dht-twopeer",
497 * Need to remove base directory, subdirectories taken care
498 * of by the testing framework.
500 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
502 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
507 /* end of test_dht_twopeer_put_get.c */