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.gads
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, 10)
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.gads"
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_SECONDS, 60)
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 struct GNUNET_TESTBED_Peer **cpeers;
71 struct GNUNET_GNS_Handle *gh;
72 struct GNUNET_GNS_LookupRequest *lookup_handle;
74 struct GNUNET_TESTBED_Operation *get_cfg_ops[3];
75 struct GNUNET_TESTBED_Operation *connect_ops[3];
76 struct GNUNET_CONFIGURATION_Handle *cfg_handles[3];
77 struct GNUNET_NAMESTORE_Handle *nh[3];
79 static int dave_is_setup;
80 static int bob_is_setup;
81 static int alice_is_setup;
84 * Check if the get_handle is being used, if so stop the request. Either
85 * way, schedule the end_badly_cont function which actually shuts down the
89 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
91 die_task = GNUNET_SCHEDULER_NO_TASK;
94 if (GNUNET_SCHEDULER_NO_TASK != wait_task)
96 GNUNET_SCHEDULER_cancel (wait_task);
97 wait_task = GNUNET_SCHEDULER_NO_TASK;
100 for (c = 0; c < 3; c++)
104 GNUNET_NAMESTORE_disconnect(nh[c]);
108 if (NULL != get_cfg_ops[c])
110 GNUNET_TESTBED_operation_cancel(get_cfg_ops[c]);
111 get_cfg_ops[c] = NULL;
113 if (NULL != connect_ops[c])
115 GNUNET_TESTBED_operation_cancel(connect_ops[c]);
116 connect_ops[c] = NULL;
118 if (NULL != cfg_handles[c])
120 GNUNET_CONFIGURATION_destroy (cfg_handles[c]);
121 cfg_handles[c] = NULL;
125 if (NULL != lookup_handle)
127 GNUNET_GNS_cancel_lookup_request (lookup_handle);
128 lookup_handle = NULL;
132 GNUNET_GNS_disconnect(gh);
136 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Test failed \n");
137 GNUNET_SCHEDULER_shutdown ();
142 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finished\n");
146 if (GNUNET_SCHEDULER_NO_TASK != die_task)
148 GNUNET_SCHEDULER_cancel (die_task);
149 die_task = GNUNET_SCHEDULER_NO_TASK;
152 for (c = 0; c < 3; c++)
156 GNUNET_NAMESTORE_disconnect(nh[c]);
159 if (NULL != cfg_handles[c])
161 GNUNET_CONFIGURATION_destroy (cfg_handles[c]);
162 cfg_handles[c] = NULL;
168 GNUNET_GNS_disconnect(gh);
173 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Test ended successful\n");
175 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Test failed\n");
176 GNUNET_SCHEDULER_shutdown ();
182 GNUNET_SCHEDULER_add_now (&end, NULL);
187 disconnect_ns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 GNUNET_NAMESTORE_disconnect (cls);
201 cont_ns (void* cls, int32_t s, const char* emsg)
203 GNUNET_SCHEDULER_add_now (&disconnect_ns, cls);
207 on_lookup_result(void *cls, uint32_t rd_count,
208 const struct GNUNET_NAMESTORE_RecordData *rd)
215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
222 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
223 for (i=0; i<rd_count; i++)
225 string_val = GNUNET_NAMESTORE_value_to_string(rd[i].record_type,
228 if (0 == strcmp(string_val, TEST_IP))
230 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
231 "%s correctly resolved to %s!\n", TEST_DOMAIN, string_val);
234 GNUNET_free (string_val);
241 commence_testing(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
245 if ((ZONE_PUT_WAIT_TIME.rel_value / 1000) == wait)
247 fprintf (stderr, "\n");
248 wait_task = GNUNET_SCHEDULER_NO_TASK;
249 lookup_handle = GNUNET_GNS_lookup(gh, TEST_DOMAIN, GNUNET_GNS_RECORD_A,
252 &on_lookup_result, TEST_DOMAIN);
253 if (GNUNET_SCHEDULER_NO_TASK != die_task)
254 GNUNET_SCHEDULER_cancel(die_task);
255 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from lookup");
259 fprintf (stderr, ".");
260 wait_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &commence_testing, NULL);
267 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Created all connections! Waiting for PUTs\n");
268 wait_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &commence_testing, NULL);
272 static void connect_peers ()
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers started\n");
280 connect_ops[0] = GNUNET_TESTBED_overlay_connect (NULL, NULL, NULL,
284 connect_ops[1] = GNUNET_TESTBED_overlay_connect (NULL, NULL, NULL,
288 connect_ops[2] = GNUNET_TESTBED_overlay_connect (NULL, NULL, NULL,
295 setup_dave (const struct GNUNET_CONFIGURATION_Handle * cfg)
298 struct GNUNET_CRYPTO_RsaPrivateKey *key;
299 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
301 struct GNUNET_NAMESTORE_RecordData rd;
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up dave\n");
304 cfg_handles[0] = GNUNET_CONFIGURATION_dup (cfg);
305 GNUNET_assert (NULL != cfg_handles[0]);
306 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
310 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
311 return GNUNET_SYSERR;
314 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
318 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
319 GNUNET_free (keyfile);
320 return GNUNET_SYSERR;
322 nh[0] = GNUNET_NAMESTORE_connect (cfg_handles[0]);
325 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
326 GNUNET_CRYPTO_rsa_key_free (key);
327 GNUNET_free (keyfile);
328 return GNUNET_SYSERR;
331 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
332 GNUNET_CRYPTO_short_hash(&pkey, sizeof(pkey), &dave_hash);
334 rd.expiration_time = UINT64_MAX;
336 web = GNUNET_malloc(sizeof(struct in_addr));
337 GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP, web));
338 rd.data_size = sizeof(struct in_addr);
340 rd.record_type = GNUNET_GNS_RECORD_A;
341 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
343 GNUNET_NAMESTORE_record_create (nh[0], key, "www", &rd, NULL, NULL);
345 rd.data_size = strlen(TEST_DAVE_PSEU);
346 rd.data = TEST_DAVE_PSEU;
347 rd.record_type = GNUNET_GNS_RECORD_PSEU;
350 GNUNET_NAMESTORE_record_create (nh[0], key, "+", &rd, &cont_ns, nh[0]);
352 GNUNET_CRYPTO_rsa_key_free(key);
353 GNUNET_free(keyfile);
355 dave_is_setup = GNUNET_YES;
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up dave done\n");
361 setup_bob (const struct GNUNET_CONFIGURATION_Handle * cfg)
364 struct GNUNET_CRYPTO_RsaPrivateKey *key;
365 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
366 struct GNUNET_NAMESTORE_RecordData rd;
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up bob\n");
369 cfg_handles[1] = GNUNET_CONFIGURATION_dup (cfg);
370 GNUNET_assert (NULL != cfg_handles[1]);
371 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
375 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
376 return GNUNET_SYSERR;
379 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
383 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
384 GNUNET_free (keyfile);
385 return GNUNET_SYSERR;
388 nh[1] = GNUNET_NAMESTORE_connect (cfg_handles[1]);
391 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
392 GNUNET_CRYPTO_rsa_key_free (key);
393 GNUNET_free (keyfile);
394 return GNUNET_SYSERR;
397 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
398 GNUNET_CRYPTO_short_hash(&pkey, sizeof(pkey), &bob_hash);
400 rd.expiration_time = UINT64_MAX;
401 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
402 rd.data = &dave_hash;
403 rd.record_type = GNUNET_GNS_RECORD_PKEY;
404 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
406 GNUNET_NAMESTORE_record_create (nh[1], key, "buddy", &rd, &cont_ns, nh[1]);
408 GNUNET_CRYPTO_rsa_key_free(key);
409 GNUNET_free(keyfile);
410 bob_is_setup = GNUNET_YES;
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up bob done\n");
416 setup_alice (const struct GNUNET_CONFIGURATION_Handle * cfg)
419 struct GNUNET_CRYPTO_RsaPrivateKey *key;
420 struct GNUNET_NAMESTORE_RecordData rd;
422 cfg_handles[2] = GNUNET_CONFIGURATION_dup (cfg);
423 GNUNET_assert (NULL != cfg);
424 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
428 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
429 return GNUNET_SYSERR;
432 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
436 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
437 GNUNET_free (keyfile);
438 return GNUNET_SYSERR;
441 nh[2] = GNUNET_NAMESTORE_connect (cfg_handles[2]);
444 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
445 GNUNET_CRYPTO_rsa_key_free (key);
446 GNUNET_free (keyfile);
447 return GNUNET_SYSERR;
450 rd.expiration_time = UINT64_MAX;
451 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
453 rd.record_type = GNUNET_GNS_RECORD_PKEY;
454 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
456 GNUNET_NAMESTORE_record_create (nh[2], key, "bob", &rd, &cont_ns, nh[2]);
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up alice gns\n");
459 gh = GNUNET_GNS_connect (cfg_handles[2]);
462 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to gns\n");
463 GNUNET_CRYPTO_rsa_key_free (key);
464 GNUNET_free (keyfile);
465 return GNUNET_SYSERR;
468 GNUNET_CRYPTO_rsa_key_free (key);
469 GNUNET_free (keyfile);
470 alice_is_setup = GNUNET_YES;
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up alice done\n");
478 if (GNUNET_SCHEDULER_NO_TASK != die_task)
479 GNUNET_SCHEDULER_cancel (die_task);
480 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
485 * Callback to be called when the requested peer information is available
487 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
488 * @param op the operation this callback corresponds to
489 * @param pinfo the result; will be NULL if the operation has failed
490 * @param emsg error message if the operation has failed; will be NULL if the
491 * operation is successfull
494 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
495 const struct GNUNET_TESTBED_PeerInformation *pinfo,
499 GNUNET_assert (GNUNET_TESTBED_PIT_CONFIGURATION == pinfo->pit);
500 if (GNUNET_NO == dave_is_setup)
501 res = setup_dave (pinfo->result.cfg);
502 else if (GNUNET_NO == bob_is_setup)
503 res = setup_bob (pinfo->result.cfg);
505 res = setup_alice (pinfo->result.cfg);
507 if (get_cfg_ops[0] == op)
508 get_cfg_ops[0] = NULL;
509 else if (get_cfg_ops[1] == op)
510 get_cfg_ops[1] = NULL;
512 get_cfg_ops[2] = NULL;
513 GNUNET_TESTBED_operation_done (op);
515 if (GNUNET_SYSERR == res)
517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to setup peer \n");
522 /*if (get_cfg_ops[0] == op)
524 GNUNET_assert (GNUNET_TESTBED_PIT_CONFIGURATION == pinfo->pit);
525 res = setup_dave (pinfo->result.cfg);
526 GNUNET_TESTBED_operation_done (get_cfg_ops[0]);
527 get_cfg_ops[0] = NULL;
528 if (GNUNET_SYSERR == res)
530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to setup dave \n");
538 else if (get_cfg_ops[1] == op)
540 GNUNET_assert (GNUNET_TESTBED_PIT_CONFIGURATION == pinfo->pit);
541 res = setup_bob (pinfo->result.cfg);
542 GNUNET_TESTBED_operation_done (get_cfg_ops[1]);
543 get_cfg_ops[1] = NULL;
544 if (GNUNET_SYSERR == res)
546 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to setup dave \n");
554 else if (get_cfg_ops[2] == op)
556 GNUNET_assert (GNUNET_TESTBED_PIT_CONFIGURATION == pinfo->pit);
557 res = setup_alice (pinfo->result.cfg);
558 GNUNET_TESTBED_operation_done (get_cfg_ops[2]);
559 get_cfg_ops[2] = NULL;
560 if (GNUNET_SYSERR == res)
562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to setup dave \n");
573 void testbed_master (void *cls,
574 unsigned int num_peers,
575 struct GNUNET_TESTBED_Peer **peers)
577 GNUNET_assert (NULL != peers);
581 GNUNET_assert (NULL != peers[0]);
582 get_cfg_ops[0] = GNUNET_TESTBED_peer_get_information (peers[0],
583 GNUNET_TESTBED_PIT_CONFIGURATION,
587 GNUNET_assert (NULL != peers[1]);
588 get_cfg_ops[1] = GNUNET_TESTBED_peer_get_information (peers[1],
589 GNUNET_TESTBED_PIT_CONFIGURATION,
590 &peerinfo_cb, NULL );
593 GNUNET_assert (NULL != peers[2]);
594 get_cfg_ops[2] = GNUNET_TESTBED_peer_get_information (peers[2],
595 GNUNET_TESTBED_PIT_CONFIGURATION,
600 void testbed_controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
602 static int connections = 0;
606 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
607 /* This part will still be called when
608 GNUNET_TESTBED_peer_get_information() succeeds. However, the code is
609 now more relevant in operation completion callback */
611 case GNUNET_TESTBED_ET_CONNECT:
613 if (connections == 3)
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers connected\n");
616 GNUNET_TESTBED_operation_done (connect_ops[0]);
617 connect_ops[0] = NULL;
618 GNUNET_TESTBED_operation_done (connect_ops[1]);
619 connect_ops[1] = NULL;
620 GNUNET_TESTBED_operation_done (connect_ops[2]);
621 connect_ops[2] = NULL;
632 main (int argc, char *argv[])
637 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
638 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
639 GNUNET_TESTBED_test_run ("test_gns_dht_three_peers", "test_gns_dht_default.conf",
641 &testbed_controller_cb, NULL,
642 &testbed_master, NULL);
643 if (GNUNET_SYSERR == ok)
648 /* end of test_gns_dht_three_peers.c */