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_cname_lookup.c
22 * @brief base testcase for testing a local GNS record lookup
26 #include "gnunet_testing_lib-new.h"
27 #include "gnunet_core_service.h"
28 #include "block_dns.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_namestore_service.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_gns_service.h"
35 #define VERBOSE GNUNET_YES
37 /* Timeout for entire testcase */
38 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20)
40 /* If number of peers not in config file, use this number */
41 #define DEFAULT_NUM_PEERS 2
43 /* test records to resolve */
44 #define TEST_DOMAIN_PLUS "www.gads"
45 #define TEST_DOMAIN_ZKEY "www2.gads"
46 #define TEST_DOMAIN_DNS "www3.gads"
47 #define TEST_IP_PLUS "127.0.0.1"
48 #define TEST_IP_ZKEY "127.0.0.2"
49 #define TEST_IP_DNS "131.159.74.67"
50 #define TEST_RECORD_CNAME_SERVER "server.gads"
51 #define TEST_RECORD_CNAME_PLUS "server.+"
52 #define TEST_RECORD_CNAME_ZKEY "www.188JSUMKEF25GVU8TTV0PBNNN8JVCPUEDFV1UHJJU884JD25V0T0.zkey"
53 #define TEST_RECORD_CNAME_DNS "gnunet.org"
54 #define TEST_RECORD_NAME_SERVER "server"
55 #define TEST_RECORD_NAME_PLUS "www"
56 #define TEST_RECORD_NAME_ZKEY "www2"
57 #define TEST_RECORD_NAME_DNS "www3"
59 #define KEYFILE_BOB "zonefiles/188JSUMKEF25GVU8TTV0PBNNN8JVCPUEDFV1UHJJU884JD25V0T0.zkey"
63 /* Task handle to use to schedule test failure */
64 GNUNET_SCHEDULER_TaskIdentifier die_task;
66 /* Global return value (0 for success, anything else for failure) */
69 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
71 static struct GNUNET_GNS_Handle *gns_handle;
73 const struct GNUNET_CONFIGURATION_Handle *cfg;
76 * Check if the get_handle is being used, if so stop the request. Either
77 * way, schedule the end_badly_cont function which actually shuts down the
81 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 die_task = GNUNET_SCHEDULER_NO_TASK;
84 if (NULL != gns_handle)
86 GNUNET_GNS_disconnect(gns_handle);
90 if (NULL != namestore_handle)
92 GNUNET_NAMESTORE_disconnect (namestore_handle);
93 namestore_handle = NULL;
96 GNUNET_SCHEDULER_shutdown ();
103 GNUNET_SCHEDULER_cancel (die_task);
104 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
107 static void shutdown_task (void *cls,
108 const struct GNUNET_SCHEDULER_TaskContext *tc)
110 GNUNET_GNS_disconnect(gns_handle);
111 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
112 GNUNET_SCHEDULER_shutdown ();
116 on_lookup_result_cname (void *cls, uint32_t rd_count,
117 const struct GNUNET_NAMESTORE_RecordData *rd)
121 if (GNUNET_SCHEDULER_NO_TASK != die_task)
123 GNUNET_SCHEDULER_cancel (die_task);
124 die_task = GNUNET_SCHEDULER_NO_TASK;
127 GNUNET_NAMESTORE_disconnect (namestore_handle);
128 namestore_handle = NULL;
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
132 "Lookup failed, rp_filtering?\n");
138 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
139 for (i=0; i<rd_count; i++)
141 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
142 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
144 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CNAME: %s\n", rd[i].data);
145 if (0 == strcmp(rd[i].data, TEST_RECORD_CNAME_SERVER))
147 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
148 "%s correctly resolved to %s!\n", TEST_DOMAIN_PLUS, rd[i].data);
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
158 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
162 on_lookup_result_dns (void *cls, uint32_t rd_count,
163 const struct GNUNET_NAMESTORE_RecordData *rd)
171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 "Lookup failed, rp_filtering?\n");
178 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
179 for (i=0; i<rd_count; i++)
181 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
182 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
184 memcpy(&a, rd[i].data, sizeof(a));
186 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
187 if (0 == strcmp(addr, TEST_IP_DNS))
189 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
190 "%s correctly resolved to %s!\n", TEST_DOMAIN_DNS, addr);
196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
201 GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN_PLUS, GNUNET_GNS_RECORD_CNAME,
204 &on_lookup_result_cname, TEST_DOMAIN_PLUS);
208 on_lookup_result_zkey (void *cls, uint32_t rd_count,
209 const struct GNUNET_NAMESTORE_RecordData *rd)
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218 "Lookup failed, rp_filtering?\n");
224 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
225 for (i=0; i<rd_count; i++)
227 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
228 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
230 memcpy(&a, rd[i].data, sizeof(a));
232 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
233 if (0 == strcmp(addr, TEST_IP_ZKEY))
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
236 "%s correctly resolved to %s!\n", TEST_DOMAIN_ZKEY, addr);
242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
247 GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_DNS, GNUNET_GNS_RECORD_A,
250 &on_lookup_result_dns, TEST_DOMAIN_DNS);
254 on_lookup_result_plus (void *cls, uint32_t rd_count,
255 const struct GNUNET_NAMESTORE_RecordData *rd)
263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264 "Lookup failed, rp_filtering?\n");
270 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
271 for (i=0; i<rd_count; i++)
273 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
274 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
276 memcpy(&a, rd[i].data, sizeof(a));
278 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
279 if (0 == strcmp(addr, TEST_IP_PLUS))
281 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
282 "%s correctly resolved to %s!\n", TEST_DOMAIN_PLUS, addr);
288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
293 GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_ZKEY, GNUNET_GNS_RECORD_A,
296 &on_lookup_result_zkey, TEST_DOMAIN_ZKEY);
300 * Function scheduled to be run on the successful start of services
301 * tries to look up the dns record for TEST_DOMAIN
304 commence_testing (void *cls, int32_t success, const char *emsg)
306 gns_handle = GNUNET_GNS_connect(cfg);
307 if (NULL == gns_handle)
309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310 "Failed to connect to GNS!\n");
315 GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_PLUS, GNUNET_GNS_RECORD_A,
318 &on_lookup_result_plus, TEST_DOMAIN_PLUS);
324 const struct GNUNET_CONFIGURATION_Handle *ccfg,
325 struct GNUNET_TESTING_Peer *peer)
327 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
328 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
329 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
333 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
336 /* put records into namestore */
337 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
338 if (NULL == namestore_handle)
340 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
345 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
349 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
354 alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
356 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
358 GNUNET_free(alice_keyfile);
360 bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
362 struct GNUNET_NAMESTORE_RecordData rd;
363 char* ip = TEST_IP_PLUS;
364 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
365 rd.expiration_time = UINT64_MAX;
366 GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
367 rd.data_size = sizeof(struct in_addr);
369 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
370 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
372 GNUNET_NAMESTORE_record_create (namestore_handle,
374 TEST_RECORD_NAME_SERVER,
379 rd.data_size = strlen (TEST_RECORD_CNAME_PLUS);
380 rd.data = TEST_RECORD_CNAME_PLUS;
381 rd.record_type = GNUNET_GNS_RECORD_CNAME;
383 GNUNET_NAMESTORE_record_create (namestore_handle,
385 TEST_RECORD_NAME_PLUS,
390 rd.data_size = strlen (TEST_RECORD_CNAME_ZKEY);
391 rd.data = TEST_RECORD_CNAME_ZKEY;
392 rd.record_type = GNUNET_GNS_RECORD_CNAME;
394 GNUNET_NAMESTORE_record_create (namestore_handle,
396 TEST_RECORD_NAME_ZKEY,
401 rd.data_size = strlen (TEST_RECORD_CNAME_DNS);
402 rd.data = TEST_RECORD_CNAME_DNS;
403 rd.record_type = GNUNET_GNS_RECORD_CNAME;
405 GNUNET_NAMESTORE_record_create (namestore_handle,
407 TEST_RECORD_NAME_DNS,
412 GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP_ZKEY, web));
413 rd.data_size = sizeof(struct in_addr);
415 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
417 GNUNET_NAMESTORE_record_create (namestore_handle,
419 TEST_RECORD_NAME_PLUS,
424 GNUNET_CRYPTO_rsa_key_free(alice_key);
425 GNUNET_CRYPTO_rsa_key_free(bob_key);
431 main (int argc, char *argv[])
435 GNUNET_log_setup ("test-gns-simple-cname-lookup",
442 GNUNET_TESTING_peer_run ("test-gns-simple-cname-lookup", "test_gns_simple_lookup.conf", &do_check, NULL);
446 /* end of test_gns_simple_cname_lookup.c */