-fix tld guessing code
[oweals/gnunet.git] / src / gns / test_gns_cname_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_cname_lookup.c
22  * @brief base testcase for testing a local GNS record lookup
23  *
24  */
25 #include "platform.h"
26 #include "gnunet_testing_lib-new.h"
27 #include "gnunet_core_service.h"
28 #include "block_dns.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_namestore_service.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_gns_service.h"
33
34 /* DEFINES */
35 #define VERBOSE GNUNET_YES
36
37 /* Timeout for entire testcase */
38 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20)
39
40 /* If number of peers not in config file, use this number */
41 #define DEFAULT_NUM_PEERS 2
42
43 /* test records to resolve */
44 #define TEST_DOMAIN_PLUS "www.gads"
45 #define TEST_DOMAIN_ZKEY "www2.gads"
46 #define TEST_DOMAIN_DNS  "www3.gads"
47 #define TEST_IP_PLUS "127.0.0.1"
48 #define TEST_IP_ZKEY "127.0.0.2"
49 #define TEST_IP_DNS  "131.159.74.67"
50 #define TEST_RECORD_CNAME_SERVER "server.gads"
51 #define TEST_RECORD_CNAME_PLUS "server.+"
52 #define TEST_RECORD_CNAME_ZKEY "www.188JSUMKEF25GVU8TTV0PBNNN8JVCPUEDFV1UHJJU884JD25V0T0.zkey"
53 #define TEST_RECORD_CNAME_DNS "gnunet.org"
54 #define TEST_RECORD_NAME_SERVER "server"
55 #define TEST_RECORD_NAME_PLUS "www"
56 #define TEST_RECORD_NAME_ZKEY "www2"
57 #define TEST_RECORD_NAME_DNS "www3"
58
59 #define KEYFILE_BOB "zonefiles/188JSUMKEF25GVU8TTV0PBNNN8JVCPUEDFV1UHJJU884JD25V0T0.zkey"
60
61 /* Globals */
62
63 /* Task handle to use to schedule test failure */
64 GNUNET_SCHEDULER_TaskIdentifier die_task;
65
66 /* Global return value (0 for success, anything else for failure) */
67 static int ok;
68
69 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
70
71 static struct GNUNET_GNS_Handle *gns_handle;
72
73 const struct GNUNET_CONFIGURATION_Handle *cfg;
74
75 /**
76  * Check if the get_handle is being used, if so stop the request.  Either
77  * way, schedule the end_badly_cont function which actually shuts down the
78  * test.
79  */
80 static void
81 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 {
83   die_task = GNUNET_SCHEDULER_NO_TASK;
84   if (NULL != gns_handle)
85   {
86     GNUNET_GNS_disconnect(gns_handle);
87     gns_handle = NULL;
88   }
89
90   if (NULL != namestore_handle)
91   {
92     GNUNET_NAMESTORE_disconnect (namestore_handle);
93     namestore_handle = NULL;
94   }
95   GNUNET_break (0);
96   GNUNET_SCHEDULER_shutdown ();
97   ok = 1;
98 }
99
100 static void
101 end_badly_now ()
102 {
103   GNUNET_SCHEDULER_cancel (die_task);
104   die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
105 }
106
107 static void shutdown_task (void *cls,
108                            const struct GNUNET_SCHEDULER_TaskContext *tc)
109 {
110   GNUNET_GNS_disconnect(gns_handle);
111   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
112   GNUNET_SCHEDULER_shutdown ();
113 }
114
115 static void
116 on_lookup_result_cname (void *cls, uint32_t rd_count,
117                  const struct GNUNET_NAMESTORE_RecordData *rd)
118 {
119   int i;
120   
121   if (GNUNET_SCHEDULER_NO_TASK != die_task)
122   {
123       GNUNET_SCHEDULER_cancel (die_task);
124       die_task = GNUNET_SCHEDULER_NO_TASK;
125   }
126
127   GNUNET_NAMESTORE_disconnect (namestore_handle);
128   namestore_handle = NULL;
129   if (rd_count == 0)
130   {
131     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
132                 "Lookup failed, rp_filtering?\n");
133     ok = 2;
134   }
135   else
136   {
137     ok = 1;
138     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
139     for (i=0; i<rd_count; i++)
140     {
141       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
142       if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
143       {
144         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CNAME: %s\n", rd[i].data);
145         if (0 == strcmp(rd[i].data, TEST_RECORD_CNAME_SERVER))
146         {
147           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
148                     "%s correctly resolved to %s!\n", TEST_DOMAIN_PLUS, rd[i].data);
149           ok = 0;
150         }
151       }
152       else
153       {
154         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
155       }
156     }
157   }
158   GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
159 }
160
161 static void
162 on_lookup_result_dns (void *cls, uint32_t rd_count,
163                  const struct GNUNET_NAMESTORE_RecordData *rd)
164 {
165   struct in_addr a;
166   int i;
167   char* addr;
168   
169   if (rd_count == 0)
170   {
171     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172                 "Lookup failed, rp_filtering?\n");
173     ok = 2;
174   }
175   else
176   {
177     ok = 1;
178     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
179     for (i=0; i<rd_count; i++)
180     {
181       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
182       if (rd[i].record_type == GNUNET_GNS_RECORD_A)
183       {
184         memcpy(&a, rd[i].data, sizeof(a));
185         addr = inet_ntoa(a);
186         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
187         if (0 == strcmp(addr, TEST_IP_DNS))
188         {
189           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
190                     "%s correctly resolved to %s!\n", TEST_DOMAIN_DNS, addr);
191           ok = 0;
192         }
193       }
194       else
195       {
196         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
197       }
198     }
199   }
200
201   GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN_PLUS, GNUNET_GNS_RECORD_CNAME,
202                      GNUNET_YES,
203                      NULL,
204                      &on_lookup_result_cname, TEST_DOMAIN_PLUS);
205 }
206
207 static void
208 on_lookup_result_zkey (void *cls, uint32_t rd_count,
209                  const struct GNUNET_NAMESTORE_RecordData *rd)
210 {
211   struct in_addr a;
212   int i;
213   char* addr;
214   
215   if (rd_count == 0)
216   {
217     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218                 "Lookup failed, rp_filtering?\n");
219     ok = 2;
220   }
221   else
222   {
223     ok = 1;
224     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
225     for (i=0; i<rd_count; i++)
226     {
227       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
228       if (rd[i].record_type == GNUNET_GNS_RECORD_A)
229       {
230         memcpy(&a, rd[i].data, sizeof(a));
231         addr = inet_ntoa(a);
232         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
233         if (0 == strcmp(addr, TEST_IP_ZKEY))
234         {
235           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
236                     "%s correctly resolved to %s!\n", TEST_DOMAIN_ZKEY, addr);
237           ok = 0;
238         }
239       }
240       else
241       {
242         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
243       }
244     }
245   }
246
247   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_DNS, GNUNET_GNS_RECORD_A,
248                     GNUNET_YES,
249                     NULL,
250                     &on_lookup_result_dns, TEST_DOMAIN_DNS);
251 }
252
253 static void
254 on_lookup_result_plus (void *cls, uint32_t rd_count,
255                  const struct GNUNET_NAMESTORE_RecordData *rd)
256 {
257   struct in_addr a;
258   int i;
259   char* addr;
260   
261   if (rd_count == 0)
262   {
263     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264                 "Lookup failed, rp_filtering?\n");
265     ok = 2;
266   }
267   else
268   {
269     ok = 1;
270     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
271     for (i=0; i<rd_count; i++)
272     {
273       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
274       if (rd[i].record_type == GNUNET_GNS_RECORD_A)
275       {
276         memcpy(&a, rd[i].data, sizeof(a));
277         addr = inet_ntoa(a);
278         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
279         if (0 == strcmp(addr, TEST_IP_PLUS))
280         {
281           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
282                     "%s correctly resolved to %s!\n", TEST_DOMAIN_PLUS, addr);
283           ok = 0;
284         }
285       }
286       else
287       {
288         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
289       }
290     }
291   }
292
293   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_ZKEY, GNUNET_GNS_RECORD_A,
294                     GNUNET_YES,
295                     NULL,
296                     &on_lookup_result_zkey, TEST_DOMAIN_ZKEY);
297 }
298
299 /**
300  * Function scheduled to be run on the successful start of services
301  * tries to look up the dns record for TEST_DOMAIN
302  */
303 static void
304 commence_testing (void *cls, int32_t success, const char *emsg)
305 {
306   gns_handle = GNUNET_GNS_connect(cfg);
307   if (NULL == gns_handle)
308   {
309     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310                 "Failed to connect to GNS!\n");
311     end_badly_now ();
312     return;
313   }
314
315   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_PLUS, GNUNET_GNS_RECORD_A,
316                     GNUNET_YES,
317                     NULL,
318                     &on_lookup_result_plus, TEST_DOMAIN_PLUS);
319 }
320
321
322 static void
323 do_check (void *cls,
324           const struct GNUNET_CONFIGURATION_Handle *ccfg,
325           struct GNUNET_TESTING_Peer *peer)
326 {
327   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
328   struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
329   struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
330   char* alice_keyfile;
331   
332   cfg = ccfg;
333   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
334
335
336   /* put records into namestore */
337   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
338   if (NULL == namestore_handle)
339   {
340     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
341     end_badly_now ();
342     return;
343   }
344
345   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
346                                                           "ZONEKEY",
347                                                           &alice_keyfile))
348   {
349     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
350     end_badly_now ();
351     return;
352   }
353
354   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
355
356   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
357   
358   GNUNET_free(alice_keyfile);
359
360   bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
361
362   struct GNUNET_NAMESTORE_RecordData rd;
363   char* ip = TEST_IP_PLUS;
364   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
365   rd.expiration_time = UINT64_MAX;
366   GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
367   rd.data_size = sizeof(struct in_addr);
368   rd.data = web;
369   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
370   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
371
372   GNUNET_NAMESTORE_record_create (namestore_handle,
373                                   alice_key,
374                                   TEST_RECORD_NAME_SERVER,
375                                   &rd,
376                                   NULL,
377                                   NULL);
378
379   rd.data_size = strlen (TEST_RECORD_CNAME_PLUS);
380   rd.data = TEST_RECORD_CNAME_PLUS;
381   rd.record_type = GNUNET_GNS_RECORD_CNAME;
382
383   GNUNET_NAMESTORE_record_create (namestore_handle,
384                                   alice_key,
385                                   TEST_RECORD_NAME_PLUS,
386                                   &rd,
387                                   NULL,
388                                   NULL);
389
390   rd.data_size = strlen (TEST_RECORD_CNAME_ZKEY);
391   rd.data = TEST_RECORD_CNAME_ZKEY;
392   rd.record_type = GNUNET_GNS_RECORD_CNAME;
393
394   GNUNET_NAMESTORE_record_create (namestore_handle,
395                                   alice_key,
396                                   TEST_RECORD_NAME_ZKEY,
397                                   &rd,
398                                   NULL,
399                                   NULL);
400
401   rd.data_size = strlen (TEST_RECORD_CNAME_DNS);
402   rd.data = TEST_RECORD_CNAME_DNS;
403   rd.record_type = GNUNET_GNS_RECORD_CNAME;
404
405   GNUNET_NAMESTORE_record_create (namestore_handle,
406                                   alice_key,
407                                   TEST_RECORD_NAME_DNS,
408                                   &rd,
409                                   NULL,
410                                   NULL);
411
412   GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP_ZKEY, web));
413   rd.data_size = sizeof(struct in_addr);
414   rd.data = web;
415   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
416   
417   GNUNET_NAMESTORE_record_create (namestore_handle,
418                                   bob_key,
419                                   TEST_RECORD_NAME_PLUS,
420                                   &rd,
421                                   &commence_testing,
422                                   NULL);
423   
424   GNUNET_CRYPTO_rsa_key_free(alice_key);
425   GNUNET_CRYPTO_rsa_key_free(bob_key);
426   GNUNET_free(web);
427
428 }
429
430 int
431 main (int argc, char *argv[])
432 {
433   ok = 1;
434
435   GNUNET_log_setup ("test-gns-simple-cname-lookup",
436 #if VERBOSE
437                     "DEBUG",
438 #else
439                     "WARNING",
440 #endif
441                     NULL);
442   GNUNET_TESTING_peer_run ("test-gns-simple-cname-lookup", "test_gns_simple_lookup.conf", &do_check, NULL);
443   return ok;
444 }
445
446 /* end of test_gns_simple_cname_lookup.c */