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 gns/test_gns_dht_threepeer.c
22 * @brief tests dht lookup over 3 peers
25 * alice <----> bob <-----> dave
27 * alice queries for www.buddy.bob.gnunet
31 #include "gnunet_disk_lib.h"
32 #include "gnunet_testing_lib.h"
33 #include "gnunet_core_service.h"
34 #include "gnunet_dht_service.h"
35 #include "block_dns.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_namestore_service.h"
38 #include "gnunet_dnsparser_lib.h"
39 #include "gnunet_gns_service.h"
42 #define VERBOSE GNUNET_YES
44 /* Timeout for entire testcase */
45 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 180)
46 #define ZONE_PUT_WAIT_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
48 /* If number of peers not in config file, use this number */
49 #define DEFAULT_NUM_PEERS 2
51 #define TEST_DOMAIN "www.buddy.bob.gnunet"
52 #define TEST_IP "1.1.1.1"
53 #define TEST_DAVE_PSEU "hagbard"
54 #define TEST_NUM_PEERS 3
55 #define TEST_NUM_CON 3
60 * Directory to store temp data in, defined in config file
62 static char *test_directory;
65 * Variable used to store the number of connections we should wait for.
67 static unsigned int expected_connections;
70 * Variable used to keep track of how many peers aren't yet started.
72 static unsigned long long peers_left;
74 struct GNUNET_TESTING_Daemon *d1;
75 struct GNUNET_TESTING_Daemon *d2;
76 struct GNUNET_TESTING_Daemon *d3;
80 * Total number of peers to run, set based on config file.
82 static unsigned long long num_peers;
85 * Global used to count how many connections we have currently
86 * been notified about (how many times has topology_callback been called
89 static unsigned int total_connections;
92 * Global used to count how many failed connections we have
93 * been notified about (how many times has topology_callback
94 * been called with failure?)
96 static unsigned int failed_connections;
98 /* Task handle to use to schedule test failure */
99 GNUNET_SCHEDULER_TaskIdentifier die_task;
101 GNUNET_SCHEDULER_TaskIdentifier bob_task;
103 /* Global return value (0 for success, anything else for failure) */
106 int bob_online, alice_online, dave_online;
108 const struct GNUNET_CONFIGURATION_Handle *alice_cfg;
109 struct GNUNET_CONFIGURATION_Handle *cfg_bob;
110 struct GNUNET_CONFIGURATION_Handle *cfg_dave;
112 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
113 struct GNUNET_CRYPTO_ShortHashCode dave_hash;
114 struct GNUNET_TESTING_Daemon *alice_daemon;
115 struct GNUNET_TESTING_Daemon *bob_daemon;
116 struct GNUNET_TESTING_Daemon *dave_daemon;
118 struct GNUNET_TESTING_PeerGroup *pg;
119 struct GNUNET_GNS_Handle *gh;
122 * Function scheduled to be run on the successful completion of this
123 * testcase. Specifically, called when our get request completes.
126 finish_testing (void *cls, const char *emsg)
128 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Test finished! (ret=%d)\n", ok);
132 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
133 * down the peers without freeing memory associated with GET request.
136 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
138 die_task = GNUNET_SCHEDULER_NO_TASK;
139 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &finish_testing, NULL);
143 * Check if the get_handle is being used, if so stop the request. Either
144 * way, schedule the end_badly_cont function which actually shuts down the
148 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
150 die_task = GNUNET_SCHEDULER_NO_TASK;
151 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failing test with error: `%s'!\n",
153 die_task = GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
159 on_lookup_result(void *cls, uint32_t rd_count,
160 const struct GNUNET_NAMESTORE_RecordData *rd)
164 const char* typename;
168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
175 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
176 for (i=0; i<rd_count; i++)
178 typename = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
179 string_val = GNUNET_NAMESTORE_value_to_string(rd[i].record_type,
182 if (0 == strcmp(string_val, TEST_IP))
184 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
185 "%s correctly resolved to %s!\n", TEST_DOMAIN, string_val);
190 GNUNET_GNS_disconnect(gh);
191 GNUNET_SCHEDULER_cancel(die_task);
192 die_task = GNUNET_SCHEDULER_NO_TASK;
193 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &finish_testing, NULL);
194 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down!\n");
199 commence_testing(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
202 gh = GNUNET_GNS_connect(alice_cfg);
204 GNUNET_GNS_lookup(gh, TEST_DOMAIN, GNUNET_GNS_RECORD_A,
207 &on_lookup_result, TEST_DOMAIN);
209 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from lookup");
214 * This function is called whenever a connection attempt is finished between two of
215 * the started peers (started with GNUNET_TESTING_daemons_start). The total
216 * number of times this function is called should equal the number returned
217 * from the GNUNET_TESTING_connect_topology call.
219 * The emsg variable is NULL on success (peers connected), and non-NULL on
220 * failure (peers failed to connect).
223 daemon_connected (void *cls, const struct GNUNET_PeerIdentity *first,
224 const struct GNUNET_PeerIdentity *second, uint32_t distance,
225 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
226 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
227 struct GNUNET_TESTING_Daemon *first_daemon,
228 struct GNUNET_TESTING_Daemon *second_daemon,
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
236 "connected peer %s to peer %s, distance %u\n",
237 first_daemon->shortname, second_daemon->shortname, distance);
243 failed_connections++;
244 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
245 "Failed to connect peer %s to peer %s with error :\n%s\n",
246 first_daemon->shortname, second_daemon->shortname, emsg);
250 if (total_connections == expected_connections)
253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
254 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
257 GNUNET_SCHEDULER_cancel (die_task);
258 die_task = GNUNET_SCHEDULER_NO_TASK;
260 // GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from connect");
262 //commence_testing();
265 else if (total_connections + failed_connections == expected_connections)
267 GNUNET_SCHEDULER_cancel (die_task);
269 GNUNET_SCHEDULER_add_now (&end_badly,
270 "from topology_callback (too many failed connections)");
275 all_connected(void *cls, const char *emsg)
277 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
278 "Created all connections! Starting next phase of testing.\n");
279 GNUNET_SCHEDULER_add_delayed (ZONE_PUT_WAIT_TIME, &commence_testing, NULL);
283 disconnect_ns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
285 GNUNET_NAMESTORE_disconnect (cls);
289 cont_ns (void* cls, int32_t s, const char* emsg)
291 GNUNET_SCHEDULER_add_now (&disconnect_ns, cls);
295 daemon_started (void *cls, const struct GNUNET_PeerIdentity *id,
296 const struct GNUNET_CONFIGURATION_Handle *cfg,
297 struct GNUNET_TESTING_Daemon *d, const char *emsg)
299 struct GNUNET_NAMESTORE_Handle *ns;
301 struct GNUNET_CRYPTO_RsaPrivateKey *key;
302 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
304 struct GNUNET_NAMESTORE_RecordData rd;
306 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY | GNUNET_NAMESTORE_RF_NONE;
307 rd.expiration_time = UINT64_MAX;
309 if (NULL == dave_daemon)
311 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
315 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
321 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
323 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This is now dave\n");
324 ns = GNUNET_NAMESTORE_connect(cfg);
326 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
327 GNUNET_CRYPTO_short_hash(&pkey, sizeof(pkey), &dave_hash);
329 web = GNUNET_malloc(sizeof(struct in_addr));
330 GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP, web));
331 rd.data_size = sizeof(struct in_addr);
333 rd.record_type = GNUNET_GNS_RECORD_A;
334 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
336 GNUNET_NAMESTORE_record_create (ns, key, "www", &rd, NULL, NULL);
338 rd.data_size = strlen(TEST_DAVE_PSEU);
339 rd.data = TEST_DAVE_PSEU;
340 rd.record_type = GNUNET_GNS_RECORD_PSEU;
342 GNUNET_NAMESTORE_record_create (ns, key, "+", &rd, &cont_ns, ns);
344 GNUNET_CRYPTO_rsa_key_free(key);
345 GNUNET_free(keyfile);
352 if (NULL == bob_daemon)
354 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
358 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
364 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
366 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This is now bob\n");
367 ns = GNUNET_NAMESTORE_connect(cfg);
369 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
370 GNUNET_CRYPTO_short_hash(&pkey, sizeof(pkey), &bob_hash);
372 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
373 rd.data = &dave_hash;
374 rd.record_type = GNUNET_GNS_RECORD_PKEY;
375 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
377 GNUNET_NAMESTORE_record_create (ns, key, "buddy", &rd, &cont_ns, ns);
379 GNUNET_CRYPTO_rsa_key_free(key);
380 GNUNET_free(keyfile);
387 if (NULL == alice_daemon)
390 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
394 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
401 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
403 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This is now alice\n");
404 ns = GNUNET_NAMESTORE_connect(cfg);
406 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
408 rd.record_type = GNUNET_GNS_RECORD_PKEY;
409 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
411 GNUNET_NAMESTORE_record_create (ns, key, "bob", &rd, &cont_ns, ns);
413 GNUNET_CRYPTO_rsa_key_free(key);
414 GNUNET_free(keyfile);
416 GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_CLIQUE,
417 GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
421 &all_connected, NULL);
428 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This is a random guy\n");
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 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "starting\n");
447 /* Get number of peers to start from configuration (should be two) */
449 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
451 num_peers = DEFAULT_NUM_PEERS;
453 /* Set peers_left so we know when all peers started */
454 peers_left = num_peers;
460 pg = GNUNET_TESTING_daemons_start (cfg, TEST_NUM_PEERS, TEST_NUM_CON,
461 TEST_NUM_CON, TIMEOUT, NULL, NULL, &daemon_started, NULL,
462 &daemon_connected, NULL, NULL);
464 /* Set up a task to end testing if peer start fails */
466 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
467 "didn't start all daemons in reasonable amount of time!!!");
472 expected_connections = 2;
475 //d1 = GNUNET_TESTING_daemon_start(cfg_alice, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
476 // NULL, NULL, NULL, &alice_started, NULL);
488 /* Arguments for GNUNET_PROGRAM_run */
489 char *const argv[] = { "test-gns-twopeer", /* Name to give running binary */
491 "test_gns_dht_default.conf", /* Config file to use */
497 struct GNUNET_GETOPT_CommandLineOption options[] = {
498 GNUNET_GETOPT_OPTION_END
500 /* Run the run function as a new program */
502 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
503 "test-gns-threepeer", "nohelp", options, &run,
505 if (ret != GNUNET_OK)
507 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
508 "`test-gns-threepeer': Failed with error code %d\n", ret);
514 main (int argc, char *argv[])
518 GNUNET_log_setup ("test-gns-threepeer",
527 * Need to remove base directory, subdirectories taken care
528 * of by the testing framework.
533 /* end of test_gns_threepeer.c */