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)
60 * Directory to store temp data in, defined in config file
62 static char *test_directory;
64 struct GNUNET_TESTING_Daemon *d1;
67 /* Task handle to use to schedule test failure */
68 GNUNET_SCHEDULER_TaskIdentifier die_task;
70 /* Global return value (0 for success, anything else for failure) */
73 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
75 static struct GNUNET_GNS_Handle *gns_handle;
77 static struct GNUNET_DHT_Handle *dht_handle;
79 const struct GNUNET_CONFIGURATION_Handle *cfg;
81 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
82 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
83 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded our_pkey;
84 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
85 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
86 struct GNUNET_CRYPTO_RsaPrivateKey *our_key;
87 struct GNUNET_CRYPTO_ShortHashCode alice_hash;
88 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
91 * Check whether peers successfully shut down.
94 shutdown_callback (void *cls, const char *emsg)
98 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
103 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
107 * Called when gns shorten finishes
110 process_shorten_result(void* cls, const char* sname)
112 GNUNET_GNS_disconnect(gns_handle);
117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118 "shorten test failed!\n");
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
124 "%s shortened to %s\n", (char*)cls, sname);
125 if (0 != strcmp(sname, TEST_EXPECTED_RESULT))
127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
128 "shorten test failed! (wanted: %s got: %s\n",
133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shorten test succeeded!\n");
136 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
137 GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
138 GNUNET_YES, GNUNET_NO);
142 do_shorten(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 GNUNET_GNS_shorten(gns_handle, TEST_DOMAIN, &process_shorten_result,
149 on_lookup_result(void *cls, uint32_t rd_count,
150 const struct GNUNET_NAMESTORE_RecordData *rd)
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159 "Lookup failed, rp_filtering?\n");
165 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
166 for (i=0; i<rd_count; i++)
168 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
169 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
171 memcpy(&a, rd[i].data, sizeof(a));
173 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
174 if (0 == strcmp(addr, TEST_IP))
176 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
177 "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
188 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &do_shorten, NULL);
193 * Function scheduled to be run on the successful start of services
194 * tries to look up the dns record for TEST_DOMAIN
197 commence_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
199 GNUNET_DHT_disconnect(dht_handle);
201 GNUNET_CRYPTO_rsa_key_free(our_key);
202 GNUNET_CRYPTO_rsa_key_free(bob_key);
203 GNUNET_CRYPTO_rsa_key_free(alice_key);
205 gns_handle = GNUNET_GNS_connect(cfg);
207 if (NULL == gns_handle)
209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210 "Failed to connect to GNS!\n");
213 GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_TYPE_A,
214 &on_lookup_result, TEST_DOMAIN);
218 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
219 * down the peers without freeing memory associated with GET request.
222 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
226 GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
227 GNUNET_YES, GNUNET_NO);
228 GNUNET_SCHEDULER_cancel (die_task);
232 * Check if the get_handle is being used, if so stop the request. Either
233 * way, schedule the end_badly_cont function which actually shuts down the
237 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
241 GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
246 put_pseu_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
248 struct GNSNameRecordBlock *nrb;
249 struct GNUNET_CRYPTO_ShortHashCode name_hash;
250 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
251 GNUNET_HashCode xor_hash;
252 GNUNET_HashCode name_hash_double;
253 GNUNET_HashCode zone_hash_double;
254 uint32_t rd_payload_length;
255 char* nrb_data = NULL;
256 struct GNUNET_CRYPTO_RsaSignature *sig;
257 struct GNUNET_NAMESTORE_RecordData rd;
259 rd.expiration = GNUNET_TIME_absolute_get_forever ();
260 rd.data_size = strlen(TEST_PSEU_ALICE)+1;
261 rd.data = TEST_PSEU_ALICE;
262 rd.record_type = GNUNET_GNS_RECORD_PSEU;
264 sig = GNUNET_NAMESTORE_create_signature(alice_key,
265 GNUNET_TIME_absolute_get_forever(),
268 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
269 nrb = GNUNET_malloc(rd_payload_length + strlen("+") + 1
270 + sizeof(struct GNSNameRecordBlock));
271 nrb->signature = *sig;
272 nrb->public_key = alice_pkey;
273 nrb->rd_count = htonl(1);
274 memset(&nrb[1], 0, strlen("+") + 1);
275 strcpy((char*)&nrb[1], "+");
276 nrb_data = (char*)&nrb[1];
277 nrb_data += strlen("+") + 1;
279 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
284 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
286 GNUNET_DHT_disconnect(dht_handle);
289 GNUNET_CRYPTO_rsa_key_free(our_key);
290 GNUNET_CRYPTO_rsa_key_free(bob_key);
291 GNUNET_CRYPTO_rsa_key_free(alice_key);
296 GNUNET_CRYPTO_short_hash("+", strlen("+"), &name_hash);
297 GNUNET_CRYPTO_short_hash(&alice_pkey,
298 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
301 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
302 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
303 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
305 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
308 GNUNET_DHT_put (dht_handle, &xor_hash,
311 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
315 DHT_OPERATION_TIMEOUT,
324 put_www_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
326 struct GNSNameRecordBlock *nrb;
327 struct GNUNET_CRYPTO_ShortHashCode name_hash;
328 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
329 GNUNET_HashCode xor_hash;
330 GNUNET_HashCode name_hash_double;
331 GNUNET_HashCode zone_hash_double;
332 uint32_t rd_payload_length;
333 char* nrb_data = NULL;
334 struct GNUNET_CRYPTO_RsaSignature *sig;
335 struct GNUNET_NAMESTORE_RecordData rd;
337 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
339 rd.expiration = GNUNET_TIME_absolute_get_forever ();
340 GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
341 rd.data_size = sizeof(struct in_addr);
343 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
345 sig = GNUNET_NAMESTORE_create_signature(alice_key,
346 GNUNET_TIME_absolute_get_forever(),
349 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
350 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
351 + sizeof(struct GNSNameRecordBlock));
352 nrb->signature = *sig;
353 nrb->public_key = alice_pkey;
354 nrb->rd_count = htonl(1);
355 memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
356 strcpy((char*)&nrb[1], TEST_RECORD_NAME);
357 nrb_data = (char*)&nrb[1];
358 nrb_data += strlen(TEST_RECORD_NAME) + 1;
360 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
365 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
367 GNUNET_DHT_disconnect(dht_handle);
369 GNUNET_CRYPTO_rsa_key_free(our_key);
370 GNUNET_CRYPTO_rsa_key_free(bob_key);
371 GNUNET_CRYPTO_rsa_key_free(alice_key);
376 GNUNET_CRYPTO_short_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
377 GNUNET_CRYPTO_short_hash(&alice_pkey,
378 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
380 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
381 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
382 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
384 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
385 strlen(TEST_RECORD_NAME) + 1;
387 GNUNET_DHT_put (dht_handle, &xor_hash,
390 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
394 DHT_OPERATION_TIMEOUT,
404 put_pkey_dht(void *cls, int32_t success, const char *emsg)
406 struct GNSNameRecordBlock *nrb;
407 struct GNUNET_CRYPTO_ShortHashCode name_hash;
408 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
409 GNUNET_HashCode xor_hash;
410 GNUNET_HashCode name_hash_double;
411 GNUNET_HashCode zone_hash_double;
412 uint32_t rd_payload_length;
413 char* nrb_data = NULL;
414 struct GNUNET_CRYPTO_RsaSignature *sig;
415 struct GNUNET_NAMESTORE_RecordData rd;
417 rd.expiration = GNUNET_TIME_absolute_get_forever ();
418 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
419 rd.data = &alice_hash;
420 rd.record_type = GNUNET_GNS_RECORD_PKEY;
422 sig = GNUNET_NAMESTORE_create_signature(bob_key,
423 GNUNET_TIME_absolute_get_forever(),
424 TEST_AUTHORITY_ALICE,
428 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
429 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_AUTHORITY_ALICE) + 1
430 + sizeof(struct GNSNameRecordBlock));
431 nrb->signature = *sig;
432 nrb->public_key = bob_pkey;
433 nrb->rd_count = htonl(1);
434 memset(&nrb[1], 0, strlen(TEST_AUTHORITY_ALICE) + 1);
435 strcpy((char*)&nrb[1], TEST_AUTHORITY_ALICE);
436 nrb_data = (char*)&nrb[1];
437 nrb_data += strlen(TEST_AUTHORITY_ALICE) + 1;
439 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
444 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
447 GNUNET_CRYPTO_rsa_key_free(our_key);
448 GNUNET_CRYPTO_rsa_key_free(bob_key);
449 GNUNET_CRYPTO_rsa_key_free(alice_key);
456 GNUNET_CRYPTO_short_hash(TEST_AUTHORITY_ALICE,
457 strlen(TEST_AUTHORITY_ALICE), &name_hash);
458 GNUNET_CRYPTO_short_hash(&bob_pkey,
459 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
461 GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
462 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
463 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
465 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
466 strlen(TEST_AUTHORITY_ALICE) + 1;
467 GNUNET_DHT_put (dht_handle, &xor_hash,
470 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
474 DHT_OPERATION_TIMEOUT,
477 GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_NO);
482 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
483 const struct GNUNET_CONFIGURATION_Handle *cfg,
484 struct GNUNET_TESTING_Daemon *d, const char *emsg)
491 GNUNET_SCHEDULER_cancel (die_task);
493 /* put records into namestore */
494 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
495 if (NULL == namestore_handle)
497 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
503 dht_handle = GNUNET_DHT_connect(cfg, 1);
504 if (NULL == dht_handle)
506 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
511 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
515 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
520 our_key = GNUNET_CRYPTO_rsa_key_create_from_file (our_keyfile);
521 bob_key = GNUNET_CRYPTO_rsa_key_create ();
522 alice_key = GNUNET_CRYPTO_rsa_key_create ();
524 GNUNET_free(our_keyfile);
526 GNUNET_CRYPTO_rsa_key_get_public (our_key, &our_pkey);
527 GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
528 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
529 GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
530 GNUNET_CRYPTO_short_hash(&alice_pkey, sizeof(alice_pkey), &alice_hash);
532 struct GNUNET_NAMESTORE_RecordData rd;
533 rd.expiration = GNUNET_TIME_absolute_get_forever ();
534 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
536 rd.record_type = GNUNET_GNS_RECORD_PKEY;
538 GNUNET_NAMESTORE_record_create (namestore_handle,
549 run (void *cls, char *const *args, const char *cfgfile,
550 const struct GNUNET_CONFIGURATION_Handle *c)
553 /* Get path from configuration file */
555 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
563 /* Set up a task to end testing if peer start fails */
565 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
566 "didn't start all daemons in reasonable amount of time!!!");
569 d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
570 NULL, NULL, NULL, &do_lookup, NULL);
578 /* Arguments for GNUNET_PROGRAM_run */
579 char *const argv[] = { "test-gns-pseu-shorten", /* Name to give running binary */
581 "test_gns_simple_lookup.conf", /* Config file to use */
587 struct GNUNET_GETOPT_CommandLineOption options[] = {
588 GNUNET_GETOPT_OPTION_END
590 /* Run the run function as a new program */
592 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
593 "test-gns-pseu-shorten", "nohelp", options, &run,
595 if (ret != GNUNET_OK)
597 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
598 "`test-gns-dht-delegated-lookup': Failed with error code %d\n", ret);
604 main (int argc, char *argv[])
608 GNUNET_log_setup ("test-gns-simple-lookup",
617 * Need to remove base directory, subdirectories taken care
618 * of by the testing framework.
623 /* end of test_gns_twopeer.c */