2 This file is part of GNUnet.
3 (C) 2012 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
23 * @author Martin Schanzenbach
26 #include "gnunet_testing_lib.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"
36 * Timeout for entire testcase
38 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20)
40 /* test records to resolve */
41 #define TEST_DOMAIN_PLUS "www.gnu"
42 #define TEST_DOMAIN_ZKEY "www2.gnu"
43 #define TEST_DOMAIN_DNS "www3.gnu"
44 #define TEST_IP_PLUS "127.0.0.1"
45 #define TEST_IP_ZKEY "127.0.0.2"
46 #define TEST_IP_DNS "131.159.74.67"
47 #define TEST_RECORD_CNAME_SERVER "server.gnu"
48 #define TEST_RECORD_CNAME_PLUS "server.+"
49 #define TEST_RECORD_CNAME_ZKEY "www.J7POEUT41A8PBFS7KVVDRF88GBOU4HK8PSU5QKVLVE3R9T91E99G.zkey"
50 #define TEST_RECORD_CNAME_DNS "gnunet.org"
51 #define TEST_RECORD_NAME_SERVER "server"
52 #define TEST_RECORD_NAME_PLUS "www"
53 #define TEST_RECORD_NAME_ZKEY "www2"
54 #define TEST_RECORD_NAME_DNS "www3"
56 #define KEYFILE_BOB "zonefiles/J7POEUT41A8PBFS7KVVDRF88GBOU4HK8PSU5QKVLVE3R9T91E99G.zkey"
59 /* Task handle to use to schedule test failure */
60 static GNUNET_SCHEDULER_TaskIdentifier die_task;
62 /* Global return value (0 for success, anything else for failure) */
65 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
67 static struct GNUNET_GNS_Handle *gns_handle;
69 static const struct GNUNET_CONFIGURATION_Handle *cfg;
73 * Check if the get_handle is being used, if so stop the request. Either
74 * way, schedule the end_badly_cont function which actually shuts down the
78 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 die_task = GNUNET_SCHEDULER_NO_TASK;
81 if (NULL != gns_handle)
83 GNUNET_GNS_disconnect (gns_handle);
86 if (NULL != namestore_handle)
88 GNUNET_NAMESTORE_disconnect (namestore_handle);
89 namestore_handle = NULL;
92 GNUNET_SCHEDULER_shutdown ();
98 shutdown_task (void *cls,
99 const struct GNUNET_SCHEDULER_TaskContext *tc)
101 GNUNET_GNS_disconnect (gns_handle);
103 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
104 GNUNET_SCHEDULER_shutdown ();
109 on_lookup_result_cname (void *cls,
111 const struct GNUNET_NAMESTORE_RecordData *rd)
115 if (GNUNET_SCHEDULER_NO_TASK != die_task)
117 GNUNET_SCHEDULER_cancel (die_task);
118 die_task = GNUNET_SCHEDULER_NO_TASK;
120 GNUNET_NAMESTORE_disconnect (namestore_handle);
121 namestore_handle = NULL;
124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
125 "Lookup failed, rp_filtering?\n");
131 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
132 for (i=0; i<rd_count; i++)
134 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
135 if (rd[i].record_type == GNUNET_DNSPARSER_TYPE_CNAME)
137 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CNAME: %s\n", rd[i].data);
138 if (0 == strcmp(rd[i].data, TEST_RECORD_CNAME_SERVER))
140 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
141 "%s correctly resolved to %s!\n", TEST_DOMAIN_PLUS, rd[i].data);
147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
151 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
156 on_lookup_result_dns (void *cls,
158 const struct GNUNET_NAMESTORE_RecordData *rd)
166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
167 "CNAME to DNS delegation failed. System offline?\n");
172 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
173 for (i=0; i<rd_count; i++)
175 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
176 if (rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
178 memcpy(&a, rd[i].data, sizeof(a));
180 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
181 if (0 == strcmp(addr, TEST_IP_DNS))
183 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
184 "%s correctly resolved to %s!\n", TEST_DOMAIN_DNS, addr);
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
194 GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN_PLUS, GNUNET_DNSPARSER_TYPE_CNAME,
197 &on_lookup_result_cname, TEST_DOMAIN_PLUS);
202 on_lookup_result_zkey (void *cls, uint32_t rd_count,
203 const struct GNUNET_NAMESTORE_RecordData *rd)
211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212 "Lookup failed, rp_filtering?\n");
214 GNUNET_SCHEDULER_shutdown ();
218 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
219 "name: %s\n", (char*)cls);
220 for (i=0; i<rd_count; i++)
222 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
223 "type: %d\n", rd[i].record_type);
224 if (rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
226 memcpy (&a, rd[i].data, sizeof(a));
228 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
229 "address: %s\n", addr);
230 if (0 == strcmp (addr, TEST_IP_ZKEY))
232 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
233 "%s correctly resolved to %s!\n",
234 TEST_DOMAIN_ZKEY, addr);
240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
244 GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN_DNS, GNUNET_DNSPARSER_TYPE_A,
247 &on_lookup_result_dns, TEST_DOMAIN_DNS);
252 on_lookup_result_plus (void *cls, uint32_t rd_count,
253 const struct GNUNET_NAMESTORE_RecordData *rd)
261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
262 "Lookup failed, rp_filtering?\n");
264 GNUNET_SCHEDULER_shutdown ();
268 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
269 for (i=0; i<rd_count; i++)
271 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
272 if (rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
274 memcpy(&a, rd[i].data, sizeof(a));
276 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
277 if (0 == strcmp(addr, TEST_IP_PLUS))
279 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
280 "%s correctly resolved to %s!\n", TEST_DOMAIN_PLUS, addr);
286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
289 GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN_ZKEY, GNUNET_DNSPARSER_TYPE_A,
292 &on_lookup_result_zkey, TEST_DOMAIN_ZKEY);
297 * Function scheduled to be run on the successful start of services
298 * tries to look up the dns record for TEST_DOMAIN
301 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
302 * GNUNET_NO if content was already there or not found
303 * GNUNET_YES (or other positive value) on success
304 * @param emsg NULL on success, otherwise an error message
307 commence_testing (void *cls, int32_t success, const char *emsg)
311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
312 "Failed to store record in namestore: %s\n",
314 GNUNET_SCHEDULER_shutdown ();
317 gns_handle = GNUNET_GNS_connect(cfg);
318 if (NULL == gns_handle)
320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
321 "Failed to connect to GNS!\n");
322 GNUNET_SCHEDULER_shutdown ();
325 GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN_PLUS, GNUNET_DNSPARSER_TYPE_A,
328 &on_lookup_result_plus, TEST_DOMAIN_PLUS);
334 const struct GNUNET_CONFIGURATION_Handle *ccfg,
335 struct GNUNET_TESTING_Peer *peer)
337 struct GNUNET_CRYPTO_EccPublicKey alice_pkey;
338 struct GNUNET_CRYPTO_EccPrivateKey *alice_key;
339 struct GNUNET_CRYPTO_EccPrivateKey *bob_key;
341 struct GNUNET_NAMESTORE_RecordData rd;
342 const char* ip = TEST_IP_PLUS;
346 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
348 /* put records into namestore */
349 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
350 if (NULL == namestore_handle)
352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
353 "Failed to connect to namestore\n");
354 GNUNET_SCHEDULER_shutdown ();
358 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
363 "Failed to get key from cfg\n");
364 GNUNET_SCHEDULER_shutdown ();
368 alice_key = GNUNET_CRYPTO_ecc_key_create_from_file (alice_keyfile);
369 GNUNET_CRYPTO_ecc_key_get_public (alice_key, &alice_pkey);
370 GNUNET_free(alice_keyfile);
372 bob_key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE_BOB);
374 rd.expiration_time = UINT64_MAX;
375 GNUNET_assert(1 == inet_pton (AF_INET, ip, &web));
376 rd.data_size = sizeof(struct in_addr);
378 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
379 rd.flags = GNUNET_NAMESTORE_RF_NONE;
381 GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
383 TEST_RECORD_NAME_SERVER,
388 rd.data_size = strlen (TEST_RECORD_CNAME_PLUS);
389 rd.data = TEST_RECORD_CNAME_PLUS;
390 rd.record_type = GNUNET_DNSPARSER_TYPE_CNAME;
392 GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
394 TEST_RECORD_NAME_PLUS,
399 rd.data_size = strlen (TEST_RECORD_CNAME_ZKEY);
400 rd.data = TEST_RECORD_CNAME_ZKEY;
401 rd.record_type = GNUNET_DNSPARSER_TYPE_CNAME;
403 GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
405 TEST_RECORD_NAME_ZKEY,
410 rd.data_size = strlen (TEST_RECORD_CNAME_DNS);
411 rd.data = TEST_RECORD_CNAME_DNS;
412 rd.record_type = GNUNET_DNSPARSER_TYPE_CNAME;
414 GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
416 TEST_RECORD_NAME_DNS,
421 GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP_ZKEY, &web));
422 rd.data_size = sizeof(struct in_addr);
424 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
426 GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
428 TEST_RECORD_NAME_PLUS,
432 GNUNET_free(alice_key);
433 GNUNET_free(bob_key);
438 main (int argc, char *argv[])
442 GNUNET_log_setup ("test-gns-simple-cname-lookup",
445 GNUNET_TESTING_peer_run ("test-gns-simple-cname-lookup", "test_gns_simple_lookup.conf", &do_check, NULL);
449 /* end of test_gns_cname_lookup.c */