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_mx_lookup.c
22 * @brief base testcase for testing GNS MX 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 "gnunet_gns_service.h"
36 #define VERBOSE GNUNET_YES
38 /* Timeout for entire testcase */
39 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
41 /* If number of peers not in config file, use this number */
42 #define DEFAULT_NUM_PEERS 2
44 /* test records to resolve */
45 #define TEST_DOMAIN "bob.gnunet"
46 #define TEST_IP "127.0.0.1"
47 #define TEST_RECORD_NAME "mail"
48 #define TEST_MX_NAME "mail.+"
49 #define TEST_EXPECTED_MX "mail.bob.gnunet"
51 #define TEST_AUTHORITY_NAME "bob"
56 * Directory to store temp data in, defined in config file
58 static char *test_directory;
60 struct GNUNET_TESTING_Daemon *d1;
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 whether peers successfully shut down.
79 shutdown_callback (void *cls, const char *emsg)
83 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
88 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
92 on_lookup_result(void *cls, uint32_t rd_count,
93 const struct GNUNET_NAMESTORE_RecordData *rd)
104 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
105 "Lookup failed, rp_filtering?\n");
111 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
112 for (i=0; i<rd_count; i++)
114 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
115 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
117 memcpy(&a, rd[i].data, sizeof(a));
119 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
120 if (0 == strcmp(addr, TEST_IP))
122 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
123 "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
127 else if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
129 mx = (char*)rd[i].data;
130 if (0 == strcmp(mx, TEST_EXPECTED_MX))
132 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
133 "%s correctly resolved to %s!\n", TEST_DOMAIN,
141 if (ip_found && mx_found)
143 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
144 "Test succeeded!\n");
148 if (!ip_found && mx_found)
150 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
151 "Test partially succeeded: A record not passed along!(NOT IMPLEMENTED)\n");
155 GNUNET_GNS_disconnect(gns_handle);
156 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
157 GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
158 GNUNET_YES, GNUNET_NO);
163 * Function scheduled to be run on the successful start of services
164 * tries to look up the dns record for TEST_DOMAIN
167 commence_testing (void *cls, int32_t success, const char *emsg)
169 GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES);
171 gns_handle = GNUNET_GNS_connect(cfg);
173 if (NULL == gns_handle)
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176 "Failed to connect to GNS!\n");
179 GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_MX,
180 &on_lookup_result, TEST_DOMAIN);
184 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
185 * down the peers without freeing memory associated with GET request.
188 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
192 GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
193 GNUNET_YES, GNUNET_NO);
194 GNUNET_SCHEDULER_cancel (die_task);
198 * Check if the get_handle is being used, if so stop the request. Either
199 * way, schedule the end_badly_cont function which actually shuts down the
203 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
207 GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
212 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
213 const struct GNUNET_CONFIGURATION_Handle *cfg,
214 struct GNUNET_TESTING_Daemon *d, const char *emsg)
216 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
217 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
218 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
219 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
220 GNUNET_HashCode bob_hash;
221 struct GNUNET_CRYPTO_RsaSignature *sig;
224 GNUNET_SCHEDULER_cancel (die_task);
226 /* put records into namestore */
227 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
228 if (NULL == namestore_handle)
230 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
235 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
239 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
244 alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
245 bob_key = GNUNET_CRYPTO_rsa_key_create ();
247 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
248 GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
250 struct GNUNET_NAMESTORE_RecordData rd;
252 struct in_addr *mail = GNUNET_malloc(sizeof(struct in_addr));
253 rd.expiration = GNUNET_TIME_absolute_get_forever ();
254 GNUNET_assert(1 == inet_pton (AF_INET, ip, mail));
256 GNUNET_CRYPTO_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
258 rd.data_size = sizeof(GNUNET_HashCode);
260 rd.record_type = GNUNET_GNS_RECORD_PKEY;
262 GNUNET_NAMESTORE_record_create (namestore_handle,
269 rd.data_size = sizeof(struct in_addr);
271 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
272 sig = GNUNET_NAMESTORE_create_signature(bob_key,
273 GNUNET_TIME_absolute_get_forever(),
277 GNUNET_NAMESTORE_record_put (namestore_handle,
287 rd.data_size = strlen(TEST_MX_NAME)+1;
288 rd.data = TEST_MX_NAME;
289 rd.record_type = GNUNET_GNS_RECORD_MX;
290 sig = GNUNET_NAMESTORE_create_signature(bob_key,
291 GNUNET_TIME_absolute_get_forever(),
294 GNUNET_NAMESTORE_record_put (namestore_handle,
305 GNUNET_CRYPTO_rsa_key_free(bob_key);
306 GNUNET_CRYPTO_rsa_key_free(alice_key);
310 run (void *cls, char *const *args, const char *cfgfile,
311 const struct GNUNET_CONFIGURATION_Handle *c)
314 /* Get path from configuration file */
316 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
324 /* Set up a task to end testing if peer start fails */
326 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
327 "didn't start all daemons in reasonable amount of time!!!");
330 d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
331 NULL, NULL, NULL, &do_lookup, NULL);
339 /* Arguments for GNUNET_PROGRAM_run */
340 char *const argv[] = { "test-gns-simple-delegated-lookup", /* Name to give running binary */
342 "test_gns_simple_lookup.conf", /* Config file to use */
348 struct GNUNET_GETOPT_CommandLineOption options[] = {
349 GNUNET_GETOPT_OPTION_END
351 /* Run the run function as a new program */
353 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
354 "test-gns-simple-delegated-lookup", "nohelp", options, &run,
356 if (ret != GNUNET_OK)
358 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
359 "`test-gns-simple-delegated-lookup': Failed with error code %d\n", ret);
365 main (int argc, char *argv[])
369 GNUNET_log_setup ("test-gns-simple-lookup",
378 * Need to remove base directory, subdirectories taken care
379 * of by the testing framework.
384 /* end of test_gns_twopeer.c */