-fix double free, linker issue
[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.gads"
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 static void shutdown_task (void *cls,
127                            const struct GNUNET_SCHEDULER_TaskContext *tc)
128 {
129   GNUNET_GNS_disconnect(gns_handle);
130   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
131   GNUNET_SCHEDULER_shutdown ();
132 }
133
134 static void
135 on_lookup_result(void *cls, uint32_t rd_count,
136                  const struct GNUNET_NAMESTORE_RecordData *rd)
137 {
138   struct in_addr a;
139   int i;
140   char* addr;
141   
142   if (GNUNET_SCHEDULER_NO_TASK != die_task)
143   {
144       GNUNET_SCHEDULER_cancel (die_task);
145       die_task = GNUNET_SCHEDULER_NO_TASK;
146   }
147
148   GNUNET_NAMESTORE_disconnect (namestore_handle);
149   namestore_handle = NULL;
150   if (rd_count == 0)
151   {
152     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153                 "Lookup failed, rp_filtering?\n");
154     ok = 2;
155   }
156   else
157   {
158     ok = 1;
159     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
160     for (i=0; i<rd_count; i++)
161     {
162       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
163       if (rd[i].record_type == GNUNET_GNS_RECORD_A)
164       {
165         memcpy(&a, rd[i].data, sizeof(a));
166         addr = inet_ntoa(a);
167         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
168         if (0 == strcmp(addr, TEST_IP))
169         {
170           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
171                     "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
172           ok = 0;
173         }
174       }
175       else
176       {
177         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
178       }
179     }
180   }
181   GNUNET_DHT_disconnect (dht_handle);
182   dht_handle = NULL;
183
184   GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
185 }
186
187
188 /**
189  * Function scheduled to be run on the successful start of services
190  * tries to look up the dns record for TEST_DOMAIN
191  */
192 static void
193 commence_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
194 {
195   gns_handle = GNUNET_GNS_connect(cfg);
196
197   if (NULL == gns_handle)
198   {
199     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200                 "Failed to connect to GNS!\n");
201     end_badly_now();
202     return;
203   }
204
205   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_A,
206                     GNUNET_NO,
207                     NULL,
208                     &on_lookup_result, TEST_DOMAIN);
209 }
210
211
212 static void
213 put_dht(void *cls, int32_t success, const char *emsg)
214 {
215   struct GNSNameRecordBlock *nrb;
216   struct GNUNET_CRYPTO_ShortHashCode name_hash;
217   struct GNUNET_CRYPTO_ShortHashCode zone_hash;
218   struct GNUNET_HashCode xor_hash;
219   struct GNUNET_HashCode name_hash_double;
220   struct GNUNET_HashCode zone_hash_double;
221   uint32_t rd_payload_length;
222   char* nrb_data = NULL;
223   struct GNUNET_CRYPTO_RsaSignature *sig;
224   struct GNUNET_NAMESTORE_RecordData rd;
225   char* ip = TEST_IP;
226   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
227   
228   rd.expiration_time = UINT64_MAX;
229   GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
230   rd.data_size = sizeof(struct in_addr);
231   rd.data = web;
232   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
233   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
234
235   sig = GNUNET_NAMESTORE_create_signature(bob_key, GNUNET_TIME_UNIT_FOREVER_ABS, TEST_RECORD_NAME,
236                                           &rd, 1);
237
238   GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_verify_signature (&bob_pkey,
239                                                                  GNUNET_TIME_UNIT_FOREVER_ABS,
240                                                                  TEST_RECORD_NAME,
241                                                                  1,
242                                                                  &rd,
243                                                                  sig));
244   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
245   nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
246                       + sizeof(struct GNSNameRecordBlock));
247   nrb->signature = *sig;
248   nrb->public_key = bob_pkey;
249   nrb->rd_count = htonl(1);
250   memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
251   strcpy((char*)&nrb[1], TEST_RECORD_NAME);
252   nrb_data = (char*)&nrb[1];
253   nrb_data += strlen(TEST_RECORD_NAME) + 1;
254
255   if (-1 == GNUNET_NAMESTORE_records_serialize (1,
256                                                 &rd,
257                                                 rd_payload_length,
258                                                 nrb_data))
259   {
260     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
261     ok = 3;
262     GNUNET_free (nrb);
263     GNUNET_free (web);
264     end_badly_now ();
265     return;
266   }
267   GNUNET_CRYPTO_short_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
268   GNUNET_CRYPTO_short_hash(&bob_pkey,
269                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
270                      &zone_hash);
271   GNUNET_CRYPTO_short_hash_double(&zone_hash, &zone_hash_double);
272   GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
273   GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
274
275   rd_payload_length += sizeof(struct GNSNameRecordBlock) +
276     strlen(TEST_RECORD_NAME) + 1;
277   GNUNET_DHT_put (dht_handle, &xor_hash,
278                   0,
279                   GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
280                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
281                   rd_payload_length,
282                   (char*)nrb,
283                   GNUNET_TIME_UNIT_FOREVER_ABS,
284                   DHT_OPERATION_TIMEOUT,
285                   NULL,
286                   NULL);
287   GNUNET_free (web);
288   GNUNET_free (nrb);
289   GNUNET_free (sig);
290   if (GNUNET_SCHEDULER_NO_TASK != die_task)
291   {
292       GNUNET_SCHEDULER_cancel (die_task);
293       die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
294   }
295   wait_task = GNUNET_SCHEDULER_add_delayed(DHT_DELAY, &commence_testing, NULL);
296 }
297
298 static void
299 do_check (void *cls,
300           const struct GNUNET_CONFIGURATION_Handle *ccfg,
301           struct GNUNET_TESTING_Peer *peer)
302 {
303   char* alice_keyfile;
304   struct GNUNET_CRYPTO_ShortHashCode bob_hash;
305   
306   cfg = ccfg;
307   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
308
309   /* put records into namestore */
310   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
311   if (NULL == namestore_handle)
312   {
313     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
314     end_badly_now ();
315     return;
316   }
317   
318   /* dht */
319   dht_handle = GNUNET_DHT_connect(cfg, 1);
320   if (NULL == dht_handle)
321   {
322     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
323     end_badly_now ();
324     return;
325   }
326
327   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
328                                                           "ZONEKEY",
329                                                           &alice_keyfile))
330   {
331     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
332     end_badly_now ();
333     return;
334   }
335
336   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
337   bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
338   
339   GNUNET_free(alice_keyfile);
340
341   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
342   GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
343   GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
344
345   struct GNUNET_NAMESTORE_RecordData rd;
346   rd.expiration_time = UINT64_MAX;
347   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
348   rd.data = &bob_hash;
349   rd.record_type = GNUNET_GNS_RECORD_PKEY;
350   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
351
352   GNUNET_NAMESTORE_record_create (namestore_handle,
353                                   alice_key,
354                                   TEST_AUTHORITY_NAME,
355                                   &rd,
356                                   &put_dht,
357                                   NULL);
358 }
359
360
361 int
362 main (int argc, char *argv[])
363 {
364   ok = 1;
365
366   GNUNET_log_setup ("test-gns-dht-delegated-lookup",
367 #if VERBOSE
368                     "DEBUG",
369 #else
370                     "WARNING",
371 #endif
372                     NULL);
373   GNUNET_TESTING_peer_run ("test-gns-dht-delegated-lookup", "test_gns_simple_lookup.conf", &do_check, NULL);
374   return ok;
375 }
376
377
378 /* end of test_gns_dht_delegated_lookup.c */