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.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.gnunet"
45 #define TEST_DOMAIN_ZKEY "www2.gnunet"
46 #define TEST_DOMAIN_DNS "www3.gnunet"
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.gnunet"
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"
64 * Directory to store temp data in, defined in config file
66 static char *test_directory;
68 static struct GNUNET_TESTING_PeerGroup *pg;
70 /* Task handle to use to schedule test failure */
71 GNUNET_SCHEDULER_TaskIdentifier die_task;
73 /* Global return value (0 for success, anything else for failure) */
76 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
78 static struct GNUNET_GNS_Handle *gns_handle;
80 const struct GNUNET_CONFIGURATION_Handle *cfg;
83 * Check whether peers successfully shut down.
86 shutdown_callback (void *cls, const char *emsg)
90 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
95 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
100 on_lookup_result_cname (void *cls, uint32_t rd_count,
101 const struct GNUNET_NAMESTORE_RecordData *rd)
107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
108 "Lookup failed, rp_filtering?\n");
114 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
115 for (i=0; i<rd_count; i++)
117 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
118 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
120 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CNAME: %s\n", rd[i].data);
121 if (0 == strcmp(rd[i].data, TEST_RECORD_CNAME_SERVER))
123 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
124 "%s correctly resolved to %s!\n", TEST_DOMAIN_PLUS, rd[i].data);
130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
134 GNUNET_GNS_disconnect(gns_handle);
135 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
136 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
140 on_lookup_result_dns (void *cls, uint32_t rd_count,
141 const struct GNUNET_NAMESTORE_RecordData *rd)
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150 "Lookup failed, rp_filtering?\n");
156 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
157 for (i=0; i<rd_count; i++)
159 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
160 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
162 memcpy(&a, rd[i].data, sizeof(a));
164 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
165 if (0 == strcmp(addr, TEST_IP_DNS))
167 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
168 "%s correctly resolved to %s!\n", TEST_DOMAIN_DNS, addr);
174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
179 GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN_PLUS, GNUNET_GNS_RECORD_CNAME,
182 &on_lookup_result_cname, TEST_DOMAIN_PLUS);
186 on_lookup_result_zkey (void *cls, uint32_t rd_count,
187 const struct GNUNET_NAMESTORE_RecordData *rd)
195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
196 "Lookup failed, rp_filtering?\n");
202 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
203 for (i=0; i<rd_count; i++)
205 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
206 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
208 memcpy(&a, rd[i].data, sizeof(a));
210 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
211 if (0 == strcmp(addr, TEST_IP_ZKEY))
213 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
214 "%s correctly resolved to %s!\n", TEST_DOMAIN_ZKEY, addr);
220 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
225 GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_DNS, GNUNET_GNS_RECORD_A,
228 &on_lookup_result_dns, TEST_DOMAIN_DNS);
232 on_lookup_result_plus (void *cls, uint32_t rd_count,
233 const struct GNUNET_NAMESTORE_RecordData *rd)
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242 "Lookup failed, rp_filtering?\n");
248 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
249 for (i=0; i<rd_count; i++)
251 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
252 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
254 memcpy(&a, rd[i].data, sizeof(a));
256 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
257 if (0 == strcmp(addr, TEST_IP_PLUS))
259 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
260 "%s correctly resolved to %s!\n", TEST_DOMAIN_PLUS, addr);
266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
271 GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_ZKEY, GNUNET_GNS_RECORD_A,
274 &on_lookup_result_zkey, TEST_DOMAIN_ZKEY);
279 disco_ns (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
281 GNUNET_NAMESTORE_disconnect (namestore_handle);
285 * Function scheduled to be run on the successful start of services
286 * tries to look up the dns record for TEST_DOMAIN
289 commence_testing (void *cls, int32_t success, const char *emsg)
292 GNUNET_SCHEDULER_add_now (disco_ns, NULL);
294 gns_handle = GNUNET_GNS_connect(cfg);
296 if (NULL == gns_handle)
298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299 "Failed to connect to GNS!\n");
303 GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_PLUS, GNUNET_GNS_RECORD_A,
306 &on_lookup_result_plus, TEST_DOMAIN_PLUS);
311 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
312 * down the peers without freeing memory associated with GET request.
315 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
319 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
320 GNUNET_SCHEDULER_cancel (die_task);
324 * Check if the get_handle is being used, if so stop the request. Either
325 * way, schedule the end_badly_cont function which actually shuts down the
329 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
333 GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
338 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
339 const struct GNUNET_CONFIGURATION_Handle *_cfg,
340 struct GNUNET_TESTING_Daemon *d, const char *emsg)
342 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
343 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
344 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
349 GNUNET_SCHEDULER_cancel (die_task);
351 /* put records into namestore */
352 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
353 if (NULL == namestore_handle)
355 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
360 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
364 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
369 alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
371 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
373 GNUNET_free(alice_keyfile);
375 bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
377 struct GNUNET_NAMESTORE_RecordData rd;
378 char* ip = TEST_IP_PLUS;
379 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
380 rd.expiration_time = UINT64_MAX;
381 GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
382 rd.data_size = sizeof(struct in_addr);
384 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
385 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
387 GNUNET_NAMESTORE_record_create (namestore_handle,
389 TEST_RECORD_NAME_SERVER,
394 rd.data_size = strlen (TEST_RECORD_CNAME_PLUS);
395 rd.data = TEST_RECORD_CNAME_PLUS;
396 rd.record_type = GNUNET_GNS_RECORD_CNAME;
398 GNUNET_NAMESTORE_record_create (namestore_handle,
400 TEST_RECORD_NAME_PLUS,
405 rd.data_size = strlen (TEST_RECORD_CNAME_ZKEY);
406 rd.data = TEST_RECORD_CNAME_ZKEY;
407 rd.record_type = GNUNET_GNS_RECORD_CNAME;
409 GNUNET_NAMESTORE_record_create (namestore_handle,
411 TEST_RECORD_NAME_ZKEY,
416 rd.data_size = strlen (TEST_RECORD_CNAME_DNS);
417 rd.data = TEST_RECORD_CNAME_DNS;
418 rd.record_type = GNUNET_GNS_RECORD_CNAME;
420 GNUNET_NAMESTORE_record_create (namestore_handle,
422 TEST_RECORD_NAME_DNS,
427 GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP_ZKEY, web));
428 rd.data_size = sizeof(struct in_addr);
430 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
432 GNUNET_NAMESTORE_record_create (namestore_handle,
434 TEST_RECORD_NAME_PLUS,
439 GNUNET_CRYPTO_rsa_key_free(alice_key);
440 GNUNET_CRYPTO_rsa_key_free(bob_key);
446 run (void *cls, char *const *args, const char *cfgfile,
447 const struct GNUNET_CONFIGURATION_Handle *c)
450 /* Get path from configuration file */
452 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
460 /* Set up a task to end testing if peer start fails */
462 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
463 "didn't start all daemons in reasonable amount of time!!!");
466 pg = GNUNET_TESTING_daemons_start(cfg, 1, 1, 1, TIMEOUT,
467 NULL, NULL, &do_lookup, NULL,
476 /* Arguments for GNUNET_PROGRAM_run */
477 char *const argv[] = { "test-gns-simple-lookup", /* Name to give running binary */
479 "test_gns_simple_lookup.conf", /* Config file to use */
485 struct GNUNET_GETOPT_CommandLineOption options[] = {
486 GNUNET_GETOPT_OPTION_END
488 /* Run the run function as a new program */
490 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
491 "test-gns-simple-lookup", "nohelp", options, &run,
493 if (ret != GNUNET_OK)
495 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
496 "`test-gns-simple-lookup': Failed with error code %d\n", ret);
502 main (int argc, char *argv[])
506 GNUNET_log_setup ("test-gns-simple-lookup",
515 * Need to remove base directory, subdirectories taken care
516 * of by the testing framework.
521 /* end of test_gns_twopeer.c */