-tests
[oweals/gnunet.git] / src / gns / test_gns_dht_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_dht_delegated_lookup.c
22  * @brief test for record lookup via DHT
23  *
24  */
25 #include "platform.h"
26 #include "gnunet_testing_lib-new.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"
35
36 /* DEFINES */
37 #define VERBOSE GNUNET_YES
38
39 /* Timeout for entire testcase */
40 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20)
41
42 /* Timeout for entire testcase */
43 #define DHT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
44
45 /* If number of peers not in config file, use this number */
46 #define DEFAULT_NUM_PEERS 2
47
48 /* test records to resolve */
49 #define TEST_DOMAIN "www.bob.gnunet"
50 #define TEST_IP "127.0.0.1"
51 #define TEST_RECORD_NAME "www"
52
53 #define TEST_AUTHORITY_NAME "bob"
54
55 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
56
57 #define KEYFILE_BOB "../namestore/zonefiles/HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"
58
59 /* Globals */
60
61 /* Task handle to use to schedule test failure */
62 GNUNET_SCHEDULER_TaskIdentifier die_task;
63 GNUNET_SCHEDULER_TaskIdentifier wait_task;
64
65 /* Global return value (0 for success, anything else for failure) */
66 static int ok;
67
68 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
69
70 static struct GNUNET_GNS_Handle *gns_handle;
71
72 static struct GNUNET_DHT_Handle *dht_handle;
73
74 const struct GNUNET_CONFIGURATION_Handle *cfg;
75
76 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
77 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
78 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
79 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
80
81
82 /**
83  * Check if the get_handle is being used, if so stop the request.  Either
84  * way, schedule the end_badly_cont function which actually shuts down the
85  * test.
86  */
87 static void
88 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
89 {
90   die_task = GNUNET_SCHEDULER_NO_TASK;
91   if (NULL != gns_handle)
92   {
93     GNUNET_GNS_disconnect (gns_handle);
94     gns_handle = NULL;
95   }
96
97   if (NULL != dht_handle)
98   {
99     GNUNET_DHT_disconnect (dht_handle);
100     dht_handle = NULL;
101   }
102
103   if (NULL != namestore_handle)
104   {
105     GNUNET_NAMESTORE_disconnect (namestore_handle);
106     namestore_handle = NULL;
107   }
108   GNUNET_break (0);
109   GNUNET_SCHEDULER_shutdown ();
110   ok = 1;
111 }
112
113 static void
114 end_badly_now ()
115 {
116
117   if (GNUNET_SCHEDULER_NO_TASK != wait_task)
118   {
119       GNUNET_SCHEDULER_cancel (wait_task);
120       wait_task = GNUNET_SCHEDULER_NO_TASK;
121   }
122   GNUNET_SCHEDULER_cancel (die_task);
123   die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
124 }
125
126
127 static void
128 on_lookup_result(void *cls, uint32_t rd_count,
129                  const struct GNUNET_NAMESTORE_RecordData *rd)
130 {
131   struct in_addr a;
132   int i;
133   char* addr;
134   
135   if (GNUNET_SCHEDULER_NO_TASK != die_task)
136   {
137       GNUNET_SCHEDULER_cancel (die_task);
138       die_task = GNUNET_SCHEDULER_NO_TASK;
139   }
140
141   GNUNET_NAMESTORE_disconnect (namestore_handle);
142   namestore_handle = NULL;
143   if (rd_count == 0)
144   {
145     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146                 "Lookup failed, rp_filtering?\n");
147     ok = 2;
148   }
149   else
150   {
151     ok = 1;
152     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
153     for (i=0; i<rd_count; i++)
154     {
155       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
156       if (rd[i].record_type == GNUNET_GNS_RECORD_A)
157       {
158         memcpy(&a, rd[i].data, sizeof(a));
159         addr = inet_ntoa(a);
160         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
161         if (0 == strcmp(addr, TEST_IP))
162         {
163           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
164                     "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
165           ok = 0;
166         }
167       }
168       else
169       {
170         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
171       }
172     }
173   }
174   GNUNET_GNS_disconnect(gns_handle);
175   gns_handle = NULL;
176   GNUNET_DHT_disconnect (dht_handle);
177   dht_handle = NULL;
178   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
179   GNUNET_SCHEDULER_shutdown ();
180 }
181
182
183 /**
184  * Function scheduled to be run on the successful start of services
185  * tries to look up the dns record for TEST_DOMAIN
186  */
187 static void
188 commence_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
189 {
190   gns_handle = GNUNET_GNS_connect(cfg);
191
192   if (NULL == gns_handle)
193   {
194     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
195                 "Failed to connect to GNS!\n");
196     end_badly_now();
197     return;
198   }
199
200   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_A,
201                     GNUNET_NO,
202                     NULL,
203                     &on_lookup_result, TEST_DOMAIN);
204 }
205
206
207 static void
208 put_dht(void *cls, int32_t success, const char *emsg)
209 {
210   struct GNSNameRecordBlock *nrb;
211   struct GNUNET_CRYPTO_ShortHashCode name_hash;
212   struct GNUNET_CRYPTO_ShortHashCode zone_hash;
213   struct GNUNET_HashCode xor_hash;
214   struct GNUNET_HashCode name_hash_double;
215   struct GNUNET_HashCode zone_hash_double;
216   uint32_t rd_payload_length;
217   char* nrb_data = NULL;
218   struct GNUNET_CRYPTO_RsaSignature *sig;
219   struct GNUNET_NAMESTORE_RecordData rd;
220   char* ip = TEST_IP;
221   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
222   
223   rd.expiration_time = UINT64_MAX;
224   GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
225   rd.data_size = sizeof(struct in_addr);
226   rd.data = web;
227   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
228   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
229
230   sig = GNUNET_NAMESTORE_create_signature(bob_key, GNUNET_TIME_UNIT_FOREVER_ABS, TEST_RECORD_NAME,
231                                           &rd, 1);
232
233   GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&bob_pkey,
234                                                                  GNUNET_TIME_UNIT_FOREVER_ABS,
235                                                                  TEST_RECORD_NAME,
236                                                                  1,
237                                                                  &rd,
238                                                                  sig));
239   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
240   nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
241                       + sizeof(struct GNSNameRecordBlock));
242   nrb->signature = *sig;
243   nrb->public_key = bob_pkey;
244   nrb->rd_count = htonl(1);
245   memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
246   strcpy((char*)&nrb[1], TEST_RECORD_NAME);
247   nrb_data = (char*)&nrb[1];
248   nrb_data += strlen(TEST_RECORD_NAME) + 1;
249
250   if (-1 == GNUNET_NAMESTORE_records_serialize (1,
251                                                 &rd,
252                                                 rd_payload_length,
253                                                 nrb_data))
254   {
255     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
256     ok = 3;
257     GNUNET_free (nrb);
258     GNUNET_free (web);
259     end_badly_now ();
260     return;
261   }
262   GNUNET_CRYPTO_short_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
263   GNUNET_CRYPTO_short_hash(&bob_pkey,
264                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
265                      &zone_hash);
266   GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
267   GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
268   GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
269
270   rd_payload_length += sizeof(struct GNSNameRecordBlock) +
271     strlen(TEST_RECORD_NAME) + 1;
272   GNUNET_DHT_put (dht_handle, &xor_hash,
273                   0,
274                   GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
275                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
276                   rd_payload_length,
277                   (char*)nrb,
278                   GNUNET_TIME_UNIT_FOREVER_ABS,
279                   DHT_OPERATION_TIMEOUT,
280                   NULL,
281                   NULL);
282   GNUNET_free (web);
283   GNUNET_free (nrb);
284   GNUNET_free (sig);
285   if (GNUNET_SCHEDULER_NO_TASK != die_task)
286   {
287       GNUNET_SCHEDULER_cancel (die_task);
288       die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
289   }
290   wait_task = GNUNET_SCHEDULER_add_delayed(DHT_DELAY, &commence_testing, NULL);
291 }
292
293 static void
294 do_check (void *cls,
295           const struct GNUNET_CONFIGURATION_Handle *ccfg,
296           struct GNUNET_TESTING_Peer *peer)
297 {
298   char* alice_keyfile;
299   struct GNUNET_CRYPTO_ShortHashCode bob_hash;
300   
301   cfg = ccfg;
302   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
303
304   /* put records into namestore */
305   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
306   if (NULL == namestore_handle)
307   {
308     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
309     end_badly_now ();
310     return;
311   }
312   
313   /* dht */
314   dht_handle = GNUNET_DHT_connect(cfg, 1);
315   if (NULL == dht_handle)
316   {
317     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
318     end_badly_now ();
319     return;
320   }
321
322   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
323                                                           "ZONEKEY",
324                                                           &alice_keyfile))
325   {
326     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
327     end_badly_now ();
328     return;
329   }
330
331   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
332   bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
333   
334   GNUNET_free(alice_keyfile);
335
336   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
337   GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
338   GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
339
340   struct GNUNET_NAMESTORE_RecordData rd;
341   rd.expiration_time = UINT64_MAX;
342   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
343   rd.data = &bob_hash;
344   rd.record_type = GNUNET_GNS_RECORD_PKEY;
345   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
346
347   GNUNET_NAMESTORE_record_create (namestore_handle,
348                                   alice_key,
349                                   TEST_AUTHORITY_NAME,
350                                   &rd,
351                                   &put_dht,
352                                   NULL);
353 }
354
355
356 int
357 main (int argc, char *argv[])
358 {
359   ok = 1;
360
361   GNUNET_log_setup ("test-gns-dht-delegated-lookup",
362 #if VERBOSE
363                     "DEBUG",
364 #else
365                     "WARNING",
366 #endif
367                     NULL);
368   GNUNET_TESTING_peer_run ("test-gns-dht-delegated-lookup", "test_gns_simple_lookup.conf", &do_check, NULL);
369   return ok;
370 }
371
372
373 /* end of test_gns_dht_delegated_lookup.c */