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