2 This file is part of GNUnet.
3 (C) 2012,2013 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_pseu_shorten.c
22 * @brief base testcase for testing on the fly pseu import and shorten
23 * @author Martin Schanzenbach
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_core_service.h"
28 #include "block_gns.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_dht_service.h"
34 #include "gnunet_gns_service.h"
36 /* Timeout for entire testcase */
37 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
39 /* test records to resolve */
40 #define TEST_DOMAIN "www.alicewonderland.bobbuilder.gnu"
41 #define TEST_IP "127.0.0.1"
42 #define TEST_RECORD_NAME "www"
44 #define TEST_PRIVATE_ZONE "private"
45 #define TEST_SHORTEN_ZONE "short"
46 #define TEST_AUTHORITY_BOB "bobbuilder"
47 #define TEST_AUTHORITY_ALICE "alicewonderland"
48 #define TEST_PSEU_ALICE "carol"
49 #define TEST_EXPECTED_RESULT "www.carol.short.private.gnu"
51 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
53 #define KEYFILE_SHORTEN = "zonefiles/188JSUMKEF25GVU8TTV0PBNNN8JVCPUEDFV1UHJJU884JD25V0T0.zkey"
54 #define KEYFILE_PRIVATE = "zonefiles/OEFL7A4VEF1B40QLEMTG5D8G1CN6EN16QUSG5R2DT71GRJN34LSG.zkey"
58 /* Task handle to use to schedule test failure */
59 static GNUNET_SCHEDULER_TaskIdentifier die_task;
61 /* Global return value (0 for success, anything else for failure) */
64 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
66 static struct GNUNET_GNS_Handle *gns_handle;
68 static struct GNUNET_DHT_Handle *dht_handle;
70 static const struct GNUNET_CONFIGURATION_Handle *cfg;
72 static struct GNUNET_CRYPTO_EccPublicKey alice_pkey;
73 static struct GNUNET_CRYPTO_EccPublicKey bob_pkey;
74 static struct GNUNET_CRYPTO_EccPublicKey our_pkey;
75 static struct GNUNET_CRYPTO_EccPublicKey priv_pkey;
76 static struct GNUNET_CRYPTO_EccPublicKey short_pkey;
77 static struct GNUNET_CRYPTO_EccPrivateKey *alice_key;
78 static struct GNUNET_CRYPTO_EccPrivateKey *bob_key;
79 static struct GNUNET_CRYPTO_EccPrivateKey *our_key;
80 static struct GNUNET_CRYPTO_EccPrivateKey *priv_key;
81 static struct GNUNET_CRYPTO_EccPrivateKey *short_key;
82 static struct GNUNET_CRYPTO_ShortHashCode alice_hash;
83 static struct GNUNET_CRYPTO_ShortHashCode bob_hash;
84 static struct GNUNET_CRYPTO_ShortHashCode our_zone;
85 static struct GNUNET_CRYPTO_ShortHashCode priv_zone;
86 static struct GNUNET_CRYPTO_ShortHashCode short_zone;
90 * Check if the get_handle is being used, if so stop the request. Either
91 * way, schedule the end_badly_cont function which actually shuts down the
95 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
97 die_task = GNUNET_SCHEDULER_NO_TASK;
98 if (NULL != gns_handle)
100 GNUNET_GNS_disconnect(gns_handle);
104 if (NULL != namestore_handle)
106 GNUNET_NAMESTORE_disconnect (namestore_handle);
107 namestore_handle = NULL;
110 if (NULL != dht_handle)
112 GNUNET_DHT_disconnect (dht_handle);
117 GNUNET_SCHEDULER_shutdown ();
125 GNUNET_SCHEDULER_cancel (die_task);
126 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
131 shutdown_task (void *cls,
132 const struct GNUNET_SCHEDULER_TaskContext *tc)
134 GNUNET_GNS_disconnect(gns_handle);
135 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
136 GNUNET_SCHEDULER_shutdown ();
140 * Called when gns shorten finishes
143 process_shorten_result (void* cls, const char* sname)
146 if (GNUNET_SCHEDULER_NO_TASK != die_task)
148 GNUNET_SCHEDULER_cancel (die_task);
149 die_task = GNUNET_SCHEDULER_NO_TASK;
152 if (NULL != dht_handle)
154 GNUNET_DHT_disconnect (dht_handle);
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
161 "shorten test failed!\n");
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167 "%s shortened to %s\n", (char*)cls, sname);
168 if (0 != strcmp(sname, TEST_EXPECTED_RESULT))
170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
171 "shorten test failed! (wanted: %s got: %s)\n",
172 TEST_EXPECTED_RESULT, sname);
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shorten test succeeded!\n");
178 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
183 on_lookup_result (void *cls, uint32_t rd_count,
184 const struct GNUNET_NAMESTORE_RecordData *rd)
192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
193 "Lookup failed, rp_filtering?\n");
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
200 for (i=0; i<rd_count; i++)
202 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
203 if (rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
205 memcpy(&a, rd[i].data, sizeof(a));
207 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
208 if (0 == strcmp(addr, TEST_IP))
210 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
211 "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
221 GNUNET_GNS_shorten_zone (gns_handle, TEST_DOMAIN,
225 &process_shorten_result,
231 * Function scheduled to be run on the successful start of services
232 * tries to look up the dns record for TEST_DOMAIN
235 commence_testing (void *cls, int success)
237 GNUNET_free(our_key);
238 GNUNET_free(bob_key);
239 GNUNET_free(alice_key);
240 GNUNET_NAMESTORE_disconnect (namestore_handle);
241 namestore_handle = NULL;
242 gns_handle = GNUNET_GNS_connect(cfg);
243 if (NULL == gns_handle)
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246 "Failed to connect to GNS!\n");
248 GNUNET_GNS_lookup_zone (gns_handle, TEST_DOMAIN,
250 GNUNET_DNSPARSER_TYPE_A,
253 &on_lookup_result, TEST_DOMAIN);
258 put_pseu_dht (void *cls, int success)
260 struct GNSNameRecordBlock *nrb;
261 struct GNUNET_CRYPTO_ShortHashCode name_hash;
262 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
263 struct GNUNET_HashCode xor_hash;
264 struct GNUNET_HashCode name_hash_double;
265 struct GNUNET_HashCode zone_hash_double;
266 uint32_t rd_payload_length;
267 char* nrb_data = NULL;
268 struct GNUNET_CRYPTO_EccSignature *sig;
269 struct GNUNET_NAMESTORE_RecordData rd;
271 memset (&rd, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
272 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
273 rd.data_size = strlen(TEST_PSEU_ALICE)+1;
274 rd.data = TEST_PSEU_ALICE;
275 rd.record_type = GNUNET_NAMESTORE_TYPE_PSEU;
278 sig = GNUNET_NAMESTORE_create_signature(alice_key,
279 GNUNET_TIME_UNIT_FOREVER_ABS,
280 GNUNET_GNS_MASTERZONE_STR,
283 GNUNET_assert (NULL != sig);
285 GNUNET_break (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&alice_pkey,
286 GNUNET_TIME_UNIT_FOREVER_ABS,
287 GNUNET_GNS_MASTERZONE_STR,
291 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
292 nrb = GNUNET_malloc(rd_payload_length + strlen(GNUNET_GNS_MASTERZONE_STR) + 1
293 + sizeof(struct GNSNameRecordBlock));
294 nrb->signature = *sig;
295 nrb->public_key = alice_pkey;
296 nrb->rd_count = htonl(1);
297 memset(&nrb[1], 0, strlen(GNUNET_GNS_MASTERZONE_STR) + 1);
298 strcpy((char*)&nrb[1], GNUNET_GNS_MASTERZONE_STR);
299 nrb_data = (char*)&nrb[1];
300 nrb_data += strlen(GNUNET_GNS_MASTERZONE_STR) + 1;
302 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
307 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
309 GNUNET_free(our_key);
310 GNUNET_free(bob_key);
311 GNUNET_free(alice_key);
317 GNUNET_CRYPTO_short_hash(GNUNET_GNS_MASTERZONE_STR, strlen(GNUNET_GNS_MASTERZONE_STR), &name_hash);
318 GNUNET_CRYPTO_short_hash(&alice_pkey,
319 sizeof(struct GNUNET_CRYPTO_EccPublicKey),
322 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
323 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
324 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
326 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
327 strlen(GNUNET_GNS_MASTERZONE_STR) + 1;
329 GNUNET_DHT_put (dht_handle, &xor_hash,
331 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
332 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
335 GNUNET_TIME_UNIT_FOREVER_ABS,
336 DHT_OPERATION_TIMEOUT,
346 put_www_dht (void *cls, int success)
348 struct GNSNameRecordBlock *nrb;
349 struct GNUNET_CRYPTO_ShortHashCode name_hash;
350 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
351 struct GNUNET_HashCode xor_hash;
352 struct GNUNET_HashCode name_hash_double;
353 struct GNUNET_HashCode zone_hash_double;
354 uint32_t rd_payload_length;
355 char* nrb_data = NULL;
356 struct GNUNET_CRYPTO_EccSignature *sig;
357 struct GNUNET_NAMESTORE_RecordData rd;
359 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
361 rd.expiration_time = UINT64_MAX;
362 GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
363 rd.data_size = sizeof(struct in_addr);
365 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
366 rd.flags = GNUNET_NAMESTORE_RF_NONE;
368 sig = GNUNET_NAMESTORE_create_signature(alice_key,
369 GNUNET_TIME_UNIT_FOREVER_ABS,
373 GNUNET_break (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&alice_pkey,
374 GNUNET_TIME_UNIT_FOREVER_ABS,
379 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
380 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
381 + sizeof(struct GNSNameRecordBlock));
382 nrb->signature = *sig;
383 nrb->public_key = alice_pkey;
384 nrb->rd_count = htonl(1);
385 memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
386 strcpy((char*)&nrb[1], TEST_RECORD_NAME);
387 nrb_data = (char*)&nrb[1];
388 nrb_data += strlen(TEST_RECORD_NAME) + 1;
390 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
395 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
397 GNUNET_free(our_key);
398 GNUNET_free(bob_key);
399 GNUNET_free(alice_key);
406 GNUNET_CRYPTO_short_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
407 GNUNET_CRYPTO_short_hash(&alice_pkey,
408 sizeof(struct GNUNET_CRYPTO_EccPublicKey),
410 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
411 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
412 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
414 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
415 strlen(TEST_RECORD_NAME) + 1;
417 GNUNET_DHT_put (dht_handle, &xor_hash,
419 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
420 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
423 GNUNET_TIME_UNIT_FOREVER_ABS,
424 DHT_OPERATION_TIMEOUT,
434 put_pkey_dht (void *cls, int32_t success, const char *emsg)
436 struct GNSNameRecordBlock *nrb;
437 struct GNUNET_CRYPTO_ShortHashCode name_hash;
438 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
439 struct GNUNET_HashCode xor_hash;
440 struct GNUNET_HashCode name_hash_double;
441 struct GNUNET_HashCode zone_hash_double;
442 uint32_t rd_payload_length;
443 char* nrb_data = NULL;
444 struct GNUNET_CRYPTO_EccSignature *sig;
445 struct GNUNET_NAMESTORE_RecordData rd;
447 rd.expiration_time = UINT64_MAX;
448 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
449 rd.data = &alice_hash;
450 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
451 rd.flags = GNUNET_NAMESTORE_RF_NONE;
453 sig = GNUNET_NAMESTORE_create_signature (bob_key,
454 GNUNET_TIME_UNIT_FOREVER_ABS,
455 TEST_AUTHORITY_ALICE,
459 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
460 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_AUTHORITY_ALICE) + 1
461 + sizeof(struct GNSNameRecordBlock));
462 nrb->signature = *sig;
463 nrb->public_key = bob_pkey;
464 nrb->rd_count = htonl(1);
465 memset(&nrb[1], 0, strlen(TEST_AUTHORITY_ALICE) + 1);
466 strcpy((char*)&nrb[1], TEST_AUTHORITY_ALICE);
467 nrb_data = (char*)&nrb[1];
468 nrb_data += strlen(TEST_AUTHORITY_ALICE) + 1;
470 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
475 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
478 GNUNET_free (our_key);
479 GNUNET_free (bob_key);
480 GNUNET_free (alice_key);
488 GNUNET_CRYPTO_short_hash (TEST_AUTHORITY_ALICE,
489 strlen (TEST_AUTHORITY_ALICE), &name_hash);
490 GNUNET_CRYPTO_short_hash (&bob_pkey,
491 sizeof(struct GNUNET_CRYPTO_EccPublicKey),
493 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
494 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
495 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
497 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
498 strlen(TEST_AUTHORITY_ALICE) + 1;
499 GNUNET_DHT_put (dht_handle, &xor_hash,
501 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
502 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
505 GNUNET_TIME_UNIT_FOREVER_ABS,
506 DHT_OPERATION_TIMEOUT,
515 fin_init_zone (void *cls, int32_t success, const char *emsg)
517 struct GNUNET_NAMESTORE_RecordData rd;
518 rd.expiration_time = UINT64_MAX;
519 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
521 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
522 rd.flags = GNUNET_NAMESTORE_RF_NONE;
524 GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
534 cont_init_zone (void *cls, int32_t success, const char *emsg)
537 struct GNUNET_NAMESTORE_RecordData rd;
539 rd.expiration_time = UINT64_MAX;
540 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
541 rd.data = &short_zone;
542 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
543 rd.flags = GNUNET_NAMESTORE_RF_NONE;
545 GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
556 const struct GNUNET_CONFIGURATION_Handle *ccfg,
557 struct GNUNET_TESTING_Peer *peer)
559 char *private_keyfile;
560 char *shorten_keyfile;
562 struct GNUNET_NAMESTORE_RecordData rd;
565 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
567 /* put records into namestore */
568 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
569 if (NULL == namestore_handle)
571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
577 dht_handle = GNUNET_DHT_connect(cfg, 1);
578 if (NULL == dht_handle)
580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
585 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
589 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
591 GNUNET_free (our_keyfile);
595 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
600 "Failed to get shorten zone key from cfg\n");
602 GNUNET_free (our_keyfile);
603 GNUNET_free (shorten_keyfile);
607 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
612 "Failed to get private zone key from cfg\n");
614 GNUNET_free (our_keyfile);
615 GNUNET_free (shorten_keyfile);
616 GNUNET_free (private_keyfile);
619 our_key = GNUNET_CRYPTO_ecc_key_create_from_file (our_keyfile);
620 priv_key = GNUNET_CRYPTO_ecc_key_create_from_file (private_keyfile);
621 short_key = GNUNET_CRYPTO_ecc_key_create_from_file (shorten_keyfile);
622 bob_key = GNUNET_CRYPTO_ecc_key_create ();
623 alice_key = GNUNET_CRYPTO_ecc_key_create ();
625 GNUNET_free (our_keyfile);
626 GNUNET_free (shorten_keyfile);
627 GNUNET_free (private_keyfile);
629 GNUNET_CRYPTO_ecc_key_get_public (our_key, &our_pkey);
630 GNUNET_CRYPTO_ecc_key_get_public (priv_key, &priv_pkey);
631 GNUNET_CRYPTO_ecc_key_get_public (short_key, &short_pkey);
632 GNUNET_CRYPTO_ecc_key_get_public (bob_key, &bob_pkey);
633 GNUNET_CRYPTO_ecc_key_get_public (alice_key, &alice_pkey);
634 GNUNET_CRYPTO_short_hash (&bob_pkey, sizeof(bob_pkey), &bob_hash);
635 GNUNET_CRYPTO_short_hash (&alice_pkey, sizeof(alice_pkey), &alice_hash);
636 GNUNET_CRYPTO_short_hash (&our_pkey, sizeof(our_pkey), &our_zone);
637 GNUNET_CRYPTO_short_hash (&priv_pkey, sizeof(priv_pkey), &priv_zone);
638 GNUNET_CRYPTO_short_hash (&short_pkey, sizeof(short_pkey), &short_zone);
640 rd.expiration_time = UINT64_MAX;
641 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
642 rd.data = &priv_zone;
643 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
644 rd.flags = GNUNET_NAMESTORE_RF_NONE;
646 GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
656 main (int argc, char *argv[])
659 GNUNET_log_setup ("test-gns-pseu-shorten",
662 GNUNET_TESTING_peer_run ("test-gns-pseu-shorten", "test_gns_simple_lookup.conf", &do_check, NULL);
666 /* end of test_gns_pseu_shorten.c */