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_simple_delegated_lookup.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_dns.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_gns_service.h"
49 /* Timeout for entire testcase */
50 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20)
52 /* test records to resolve */
53 #define TEST_DOMAIN "www.bob.gnu"
54 #define TEST_IP "127.0.0.1"
55 #define TEST_RECORD_NAME "www"
57 #define TEST_AUTHORITY_NAME "bob"
59 #define KEYFILE_BOB "../namestore/zonefiles/HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"
61 /* Task handle to use to schedule test failure */
62 static GNUNET_SCHEDULER_TaskIdentifier die_task;
64 /* Global return value (0 for success, anything else for failure) */
67 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
69 static struct GNUNET_GNS_Handle *gns_handle;
71 static const struct GNUNET_CONFIGURATION_Handle *cfg;
75 * Check if the get_handle is being used, if so stop the request. Either
76 * way, schedule the end_badly_cont function which actually shuts down the
80 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 die_task = GNUNET_SCHEDULER_NO_TASK;
83 if (NULL != gns_handle)
85 GNUNET_GNS_disconnect(gns_handle);
88 if (NULL != namestore_handle)
90 GNUNET_NAMESTORE_disconnect (namestore_handle);
91 namestore_handle = NULL;
94 GNUNET_SCHEDULER_shutdown ();
100 shutdown_task (void *cls,
101 const struct GNUNET_SCHEDULER_TaskContext *tc)
103 GNUNET_GNS_disconnect (gns_handle);
105 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
106 GNUNET_SCHEDULER_shutdown ();
111 on_lookup_result (void *cls, uint32_t rd_count,
112 const struct GNUNET_NAMESTORE_RecordData *rd)
114 const char *name = cls;
119 GNUNET_NAMESTORE_disconnect (namestore_handle);
120 namestore_handle = NULL;
123 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
126 GNUNET_SCHEDULER_shutdown ();
129 if (GNUNET_SCHEDULER_NO_TASK != die_task)
131 GNUNET_SCHEDULER_cancel (die_task);
132 die_task = GNUNET_SCHEDULER_NO_TASK;
135 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
138 for (i=0; i<rd_count; i++)
140 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
141 if (rd[i].record_type != GNUNET_DNSPARSER_TYPE_A)
143 memcpy (&a, rd[i].data, sizeof (a));
144 addr = inet_ntoa (a);
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "address: %s\n", addr);
147 if (0 != strcmp (addr, TEST_IP))
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150 "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
153 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
158 * Function scheduled to be run on the successful start of services
159 * tries to look up the dns record for TEST_DOMAIN
162 commence_testing (void *cls, int32_t success, const char *emsg)
166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
167 "Failed to store record in namestore: %s\n",
169 GNUNET_SCHEDULER_shutdown ();
172 gns_handle = GNUNET_GNS_connect(cfg);
173 if (NULL == gns_handle)
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176 "Failed to connect to GNS!\n");
177 GNUNET_SCHEDULER_shutdown ();
180 GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN, GNUNET_DNSPARSER_TYPE_A,
183 &on_lookup_result, TEST_DOMAIN);
189 const struct GNUNET_CONFIGURATION_Handle *ccfg,
190 struct GNUNET_TESTING_Peer *peer)
192 struct GNUNET_CRYPTO_EccPublicKey alice_pkey;
193 struct GNUNET_CRYPTO_EccPublicKey bob_pkey;
194 struct GNUNET_CRYPTO_EccPrivateKey *alice_key;
195 struct GNUNET_CRYPTO_EccPrivateKey *bob_key;
196 struct GNUNET_CRYPTO_ShortHashCode bob_hash;
197 struct GNUNET_CRYPTO_EccSignature *sig;
199 struct GNUNET_TIME_Absolute et;
200 struct GNUNET_NAMESTORE_RecordData rd;
201 const char* ip = TEST_IP;
205 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
207 /* put records into namestore */
208 namestore_handle = GNUNET_NAMESTORE_connect(cfg);
209 if (NULL == namestore_handle)
211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212 "Failed to connect to namestore\n");
213 GNUNET_SCHEDULER_shutdown ();
218 GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
223 GNUNET_SCHEDULER_shutdown ();
226 alice_key = GNUNET_CRYPTO_ecc_key_create_from_file (alice_keyfile);
227 bob_key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE_BOB);
228 GNUNET_CRYPTO_ecc_key_get_public (alice_key, &alice_pkey);
229 GNUNET_CRYPTO_ecc_key_get_public (bob_key, &bob_pkey);
230 rd.expiration_time = UINT64_MAX;
231 GNUNET_assert (1 == inet_pton (AF_INET, ip, &web));
232 GNUNET_CRYPTO_short_hash (&bob_pkey, sizeof(bob_pkey), &bob_hash);
233 rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
235 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
236 rd.flags = GNUNET_NAMESTORE_RF_NONE;
237 GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
243 rd.data_size = sizeof(struct in_addr);
245 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
246 sig = GNUNET_NAMESTORE_create_signature (bob_key,
247 GNUNET_TIME_UNIT_FOREVER_ABS,
250 et.abs_value_us = rd.expiration_time;
251 GNUNET_NAMESTORE_record_put (namestore_handle,
261 GNUNET_free (alice_keyfile);
262 GNUNET_free (bob_key);
263 GNUNET_free (alice_key);
268 main (int argc, char *argv[])
271 GNUNET_log_setup ("test-gns-simple-delegated-lookup",
274 GNUNET_TESTING_peer_run ("test-gns-simple-delegated-lookup",
275 "test_gns_simple_lookup.conf",
280 /* end of test_gns_simple_delegated_lookup.c */