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_simple_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, 120)
40 /* test records to resolve */
41 #define TEST_DOMAIN "www.gnu"
42 #define TEST_IP "127.0.0.1"
43 #define TEST_RECORD_NAME "www"
46 * Task handle to use to schedule test failure
48 static GNUNET_SCHEDULER_TaskIdentifier die_task;
51 * Global return value (0 for success, anything else for failure)
55 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
57 static struct GNUNET_GNS_Handle *gns_handle;
59 static const struct GNUNET_CONFIGURATION_Handle *cfg;
61 static struct GNUNET_GNS_LookupRequest *lr;
63 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
67 * Check if the get_handle is being used, if so stop the request. Either
68 * way, schedule the end_badly_cont function which actually shuts down the
73 const struct GNUNET_SCHEDULER_TaskContext *tc)
77 GNUNET_NAMESTORE_cancel (nsqe);
82 GNUNET_GNS_cancel_lookup_request (lr);
85 if (NULL != gns_handle)
87 GNUNET_GNS_disconnect (gns_handle);
91 if (NULL != namestore_handle)
93 GNUNET_NAMESTORE_disconnect (namestore_handle);
94 namestore_handle = NULL;
97 GNUNET_SCHEDULER_shutdown ();
105 GNUNET_SCHEDULER_cancel (die_task);
106 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
111 shutdown_task (void *cls,
112 const struct GNUNET_SCHEDULER_TaskContext *tc)
114 GNUNET_GNS_disconnect (gns_handle);
115 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
116 GNUNET_SCHEDULER_shutdown ();
121 * Function called on result for a GNS lookup
123 * @param cls closure, unused
124 * @param rd_count number of records
125 * @param rd the records in reply
128 on_lookup_result(void *cls, uint32_t rd_count,
129 const struct GNUNET_NAMESTORE_RecordData *rd)
136 if (GNUNET_SCHEDULER_NO_TASK != die_task)
138 GNUNET_SCHEDULER_cancel (die_task);
139 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_DNSPARSER_TYPE_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",
171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
181 * Function scheduled to be run on the successful start of services
182 * tries to look up the dns record for TEST_DOMAIN
185 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
186 * GNUNET_NO if content was already there or not found
187 * GNUNET_YES (or other positive value) on success
188 * @param emsg NULL on success, otherwise an error message
191 commence_testing (void *cls,
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
199 "Failed to store record in namestore: %s\n",
204 gns_handle = GNUNET_GNS_connect (cfg);
205 if (NULL == gns_handle)
207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
208 "Failed to connect to GNS!\n");
212 lr = GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN, GNUNET_DNSPARSER_TYPE_A,
215 &on_lookup_result, TEST_DOMAIN);
221 const struct GNUNET_CONFIGURATION_Handle *ccfg,
222 struct GNUNET_TESTING_Peer *peer)
224 struct GNUNET_CRYPTO_EccPublicKey alice_pkey;
225 struct GNUNET_CRYPTO_EccPrivateKey *alice_key;
226 struct GNUNET_NAMESTORE_RecordData rd;
232 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
234 /* put records into namestore */
235 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
236 if (NULL == namestore_handle)
238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
239 "Failed to connect to namestore\n");
244 GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
249 "Failed to get key from cfg\n");
254 alice_key = GNUNET_CRYPTO_ecc_key_create_from_file (alice_keyfile);
255 GNUNET_CRYPTO_ecc_key_get_public (alice_key, &alice_pkey);
256 GNUNET_free (alice_keyfile);
257 rd.expiration_time = UINT64_MAX;
258 GNUNET_assert (1 == inet_pton (AF_INET, ip, &web));
259 rd.data_size = sizeof(struct in_addr);
261 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
262 rd.flags = GNUNET_NAMESTORE_RF_NONE;
263 nsqe = GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
269 GNUNET_free (alice_key);
274 main (int argc, char *argv[])
277 GNUNET_log_setup ("test-gns-simple-lookup",
280 GNUNET_TESTING_peer_run ("test-gns-simple-lookup",
281 "test_gns_simple_lookup.conf",
286 /* end of test_gns_simple_lookup.c */