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.bob.gnunet"
61 #define TEST_IP "127.0.0.1"
62 #define TEST_RECORD_NAME "www"
64 #define TEST_AUTHORITY_NAME "bob"
66 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
71 * Directory to store temp data in, defined in config file
73 static char *test_directory;
75 struct GNUNET_TESTING_Daemon *d1;
78 /* Task handle to use to schedule test failure */
79 GNUNET_SCHEDULER_TaskIdentifier die_task;
81 /* Global return value (0 for success, anything else for failure) */
84 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
86 static struct GNUNET_DHT_Handle *dht_handle;
88 const struct GNUNET_CONFIGURATION_Handle *cfg;
90 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
91 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
92 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
93 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
96 * Check whether peers successfully shut down.
99 shutdown_callback (void *cls, const char *emsg)
103 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
108 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
112 * Function scheduled to be run on the successful start of services
113 * tries to look up the dns record for TEST_DOMAIN
116 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
122 GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES);
123 GNUNET_DHT_disconnect(dht_handle);
125 he = gethostbyname (TEST_DOMAIN);
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
130 "gethostbyname failed, rp_filtering?\n");
136 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", he->h_name);
137 while (*he->h_addr_list)
139 memcpy(&a, *he->h_addr_list++, sizeof(a));
141 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
142 if (0 == strcmp(addr, TEST_IP))
144 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
145 "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
154 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
155 GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
156 GNUNET_YES, GNUNET_NO);
160 * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
161 * down the peers without freeing memory associated with GET request.
164 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
168 GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
169 GNUNET_YES, GNUNET_NO);
170 GNUNET_SCHEDULER_cancel (die_task);
174 * Check if the get_handle is being used, if so stop the request. Either
175 * way, schedule the end_badly_cont function which actually shuts down the
179 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
183 GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
189 put_dht(void *cls, int32_t success, const char *emsg)
191 struct GNSNameRecordBlock *nrb;
192 GNUNET_HashCode name_hash;
193 GNUNET_HashCode xor_hash;
194 GNUNET_HashCode zone_hash;
195 uint32_t rd_payload_length;
196 char* nrb_data = NULL;
197 struct GNUNET_CRYPTO_RsaSignature *sig;
198 struct GNUNET_NAMESTORE_RecordData rd;
200 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
202 rd.expiration = GNUNET_TIME_absolute_get_forever ();
203 GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
204 rd.data_size = sizeof(struct in_addr);
206 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
207 sig = GNUNET_NAMESTORE_create_signature(bob_key, TEST_RECORD_NAME,
209 rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
210 nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
211 + sizeof(struct GNSNameRecordBlock));
212 nrb->signature = *sig;
213 nrb->public_key = bob_pkey;
214 nrb->rd_count = htonl(1);
215 memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
216 memcpy(&nrb[1], TEST_RECORD_NAME, strlen(TEST_RECORD_NAME));
217 nrb_data = (char*)&nrb[1];
218 nrb_data += strlen(TEST_RECORD_NAME) + 1;
220 if (-1 == GNUNET_NAMESTORE_records_serialize (1,
225 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
229 GNUNET_CRYPTO_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
230 GNUNET_CRYPTO_hash(&bob_pkey,
231 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
233 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
235 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
236 strlen(TEST_RECORD_NAME) + 1;
237 GNUNET_DHT_put (dht_handle, &xor_hash,
240 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
244 DHT_OPERATION_TIMEOUT,
247 GNUNET_SCHEDULER_add_delayed(TIMEOUT, &finish_testing, NULL);
251 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
252 const struct GNUNET_CONFIGURATION_Handle *cfg,
253 struct GNUNET_TESTING_Daemon *d, const char *emsg)
258 GNUNET_HashCode bob_hash;
261 GNUNET_SCHEDULER_cancel (die_task);
263 /* put records into namestore */
264 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
265 if (NULL == namestore_handle)
267 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
273 dht_handle = GNUNET_DHT_connect(cfg, 1);
274 if (NULL == dht_handle)
276 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
281 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "gns",
285 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
290 alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
291 bob_key = GNUNET_CRYPTO_rsa_key_create ();
293 GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
294 GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
295 GNUNET_CRYPTO_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
297 struct GNUNET_NAMESTORE_RecordData rd;
298 rd.expiration = GNUNET_TIME_absolute_get_forever ();
299 rd.data_size = sizeof(GNUNET_HashCode);
301 rd.record_type = GNUNET_GNS_RECORD_PKEY;
303 GNUNET_NAMESTORE_record_create (namestore_handle,
315 run (void *cls, char *const *args, const char *cfgfile,
316 const struct GNUNET_CONFIGURATION_Handle *c)
319 /* Get path from configuration file */
321 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
329 /* Set up a task to end testing if peer start fails */
331 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
332 "didn't start all daemons in reasonable amount of time!!!");
335 d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
336 NULL, NULL, NULL, &do_lookup, NULL);
344 /* Arguments for GNUNET_PROGRAM_run */
345 char *const argv[] = { "test-gns-simple-lookup", /* Name to give running binary */
347 "test_gns_simple_lookup.conf", /* Config file to use */
353 struct GNUNET_GETOPT_CommandLineOption options[] = {
354 GNUNET_GETOPT_OPTION_END
356 /* Run the run function as a new program */
358 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
359 "test-gns-simple-lookup", "nohelp", options, &run,
361 if (ret != GNUNET_OK)
363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
364 "`test-gns-simple-lookup': Failed with error code %d\n", ret);
370 main (int argc, char *argv[])
374 GNUNET_log_setup ("test-gns-simple-lookup",
383 * Need to remove base directory, subdirectories taken care
384 * of by the testing framework.
389 /* end of test_gns_twopeer.c */