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_pseu_shorten.c
22 * @brief base testcase for testing on the fly pseu import and shorten
26 #include "gnunet_testing_lib-new.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"
37 #define VERBOSE GNUNET_YES
39 /* Timeout for entire testcase */
40 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
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 "www.alicewonderland.bobbuilder.gads"
47 #define TEST_IP "127.0.0.1"
48 #define TEST_RECORD_NAME "www"
50 #define TEST_PRIVATE_ZONE "private"
51 #define TEST_SHORTEN_ZONE "short"
52 #define TEST_AUTHORITY_BOB "bobbuilder"
53 #define TEST_AUTHORITY_ALICE "alicewonderland"
54 #define TEST_PSEU_ALICE "carol"
55 #define TEST_EXPECTED_RESULT "www.carol.short.private.gads"
57 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
59 #define KEYFILE_SHORTEN = "zonefiles/188JSUMKEF25GVU8TTV0PBNNN8JVCPUEDFV1UHJJU884JD25V0T0.zkey"
60 #define KEYFILE_PRIVATE = "zonefiles/OEFL7A4VEF1B40QLEMTG5D8G1CN6EN16QUSG5R2DT71GRJN34LSG.zkey"
61 #define KEYFILE_BOB "../namestore/zonefiles/HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"
62 #define KEYFILE_ALICE "../namestore/zonefiles/N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"
66 /* Task handle to use to schedule test failure */
67 static 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 static struct GNUNET_DHT_Handle *dht_handle;
78 const struct GNUNET_CONFIGURATION_Handle *cfg;
80 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
81 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
82 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded our_pkey;
83 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded priv_pkey;
84 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded short_pkey;
85 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
86 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
87 struct GNUNET_CRYPTO_RsaPrivateKey *our_key;
88 struct GNUNET_CRYPTO_RsaPrivateKey *priv_key;
89 struct GNUNET_CRYPTO_RsaPrivateKey *short_key;
90 struct GNUNET_CRYPTO_ShortHashCode alice_hash;
91 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
92 struct GNUNET_CRYPTO_ShortHashCode our_zone;
93 struct GNUNET_CRYPTO_ShortHashCode priv_zone;
94 struct GNUNET_CRYPTO_ShortHashCode short_zone;
98 * Check if the get_handle is being used, if so stop the request. Either
99 * way, schedule the end_badly_cont function which actually shuts down the
103 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
105 die_task = GNUNET_SCHEDULER_NO_TASK;
106 if (NULL != gns_handle)
108 GNUNET_GNS_disconnect(gns_handle);
112 if (NULL != namestore_handle)
114 GNUNET_NAMESTORE_disconnect (namestore_handle);
115 namestore_handle = NULL;
118 if (NULL != dht_handle)
120 GNUNET_DHT_disconnect (dht_handle);
125 GNUNET_SCHEDULER_shutdown ();
132 GNUNET_SCHEDULER_cancel (die_task);
133 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
136 static void shutdown_task (void *cls,
137 const struct GNUNET_SCHEDULER_TaskContext *tc)
139 GNUNET_GNS_disconnect(gns_handle);
140 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
141 GNUNET_SCHEDULER_shutdown ();
145 * Called when gns shorten finishes
148 process_shorten_result(void* cls, const char* sname)
151 if (GNUNET_SCHEDULER_NO_TASK != die_task)
153 GNUNET_SCHEDULER_cancel (die_task);
154 die_task = GNUNET_SCHEDULER_NO_TASK;
157 if (NULL != dht_handle)
159 GNUNET_DHT_disconnect (dht_handle);
165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
166 "shorten test failed!\n");
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172 "%s shortened to %s\n", (char*)cls, sname);
173 if (0 != strcmp(sname, TEST_EXPECTED_RESULT))
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176 "shorten test failed! (wanted: %s got: %s\n",
177 TEST_EXPECTED_RESULT, sname);
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shorten test succeeded!\n");
183 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
188 on_lookup_result(void *cls, uint32_t rd_count,
189 const struct GNUNET_NAMESTORE_RecordData *rd)
197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
198 "Lookup failed, rp_filtering?\n");
204 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
205 for (i=0; i<rd_count; i++)
207 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
208 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
210 memcpy(&a, rd[i].data, sizeof(a));
212 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
213 if (0 == strcmp(addr, TEST_IP))
215 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
216 "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
226 GNUNET_GNS_shorten_zone (gns_handle, TEST_DOMAIN,
230 &process_shorten_result,
236 * Function scheduled to be run on the successful start of services
237 * tries to look up the dns record for TEST_DOMAIN
240 commence_testing (void *cls, int success)
243 GNUNET_CRYPTO_rsa_key_free(our_key);
244 GNUNET_CRYPTO_rsa_key_free(bob_key);
245 GNUNET_CRYPTO_rsa_key_free(alice_key);
247 GNUNET_NAMESTORE_disconnect (namestore_handle);
248 namestore_handle = NULL;
250 gns_handle = GNUNET_GNS_connect(cfg);
251 if (NULL == gns_handle)
253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
254 "Failed to connect to GNS!\n");
257 GNUNET_GNS_lookup_zone (gns_handle, TEST_DOMAIN,
262 &on_lookup_result, TEST_DOMAIN);
267 put_pseu_dht(void *cls, int success)
269 struct GNSNameRecordBlock *nrb;
270 struct GNUNET_CRYPTO_ShortHashCode name_hash;
271 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
272 struct GNUNET_HashCode xor_hash;
273 struct GNUNET_HashCode name_hash_double;
274 struct GNUNET_HashCode zone_hash_double;
275 uint32_t rd_payload_length;
276 char* nrb_data = NULL;
277 struct GNUNET_CRYPTO_RsaSignature *sig;
278 struct GNUNET_NAMESTORE_RecordData rd;
280 memset (&rd, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
281 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
282 rd.data_size = strlen(TEST_PSEU_ALICE)+1;
283 rd.data = TEST_PSEU_ALICE;
284 rd.record_type = GNUNET_GNS_RECORD_PSEU;
287 sig = GNUNET_NAMESTORE_create_signature(alice_key,
288 GNUNET_TIME_UNIT_FOREVER_ABS,
292 GNUNET_assert (NULL != sig);
294 GNUNET_break (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&alice_pkey,
295 GNUNET_TIME_UNIT_FOREVER_ABS,
300 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
301 nrb = GNUNET_malloc(rd_payload_length + strlen("+") + 1
302 + sizeof(struct GNSNameRecordBlock));
303 nrb->signature = *sig;
304 nrb->public_key = alice_pkey;
305 nrb->rd_count = htonl(1);
306 memset(&nrb[1], 0, strlen("+") + 1);
307 strcpy((char*)&nrb[1], "+");
308 nrb_data = (char*)&nrb[1];
309 nrb_data += strlen("+") + 1;
311 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
316 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
318 GNUNET_CRYPTO_rsa_key_free(our_key);
319 GNUNET_CRYPTO_rsa_key_free(bob_key);
320 GNUNET_CRYPTO_rsa_key_free(alice_key);
326 GNUNET_CRYPTO_short_hash("+", strlen("+"), &name_hash);
327 GNUNET_CRYPTO_short_hash(&alice_pkey,
328 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
331 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
332 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
333 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
335 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
338 GNUNET_DHT_put (dht_handle, &xor_hash,
340 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
341 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
344 GNUNET_TIME_UNIT_FOREVER_ABS,
345 DHT_OPERATION_TIMEOUT,
354 put_www_dht(void *cls, int success)
356 struct GNSNameRecordBlock *nrb;
357 struct GNUNET_CRYPTO_ShortHashCode name_hash;
358 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
359 struct GNUNET_HashCode xor_hash;
360 struct GNUNET_HashCode name_hash_double;
361 struct GNUNET_HashCode zone_hash_double;
362 uint32_t rd_payload_length;
363 char* nrb_data = NULL;
364 struct GNUNET_CRYPTO_RsaSignature *sig;
365 struct GNUNET_NAMESTORE_RecordData rd;
367 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
369 rd.expiration_time = UINT64_MAX;
370 GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
371 rd.data_size = sizeof(struct in_addr);
373 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
374 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
376 sig = GNUNET_NAMESTORE_create_signature(alice_key,
377 GNUNET_TIME_UNIT_FOREVER_ABS,
381 GNUNET_break (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&alice_pkey,
382 GNUNET_TIME_UNIT_FOREVER_ABS,
387 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
388 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
389 + sizeof(struct GNSNameRecordBlock));
390 nrb->signature = *sig;
391 nrb->public_key = alice_pkey;
392 nrb->rd_count = htonl(1);
393 memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
394 strcpy((char*)&nrb[1], TEST_RECORD_NAME);
395 nrb_data = (char*)&nrb[1];
396 nrb_data += strlen(TEST_RECORD_NAME) + 1;
398 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
403 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
405 GNUNET_CRYPTO_rsa_key_free(our_key);
406 GNUNET_CRYPTO_rsa_key_free(bob_key);
407 GNUNET_CRYPTO_rsa_key_free(alice_key);
414 GNUNET_CRYPTO_short_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
415 GNUNET_CRYPTO_short_hash(&alice_pkey,
416 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
418 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
419 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
420 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
422 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
423 strlen(TEST_RECORD_NAME) + 1;
425 GNUNET_DHT_put (dht_handle, &xor_hash,
427 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
428 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
431 GNUNET_TIME_UNIT_FOREVER_ABS,
432 DHT_OPERATION_TIMEOUT,
442 put_pkey_dht(void *cls, int32_t success, const char *emsg)
444 struct GNSNameRecordBlock *nrb;
445 struct GNUNET_CRYPTO_ShortHashCode name_hash;
446 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
447 struct GNUNET_HashCode xor_hash;
448 struct GNUNET_HashCode name_hash_double;
449 struct GNUNET_HashCode zone_hash_double;
450 uint32_t rd_payload_length;
451 char* nrb_data = NULL;
452 struct GNUNET_CRYPTO_RsaSignature *sig;
453 struct GNUNET_NAMESTORE_RecordData rd;
455 rd.expiration_time = UINT64_MAX;
456 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
457 rd.data = &alice_hash;
458 rd.record_type = GNUNET_GNS_RECORD_PKEY;
459 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
461 sig = GNUNET_NAMESTORE_create_signature(bob_key,
462 GNUNET_TIME_UNIT_FOREVER_ABS,
463 TEST_AUTHORITY_ALICE,
467 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
468 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_AUTHORITY_ALICE) + 1
469 + sizeof(struct GNSNameRecordBlock));
470 nrb->signature = *sig;
471 nrb->public_key = bob_pkey;
472 nrb->rd_count = htonl(1);
473 memset(&nrb[1], 0, strlen(TEST_AUTHORITY_ALICE) + 1);
474 strcpy((char*)&nrb[1], TEST_AUTHORITY_ALICE);
475 nrb_data = (char*)&nrb[1];
476 nrb_data += strlen(TEST_AUTHORITY_ALICE) + 1;
478 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
483 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
486 GNUNET_CRYPTO_rsa_key_free (our_key);
487 GNUNET_CRYPTO_rsa_key_free (bob_key);
488 GNUNET_CRYPTO_rsa_key_free (alice_key);
496 GNUNET_CRYPTO_short_hash(TEST_AUTHORITY_ALICE,
497 strlen(TEST_AUTHORITY_ALICE), &name_hash);
498 GNUNET_CRYPTO_short_hash(&bob_pkey,
499 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
501 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
502 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
503 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
505 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
506 strlen(TEST_AUTHORITY_ALICE) + 1;
507 GNUNET_DHT_put (dht_handle, &xor_hash,
509 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
510 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
513 GNUNET_TIME_UNIT_FOREVER_ABS,
514 DHT_OPERATION_TIMEOUT,
522 fin_init_zone (void *cls, int32_t success, const char *emsg)
524 struct GNUNET_NAMESTORE_RecordData rd;
525 rd.expiration_time = UINT64_MAX;
526 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
528 rd.record_type = GNUNET_GNS_RECORD_PKEY;
529 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
531 GNUNET_NAMESTORE_record_create (namestore_handle,
541 cont_init_zone (void *cls, int32_t success, const char *emsg)
544 struct GNUNET_NAMESTORE_RecordData rd;
545 rd.expiration_time = UINT64_MAX;
546 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
547 rd.data = &short_zone;
548 rd.record_type = GNUNET_GNS_RECORD_PKEY;
549 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
551 GNUNET_NAMESTORE_record_create (namestore_handle,
561 const struct GNUNET_CONFIGURATION_Handle *ccfg,
562 struct GNUNET_TESTING_Peer *peer)
564 char* private_keyfile;
565 char* shorten_keyfile;
569 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
571 /* put records into namestore */
572 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
573 if (NULL == namestore_handle)
575 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
581 dht_handle = GNUNET_DHT_connect(cfg, 1);
582 if (NULL == dht_handle)
584 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
589 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
593 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
598 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
602 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
603 "Failed to get shorten zone key from cfg\n");
608 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
612 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
613 "Failed to get private zone key from cfg\n");
617 our_key = GNUNET_CRYPTO_rsa_key_create_from_file (our_keyfile);
618 priv_key = GNUNET_CRYPTO_rsa_key_create_from_file (private_keyfile);
619 short_key = GNUNET_CRYPTO_rsa_key_create_from_file (shorten_keyfile);
620 bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
621 alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_ALICE);
623 GNUNET_free(our_keyfile);
624 GNUNET_free(shorten_keyfile);
625 GNUNET_free(private_keyfile);
627 GNUNET_CRYPTO_rsa_key_get_public (our_key, &our_pkey);
628 GNUNET_CRYPTO_rsa_key_get_public (priv_key, &priv_pkey);
629 GNUNET_CRYPTO_rsa_key_get_public (short_key, &short_pkey);
630 GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
631 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
632 GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
633 GNUNET_CRYPTO_short_hash(&alice_pkey, sizeof(alice_pkey), &alice_hash);
634 GNUNET_CRYPTO_short_hash(&our_pkey, sizeof(our_pkey), &our_zone);
635 GNUNET_CRYPTO_short_hash(&priv_pkey, sizeof(priv_pkey), &priv_zone);
636 GNUNET_CRYPTO_short_hash(&short_pkey, sizeof(short_pkey), &short_zone);
638 struct GNUNET_NAMESTORE_RecordData rd;
639 rd.expiration_time = UINT64_MAX;
640 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
641 rd.data = &priv_zone;
642 rd.record_type = GNUNET_GNS_RECORD_PKEY;
643 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
645 GNUNET_NAMESTORE_record_create (namestore_handle,
654 main (int argc, char *argv[])
658 GNUNET_log_setup ("test-gns-pseu-shorten",
665 GNUNET_TESTING_peer_run ("test-gns-pseu-shorten", "test_gns_simple_lookup.conf", &do_check, NULL);
669 /* end of test_gns_pseu_shorten.c */