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_NO
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
63 * Directory to store temp data in, defined in config file
65 static char *test_directory;
68 * Variable used to store the number of connections we should wait for.
70 static unsigned int expected_connections;
73 * Variable used to keep track of how many peers aren't yet started.
75 static unsigned long long peers_left;
78 * Handle to the set of all peers run for this test.
80 static struct GNUNET_TESTING_PeerGroup *pg;
83 * Global handle we will use for GET requests.
85 struct GNUNET_DHT_GetHandle *global_get_handle;
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 struct GNUNET_DNS_Record data;
118 * Peer identity of the first peer started.
120 static struct GNUNET_PeerIdentity peer1id;
123 * Peer identity of the second peer started.
125 static struct GNUNET_PeerIdentity peer2id;
128 * Handle to the first peers DHT service (via the API)
130 static struct GNUNET_DHT_Handle *peer1dht;
133 * Handle to the second peers DHT service (via the API)
135 static struct GNUNET_DHT_Handle *peer2dht;
138 * Check whether peers successfully shut down.
141 shutdown_callback (void *cls, const char *emsg)
151 * Function scheduled to be run on the successful completion of this
152 * testcase. Specifically, called when our get request completes.
155 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 GNUNET_assert (pg != NULL);
158 GNUNET_assert (peer1dht != NULL);
159 GNUNET_assert (peer2dht != NULL);
160 GNUNET_DHT_disconnect (peer1dht);
161 GNUNET_DHT_disconnect (peer2dht);
162 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
167 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
168 * down the peers without freeing memory associated with GET request.
171 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
173 if (peer1dht != NULL)
174 GNUNET_DHT_disconnect (peer1dht);
176 if (peer2dht != NULL)
177 GNUNET_DHT_disconnect (peer2dht);
180 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
184 * Check if the get_handle is being used, if so stop the request. Either
185 * way, schedule the end_badly_cont function which actually shuts down the
189 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failing test with error: `%s'!\n",
193 if (global_get_handle != NULL)
195 GNUNET_DHT_get_stop (global_get_handle);
196 global_get_handle = NULL;
198 GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
203 * Iterator called if the GET request initiated returns a response.
206 * @param exp when will this value expire
207 * @param key key of the result
208 * @param type type of the result
209 * @param size number of bytes in data
210 * @param data pointer to the result data
213 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
214 const GNUNET_HashCode * key,
215 const struct GNUNET_PeerIdentity *const *get_path,
216 const struct GNUNET_PeerIdentity *const *put_path,
217 enum GNUNET_BLOCK_Type type, size_t size,
218 const void *result_data)
220 GNUNET_HashCode original_key; /* Key data was stored data under */
221 char original_data[4]; /* Made up data that was stored */
223 memset (&original_key, 42, sizeof (GNUNET_HashCode)); /* Set the key to what it was set to previously */
224 memset (original_data, 43, sizeof (original_data));
227 if ((sizeof (original_data) != size) ||
228 (0 != memcmp (&data.service_descriptor, key, sizeof (GNUNET_HashCode))) ||
229 (0 != memcmp ((char *) &data, result_data, sizeof (original_data))))
231 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
232 "Key or data is not the same as was inserted!\n");
233 GNUNET_SCHEDULER_cancel (die_task);
234 GNUNET_SCHEDULER_add_now (&end_badly,
235 "key or data mismatch in get response!\n");
239 if ((sizeof (original_data) != size) ||
240 (0 != memcmp (&original_key, key, sizeof (GNUNET_HashCode))) ||
241 (0 != memcmp (original_data, result_data, sizeof (original_data))))
243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
244 "Key or data is not the same as was inserted!\n");
245 GNUNET_SCHEDULER_cancel (die_task);
246 GNUNET_SCHEDULER_add_now (&end_badly,
247 "key or data mismatch in get response!\n");
252 GNUNET_SCHEDULER_cancel (die_task);
253 GNUNET_DHT_get_stop (global_get_handle);
254 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
258 * Start the GET request for the same key/data that was inserted.
261 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
263 GNUNET_HashCode key; /* Key for data lookup */
266 memcpy (&key, &data.service_descriptor, sizeof (GNUNET_HashCode));
268 memset (&key, 42, sizeof (GNUNET_HashCode)); /* Set the key to the same thing as when data was inserted */
271 GNUNET_DHT_get_start (peer2dht, GNUNET_TIME_relative_get_forever (),
273 GNUNET_BLOCK_TYPE_DNS,
275 GNUNET_BLOCK_TYPE_TEST,
277 &key, DEFAULT_GET_REPLICATION, GNUNET_DHT_RO_NONE,
278 NULL, 0, NULL, 0, &get_result_iterator, NULL);
282 * Called when the PUT request has been transmitted to the DHT service.
283 * Schedule the GET request for some time in the future.
286 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
288 GNUNET_SCHEDULER_cancel (die_task);
290 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &end_badly,
291 "waiting for get response (data not found)");
292 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
293 (GNUNET_TIME_UNIT_SECONDS, 10), &do_get, NULL);
299 * Set up some data, and call API PUT function
302 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
304 GNUNET_HashCode key; /* Made up key to store data under */
305 char data[4]; /* Made up data to store */
307 memset (&key, 42, sizeof (GNUNET_HashCode)); /* Set the key to something simple so we can issue GET request */
308 memset (data, 43, sizeof (data));
310 /* Insert the data at the first peer */
311 GNUNET_DHT_put (peer1dht, &key, DEFAULT_PUT_REPLICATION, GNUNET_DHT_RO_NONE,
312 GNUNET_BLOCK_TYPE_TEST, sizeof (data), data,
313 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
314 &put_finished, NULL);
319 * Set up some data, and call API PUT function
322 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
324 char *name = "philipptoelke.gnunet.";
325 size_t size = sizeof (struct GNUNET_DNS_Record);
327 memset (&data, 0, size);
329 data.purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
330 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
332 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
334 data.service_type = htonl (GNUNET_DNS_SERVICE_TYPE_UDP);
335 data.ports = htons (69);
339 GNUNET_asprintf (&keyfile, "/tmp/test_dns_data_key");
340 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
341 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
342 GNUNET_free (keyfile);
343 GNUNET_assert (my_private_key != NULL);
345 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
347 data.expiration_time =
348 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
352 GNUNET_CRYPTO_rsa_sign (my_private_key, &data.purpose, &data.signature))
354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
357 GNUNET_CRYPTO_rsa_key_free (my_private_key);
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting with key %08x\n",
360 *((unsigned int *) &data.service_descriptor));
362 GNUNET_DHT_put (peer1dht, &data.service_descriptor, DEFAULT_PUT_REPLICATION,
363 GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_DNS, size,
365 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
366 GNUNET_TIME_UNIT_MINUTES, &put_finished, NULL);
371 * This function is called whenever a connection attempt is finished between two of
372 * the started peers (started with GNUNET_TESTING_daemons_start). The total
373 * number of times this function is called should equal the number returned
374 * from the GNUNET_TESTING_connect_topology call.
376 * The emsg variable is NULL on success (peers connected), and non-NULL on
377 * failure (peers failed to connect).
380 topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
381 const struct GNUNET_PeerIdentity *second, uint32_t distance,
382 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
383 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
384 struct GNUNET_TESTING_Daemon *first_daemon,
385 struct GNUNET_TESTING_Daemon *second_daemon,
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393 "connected peer %s to peer %s, distance %u\n",
394 first_daemon->shortname, second_daemon->shortname, distance);
400 failed_connections++;
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "Failed to connect peer %s to peer %s with error :\n%s\n",
403 first_daemon->shortname, second_daemon->shortname, emsg);
407 if (total_connections == expected_connections)
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
414 GNUNET_SCHEDULER_cancel (die_task);
416 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from test gets");
418 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
419 (GNUNET_TIME_UNIT_SECONDS, 2), &do_put, NULL);
421 else if (total_connections + failed_connections == expected_connections)
423 GNUNET_SCHEDULER_cancel (die_task);
425 GNUNET_SCHEDULER_add_now (&end_badly,
426 "from topology_callback (too many failed connections)");
432 * Callback which is called whenever a peer is started (as a result of the
433 * GNUNET_TESTING_daemons_start call.
435 * @param cls closure argument given to GNUNET_TESTING_daemons_start
436 * @param id the GNUNET_PeerIdentity of the started peer
437 * @param cfg the configuration for this specific peer (needed to connect
439 * @param d the handle to the daemon started
440 * @param emsg NULL if peer started, non-NULL on error
443 peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
444 const struct GNUNET_CONFIGURATION_Handle *cfg,
445 struct GNUNET_TESTING_Daemon *d, const char *emsg)
449 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
450 "Failed to start daemon with error: `%s'\n", emsg);
453 GNUNET_assert (id != NULL);
455 /* This is the first peer started */
456 if (peers_left == num_peers)
458 memcpy (&peer1id, id, sizeof (struct GNUNET_PeerIdentity)); /* Save the peer id */
459 peer1dht = GNUNET_DHT_connect (cfg, 100); /* Connect to the first peers DHT service */
460 if (peer1dht == NULL) /* If DHT connect failed */
462 GNUNET_SCHEDULER_cancel (die_task);
463 GNUNET_SCHEDULER_add_now (&end_badly, "Failed to get dht handle!\n");
466 else /* This is the second peer started */
468 memcpy (&peer2id, id, sizeof (struct GNUNET_PeerIdentity)); /* Same as for first peer... */
469 peer2dht = GNUNET_DHT_connect (cfg, 100);
470 if (peer2dht == NULL)
472 GNUNET_SCHEDULER_cancel (die_task);
473 GNUNET_SCHEDULER_add_now (&end_badly, "Failed to get dht handle!\n");
477 /* Decrement number of peers left to start */
480 if (peers_left == 0) /* Indicates all peers started */
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "All %d daemons started, now connecting peers!\n", num_peers);
486 expected_connections = -1;
487 if ((pg != NULL)) /* Sanity check */
489 /* Connect peers in a "straight line" topology, return the number of expected connections */
490 expected_connections =
491 GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_LINE,
492 GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
493 0.0, TIMEOUT, 12, NULL, NULL);
496 /* Cancel current timeout fail task */
497 GNUNET_SCHEDULER_cancel (die_task);
498 if (expected_connections == GNUNET_SYSERR) /* Some error happened */
500 GNUNET_SCHEDULER_add_now (&end_badly,
501 "from connect topology (bad return)");
503 /* Schedule timeout on failure task */
505 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
506 "from connect topology (timeout)");
512 run (void *cls, char *const *args, const char *cfgfile,
513 const struct GNUNET_CONFIGURATION_Handle *cfg)
516 /* Get path from configuration file */
518 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
525 /* Get number of peers to start from configuration (should be two) */
527 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
529 num_peers = DEFAULT_NUM_PEERS;
531 /* Set peers_left so we know when all peers started */
532 peers_left = num_peers;
534 /* Set up a task to end testing if peer start fails */
536 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
537 "didn't start all daemons in reasonable amount of time!!!");
539 /* Start num_peers peers, call peers_started_callback on peer start, topology_callback on peer connect */
540 /* Read the API documentation for other parameters! */
541 pg = GNUNET_TESTING_daemons_start (cfg, num_peers, 2, 2, TIMEOUT, NULL, NULL,
542 &peers_started_callback, NULL,
543 &topology_callback, NULL, NULL);
552 /* Arguments for GNUNET_PROGRAM_run */
553 char *const argv[] = { "test-dht-twopeer-put-get", /* Name to give running binary */
555 "test_dht_twopeer_data.conf", /* Config file to use */
561 struct GNUNET_GETOPT_CommandLineOption options[] = {
562 GNUNET_GETOPT_OPTION_END
564 /* Run the run function as a new program */
566 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
567 "test-dht-twopeer-put-get", "nohelp", options, &run,
569 if (ret != GNUNET_OK)
571 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
572 "`test-dht-twopeer': Failed with error code %d\n", ret);
578 main (int argc, char *argv[])
582 GNUNET_log_setup ("test-dht-twopeer",
591 * Need to remove base directory, subdirectories taken care
592 * of by the testing framework.
594 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
596 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
597 "Failed to remove testing directory %s\n", test_directory);
602 /* end of test_dht_twopeer_put_get.c */