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_common.h"
32 #include "gnunet_disk_lib.h"
33 #include "gnunet_testing_lib-new.h"
34 #include "gnunet_testbed_service.h"
35 #include "gnunet_core_service.h"
36 #include "gnunet_dht_service.h"
37 #include "block_dns.h"
38 #include "gnunet_signatures.h"
39 #include "gnunet_namestore_service.h"
40 #include "gnunet_dnsparser_lib.h"
41 #include "gnunet_gns_service.h"
43 #define ZONE_PUT_WAIT_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
45 /* If number of peers not in config file, use this number */
46 #define DEFAULT_NUM_PEERS 2
48 #define TEST_DOMAIN "www.buddy.bob.gnunet"
49 #define TEST_IP "1.1.1.1"
50 #define TEST_DAVE_PSEU "hagbard"
51 #define TEST_NUM_PEERS 3
52 #define TEST_NUM_CON 3
55 /* Timeout for entire testcase */
56 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
58 /* Global return value (0 for success, anything else for failure) */
61 /* Task handle to use to schedule test failure */
62 GNUNET_SCHEDULER_TaskIdentifier die_task;
63 GNUNET_SCHEDULER_TaskIdentifier wait_task;
65 struct GNUNET_CRYPTO_ShortHashCode dave_hash;
67 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
69 const struct GNUNET_CONFIGURATION_Handle *alice_cfg;
71 struct GNUNET_TESTBED_Peer **cpeers;
73 struct GNUNET_GNS_Handle *gh;
75 struct GNUNET_TESTBED_Operation *get_cfg_ops[3];
76 struct GNUNET_TESTBED_Operation *connect_ops[3];
79 * Check if the get_handle is being used, if so stop the request. Either
80 * way, schedule the end_badly_cont function which actually shuts down the
84 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
86 die_task = GNUNET_SCHEDULER_NO_TASK;
89 if (GNUNET_SCHEDULER_NO_TASK != wait_task)
91 GNUNET_SCHEDULER_cancel (wait_task);
92 wait_task = GNUNET_SCHEDULER_NO_TASK;
95 for (c = 0; c < 3; c++)
97 if (NULL != get_cfg_ops[c])
99 GNUNET_TESTBED_operation_cancel(get_cfg_ops[c]);
100 get_cfg_ops[c] = NULL;
102 if (NULL != connect_ops[c])
104 GNUNET_TESTBED_operation_cancel(connect_ops[c]);
105 connect_ops[c] = NULL;
111 GNUNET_GNS_disconnect (gh);
115 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Test failed \n");
117 GNUNET_SCHEDULER_shutdown ();
122 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
124 if (GNUNET_SCHEDULER_NO_TASK != die_task)
126 GNUNET_SCHEDULER_cancel (die_task);
127 die_task = GNUNET_SCHEDULER_NO_TASK;
130 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Test successful \n");
132 GNUNET_SCHEDULER_shutdown ();
139 GNUNET_SCHEDULER_add_now (&end, NULL);
144 disconnect_ns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
146 GNUNET_NAMESTORE_disconnect (cls);
151 cont_ns (void* cls, int32_t s, const char* emsg)
153 GNUNET_SCHEDULER_add_now (&disconnect_ns, cls);
157 on_lookup_result(void *cls, uint32_t rd_count,
158 const struct GNUNET_NAMESTORE_RecordData *rd)
165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
173 for (i=0; i<rd_count; i++)
175 string_val = GNUNET_NAMESTORE_value_to_string(rd[i].record_type,
178 if (0 == strcmp(string_val, TEST_IP))
180 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
181 "%s correctly resolved to %s!\n", TEST_DOMAIN, string_val);
186 GNUNET_GNS_disconnect(gh);
192 commence_testing(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
196 if ((ZONE_PUT_WAIT_TIME.rel_value / 1000) == wait)
198 fprintf (stderr, "\n");
199 wait_task = GNUNET_SCHEDULER_NO_TASK;
200 gh = GNUNET_GNS_connect(alice_cfg);
202 GNUNET_GNS_lookup(gh, TEST_DOMAIN, GNUNET_GNS_RECORD_A,
205 &on_lookup_result, TEST_DOMAIN);
206 if (GNUNET_SCHEDULER_NO_TASK != die_task)
207 GNUNET_SCHEDULER_cancel(die_task);
208 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from lookup");
212 fprintf (stderr, ".");
213 wait_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &commence_testing, NULL);
220 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Created all connections! Waiting for PUTs\n");
221 wait_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &commence_testing, NULL);
225 static void connect_peers ()
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers started\n");
234 connect_ops[0] = GNUNET_TESTBED_overlay_connect (NULL,
238 connect_ops[1] = GNUNET_TESTBED_overlay_connect (NULL,
242 connect_ops[2] = GNUNET_TESTBED_overlay_connect (NULL,
249 setup_dave (const struct GNUNET_CONFIGURATION_Handle * cfg)
251 struct GNUNET_NAMESTORE_Handle *ns;
253 struct GNUNET_CRYPTO_RsaPrivateKey *key;
254 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
256 struct GNUNET_NAMESTORE_RecordData rd;
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up dave\n");
259 GNUNET_assert (NULL != cfg);
260 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
264 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
265 return GNUNET_SYSERR;
268 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
272 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
273 GNUNET_free (keyfile);
274 return GNUNET_SYSERR;
277 ns = GNUNET_NAMESTORE_connect (cfg);
280 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
281 GNUNET_CRYPTO_rsa_key_free (key);
282 GNUNET_free (keyfile);
283 return GNUNET_SYSERR;
286 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
287 GNUNET_CRYPTO_short_hash(&pkey, sizeof(pkey), &dave_hash);
289 web = GNUNET_malloc(sizeof(struct in_addr));
290 GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP, web));
291 rd.data_size = sizeof(struct in_addr);
293 rd.record_type = GNUNET_GNS_RECORD_A;
294 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
296 GNUNET_NAMESTORE_record_create (ns, key, "www", &rd, NULL, NULL);
298 rd.data_size = strlen(TEST_DAVE_PSEU);
299 rd.data = TEST_DAVE_PSEU;
300 rd.record_type = GNUNET_GNS_RECORD_PSEU;
302 GNUNET_NAMESTORE_record_create (ns, key, "+", &rd, &cont_ns, ns);
304 GNUNET_CRYPTO_rsa_key_free(key);
305 GNUNET_free(keyfile);
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up dave done\n");
312 setup_bob (const struct GNUNET_CONFIGURATION_Handle * cfg)
314 struct GNUNET_NAMESTORE_Handle *ns;
316 struct GNUNET_CRYPTO_RsaPrivateKey *key;
317 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
318 struct GNUNET_NAMESTORE_RecordData rd;
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up bob\n");
321 GNUNET_assert (NULL != cfg);
322 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
326 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
327 return GNUNET_SYSERR;
330 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
334 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
335 GNUNET_free (keyfile);
336 return GNUNET_SYSERR;
339 ns = GNUNET_NAMESTORE_connect (cfg);
342 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
343 GNUNET_CRYPTO_rsa_key_free (key);
344 GNUNET_free (keyfile);
345 return GNUNET_SYSERR;
348 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
349 GNUNET_CRYPTO_short_hash(&pkey, sizeof(pkey), &bob_hash);
351 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
352 rd.data = &dave_hash;
353 rd.record_type = GNUNET_GNS_RECORD_PKEY;
354 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
356 GNUNET_NAMESTORE_record_create (ns, key, "buddy", &rd, &cont_ns, ns);
358 GNUNET_CRYPTO_rsa_key_free(key);
359 GNUNET_free(keyfile);
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up bob done\n");
365 setup_alice (const struct GNUNET_CONFIGURATION_Handle * cfg)
367 struct GNUNET_NAMESTORE_Handle *ns;
369 struct GNUNET_CRYPTO_RsaPrivateKey *key;
370 struct GNUNET_NAMESTORE_RecordData rd;
373 GNUNET_assert (NULL != cfg);
375 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
379 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
380 return GNUNET_SYSERR;
383 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
387 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
388 GNUNET_free (keyfile);
389 return GNUNET_SYSERR;
392 ns = GNUNET_NAMESTORE_connect (cfg);
395 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
396 GNUNET_CRYPTO_rsa_key_free (key);
397 GNUNET_free (keyfile);
398 return GNUNET_SYSERR;
401 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
403 rd.record_type = GNUNET_GNS_RECORD_PKEY;
404 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
406 GNUNET_NAMESTORE_record_create (ns, key, "bob", &rd, &cont_ns, ns);
408 GNUNET_CRYPTO_rsa_key_free(key);
409 GNUNET_free(keyfile);
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up alice done\n");
417 if (GNUNET_SCHEDULER_NO_TASK != die_task)
418 GNUNET_SCHEDULER_cancel (die_task);
419 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
422 void testbed_master (void *cls,
423 unsigned int num_peers,
424 struct GNUNET_TESTBED_Peer **peers)
426 GNUNET_assert (NULL != peers);
430 GNUNET_assert (NULL != peers[0]);
431 get_cfg_ops[0] = GNUNET_TESTBED_peer_get_information (peers[0], GNUNET_TESTBED_PIT_CONFIGURATION);
434 GNUNET_assert (NULL != peers[1]);
435 get_cfg_ops[1] = GNUNET_TESTBED_peer_get_information (peers[1], GNUNET_TESTBED_PIT_CONFIGURATION);
438 GNUNET_assert (NULL != peers[2]);
439 get_cfg_ops[2] = GNUNET_TESTBED_peer_get_information (peers[2], GNUNET_TESTBED_PIT_CONFIGURATION);
443 void testbed_controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
445 static int connections = 0;
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback of type: %u %p \n", event->type, cls);
451 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
452 if (get_cfg_ops[0] == event->details.operation_finished.operation)
454 res = setup_dave (event->details.operation_finished.op_result.cfg);
455 get_cfg_ops[0] = NULL;
456 if (GNUNET_SYSERR == res)
458 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to setup dave \n");
466 else if (get_cfg_ops[1] == event->details.operation_finished.operation)
468 res = setup_bob (event->details.operation_finished.op_result.cfg);
469 get_cfg_ops[1] = NULL;
470 if (GNUNET_SYSERR == res)
472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to setup dave \n");
480 else if (get_cfg_ops[2] == event->details.operation_finished.operation)
482 res = setup_alice (event->details.operation_finished.op_result.cfg);
483 get_cfg_ops[2] = NULL;
484 if (GNUNET_SYSERR == res)
486 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to setup dave \n");
495 case GNUNET_TESTBED_ET_CONNECT:
497 if (connections == 3)
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers connected\n");
500 connect_ops[0] = NULL;
501 connect_ops[1] = NULL;
502 connect_ops[2] = NULL;
513 main (int argc, char *argv[])
519 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
520 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
521 GNUNET_TESTBED_test_run ("test_gns_dht_three_peers", "test_gns_dht_default.conf",
523 &testbed_controller_cb, NULL,
524 &testbed_master, NULL);
525 if (GNUNET_SYSERR == ok)
530 /* end of test_gns_dht_three_peers.c */