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.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"
36 /* Timeout for entire testcase */
37 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20)
39 /* Timeout for entire testcase */
40 #define DHT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
42 /* test records to resolve */
43 #define TEST_DOMAIN "www.bob.gads"
44 #define TEST_IP "127.0.0.1"
45 #define TEST_RECORD_NAME "www"
47 #define TEST_AUTHORITY_NAME "bob"
49 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
51 #define KEYFILE_BOB "../namestore/zonefiles/HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"
53 /* Task handle to use to schedule test failure */
54 static GNUNET_SCHEDULER_TaskIdentifier die_task;
55 static GNUNET_SCHEDULER_TaskIdentifier wait_task;
57 /* Global return value (0 for success, anything else for failure) */
60 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
62 static struct GNUNET_GNS_Handle *gns_handle;
64 static struct GNUNET_DHT_Handle *dht_handle;
66 static const struct GNUNET_CONFIGURATION_Handle *cfg;
68 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded alice_pkey;
69 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded bob_pkey;
70 static struct GNUNET_CRYPTO_EccPrivateKey *alice_key;
71 static struct GNUNET_CRYPTO_EccPrivateKey *bob_key;
75 * Check if the get_handle is being used, if so stop the request. Either
76 * way, schedule the end_badly_cont function which actually shuts down the
80 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 die_task = GNUNET_SCHEDULER_NO_TASK;
83 if (NULL != gns_handle)
85 GNUNET_GNS_disconnect (gns_handle);
89 if (NULL != dht_handle)
91 GNUNET_DHT_disconnect (dht_handle);
95 if (NULL != namestore_handle)
97 GNUNET_NAMESTORE_disconnect (namestore_handle);
98 namestore_handle = NULL;
101 GNUNET_SCHEDULER_shutdown ();
110 if (GNUNET_SCHEDULER_NO_TASK != wait_task)
112 GNUNET_SCHEDULER_cancel (wait_task);
113 wait_task = GNUNET_SCHEDULER_NO_TASK;
115 GNUNET_SCHEDULER_cancel (die_task);
116 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
121 shutdown_task (void *cls,
122 const struct GNUNET_SCHEDULER_TaskContext *tc)
124 GNUNET_GNS_disconnect(gns_handle);
125 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
126 GNUNET_SCHEDULER_shutdown ();
131 on_lookup_result(void *cls, uint32_t rd_count,
132 const struct GNUNET_NAMESTORE_RecordData *rd)
138 if (GNUNET_SCHEDULER_NO_TASK != die_task)
140 GNUNET_SCHEDULER_cancel (die_task);
141 die_task = GNUNET_SCHEDULER_NO_TASK;
144 GNUNET_NAMESTORE_disconnect (namestore_handle);
145 namestore_handle = NULL;
148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
149 "Lookup failed, rp_filtering?\n");
155 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
156 for (i=0; i<rd_count; i++)
158 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
159 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
161 memcpy(&a, rd[i].data, sizeof(a));
163 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
164 if (0 == strcmp(addr, TEST_IP))
166 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
167 "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
177 GNUNET_DHT_disconnect (dht_handle);
180 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
185 * Function scheduled to be run on the successful start of services
186 * tries to look up the dns record for TEST_DOMAIN
189 commence_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
191 gns_handle = GNUNET_GNS_connect(cfg);
193 if (NULL == gns_handle)
195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
196 "Failed to connect to GNS!\n");
201 GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_A,
204 &on_lookup_result, TEST_DOMAIN);
209 put_dht(void *cls, int32_t success, const char *emsg)
211 struct GNSNameRecordBlock *nrb;
212 struct GNUNET_CRYPTO_ShortHashCode name_hash;
213 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
214 struct GNUNET_HashCode xor_hash;
215 struct GNUNET_HashCode name_hash_double;
216 struct GNUNET_HashCode zone_hash_double;
217 uint32_t rd_payload_length;
218 char* nrb_data = NULL;
219 struct GNUNET_CRYPTO_EccSignature *sig;
220 struct GNUNET_NAMESTORE_RecordData rd;
222 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
224 rd.expiration_time = UINT64_MAX;
225 GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
226 rd.data_size = sizeof(struct in_addr);
228 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
229 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
231 sig = GNUNET_NAMESTORE_create_signature(bob_key, GNUNET_TIME_UNIT_FOREVER_ABS, TEST_RECORD_NAME,
234 GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&bob_pkey,
235 GNUNET_TIME_UNIT_FOREVER_ABS,
240 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
241 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
242 + sizeof(struct GNSNameRecordBlock));
243 nrb->signature = *sig;
244 nrb->public_key = bob_pkey;
245 nrb->rd_count = htonl(1);
246 memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
247 strcpy((char*)&nrb[1], TEST_RECORD_NAME);
248 nrb_data = (char*)&nrb[1];
249 nrb_data += strlen(TEST_RECORD_NAME) + 1;
251 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
256 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
263 GNUNET_CRYPTO_short_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
264 GNUNET_CRYPTO_short_hash(&bob_pkey,
265 sizeof(struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
267 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
268 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
269 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
271 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
272 strlen(TEST_RECORD_NAME) + 1;
273 GNUNET_DHT_put (dht_handle, &xor_hash,
275 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
276 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
279 GNUNET_TIME_UNIT_FOREVER_ABS,
280 DHT_OPERATION_TIMEOUT,
286 if (GNUNET_SCHEDULER_NO_TASK != die_task)
288 GNUNET_SCHEDULER_cancel (die_task);
289 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
291 wait_task = GNUNET_SCHEDULER_add_delayed(DHT_DELAY, &commence_testing, NULL);
297 const struct GNUNET_CONFIGURATION_Handle *ccfg,
298 struct GNUNET_TESTING_Peer *peer)
301 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
304 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
306 /* put records into namestore */
307 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
308 if (NULL == namestore_handle)
310 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
316 dht_handle = GNUNET_DHT_connect(cfg, 1);
317 if (NULL == dht_handle)
319 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
324 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
328 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
333 alice_key = GNUNET_CRYPTO_ecc_key_create_from_file (alice_keyfile);
334 bob_key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE_BOB);
336 GNUNET_free(alice_keyfile);
338 GNUNET_CRYPTO_ecc_key_get_public (alice_key, &alice_pkey);
339 GNUNET_CRYPTO_ecc_key_get_public (bob_key, &bob_pkey);
340 GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
342 struct GNUNET_NAMESTORE_RecordData rd;
343 rd.expiration_time = UINT64_MAX;
344 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
346 rd.record_type = GNUNET_GNS_RECORD_PKEY;
347 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
349 GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
359 main (int argc, char *argv[])
363 GNUNET_log_setup ("test-gns-dht-delegated-lookup",
366 GNUNET_TESTING_peer_run ("test-gns-dht-delegated-lookup", "test_gns_simple_lookup.conf", &do_check, NULL);
371 /* end of test_gns_dht_delegated_lookup.c */