-dead code elimination
[oweals/gnunet.git] / src / gns / test_gns_simple_delegated_lookup.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file gns/test_gns_simple_delegated_lookup.c
22  * @brief base testcase for testing DHT service with
23  *        two running peers.
24  *
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
37  * the peers.
38  */
39 #include "platform.h"
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"
48
49 /* Timeout for entire testcase */
50 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20)
51
52 /* test records to resolve */
53 #define TEST_DOMAIN "www.bob.gads"
54 #define TEST_IP "127.0.0.1"
55 #define TEST_RECORD_NAME "www"
56
57 #define TEST_AUTHORITY_NAME "bob"
58
59 #define KEYFILE_BOB "../namestore/zonefiles/HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"
60
61 /* Task handle to use to schedule test failure */
62 static GNUNET_SCHEDULER_TaskIdentifier die_task;
63
64 /* Global return value (0 for success, anything else for failure) */
65 static int ok;
66
67 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
68
69 static struct GNUNET_GNS_Handle *gns_handle;
70
71 static const struct GNUNET_CONFIGURATION_Handle *cfg;
72
73
74 /**
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
77  * test.
78  */
79 static void
80 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81 {
82   die_task = GNUNET_SCHEDULER_NO_TASK;
83   if (NULL != gns_handle)
84   {
85     GNUNET_GNS_disconnect(gns_handle);
86     gns_handle = NULL;
87   }
88   if (NULL != namestore_handle)
89   {
90     GNUNET_NAMESTORE_disconnect (namestore_handle);
91     namestore_handle = NULL;
92   }
93   GNUNET_break (0);
94   GNUNET_SCHEDULER_shutdown ();
95   ok = 1;
96 }
97
98
99 static void 
100 shutdown_task (void *cls,
101                const struct GNUNET_SCHEDULER_TaskContext *tc)
102 {
103   GNUNET_GNS_disconnect (gns_handle);
104   gns_handle = NULL;
105   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
106   GNUNET_SCHEDULER_shutdown ();
107 }
108
109
110 static void
111 on_lookup_result (void *cls, uint32_t rd_count,
112                   const struct GNUNET_NAMESTORE_RecordData *rd)
113 {
114   const char *name = cls;
115   uint32_t i;
116   const char* addr;
117   struct in_addr a;
118
119   GNUNET_NAMESTORE_disconnect (namestore_handle);
120   namestore_handle = NULL;
121   if (0 == rd_count)
122   {
123     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
124                 "Lookup failed!\n");
125     ok = 2;
126     GNUNET_SCHEDULER_shutdown ();
127     return;
128   }
129   if (GNUNET_SCHEDULER_NO_TASK != die_task)
130   {
131       GNUNET_SCHEDULER_cancel (die_task);
132       die_task = GNUNET_SCHEDULER_NO_TASK;
133   }
134   ok = 1;
135   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
136               "name: %s\n", 
137               name);
138   for (i=0; i<rd_count; i++)
139   {
140     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
141     if (rd[i].record_type != GNUNET_DNSPARSER_TYPE_A)
142       continue;
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))
148       continue;
149     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150                 "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
151     ok = 0;
152   }
153   GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
154 }
155
156
157 /**
158  * Function scheduled to be run on the successful start of services
159  * tries to look up the dns record for TEST_DOMAIN
160  */
161 static void
162 commence_testing (void *cls, int32_t success, const char *emsg)
163 {
164   if (NULL != emsg)
165   {
166     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
167                 "Failed to store record in namestore: %s\n",
168                 emsg);
169     GNUNET_SCHEDULER_shutdown ();
170     return;
171   }
172   gns_handle = GNUNET_GNS_connect(cfg);
173   if (NULL == gns_handle)
174   {
175     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176                 "Failed to connect to GNS!\n");
177     GNUNET_SCHEDULER_shutdown ();
178     return;
179   }
180   GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN, GNUNET_DNSPARSER_TYPE_A,
181                      GNUNET_NO,
182                      NULL,
183                      &on_lookup_result, TEST_DOMAIN);
184 }
185
186
187 static void
188 do_check (void *cls,
189           const struct GNUNET_CONFIGURATION_Handle *ccfg,
190           struct GNUNET_TESTING_Peer *peer)
191 {
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;
198   char* alice_keyfile;
199   struct GNUNET_TIME_Absolute et;
200   struct GNUNET_NAMESTORE_RecordData rd;
201   const char* ip = TEST_IP;
202   struct in_addr web;
203
204   cfg = ccfg;
205   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
206
207   /* put records into namestore */
208   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
209   if (NULL == namestore_handle)
210   {
211     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
212                 "Failed to connect to namestore\n");
213     GNUNET_SCHEDULER_shutdown ();
214     return;
215   }
216
217   if (GNUNET_OK != 
218       GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
219                                                "ZONEKEY",
220                                                &alice_keyfile))
221   {
222     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
223     GNUNET_SCHEDULER_shutdown ();
224     return;
225   }
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);
234   rd.data = &bob_hash;
235   rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
236   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
237   GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
238                                             alice_key,
239                                             TEST_AUTHORITY_NAME,
240                                             1, &rd,
241                                             NULL,
242                                             NULL);
243   rd.data_size = sizeof(struct in_addr);
244   rd.data = &web;
245   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
246   sig = GNUNET_NAMESTORE_create_signature (bob_key,
247                                            GNUNET_TIME_UNIT_FOREVER_ABS,
248                                            TEST_RECORD_NAME,
249                                            &rd, 1);
250   et.abs_value_us = rd.expiration_time;
251   GNUNET_NAMESTORE_record_put (namestore_handle,
252                                &bob_pkey,
253                                TEST_RECORD_NAME,
254                                et,
255                                1,
256                                &rd,
257                                sig,
258                                &commence_testing,
259                                NULL);
260   GNUNET_free (sig);
261   GNUNET_free (alice_keyfile);
262   GNUNET_CRYPTO_ecc_key_free (bob_key);
263   GNUNET_CRYPTO_ecc_key_free (alice_key);
264 }
265
266
267 int
268 main (int argc, char *argv[])
269 {
270   ok = 1;
271   GNUNET_log_setup ("test-gns-simple-delegated-lookup",
272                     "WARNING",
273                     NULL);
274   GNUNET_TESTING_peer_run ("test-gns-simple-delegated-lookup", 
275                            "test_gns_simple_lookup.conf",
276                            &do_check, NULL);
277   return ok;
278 }
279
280 /* end of test_gns_simple_delegated_lookup.c */