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_twopeer.c
22 * @brief base testcase for testing DHT service with
25 * This testcase starts peers using the GNUNET_TESTING_daemons_start
26 * function call. On peer start, connects to the peers DHT service
27 * by calling GNUNET_DHT_connected. Once notified about all peers
28 * being started (by the peers_started_callback function), calls
29 * GNUNET_TESTING_connect_topology, which connects the peers in a
30 * "straight line" topology. On notification that all peers have
31 * been properly connected, calls the do_get function which initiates
32 * a GNUNET_DHT_get from the *second* peer. Once the GNUNET_DHT_get
33 * function starts, runs the do_put function to insert data at the first peer.
34 * If the GET is successful, schedules finish_testing
35 * to stop the test and shut down peers. If GET is unsuccessful
36 * after GET_TIMEOUT seconds, prints an error message and shuts down
40 #include "gnunet_testing_lib.h"
41 #include "gnunet_core_service.h"
42 #include "block_gns.h"
43 #include "gnunet_signatures.h"
44 #include "gnunet_namestore_service.h"
45 #include "../namestore/namestore.h"
46 #include "gnunet_dnsparser_lib.h"
47 #include "gnunet_dht_service.h"
48 #include "gnunet_gns_service.h"
51 #define VERBOSE GNUNET_YES
53 /* Timeout for entire testcase */
54 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
56 /* If number of peers not in config file, use this number */
57 #define DEFAULT_NUM_PEERS 2
59 /* test records to resolve */
60 #define TEST_DOMAIN "www.alice.bob.gnunet"
61 #define TEST_IP "127.0.0.1"
62 #define TEST_RECORD_NAME "www"
64 #define TEST_AUTHORITY_NAME "bob"
65 #define TEST_AUTHORITY_ALICE "alice"
66 #define TEST_ALICE_PSEU "carol"
67 #define TEST_EXPECTED_RESULT "www.carol.gnunet"
69 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
74 * Directory to store temp data in, defined in config file
76 static char *test_directory;
78 struct GNUNET_TESTING_Daemon *d1;
81 /* Task handle to use to schedule test failure */
82 GNUNET_SCHEDULER_TaskIdentifier die_task;
84 /* Global return value (0 for success, anything else for failure) */
87 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
89 static struct GNUNET_GNS_Handle *gns_handle;
91 static struct GNUNET_DHT_Handle *dht_handle;
93 const struct GNUNET_CONFIGURATION_Handle *cfg;
95 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
96 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
97 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
98 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
101 * Check whether peers successfully shut down.
104 shutdown_callback (void *cls, const char *emsg)
108 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
113 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
118 * Called when gns shorten finishes
121 process_shorten_result(void* cls, const char* sname)
123 GNUNET_GNS_disconnect(gns_handle);
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
130 "shorten test failed!\n");
135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136 "%s shortened to %s\n", (char*)cls, sname);
137 if (0 != strcmp(sname, TEST_EXPECTED_RESULT))
139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
140 "shorten test failed! (wanted: %s got: %s\n",
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shorten test succeeded!\n");
149 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
150 GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
151 GNUNET_YES, GNUNET_NO);
155 * Function scheduled to be run on the successful start of services
156 * tries to look up the dns record for TEST_DOMAIN
159 commence_testing (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES);
163 gns_handle = GNUNET_GNS_connect(cfg);
165 if (NULL == gns_handle)
167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168 "Failed to connect to GNS!\n");
172 GNUNET_GNS_shorten(gns_handle, TEST_DOMAIN, &process_shorten_result,
177 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
178 * down the peers without freeing memory associated with GET request.
181 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
185 GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
186 GNUNET_YES, GNUNET_NO);
187 GNUNET_SCHEDULER_cancel (die_task);
191 * Check if the get_handle is being used, if so stop the request. Either
192 * way, schedule the end_badly_cont function which actually shuts down the
196 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
200 GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
206 put_www_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
208 struct GNSNameRecordBlock *nrb;
209 GNUNET_HashCode name_hash;
210 GNUNET_HashCode xor_hash;
211 GNUNET_HashCode zone_hash;
212 uint32_t rd_payload_length;
213 char* nrb_data = NULL;
214 struct GNUNET_CRYPTO_RsaSignature *sig;
215 struct GNUNET_NAMESTORE_RecordData rd;
217 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
219 rd.expiration = GNUNET_TIME_absolute_get_forever ();
220 GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
221 rd.data_size = sizeof(struct in_addr);
223 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
225 sig = GNUNET_NAMESTORE_create_signature(alice_key,
226 GNUNET_TIME_absolute_get_forever(),
229 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
230 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
231 + sizeof(struct GNSNameRecordBlock));
232 nrb->signature = *sig;
233 nrb->public_key = alice_pkey;
234 nrb->rd_count = htonl(1);
235 memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
236 memcpy(&nrb[1], TEST_RECORD_NAME, strlen(TEST_RECORD_NAME));
237 nrb_data = (char*)&nrb[1];
238 nrb_data += strlen(TEST_RECORD_NAME) + 1;
240 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
245 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
250 GNUNET_CRYPTO_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
251 GNUNET_CRYPTO_hash(&alice_pkey,
252 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
254 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
256 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
257 strlen(TEST_RECORD_NAME) + 1;
258 GNUNET_DHT_put (dht_handle, &xor_hash,
261 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
265 DHT_OPERATION_TIMEOUT,
269 GNUNET_SCHEDULER_add_delayed(TIMEOUT, &commence_testing, NULL);
274 put_alice_pseu_dht(void *cls, int32_t success, const char *emsg)
276 struct GNSNameRecordBlock *nrb;
277 GNUNET_HashCode name_hash;
278 GNUNET_HashCode xor_hash;
279 GNUNET_HashCode zone_hash;
280 uint32_t rd_payload_length;
281 char* nrb_data = NULL;
282 struct GNUNET_CRYPTO_RsaSignature *sig;
283 struct GNUNET_NAMESTORE_RecordData rd;
285 rd.expiration = GNUNET_TIME_absolute_get_forever ();
286 rd.data_size = strlen(TEST_ALICE_PSEU);
287 rd.data = TEST_ALICE_PSEU;
288 rd.record_type = GNUNET_GNS_RECORD_PSEU;
290 sig = GNUNET_NAMESTORE_create_signature(alice_key,
291 GNUNET_TIME_absolute_get_forever(),
292 "+", //empty name for pseu
294 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
295 nrb = GNUNET_malloc(rd_payload_length + strlen("") + 1
296 + sizeof(struct GNSNameRecordBlock));
297 nrb->signature = *sig;
298 nrb->public_key = alice_pkey;
299 nrb->rd_count = htonl(1);
300 memset(&nrb[1], 0, strlen("+") + 1);
301 memcpy(&nrb[1], "+", strlen("+"));
302 nrb_data = (char*)&nrb[1];
303 nrb_data += strlen("+") + 1;
305 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
310 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
315 GNUNET_CRYPTO_hash("+", strlen("+"), &name_hash);
316 GNUNET_CRYPTO_hash(&alice_pkey,
317 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
319 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
321 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
323 GNUNET_DHT_put (dht_handle, &xor_hash,
326 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
330 DHT_OPERATION_TIMEOUT,
334 GNUNET_SCHEDULER_add_delayed(TIMEOUT, &put_www_dht, NULL);
338 do_shorten(void *cls, const struct GNUNET_PeerIdentity *id,
339 const struct GNUNET_CONFIGURATION_Handle *cfg,
340 struct GNUNET_TESTING_Daemon *d, const char *emsg)
344 struct GNUNET_CRYPTO_RsaPrivateKey *my_key;
345 GNUNET_HashCode bob_hash;
346 GNUNET_HashCode alice_hash;
347 struct GNUNET_CRYPTO_RsaSignature *sig;
350 GNUNET_SCHEDULER_cancel (die_task);
352 /* put records into namestore */
353 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
354 if (NULL == namestore_handle)
356 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
362 dht_handle = GNUNET_DHT_connect(cfg, 1);
363 if (NULL == dht_handle)
365 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
370 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "gns",
374 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
379 my_key = GNUNET_CRYPTO_rsa_key_create_from_file (my_keyfile);
380 alice_key = GNUNET_CRYPTO_rsa_key_create ();
381 bob_key = GNUNET_CRYPTO_rsa_key_create ();
383 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
384 GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
385 GNUNET_CRYPTO_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
386 GNUNET_CRYPTO_hash(&alice_pkey, sizeof(alice_pkey), &alice_hash);
388 struct GNUNET_NAMESTORE_RecordData rd;
389 rd.expiration = GNUNET_TIME_absolute_get_forever ();
390 rd.data_size = sizeof(GNUNET_HashCode);
392 rd.record_type = GNUNET_GNS_RECORD_PKEY;
394 GNUNET_NAMESTORE_record_create (namestore_handle,
401 rd.data = &alice_hash;
403 sig = GNUNET_NAMESTORE_create_signature(bob_key,
404 GNUNET_TIME_absolute_get_forever(),
405 TEST_AUTHORITY_ALICE,
409 GNUNET_NAMESTORE_record_put (namestore_handle,
411 TEST_AUTHORITY_ALICE,
412 GNUNET_TIME_absolute_get_forever(),
426 run (void *cls, char *const *args, const char *cfgfile,
427 const struct GNUNET_CONFIGURATION_Handle *c)
430 /* Get path from configuration file */
432 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
440 /* Set up a task to end testing if peer start fails */
442 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
443 "didn't start all daemons in reasonable amount of time!!!");
446 d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
447 NULL, NULL, NULL, &do_shorten, NULL);
455 /* Arguments for GNUNET_PROGRAM_run */
456 char *const argv[] = { "test-gns-dht-delegated-lookup", /* Name to give running binary */
458 "test_gns_simple_lookup.conf", /* Config file to use */
464 struct GNUNET_GETOPT_CommandLineOption options[] = {
465 GNUNET_GETOPT_OPTION_END
467 /* Run the run function as a new program */
469 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
470 "test-gns-dht-delegated-lookup", "nohelp", options, &run,
472 if (ret != GNUNET_OK)
474 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
475 "`test-gns-dht-delegated-lookup': Failed with error code %d\n", ret);
481 main (int argc, char *argv[])
485 GNUNET_log_setup ("test-gns-simple-lookup",
494 * Need to remove base directory, subdirectories taken care
495 * of by the testing framework.
500 /* end of test_gns_twopeer.c */