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;
84 * Global scheduler, used for all GNUNET_SCHEDULER_* functions.
86 static struct GNUNET_SCHEDULER_Handle *sched;
89 * Total number of peers to run, set based on config file.
91 static unsigned long long num_peers;
94 * Global used to count how many connections we have currently
95 * been notified about (how many times has topology_callback been called
98 static unsigned int total_connections;
101 * Global used to count how many failed connections we have
102 * been notified about (how many times has topology_callback
103 * been called with failure?)
105 static unsigned int failed_connections;
107 /* Task handle to use to schedule test failure */
108 GNUNET_SCHEDULER_TaskIdentifier die_task;
110 /* Global return value (0 for success, anything else for failure) */
114 * Peer identity of the first peer started.
116 static struct GNUNET_PeerIdentity peer1id;
119 * Peer identity of the second peer started.
121 static struct GNUNET_PeerIdentity peer2id;
124 * Handle to the first peers DHT service (via the API)
126 static struct GNUNET_DHT_Handle *peer1dht;
129 * Handle to the second peers DHT service (via the API)
131 static struct GNUNET_DHT_Handle *peer2dht;
134 * Check whether peers successfully shut down.
136 void shutdown_callback (void *cls,
147 * Function scheduled to be run on the successful completion of this
148 * testcase. Specifically, called when our get request completes.
151 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
153 GNUNET_assert (pg != NULL);
154 GNUNET_assert (peer1dht != NULL);
155 GNUNET_assert (peer2dht != NULL);
156 GNUNET_DHT_disconnect(peer1dht);
157 GNUNET_DHT_disconnect(peer2dht);
158 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
163 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
164 * down the peers without freeing memory associated with GET request.
167 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
169 if (peer1dht != NULL)
170 GNUNET_DHT_disconnect(peer1dht);
172 if (peer2dht != NULL)
173 GNUNET_DHT_disconnect(peer2dht);
176 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
180 * Check if the get_handle is being used, if so stop the request. Either
181 * way, schedule the end_badly_cont function which actually shuts down the
185 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failing test with error: `%s'!\n", (char *)cls);
188 if (global_get_handle != NULL)
190 GNUNET_DHT_get_stop(global_get_handle);
191 global_get_handle = NULL;
193 GNUNET_SCHEDULER_add_now(sched, &end_badly_cont, NULL);
198 * Iterator called if the GET request initiated returns a response.
201 * @param exp when will this value expire
202 * @param key key of the result
203 * @param type type of the result
204 * @param size number of bytes in data
205 * @param data pointer to the result data
207 void get_result_iterator (void *cls,
208 struct GNUNET_TIME_Absolute exp,
209 const GNUNET_HashCode * key,
210 const struct GNUNET_PeerIdentity * const *get_path,
211 const struct GNUNET_PeerIdentity * const *put_path,
212 enum GNUNET_BLOCK_Type type,
216 GNUNET_HashCode original_key; /* Key data was stored data under */
217 char original_data[4]; /* Made up data that was stored */
218 memset(&original_key, 42, sizeof(GNUNET_HashCode)); /* Set the key to what it was set to previously */
219 memset(original_data, 43, sizeof(original_data));
221 if ((0 != memcmp(&original_key, key, sizeof (GNUNET_HashCode))) || (0 != memcmp(original_data, data, sizeof(original_data))))
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Key or data is not the same as was inserted!\n");
224 GNUNET_SCHEDULER_cancel(sched, die_task);
225 GNUNET_SCHEDULER_add_now(sched, &end_badly, "key or data mismatch in get response!\n");
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
230 GNUNET_SCHEDULER_cancel(sched, die_task);
231 GNUNET_DHT_get_stop(global_get_handle);
232 GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
236 * Start the GET request for the same key/data that was inserted.
239 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
241 GNUNET_HashCode key; /* Key for data lookup */
242 memset(&key, 42, sizeof(GNUNET_HashCode)); /* Set the key to the same thing as when data was inserted */
243 global_get_handle = GNUNET_DHT_get_start(peer2dht, GNUNET_TIME_relative_get_forever(),
244 GNUNET_BLOCK_TYPE_TEST,
249 &get_result_iterator, NULL);
253 * Called when the PUT request has been transmitted to the DHT service.
254 * Schedule the GET request for some time in the future.
257 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
259 GNUNET_SCHEDULER_cancel (sched, die_task);
260 die_task = GNUNET_SCHEDULER_add_delayed (sched, GET_TIMEOUT,
261 &end_badly, "waiting for get response (data not found)");
262 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10), &do_get, NULL);
266 * Set up some data, and call API PUT function
269 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
271 GNUNET_HashCode key; /* Made up key to store data under */
272 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,
280 GNUNET_BLOCK_TYPE_TEST,
282 GNUNET_TIME_UNIT_FOREVER_ABS,
283 GNUNET_TIME_UNIT_FOREVER_REL,
284 &put_finished, NULL);
288 * This function is called whenever a connection attempt is finished between two of
289 * the started peers (started with GNUNET_TESTING_daemons_start). The total
290 * number of times this function is called should equal the number returned
291 * from the GNUNET_TESTING_connect_topology call.
293 * The emsg variable is NULL on success (peers connected), and non-NULL on
294 * failure (peers failed to connect).
297 topology_callback (void *cls,
298 const struct GNUNET_PeerIdentity *first,
299 const struct GNUNET_PeerIdentity *second,
301 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
302 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
303 struct GNUNET_TESTING_Daemon *first_daemon,
304 struct GNUNET_TESTING_Daemon *second_daemon,
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
312 first_daemon->shortname,
313 second_daemon->shortname,
320 failed_connections++;
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
322 first_daemon->shortname,
323 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 (sched, die_task);
335 die_task = GNUNET_SCHEDULER_add_delayed (sched, TIMEOUT,
336 &end_badly, "from test gets");
338 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2), &do_put, NULL);
340 else if (total_connections + failed_connections == expected_connections)
342 GNUNET_SCHEDULER_cancel (sched, die_task);
343 die_task = GNUNET_SCHEDULER_add_now (sched,
344 &end_badly, "from topology_callback (too many failed connections)");
350 * Callback which is called whenever a peer is started (as a result of the
351 * GNUNET_TESTING_daemons_start call.
353 * @param cls closure argument given to GNUNET_TESTING_daemons_start
354 * @param id the GNUNET_PeerIdentity of the started peer
355 * @param cfg the configuration for this specific peer (needed to connect
357 * @param d the handle to the daemon started
358 * @param emsg NULL if peer started, non-NULL on error
361 peers_started_callback (void *cls,
362 const struct GNUNET_PeerIdentity *id,
363 const struct GNUNET_CONFIGURATION_Handle *cfg,
364 struct GNUNET_TESTING_Daemon *d, const char *emsg)
368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to start daemon with error: `%s'\n",
372 GNUNET_assert (id != NULL);
374 /* This is the first peer started */
375 if (peers_left == num_peers)
377 memcpy(&peer1id, id, sizeof(struct GNUNET_PeerIdentity)); /* Save the peer id */
378 peer1dht = GNUNET_DHT_connect(sched, cfg, 100); /* Connect to the first peers DHT service */
379 if (peer1dht == NULL) /* If DHT connect failed */
381 GNUNET_SCHEDULER_cancel (sched, die_task);
382 GNUNET_SCHEDULER_add_now(sched, &end_badly, "Failed to get dht handle!\n");
385 else /* This is the second peer started */
387 memcpy(&peer2id, id, sizeof(struct GNUNET_PeerIdentity)); /* Same as for first peer... */
388 peer2dht = GNUNET_DHT_connect(sched, cfg, 100);
389 if (peer2dht == NULL)
391 GNUNET_SCHEDULER_cancel (sched, die_task);
392 GNUNET_SCHEDULER_add_now(sched, &end_badly, "Failed to get dht handle!\n");
396 /* Decrement number of peers left to start */
399 if (peers_left == 0) /* Indicates all peers started */
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "All %d daemons started, now connecting peers!\n",
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 = GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_LINE, GNUNET_TESTING_TOPOLOGY_OPTION_ALL, 0.0, NULL, NULL);
413 /* Cancel current timeout fail task */
414 GNUNET_SCHEDULER_cancel (sched, die_task);
415 if (expected_connections == GNUNET_SYSERR) /* Some error happened */
416 die_task = GNUNET_SCHEDULER_add_now (sched,
417 &end_badly, "from connect topology (bad return)");
419 /* Schedule timeout on failure task */
420 die_task = GNUNET_SCHEDULER_add_delayed (sched,
422 &end_badly, "from connect topology (timeout)");
429 struct GNUNET_SCHEDULER_Handle *s,
431 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
435 /* Get path from configuration file */
436 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
442 /* Get number of peers to start from configuration (should be two) */
444 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
446 num_peers = DEFAULT_NUM_PEERS;
448 /* Set peers_left so we know when all peers started */
449 peers_left = num_peers;
451 /* Set up a task to end testing if peer start fails */
452 die_task = GNUNET_SCHEDULER_add_delayed (sched,
454 &end_badly, "didn't start all daemons in reasonable amount of time!!!");
456 /* Start num_peers peers, call peers_started_callback on peer start, topology_callback on peer connect */
457 /* Read the API documentation for other parameters! */
458 pg = GNUNET_TESTING_daemons_start (sched, cfg,
459 num_peers, TIMEOUT, NULL, NULL, &peers_started_callback, NULL,
460 &topology_callback, NULL, NULL);
468 /* Arguments for GNUNET_PROGRAM_run */
469 char *const argv[] = {"test-dht-twopeer-put-get", /* Name to give running binary */
471 "test_dht_twopeer_data.conf", /* Config file to use */
477 struct GNUNET_GETOPT_CommandLineOption options[] = {
478 GNUNET_GETOPT_OPTION_END
480 /* Run the run function as a new program */
481 ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
482 argv, "test-dht-twopeer-put-get", "nohelp",
484 if (ret != GNUNET_OK)
486 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`test-dht-twopeer': Failed with error code %d\n", ret);
492 main (int argc, char *argv[])
496 GNUNET_log_setup ("test-dht-twopeer",
505 * Need to remove base directory, subdirectories taken care
506 * of by the testing framework.
508 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
510 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
515 /* end of test_dht_twopeer_put_get.c */