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_delegated_lookup.c
22 * @brief test for record lookup via DHT
26 #include "gnunet_testing_lib-new.h"
27 #include "gnunet_core_service.h"
28 #include "block_gns.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_namestore_service.h"
31 #include "../namestore/namestore.h"
32 #include "gnunet_dnsparser_lib.h"
33 #include "gnunet_dht_service.h"
34 #include "gnunet_gns_service.h"
37 #define VERBOSE GNUNET_YES
39 /* Timeout for entire testcase */
40 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20)
42 /* Timeout for entire testcase */
43 #define DHT_DELAY 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 /* test records to resolve */
49 #define TEST_DOMAIN "www.bob.gads"
50 #define TEST_IP "127.0.0.1"
51 #define TEST_RECORD_NAME "www"
53 #define TEST_AUTHORITY_NAME "bob"
55 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
57 #define KEYFILE_BOB "../namestore/zonefiles/HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"
61 /* Task handle to use to schedule test failure */
62 GNUNET_SCHEDULER_TaskIdentifier die_task;
63 GNUNET_SCHEDULER_TaskIdentifier wait_task;
65 /* Global return value (0 for success, anything else for failure) */
68 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
70 static struct GNUNET_GNS_Handle *gns_handle;
72 static struct GNUNET_DHT_Handle *dht_handle;
74 const struct GNUNET_CONFIGURATION_Handle *cfg;
76 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
77 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
78 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
79 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
83 * Check if the get_handle is being used, if so stop the request. Either
84 * way, schedule the end_badly_cont function which actually shuts down the
88 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
90 die_task = GNUNET_SCHEDULER_NO_TASK;
91 if (NULL != gns_handle)
93 GNUNET_GNS_disconnect (gns_handle);
97 if (NULL != dht_handle)
99 GNUNET_DHT_disconnect (dht_handle);
103 if (NULL != namestore_handle)
105 GNUNET_NAMESTORE_disconnect (namestore_handle);
106 namestore_handle = NULL;
109 GNUNET_SCHEDULER_shutdown ();
117 if (GNUNET_SCHEDULER_NO_TASK != wait_task)
119 GNUNET_SCHEDULER_cancel (wait_task);
120 wait_task = GNUNET_SCHEDULER_NO_TASK;
122 GNUNET_SCHEDULER_cancel (die_task);
123 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
126 static void shutdown_task (void *cls,
127 const struct GNUNET_SCHEDULER_TaskContext *tc)
129 GNUNET_GNS_disconnect(gns_handle);
130 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
131 GNUNET_SCHEDULER_shutdown ();
135 on_lookup_result(void *cls, uint32_t rd_count,
136 const struct GNUNET_NAMESTORE_RecordData *rd)
142 if (GNUNET_SCHEDULER_NO_TASK != die_task)
144 GNUNET_SCHEDULER_cancel (die_task);
145 die_task = GNUNET_SCHEDULER_NO_TASK;
148 GNUNET_NAMESTORE_disconnect (namestore_handle);
149 namestore_handle = NULL;
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153 "Lookup failed, rp_filtering?\n");
159 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
160 for (i=0; i<rd_count; i++)
162 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
163 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
165 memcpy(&a, rd[i].data, sizeof(a));
167 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
168 if (0 == strcmp(addr, TEST_IP))
170 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
171 "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
181 GNUNET_DHT_disconnect (dht_handle);
184 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
189 * Function scheduled to be run on the successful start of services
190 * tries to look up the dns record for TEST_DOMAIN
193 commence_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
195 gns_handle = GNUNET_GNS_connect(cfg);
197 if (NULL == gns_handle)
199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200 "Failed to connect to GNS!\n");
205 GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_A,
208 &on_lookup_result, TEST_DOMAIN);
213 put_dht(void *cls, int32_t success, const char *emsg)
215 struct GNSNameRecordBlock *nrb;
216 struct GNUNET_CRYPTO_ShortHashCode name_hash;
217 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
218 struct GNUNET_HashCode xor_hash;
219 struct GNUNET_HashCode name_hash_double;
220 struct GNUNET_HashCode zone_hash_double;
221 uint32_t rd_payload_length;
222 char* nrb_data = NULL;
223 struct GNUNET_CRYPTO_RsaSignature *sig;
224 struct GNUNET_NAMESTORE_RecordData rd;
226 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
228 rd.expiration_time = UINT64_MAX;
229 GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
230 rd.data_size = sizeof(struct in_addr);
232 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
233 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
235 sig = GNUNET_NAMESTORE_create_signature(bob_key, GNUNET_TIME_UNIT_FOREVER_ABS, TEST_RECORD_NAME,
238 GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&bob_pkey,
239 GNUNET_TIME_UNIT_FOREVER_ABS,
244 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
245 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
246 + sizeof(struct GNSNameRecordBlock));
247 nrb->signature = *sig;
248 nrb->public_key = bob_pkey;
249 nrb->rd_count = htonl(1);
250 memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
251 strcpy((char*)&nrb[1], TEST_RECORD_NAME);
252 nrb_data = (char*)&nrb[1];
253 nrb_data += strlen(TEST_RECORD_NAME) + 1;
255 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
260 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
267 GNUNET_CRYPTO_short_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
268 GNUNET_CRYPTO_short_hash(&bob_pkey,
269 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
271 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
272 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
273 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
275 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
276 strlen(TEST_RECORD_NAME) + 1;
277 GNUNET_DHT_put (dht_handle, &xor_hash,
279 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
280 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
283 GNUNET_TIME_UNIT_FOREVER_ABS,
284 DHT_OPERATION_TIMEOUT,
290 if (GNUNET_SCHEDULER_NO_TASK != die_task)
292 GNUNET_SCHEDULER_cancel (die_task);
293 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
295 wait_task = GNUNET_SCHEDULER_add_delayed(DHT_DELAY, &commence_testing, NULL);
300 const struct GNUNET_CONFIGURATION_Handle *ccfg,
301 struct GNUNET_TESTING_Peer *peer)
304 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
307 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
309 /* put records into namestore */
310 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
311 if (NULL == namestore_handle)
313 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
319 dht_handle = GNUNET_DHT_connect(cfg, 1);
320 if (NULL == dht_handle)
322 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
327 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
331 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
336 alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
337 bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
339 GNUNET_free(alice_keyfile);
341 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
342 GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
343 GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
345 struct GNUNET_NAMESTORE_RecordData rd;
346 rd.expiration_time = UINT64_MAX;
347 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
349 rd.record_type = GNUNET_GNS_RECORD_PKEY;
350 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
352 GNUNET_NAMESTORE_record_create (namestore_handle,
362 main (int argc, char *argv[])
366 GNUNET_log_setup ("test-gns-dht-delegated-lookup",
373 GNUNET_TESTING_peer_run ("test-gns-dht-delegated-lookup", "test_gns_simple_lookup.conf", &do_check, NULL);
378 /* end of test_gns_dht_delegated_lookup.c */