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.gnunet"
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);
128 on_lookup_result(void *cls, uint32_t rd_count,
129 const struct GNUNET_NAMESTORE_RecordData *rd)
135 if (GNUNET_SCHEDULER_NO_TASK != die_task)
137 GNUNET_SCHEDULER_cancel (die_task);
138 die_task = GNUNET_SCHEDULER_NO_TASK;
141 GNUNET_NAMESTORE_disconnect (namestore_handle);
142 namestore_handle = NULL;
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146 "Lookup failed, rp_filtering?\n");
152 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
153 for (i=0; i<rd_count; i++)
155 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
156 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
158 memcpy(&a, rd[i].data, sizeof(a));
160 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
161 if (0 == strcmp(addr, TEST_IP))
163 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
164 "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
174 GNUNET_GNS_disconnect(gns_handle);
176 GNUNET_DHT_disconnect (dht_handle);
178 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
179 GNUNET_SCHEDULER_shutdown ();
184 * Function scheduled to be run on the successful start of services
185 * tries to look up the dns record for TEST_DOMAIN
188 commence_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 gns_handle = GNUNET_GNS_connect(cfg);
192 if (NULL == gns_handle)
194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
195 "Failed to connect to GNS!\n");
200 GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_A,
203 &on_lookup_result, TEST_DOMAIN);
208 put_dht(void *cls, int32_t success, const char *emsg)
210 struct GNSNameRecordBlock *nrb;
211 struct GNUNET_CRYPTO_ShortHashCode name_hash;
212 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
213 struct GNUNET_HashCode xor_hash;
214 struct GNUNET_HashCode name_hash_double;
215 struct GNUNET_HashCode zone_hash_double;
216 uint32_t rd_payload_length;
217 char* nrb_data = NULL;
218 struct GNUNET_CRYPTO_RsaSignature *sig;
219 struct GNUNET_NAMESTORE_RecordData rd;
221 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
223 rd.expiration_time = UINT64_MAX;
224 GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
225 rd.data_size = sizeof(struct in_addr);
227 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
228 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
230 sig = GNUNET_NAMESTORE_create_signature(bob_key, GNUNET_TIME_UNIT_FOREVER_ABS, TEST_RECORD_NAME,
233 GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&bob_pkey,
234 GNUNET_TIME_UNIT_FOREVER_ABS,
239 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
240 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
241 + sizeof(struct GNSNameRecordBlock));
242 nrb->signature = *sig;
243 nrb->public_key = bob_pkey;
244 nrb->rd_count = htonl(1);
245 memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
246 strcpy((char*)&nrb[1], TEST_RECORD_NAME);
247 nrb_data = (char*)&nrb[1];
248 nrb_data += strlen(TEST_RECORD_NAME) + 1;
250 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
255 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
262 GNUNET_CRYPTO_short_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
263 GNUNET_CRYPTO_short_hash(&bob_pkey,
264 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
266 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
267 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
268 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
270 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
271 strlen(TEST_RECORD_NAME) + 1;
272 GNUNET_DHT_put (dht_handle, &xor_hash,
275 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
278 GNUNET_TIME_UNIT_FOREVER_ABS,
279 DHT_OPERATION_TIMEOUT,
285 if (GNUNET_SCHEDULER_NO_TASK != die_task)
287 GNUNET_SCHEDULER_cancel (die_task);
288 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
290 wait_task = GNUNET_SCHEDULER_add_delayed(DHT_DELAY, &commence_testing, NULL);
295 const struct GNUNET_CONFIGURATION_Handle *ccfg,
296 struct GNUNET_TESTING_Peer *peer)
299 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
302 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
304 /* put records into namestore */
305 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
306 if (NULL == namestore_handle)
308 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
314 dht_handle = GNUNET_DHT_connect(cfg, 1);
315 if (NULL == dht_handle)
317 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
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");
331 alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
332 bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
334 GNUNET_free(alice_keyfile);
336 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
337 GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
338 GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
340 struct GNUNET_NAMESTORE_RecordData rd;
341 rd.expiration_time = UINT64_MAX;
342 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
344 rd.record_type = GNUNET_GNS_RECORD_PKEY;
345 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
347 GNUNET_NAMESTORE_record_create (namestore_handle,
357 main (int argc, char *argv[])
361 GNUNET_log_setup ("test-gns-dht-delegated-lookup",
368 GNUNET_TESTING_peer_run ("test-gns-dht-delegated-lookup", "test_gns_simple_lookup.conf", &do_check, NULL);
373 /* end of test_gns_dht_delegated_lookup.c */