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_simple_srv_lookup.c
22 * @brief base testcase for testing GNS SRV lookups
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 "../namestore/namestore.h"
32 #include "gnunet_dnsparser_lib.h"
33 #include "../dns/dnsparser.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 /* If number of peers not in config file, use this number */
43 #define DEFAULT_NUM_PEERS 2
45 /* test records to resolve */
46 #define TEST_DOMAIN "_sip._tcp.bob.gnunet"
47 #define TEST_IP "127.0.0.1"
48 #define TEST_RECORD_NAME "sipserver"
49 #define TEST_RECORD_NAME_SRV "_sip._tcp"
50 #define TEST_SRV_NAME "sipserver.+"
51 #define TEST_EXPECTED_SRV "sipserver.bob.gnunet"
53 #define TEST_AUTHORITY_NAME "bob"
55 #define KEYFILE_BOB "../namestore/zonefiles/HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"
60 * Directory to store temp data in, defined in config file
62 static char *test_directory;
64 static struct GNUNET_TESTING_PeerGroup *pg;
66 /* Task handle to use to schedule test failure */
67 GNUNET_SCHEDULER_TaskIdentifier die_task;
69 /* Global return value (0 for success, anything else for failure) */
72 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
74 static struct GNUNET_GNS_Handle *gns_handle;
76 const struct GNUNET_CONFIGURATION_Handle *cfg;
79 * Check whether peers successfully shut down.
82 shutdown_callback (void *cls, const char *emsg)
86 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
91 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
95 on_lookup_result(void *cls, uint32_t rd_count,
96 const struct GNUNET_NAMESTORE_RecordData *rd)
102 GNUNET_NAMESTORE_disconnect (namestore_handle);
105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
106 "Lookup failed, rp_filtering?\n");
112 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
113 for (i=0; i<rd_count; i++)
115 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
116 if (rd[i].record_type == GNUNET_GNS_RECORD_SRV)
118 srv_data = (uint16_t*)rd[i].data;
119 srv = (char*)&srv_data[3];
120 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
121 "Got SRV %s with p=%d,w=%d,port=%d\n",
122 srv, srv_data, &srv_data[1], &srv_data[2]);
123 if (0 == strcmp(srv, TEST_EXPECTED_SRV))
125 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
126 "%s correctly resolved to %s!\n", TEST_DOMAIN,
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);
141 * Function scheduled to be run on the successful start of services
142 * tries to look up the dns record for TEST_DOMAIN
145 commence_testing (void *cls, int32_t success, const char *emsg)
148 gns_handle = GNUNET_GNS_connect(cfg);
150 if (NULL == gns_handle)
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153 "Failed to connect to GNS!\n");
156 GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_SRV,
159 &on_lookup_result, TEST_DOMAIN);
163 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
164 * down the peers without freeing memory associated with GET request.
167 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
172 GNUNET_SCHEDULER_cancel (die_task);
176 * Check if the get_handle is being used, if so stop the request. Either
177 * way, schedule the end_badly_cont function which actually shuts down the
181 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
185 GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
191 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
192 const struct GNUNET_CONFIGURATION_Handle *_cfg,
193 struct GNUNET_TESTING_Daemon *d, const char *emsg)
195 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
196 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
197 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
198 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
199 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
200 struct GNUNET_CRYPTO_RsaSignature *sig;
202 struct srv_data *srv_data;
203 struct GNUNET_TIME_Absolute et;
207 GNUNET_SCHEDULER_cancel (die_task);
209 /* put records into namestore */
210 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
211 if (NULL == namestore_handle)
213 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
218 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
222 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
227 alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
228 bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
230 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
231 GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
233 struct GNUNET_NAMESTORE_RecordData rd;
235 struct in_addr *sipserver = GNUNET_malloc (sizeof (struct in_addr));
236 srv_data = GNUNET_malloc (sizeof (struct srv_data) + strlen (TEST_SRV_NAME) + 1);
237 uint16_t srv_weight = 60;
238 uint16_t srv_prio = 50;
239 uint16_t srv_port = 5060;
241 rd.expiration_time = UINT64_MAX;
242 GNUNET_assert(1 == inet_pton (AF_INET, ip, sipserver));
244 GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
246 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
248 rd.record_type = GNUNET_GNS_RECORD_PKEY;
249 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
251 GNUNET_NAMESTORE_record_create (namestore_handle,
258 rd.data_size = sizeof (struct in_addr);
260 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
261 sig = GNUNET_NAMESTORE_create_signature(bob_key,
262 GNUNET_TIME_UNIT_FOREVER_ABS,
265 et.abs_value = rd.expiration_time;
266 GNUNET_NAMESTORE_record_put (namestore_handle,
276 rd.data_size = sizeof (struct srv_data)+strlen(TEST_SRV_NAME)+1;
277 srv_data->port = srv_port;
278 srv_data->prio = srv_prio;
279 srv_data->weight = srv_weight;
280 strcpy((char*)&srv_data[1], TEST_SRV_NAME);
282 rd.record_type = GNUNET_GNS_RECORD_SRV;
283 sig = GNUNET_NAMESTORE_create_signature(bob_key,
284 GNUNET_TIME_UNIT_FOREVER_ABS,
285 TEST_RECORD_NAME_SRV,
287 et.abs_value = rd.expiration_time;
288 GNUNET_NAMESTORE_record_put (namestore_handle,
290 TEST_RECORD_NAME_SRV,
297 GNUNET_free(srv_data);
298 GNUNET_free(sipserver);
300 GNUNET_CRYPTO_rsa_key_free(bob_key);
301 GNUNET_CRYPTO_rsa_key_free(alice_key);
305 run (void *cls, char *const *args, const char *cfgfile,
306 const struct GNUNET_CONFIGURATION_Handle *c)
309 /* Get path from configuration file */
311 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
319 /* Set up a task to end testing if peer start fails */
321 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
322 "didn't start all daemons in reasonable amount of time!!!");
325 pg = GNUNET_TESTING_daemons_start(cfg, 1, 1, 1, TIMEOUT,
326 NULL, NULL, &do_lookup, NULL,
335 /* Arguments for GNUNET_PROGRAM_run */
336 char *const argv[] = { "test-gns-simple-mx-lookup", /* Name to give running binary */
338 "test_gns_simple_lookup.conf", /* Config file to use */
344 struct GNUNET_GETOPT_CommandLineOption options[] = {
345 GNUNET_GETOPT_OPTION_END
347 /* Run the run function as a new program */
349 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
350 "test-gns-simple-mx-lookup", "nohelp", options, &run,
352 if (ret != GNUNET_OK)
354 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
355 "`test-gns-simple-mx-lookup': Failed with error code %d\n", ret);
361 main (int argc, char *argv[])
365 GNUNET_log_setup ("test-gns-simple-mx-lookup",
374 * Need to remove base directory, subdirectories taken care
375 * of by the testing framework.
380 /* end of test_gns_simple_mx_lookup.c */