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_shorten.c
22 * @brief basic shorten test for gns api
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, 20)
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 "www.alice.bob.gnunet"
46 #define TEST_IP "127.0.0.1"
47 #define TEST_RECORD_NAME "www"
49 #define TEST_AUTHORITY_BOB "bob"
50 #define TEST_AUTHORITY_ALICE "alice"
51 #define TEST_ALICE_PSEU "carol"
52 #define TEST_EXPECTED_RESULT "www.carol.gnunet"
54 #define KEYFILE_BOB "../namestore/zonefiles/HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"
55 #define KEYFILE_ALICE "../namestore/zonefiles/N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.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;
78 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded priv_pkey;
79 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded short_pkey;
80 struct GNUNET_CRYPTO_RsaPrivateKey *priv_key;
81 struct GNUNET_CRYPTO_RsaPrivateKey *short_key;
83 struct GNUNET_CRYPTO_ShortHashCode priv_zone;
84 struct GNUNET_CRYPTO_ShortHashCode short_zone;
87 * Check whether peers successfully shut down.
90 shutdown_callback (void *cls, const char *emsg)
94 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
99 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
103 * Called when gns shorten finishes
106 process_shorten_result(void* cls, const char* sname)
108 GNUNET_GNS_disconnect(gns_handle);
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112 "disconnecting from namestore\n");
113 GNUNET_NAMESTORE_disconnect (namestore_handle);
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119 "shorten test failed!\n");
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125 "%s shortened to %s\n", (char*)cls, sname);
126 if (0 != strcmp(sname, TEST_EXPECTED_RESULT))
128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
129 "shorten test failed! (wanted: %s got: %s\n",
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shorten test succeeded!\n");
138 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
139 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
143 * Function scheduled to be run on the successful start of services
144 * tries to shorten the name TEST_DOMAIN using gns
147 commence_testing (void *cls, int32_t success, const char *emsg)
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "connecting to gns\n");
154 gns_handle = GNUNET_GNS_connect(cfg);
156 if (NULL == gns_handle)
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159 "failed to connect to gns\n");
164 GNUNET_GNS_shorten(gns_handle, TEST_DOMAIN,
167 &process_shorten_result,
173 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
174 * down the peers without freeing memory associated with GET request.
177 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
181 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
182 GNUNET_SCHEDULER_cancel (die_task);
186 * Check if the get_handle is being used, if so stop the request. Either
187 * way, schedule the end_badly_cont function which actually shuts down the
191 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
195 GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
200 do_shorten(void *cls, const struct GNUNET_PeerIdentity *id,
201 const struct GNUNET_CONFIGURATION_Handle *_cfg,
202 struct GNUNET_TESTING_Daemon *d, const char *emsg)
204 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded our_pkey;
205 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
206 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
207 struct GNUNET_CRYPTO_RsaPrivateKey *our_key;
208 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
209 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
210 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
211 struct GNUNET_CRYPTO_ShortHashCode alice_hash;
212 struct GNUNET_CRYPTO_RsaSignature *sig;
214 char* private_keyfile;
215 char* shorten_keyfile;
219 GNUNET_SCHEDULER_cancel (die_task);
221 /* put records into namestore */
222 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
223 if (NULL == namestore_handle)
225 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
230 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
234 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
239 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
243 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
244 "Failed to get shorten zone key from cfg\n");
249 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
253 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
254 "Failed to get private zone key from cfg\n");
259 our_key = GNUNET_CRYPTO_rsa_key_create_from_file (our_keyfile);
260 GNUNET_free(our_keyfile);
262 bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
263 alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_ALICE);
264 priv_key = GNUNET_CRYPTO_rsa_key_create_from_file (private_keyfile);
265 short_key = GNUNET_CRYPTO_rsa_key_create_from_file (shorten_keyfile);
267 GNUNET_free(shorten_keyfile);
268 GNUNET_free(private_keyfile);
270 GNUNET_CRYPTO_rsa_key_get_public (our_key, &our_pkey);
271 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
272 GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
273 GNUNET_CRYPTO_rsa_key_get_public (priv_key, &priv_pkey);
274 GNUNET_CRYPTO_rsa_key_get_public (short_key, &short_pkey);
276 GNUNET_CRYPTO_short_hash(&priv_pkey, sizeof(priv_pkey), &priv_zone);
277 GNUNET_CRYPTO_short_hash(&short_pkey, sizeof(short_pkey), &short_zone);
279 struct GNUNET_NAMESTORE_RecordData rd;
281 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
282 rd.expiration_time = UINT64_MAX;
283 GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
285 GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
287 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
289 rd.record_type = GNUNET_GNS_RECORD_PKEY;
290 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
292 /* put bob into our zone */
293 GNUNET_NAMESTORE_record_create (namestore_handle,
300 /* put alice into bobs zone */
301 GNUNET_CRYPTO_short_hash(&alice_pkey, sizeof(alice_pkey), &alice_hash);
302 rd.data = &alice_hash;
303 sig = GNUNET_NAMESTORE_create_signature(bob_key,
304 GNUNET_TIME_UNIT_FOREVER_ABS,
305 TEST_AUTHORITY_ALICE,
308 GNUNET_NAMESTORE_record_put (namestore_handle,
310 TEST_AUTHORITY_ALICE,
311 GNUNET_TIME_UNIT_FOREVER_ABS,
318 /* put www A record and PSEU into alice's zone */
320 rd.data_size = sizeof(struct in_addr);
322 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
323 sig = GNUNET_NAMESTORE_create_signature(alice_key,
324 GNUNET_TIME_UNIT_FOREVER_ABS,
328 GNUNET_NAMESTORE_record_put (namestore_handle,
331 GNUNET_TIME_UNIT_FOREVER_ABS,
338 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
339 rd.data = &alice_hash;
340 rd.record_type = GNUNET_GNS_RECORD_PKEY;
343 GNUNET_NAMESTORE_record_create (namestore_handle,
351 GNUNET_CRYPTO_rsa_key_free(our_key);
352 GNUNET_CRYPTO_rsa_key_free(bob_key);
353 GNUNET_CRYPTO_rsa_key_free(alice_key);
354 GNUNET_CRYPTO_rsa_key_free(priv_key);
355 GNUNET_CRYPTO_rsa_key_free(short_key);
359 run (void *cls, char *const *args, const char *cfgfile,
360 const struct GNUNET_CONFIGURATION_Handle *c)
363 /* Get path from configuration file */
365 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
373 /* Set up a task to end testing if peer start fails */
375 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
376 "didn't start all daemons in reasonable amount of time!!!");
379 pg = GNUNET_TESTING_daemons_start(cfg, 1, 1, 1, TIMEOUT,
380 NULL, NULL, &do_shorten, NULL,
389 /* Arguments for GNUNET_PROGRAM_run */
390 char *const argv[] = { "test-gns-simple-shorten", /* Name to give running binary */
392 "test_gns_simple_lookup.conf", /* Config file to use */
398 struct GNUNET_GETOPT_CommandLineOption options[] = {
399 GNUNET_GETOPT_OPTION_END
401 /* Run the run function as a new program */
403 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
404 "test-gns-simple-shorten", "nohelp", options, &run,
406 if (ret != GNUNET_OK)
408 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
409 "`test-gns-simple-shorten': Failed with error code %d\n", ret);
415 main (int argc, char *argv[])
419 GNUNET_log_setup ("test-gns-simple-shorten",
428 * Need to remove base directory, subdirectories taken care
429 * of by the testing framework.
434 /* end of test_gns_twopeer.c */