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.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, 5)
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.alice.bob.gnunet"
47 #define TEST_IP "127.0.0.1"
48 #define TEST_RECORD_NAME "www"
50 #define TEST_AUTHORITY_BOB "bob"
51 #define TEST_AUTHORITY_ALICE "alice"
52 #define TEST_PSEU_ALICE "carol"
53 #define TEST_EXPECTED_RESULT "www.carol.gnunet"
55 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
57 #define KEYFILE_BOB "../namestore/zonefiles/HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"
58 #define KEYFILE_ALICE "../namestore/zonefiles/N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"
63 * Directory to store temp data in, defined in config file
65 static char *test_directory;
67 static struct GNUNET_TESTING_PeerGroup *pg;
69 /* Task handle to use to schedule test failure */
70 static GNUNET_SCHEDULER_TaskIdentifier die_task;
72 static GNUNET_SCHEDULER_TaskIdentifier disco_task;
74 /* Global return value (0 for success, anything else for failure) */
77 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
79 static struct GNUNET_GNS_Handle *gns_handle;
81 static struct GNUNET_DHT_Handle *dht_handle;
83 const struct GNUNET_CONFIGURATION_Handle *cfg;
85 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
86 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
87 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded our_pkey;
88 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
89 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
90 struct GNUNET_CRYPTO_RsaPrivateKey *our_key;
91 struct GNUNET_CRYPTO_ShortHashCode alice_hash;
92 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
93 struct GNUNET_CRYPTO_ShortHashCode our_zone;
96 * Check whether peers successfully shut down.
99 shutdown_callback (void *cls, const char *emsg)
101 if (disco_task != GNUNET_SCHEDULER_NO_TASK)
103 disco_task = GNUNET_SCHEDULER_NO_TASK;
104 GNUNET_SCHEDULER_cancel(disco_task);
105 GNUNET_DHT_disconnect(dht_handle);
111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
116 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
120 disco_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
122 disco_task = GNUNET_SCHEDULER_NO_TASK;
123 GNUNET_DHT_disconnect(dht_handle);
128 * Called when gns shorten finishes
131 process_shorten_result(void* cls, const char* sname)
133 GNUNET_GNS_disconnect(gns_handle);
134 //GNUNET_SCHEDULER_add_now(disco_dht, NULL);
139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
140 "shorten test failed!\n");
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "%s shortened to %s\n", (char*)cls, sname);
147 if (0 != strcmp(sname, TEST_EXPECTED_RESULT))
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150 "shorten test failed! (wanted: %s got: %s\n",
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shorten test succeeded!\n");
158 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
159 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
163 do_shorten(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
165 GNUNET_GNS_shorten_zone (gns_handle, TEST_DOMAIN,
166 &our_zone, &our_zone,
167 &process_shorten_result,
172 on_lookup_result(void *cls, uint32_t rd_count,
173 const struct GNUNET_NAMESTORE_RecordData *rd)
181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182 "Lookup failed, rp_filtering?\n");
188 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
189 for (i=0; i<rd_count; i++)
191 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
192 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
194 memcpy(&a, rd[i].data, sizeof(a));
196 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
197 if (0 == strcmp(addr, TEST_IP))
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
200 "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
211 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &do_shorten, NULL);
216 * Function scheduled to be run on the successful start of services
217 * tries to look up the dns record for TEST_DOMAIN
220 commence_testing (void *cls, int success)
222 GNUNET_SCHEDULER_add_now(disco_dht, NULL);
223 //GNUNET_DHT_disconnect(dht_handle);
225 GNUNET_CRYPTO_rsa_key_free(our_key);
226 GNUNET_CRYPTO_rsa_key_free(bob_key);
227 GNUNET_CRYPTO_rsa_key_free(alice_key);
229 gns_handle = GNUNET_GNS_connect(cfg);
231 if (NULL == gns_handle)
233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
234 "Failed to connect to GNS!\n");
237 GNUNET_GNS_lookup_zone (gns_handle, TEST_DOMAIN,
238 &our_zone, &our_zone,
239 GNUNET_GNS_RECORD_TYPE_A,
241 &on_lookup_result, TEST_DOMAIN);
245 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
246 * down the peers without freeing memory associated with GET request.
249 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
255 if (disco_task != GNUNET_SCHEDULER_NO_TASK)
257 disco_task = GNUNET_SCHEDULER_NO_TASK;
258 GNUNET_SCHEDULER_cancel(disco_task);
259 GNUNET_DHT_disconnect(dht_handle);
263 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
264 GNUNET_SCHEDULER_cancel (die_task);
268 put_pseu_dht(void *cls, int success)
270 struct GNSNameRecordBlock *nrb;
271 struct GNUNET_CRYPTO_ShortHashCode name_hash;
272 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
273 struct GNUNET_HashCode xor_hash;
274 struct GNUNET_HashCode name_hash_double;
275 struct GNUNET_HashCode zone_hash_double;
276 uint32_t rd_payload_length;
277 char* nrb_data = NULL;
278 struct GNUNET_CRYPTO_RsaSignature *sig;
279 struct GNUNET_NAMESTORE_RecordData rd;
281 rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
282 rd.data_size = strlen(TEST_PSEU_ALICE)+1;
283 rd.data = TEST_PSEU_ALICE;
284 rd.record_type = GNUNET_GNS_RECORD_PSEU;
286 sig = GNUNET_NAMESTORE_create_signature(alice_key,
287 GNUNET_TIME_UNIT_FOREVER_ABS,
290 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
291 nrb = GNUNET_malloc(rd_payload_length + strlen("+") + 1
292 + sizeof(struct GNSNameRecordBlock));
293 nrb->signature = *sig;
294 nrb->public_key = alice_pkey;
295 nrb->rd_count = htonl(1);
296 memset(&nrb[1], 0, strlen("+") + 1);
297 strcpy((char*)&nrb[1], "+");
298 nrb_data = (char*)&nrb[1];
299 nrb_data += strlen("+") + 1;
301 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
306 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
308 GNUNET_DHT_disconnect(dht_handle);
311 GNUNET_CRYPTO_rsa_key_free(our_key);
312 GNUNET_CRYPTO_rsa_key_free(bob_key);
313 GNUNET_CRYPTO_rsa_key_free(alice_key);
318 GNUNET_CRYPTO_short_hash("+", strlen("+"), &name_hash);
319 GNUNET_CRYPTO_short_hash(&alice_pkey,
320 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
323 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
324 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
325 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
327 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
330 GNUNET_DHT_put (dht_handle, &xor_hash,
333 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
337 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_RsaSignature *sig;
357 struct GNUNET_NAMESTORE_RecordData rd;
359 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
361 rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
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;
367 sig = GNUNET_NAMESTORE_create_signature(alice_key,
368 GNUNET_TIME_UNIT_FOREVER_ABS,
371 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
372 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
373 + sizeof(struct GNSNameRecordBlock));
374 nrb->signature = *sig;
375 nrb->public_key = alice_pkey;
376 nrb->rd_count = htonl(1);
377 memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
378 strcpy((char*)&nrb[1], TEST_RECORD_NAME);
379 nrb_data = (char*)&nrb[1];
380 nrb_data += strlen(TEST_RECORD_NAME) + 1;
382 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
387 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
389 GNUNET_DHT_disconnect(dht_handle);
391 GNUNET_CRYPTO_rsa_key_free(our_key);
392 GNUNET_CRYPTO_rsa_key_free(bob_key);
393 GNUNET_CRYPTO_rsa_key_free(alice_key);
398 GNUNET_CRYPTO_short_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
399 GNUNET_CRYPTO_short_hash(&alice_pkey,
400 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
402 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
403 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
404 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
406 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
407 strlen(TEST_RECORD_NAME) + 1;
409 GNUNET_DHT_put (dht_handle, &xor_hash,
412 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
416 DHT_OPERATION_TIMEOUT,
426 put_pkey_dht(void *cls, int32_t success, const char *emsg)
428 struct GNSNameRecordBlock *nrb;
429 struct GNUNET_CRYPTO_ShortHashCode name_hash;
430 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
431 struct GNUNET_HashCode xor_hash;
432 struct GNUNET_HashCode name_hash_double;
433 struct GNUNET_HashCode zone_hash_double;
434 uint32_t rd_payload_length;
435 char* nrb_data = NULL;
436 struct GNUNET_CRYPTO_RsaSignature *sig;
437 struct GNUNET_NAMESTORE_RecordData rd;
439 rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
440 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
441 rd.data = &alice_hash;
442 rd.record_type = GNUNET_GNS_RECORD_PKEY;
444 sig = GNUNET_NAMESTORE_create_signature(bob_key,
445 GNUNET_TIME_UNIT_FOREVER_ABS,
446 TEST_AUTHORITY_ALICE,
450 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
451 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_AUTHORITY_ALICE) + 1
452 + sizeof(struct GNSNameRecordBlock));
453 nrb->signature = *sig;
454 nrb->public_key = bob_pkey;
455 nrb->rd_count = htonl(1);
456 memset(&nrb[1], 0, strlen(TEST_AUTHORITY_ALICE) + 1);
457 strcpy((char*)&nrb[1], TEST_AUTHORITY_ALICE);
458 nrb_data = (char*)&nrb[1];
459 nrb_data += strlen(TEST_AUTHORITY_ALICE) + 1;
461 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
466 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
469 GNUNET_CRYPTO_rsa_key_free(our_key);
470 GNUNET_CRYPTO_rsa_key_free(bob_key);
471 GNUNET_CRYPTO_rsa_key_free(alice_key);
478 GNUNET_CRYPTO_short_hash(TEST_AUTHORITY_ALICE,
479 strlen(TEST_AUTHORITY_ALICE), &name_hash);
480 GNUNET_CRYPTO_short_hash(&bob_pkey,
481 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
483 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
484 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
485 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
487 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
488 strlen(TEST_AUTHORITY_ALICE) + 1;
489 GNUNET_DHT_put (dht_handle, &xor_hash,
492 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
496 DHT_OPERATION_TIMEOUT,
499 GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_NO);
504 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
505 const struct GNUNET_CONFIGURATION_Handle *_cfg,
506 struct GNUNET_TESTING_Daemon *d, const char *emsg)
514 GNUNET_SCHEDULER_cancel (die_task);
516 /* put records into namestore */
517 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
518 if (NULL == namestore_handle)
520 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
526 dht_handle = GNUNET_DHT_connect(cfg, 1);
527 if (NULL == dht_handle)
529 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
534 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
538 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
543 our_key = GNUNET_CRYPTO_rsa_key_create_from_file (our_keyfile);
544 bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
545 alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_ALICE);
547 GNUNET_free(our_keyfile);
549 GNUNET_CRYPTO_rsa_key_get_public (our_key, &our_pkey);
550 GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
551 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
552 GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
553 GNUNET_CRYPTO_short_hash(&alice_pkey, sizeof(alice_pkey), &alice_hash);
554 GNUNET_CRYPTO_short_hash(&our_pkey, sizeof(our_pkey), &our_zone);
556 struct GNUNET_NAMESTORE_RecordData rd;
557 rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
558 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
560 rd.record_type = GNUNET_GNS_RECORD_PKEY;
562 GNUNET_NAMESTORE_record_create (namestore_handle,
573 run (void *cls, char *const *args, const char *cfgfile,
574 const struct GNUNET_CONFIGURATION_Handle *c)
577 /* Get path from configuration file */
579 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
587 /* Set up a task to end testing if peer start fails */
589 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
590 "didn't start all daemons in reasonable amount of time!!!");
593 //d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
594 // NULL, NULL, NULL, &do_lookup, NULL);
595 pg = GNUNET_TESTING_daemons_start(cfg, 1, 1, 1, TIMEOUT,
596 NULL, NULL, &do_lookup, NULL,
605 /* Arguments for GNUNET_PROGRAM_run */
606 char *const argv[] = { "test-gns-pseu-shorten", /* Name to give running binary */
608 "test_gns_simple_lookup.conf", /* Config file to use */
614 struct GNUNET_GETOPT_CommandLineOption options[] = {
615 GNUNET_GETOPT_OPTION_END
617 /* Run the run function as a new program */
619 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
620 "test-gns-pseu-shorten", "nohelp", options, &run,
622 if (ret != GNUNET_OK)
624 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
625 "`test-gns-pseu-shorten': Failed with error code %d\n", ret);
631 main (int argc, char *argv[])
635 GNUNET_log_setup ("test-gns-pseu-shorten",
644 * Need to remove base directory, subdirectories taken care
645 * of by the testing framework.
650 /* end of test_gns_twopeer.c */