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;
95 * Check whether peers successfully shut down.
98 shutdown_callback (void *cls, const char *emsg)
100 if (disco_task != GNUNET_SCHEDULER_NO_TASK)
102 disco_task = GNUNET_SCHEDULER_NO_TASK;
103 GNUNET_SCHEDULER_cancel(disco_task);
104 GNUNET_DHT_disconnect(dht_handle);
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
115 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
119 disco_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
121 disco_task = GNUNET_SCHEDULER_NO_TASK;
122 GNUNET_DHT_disconnect(dht_handle);
127 * Called when gns shorten finishes
130 process_shorten_result(void* cls, const char* sname)
132 GNUNET_GNS_disconnect(gns_handle);
133 //GNUNET_SCHEDULER_add_now(disco_dht, NULL);
138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
139 "shorten test failed!\n");
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145 "%s shortened to %s\n", (char*)cls, sname);
146 if (0 != strcmp(sname, TEST_EXPECTED_RESULT))
148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
149 "shorten test failed! (wanted: %s got: %s\n",
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shorten test succeeded!\n");
157 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
158 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
162 do_shorten(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
164 GNUNET_GNS_shorten(gns_handle, TEST_DOMAIN, &process_shorten_result,
169 on_lookup_result(void *cls, uint32_t rd_count,
170 const struct GNUNET_NAMESTORE_RecordData *rd)
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179 "Lookup failed, rp_filtering?\n");
185 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
186 for (i=0; i<rd_count; i++)
188 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
189 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
191 memcpy(&a, rd[i].data, sizeof(a));
193 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
194 if (0 == strcmp(addr, TEST_IP))
196 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
197 "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
208 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &do_shorten, NULL);
213 * Function scheduled to be run on the successful start of services
214 * tries to look up the dns record for TEST_DOMAIN
217 commence_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
219 GNUNET_SCHEDULER_add_now(disco_dht, NULL);
220 //GNUNET_DHT_disconnect(dht_handle);
222 GNUNET_CRYPTO_rsa_key_free(our_key);
223 GNUNET_CRYPTO_rsa_key_free(bob_key);
224 GNUNET_CRYPTO_rsa_key_free(alice_key);
226 gns_handle = GNUNET_GNS_connect(cfg);
228 if (NULL == gns_handle)
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 "Failed to connect to GNS!\n");
234 GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_TYPE_A,
235 &on_lookup_result, TEST_DOMAIN);
239 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
240 * down the peers without freeing memory associated with GET request.
243 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
249 if (disco_task != GNUNET_SCHEDULER_NO_TASK)
251 disco_task = GNUNET_SCHEDULER_NO_TASK;
252 GNUNET_SCHEDULER_cancel(disco_task);
253 GNUNET_DHT_disconnect(dht_handle);
257 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
258 GNUNET_SCHEDULER_cancel (die_task);
262 put_pseu_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
264 struct GNSNameRecordBlock *nrb;
265 struct GNUNET_CRYPTO_ShortHashCode name_hash;
266 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
267 GNUNET_HashCode xor_hash;
268 GNUNET_HashCode name_hash_double;
269 GNUNET_HashCode zone_hash_double;
270 uint32_t rd_payload_length;
271 char* nrb_data = NULL;
272 struct GNUNET_CRYPTO_RsaSignature *sig;
273 struct GNUNET_NAMESTORE_RecordData rd;
275 rd.expiration = GNUNET_TIME_absolute_get_forever ();
276 rd.data_size = strlen(TEST_PSEU_ALICE)+1;
277 rd.data = TEST_PSEU_ALICE;
278 rd.record_type = GNUNET_GNS_RECORD_PSEU;
280 sig = GNUNET_NAMESTORE_create_signature(alice_key,
281 GNUNET_TIME_absolute_get_forever(),
284 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
285 nrb = GNUNET_malloc(rd_payload_length + strlen("+") + 1
286 + sizeof(struct GNSNameRecordBlock));
287 nrb->signature = *sig;
288 nrb->public_key = alice_pkey;
289 nrb->rd_count = htonl(1);
290 memset(&nrb[1], 0, strlen("+") + 1);
291 strcpy((char*)&nrb[1], "+");
292 nrb_data = (char*)&nrb[1];
293 nrb_data += strlen("+") + 1;
295 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
300 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
302 GNUNET_DHT_disconnect(dht_handle);
305 GNUNET_CRYPTO_rsa_key_free(our_key);
306 GNUNET_CRYPTO_rsa_key_free(bob_key);
307 GNUNET_CRYPTO_rsa_key_free(alice_key);
312 GNUNET_CRYPTO_short_hash("+", strlen("+"), &name_hash);
313 GNUNET_CRYPTO_short_hash(&alice_pkey,
314 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
317 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
318 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
319 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
321 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
324 GNUNET_DHT_put (dht_handle, &xor_hash,
327 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
331 DHT_OPERATION_TIMEOUT,
340 put_www_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
342 struct GNSNameRecordBlock *nrb;
343 struct GNUNET_CRYPTO_ShortHashCode name_hash;
344 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
345 GNUNET_HashCode xor_hash;
346 GNUNET_HashCode name_hash_double;
347 GNUNET_HashCode zone_hash_double;
348 uint32_t rd_payload_length;
349 char* nrb_data = NULL;
350 struct GNUNET_CRYPTO_RsaSignature *sig;
351 struct GNUNET_NAMESTORE_RecordData rd;
353 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
355 rd.expiration = GNUNET_TIME_absolute_get_forever ();
356 GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
357 rd.data_size = sizeof(struct in_addr);
359 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
361 sig = GNUNET_NAMESTORE_create_signature(alice_key,
362 GNUNET_TIME_absolute_get_forever(),
365 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
366 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
367 + sizeof(struct GNSNameRecordBlock));
368 nrb->signature = *sig;
369 nrb->public_key = alice_pkey;
370 nrb->rd_count = htonl(1);
371 memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
372 strcpy((char*)&nrb[1], TEST_RECORD_NAME);
373 nrb_data = (char*)&nrb[1];
374 nrb_data += strlen(TEST_RECORD_NAME) + 1;
376 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
381 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
383 GNUNET_DHT_disconnect(dht_handle);
385 GNUNET_CRYPTO_rsa_key_free(our_key);
386 GNUNET_CRYPTO_rsa_key_free(bob_key);
387 GNUNET_CRYPTO_rsa_key_free(alice_key);
392 GNUNET_CRYPTO_short_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
393 GNUNET_CRYPTO_short_hash(&alice_pkey,
394 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
396 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
397 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
398 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
400 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
401 strlen(TEST_RECORD_NAME) + 1;
403 GNUNET_DHT_put (dht_handle, &xor_hash,
406 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
410 DHT_OPERATION_TIMEOUT,
420 put_pkey_dht(void *cls, int32_t success, const char *emsg)
422 struct GNSNameRecordBlock *nrb;
423 struct GNUNET_CRYPTO_ShortHashCode name_hash;
424 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
425 GNUNET_HashCode xor_hash;
426 GNUNET_HashCode name_hash_double;
427 GNUNET_HashCode zone_hash_double;
428 uint32_t rd_payload_length;
429 char* nrb_data = NULL;
430 struct GNUNET_CRYPTO_RsaSignature *sig;
431 struct GNUNET_NAMESTORE_RecordData rd;
433 rd.expiration = GNUNET_TIME_absolute_get_forever ();
434 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
435 rd.data = &alice_hash;
436 rd.record_type = GNUNET_GNS_RECORD_PKEY;
438 sig = GNUNET_NAMESTORE_create_signature(bob_key,
439 GNUNET_TIME_absolute_get_forever(),
440 TEST_AUTHORITY_ALICE,
444 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
445 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_AUTHORITY_ALICE) + 1
446 + sizeof(struct GNSNameRecordBlock));
447 nrb->signature = *sig;
448 nrb->public_key = bob_pkey;
449 nrb->rd_count = htonl(1);
450 memset(&nrb[1], 0, strlen(TEST_AUTHORITY_ALICE) + 1);
451 strcpy((char*)&nrb[1], TEST_AUTHORITY_ALICE);
452 nrb_data = (char*)&nrb[1];
453 nrb_data += strlen(TEST_AUTHORITY_ALICE) + 1;
455 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
460 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
463 GNUNET_CRYPTO_rsa_key_free(our_key);
464 GNUNET_CRYPTO_rsa_key_free(bob_key);
465 GNUNET_CRYPTO_rsa_key_free(alice_key);
472 GNUNET_CRYPTO_short_hash(TEST_AUTHORITY_ALICE,
473 strlen(TEST_AUTHORITY_ALICE), &name_hash);
474 GNUNET_CRYPTO_short_hash(&bob_pkey,
475 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
477 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
478 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
479 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
481 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
482 strlen(TEST_AUTHORITY_ALICE) + 1;
483 GNUNET_DHT_put (dht_handle, &xor_hash,
486 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
490 DHT_OPERATION_TIMEOUT,
493 GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_NO);
498 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
499 const struct GNUNET_CONFIGURATION_Handle *_cfg,
500 struct GNUNET_TESTING_Daemon *d, const char *emsg)
508 GNUNET_SCHEDULER_cancel (die_task);
510 /* put records into namestore */
511 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
512 if (NULL == namestore_handle)
514 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
520 dht_handle = GNUNET_DHT_connect(cfg, 1);
521 if (NULL == dht_handle)
523 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
528 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
532 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
537 our_key = GNUNET_CRYPTO_rsa_key_create_from_file (our_keyfile);
538 bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
539 alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_ALICE);
541 GNUNET_free(our_keyfile);
543 GNUNET_CRYPTO_rsa_key_get_public (our_key, &our_pkey);
544 GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
545 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
546 GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
547 GNUNET_CRYPTO_short_hash(&alice_pkey, sizeof(alice_pkey), &alice_hash);
549 struct GNUNET_NAMESTORE_RecordData rd;
550 rd.expiration = GNUNET_TIME_absolute_get_forever ();
551 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
553 rd.record_type = GNUNET_GNS_RECORD_PKEY;
555 GNUNET_NAMESTORE_record_create (namestore_handle,
566 run (void *cls, char *const *args, const char *cfgfile,
567 const struct GNUNET_CONFIGURATION_Handle *c)
570 /* Get path from configuration file */
572 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
580 /* Set up a task to end testing if peer start fails */
582 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
583 "didn't start all daemons in reasonable amount of time!!!");
586 //d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
587 // NULL, NULL, NULL, &do_lookup, NULL);
588 pg = GNUNET_TESTING_daemons_start(cfg, 1, 1, 1, TIMEOUT,
589 NULL, NULL, &do_lookup, NULL,
598 /* Arguments for GNUNET_PROGRAM_run */
599 char *const argv[] = { "test-gns-pseu-shorten", /* Name to give running binary */
601 "test_gns_simple_lookup.conf", /* Config file to use */
607 struct GNUNET_GETOPT_CommandLineOption options[] = {
608 GNUNET_GETOPT_OPTION_END
610 /* Run the run function as a new program */
612 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
613 "test-gns-pseu-shorten", "nohelp", options, &run,
615 if (ret != GNUNET_OK)
617 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
618 "`test-gns-pseu-shorten': Failed with error code %d\n", ret);
624 main (int argc, char *argv[])
628 GNUNET_log_setup ("test-gns-pseu-shorten",
637 * Need to remove base directory, subdirectories taken care
638 * of by the testing framework.
643 /* end of test_gns_twopeer.c */