-fixes
[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
108 static void
109 on_lookup_result_cname (void *cls, uint32_t rd_count,
110                  const struct GNUNET_NAMESTORE_RecordData *rd)
111 {
112   int i;
113   
114   if (GNUNET_SCHEDULER_NO_TASK != die_task)
115   {
116       GNUNET_SCHEDULER_cancel (die_task);
117       die_task = GNUNET_SCHEDULER_NO_TASK;
118   }
119
120   GNUNET_NAMESTORE_disconnect (namestore_handle);
121   namestore_handle = NULL;
122   if (rd_count == 0)
123   {
124     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
125                 "Lookup failed, rp_filtering?\n");
126     ok = 2;
127   }
128   else
129   {
130     ok = 1;
131     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
132     for (i=0; i<rd_count; i++)
133     {
134       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
135       if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
136       {
137         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CNAME: %s\n", rd[i].data);
138         if (0 == strcmp(rd[i].data, TEST_RECORD_CNAME_SERVER))
139         {
140           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
141                     "%s correctly resolved to %s!\n", TEST_DOMAIN_PLUS, rd[i].data);
142           ok = 0;
143         }
144       }
145       else
146       {
147         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
148       }
149     }
150   }
151   GNUNET_GNS_disconnect(gns_handle);
152   gns_handle = NULL;
153   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
154   GNUNET_SCHEDULER_shutdown ();
155 }
156
157 static void
158 on_lookup_result_dns (void *cls, uint32_t rd_count,
159                  const struct GNUNET_NAMESTORE_RecordData *rd)
160 {
161   struct in_addr a;
162   int i;
163   char* addr;
164   
165   if (rd_count == 0)
166   {
167     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168                 "Lookup failed, rp_filtering?\n");
169     ok = 2;
170   }
171   else
172   {
173     ok = 1;
174     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
175     for (i=0; i<rd_count; i++)
176     {
177       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
178       if (rd[i].record_type == GNUNET_GNS_RECORD_A)
179       {
180         memcpy(&a, rd[i].data, sizeof(a));
181         addr = inet_ntoa(a);
182         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
183         if (0 == strcmp(addr, TEST_IP_DNS))
184         {
185           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
186                     "%s correctly resolved to %s!\n", TEST_DOMAIN_DNS, addr);
187           ok = 0;
188         }
189       }
190       else
191       {
192         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
193       }
194     }
195   }
196
197   GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN_PLUS, GNUNET_GNS_RECORD_CNAME,
198                      GNUNET_YES,
199                      NULL,
200                      &on_lookup_result_cname, TEST_DOMAIN_PLUS);
201 }
202
203 static void
204 on_lookup_result_zkey (void *cls, uint32_t rd_count,
205                  const struct GNUNET_NAMESTORE_RecordData *rd)
206 {
207   struct in_addr a;
208   int i;
209   char* addr;
210   
211   if (rd_count == 0)
212   {
213     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
214                 "Lookup failed, rp_filtering?\n");
215     ok = 2;
216   }
217   else
218   {
219     ok = 1;
220     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
221     for (i=0; i<rd_count; i++)
222     {
223       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
224       if (rd[i].record_type == GNUNET_GNS_RECORD_A)
225       {
226         memcpy(&a, rd[i].data, sizeof(a));
227         addr = inet_ntoa(a);
228         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
229         if (0 == strcmp(addr, TEST_IP_ZKEY))
230         {
231           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
232                     "%s correctly resolved to %s!\n", TEST_DOMAIN_ZKEY, addr);
233           ok = 0;
234         }
235       }
236       else
237       {
238         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
239       }
240     }
241   }
242
243   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_DNS, GNUNET_GNS_RECORD_A,
244                     GNUNET_YES,
245                     NULL,
246                     &on_lookup_result_dns, TEST_DOMAIN_DNS);
247 }
248
249 static void
250 on_lookup_result_plus (void *cls, uint32_t rd_count,
251                  const struct GNUNET_NAMESTORE_RecordData *rd)
252 {
253   struct in_addr a;
254   int i;
255   char* addr;
256   
257   if (rd_count == 0)
258   {
259     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260                 "Lookup failed, rp_filtering?\n");
261     ok = 2;
262   }
263   else
264   {
265     ok = 1;
266     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
267     for (i=0; i<rd_count; i++)
268     {
269       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
270       if (rd[i].record_type == GNUNET_GNS_RECORD_A)
271       {
272         memcpy(&a, rd[i].data, sizeof(a));
273         addr = inet_ntoa(a);
274         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
275         if (0 == strcmp(addr, TEST_IP_PLUS))
276         {
277           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
278                     "%s correctly resolved to %s!\n", TEST_DOMAIN_PLUS, addr);
279           ok = 0;
280         }
281       }
282       else
283       {
284         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
285       }
286     }
287   }
288
289   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_ZKEY, GNUNET_GNS_RECORD_A,
290                     GNUNET_YES,
291                     NULL,
292                     &on_lookup_result_zkey, TEST_DOMAIN_ZKEY);
293 }
294
295 /**
296  * Function scheduled to be run on the successful start of services
297  * tries to look up the dns record for TEST_DOMAIN
298  */
299 static void
300 commence_testing (void *cls, int32_t success, const char *emsg)
301 {
302   gns_handle = GNUNET_GNS_connect(cfg);
303   if (NULL == gns_handle)
304   {
305     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
306                 "Failed to connect to GNS!\n");
307     end_badly_now ();
308     return;
309   }
310
311   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_PLUS, GNUNET_GNS_RECORD_A,
312                     GNUNET_YES,
313                     NULL,
314                     &on_lookup_result_plus, TEST_DOMAIN_PLUS);
315 }
316
317
318 static void
319 do_check (void *cls,
320           const struct GNUNET_CONFIGURATION_Handle *ccfg,
321           struct GNUNET_TESTING_Peer *peer)
322 {
323   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
324   struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
325   struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
326   char* alice_keyfile;
327   
328   cfg = ccfg;
329   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
330
331
332   /* put records into namestore */
333   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
334   if (NULL == namestore_handle)
335   {
336     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
337     end_badly_now ();
338     return;
339   }
340
341   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
342                                                           "ZONEKEY",
343                                                           &alice_keyfile))
344   {
345     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
346     end_badly_now ();
347     return;
348   }
349
350   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
351
352   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
353   
354   GNUNET_free(alice_keyfile);
355
356   bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
357
358   struct GNUNET_NAMESTORE_RecordData rd;
359   char* ip = TEST_IP_PLUS;
360   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
361   rd.expiration_time = UINT64_MAX;
362   GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
363   rd.data_size = sizeof(struct in_addr);
364   rd.data = web;
365   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
366   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
367
368   GNUNET_NAMESTORE_record_create (namestore_handle,
369                                   alice_key,
370                                   TEST_RECORD_NAME_SERVER,
371                                   &rd,
372                                   NULL,
373                                   NULL);
374
375   rd.data_size = strlen (TEST_RECORD_CNAME_PLUS);
376   rd.data = TEST_RECORD_CNAME_PLUS;
377   rd.record_type = GNUNET_GNS_RECORD_CNAME;
378
379   GNUNET_NAMESTORE_record_create (namestore_handle,
380                                   alice_key,
381                                   TEST_RECORD_NAME_PLUS,
382                                   &rd,
383                                   NULL,
384                                   NULL);
385
386   rd.data_size = strlen (TEST_RECORD_CNAME_ZKEY);
387   rd.data = TEST_RECORD_CNAME_ZKEY;
388   rd.record_type = GNUNET_GNS_RECORD_CNAME;
389
390   GNUNET_NAMESTORE_record_create (namestore_handle,
391                                   alice_key,
392                                   TEST_RECORD_NAME_ZKEY,
393                                   &rd,
394                                   NULL,
395                                   NULL);
396
397   rd.data_size = strlen (TEST_RECORD_CNAME_DNS);
398   rd.data = TEST_RECORD_CNAME_DNS;
399   rd.record_type = GNUNET_GNS_RECORD_CNAME;
400
401   GNUNET_NAMESTORE_record_create (namestore_handle,
402                                   alice_key,
403                                   TEST_RECORD_NAME_DNS,
404                                   &rd,
405                                   NULL,
406                                   NULL);
407
408   GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP_ZKEY, web));
409   rd.data_size = sizeof(struct in_addr);
410   rd.data = web;
411   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
412   
413   GNUNET_NAMESTORE_record_create (namestore_handle,
414                                   bob_key,
415                                   TEST_RECORD_NAME_PLUS,
416                                   &rd,
417                                   &commence_testing,
418                                   NULL);
419   
420   GNUNET_CRYPTO_rsa_key_free(alice_key);
421   GNUNET_CRYPTO_rsa_key_free(bob_key);
422   GNUNET_free(web);
423
424 }
425
426 int
427 main (int argc, char *argv[])
428 {
429   ok = 1;
430
431   GNUNET_log_setup ("test-gns-simple-cname-lookup",
432 #if VERBOSE
433                     "DEBUG",
434 #else
435                     "WARNING",
436 #endif
437                     NULL);
438   GNUNET_TESTING_peer_run ("test-gns-simple-cname-lookup", "test_gns_simple_lookup.conf", &do_check, NULL);
439   return ok;
440 }
441
442 /* end of test_gns_simple_cname_lookup.c */