-fix cnames #2448
[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.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.gnunet"
45 #define TEST_DOMAIN_ZKEY "www2.gnunet"
46 #define TEST_DOMAIN_DNS  "www3.gnunet"
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.gnunet"
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 /**
64  * Directory to store temp data in, defined in config file
65  */
66 static char *test_directory;
67
68 static struct GNUNET_TESTING_PeerGroup *pg;
69
70 /* Task handle to use to schedule test failure */
71 GNUNET_SCHEDULER_TaskIdentifier die_task;
72
73 /* Global return value (0 for success, anything else for failure) */
74 static int ok;
75
76 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
77
78 static struct GNUNET_GNS_Handle *gns_handle;
79
80 const struct GNUNET_CONFIGURATION_Handle *cfg;
81
82 /**
83  * Check whether peers successfully shut down.
84  */
85 void
86 shutdown_callback (void *cls, const char *emsg)
87 {
88   if (emsg != NULL)
89   {
90     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
91     if (ok == 0)
92       ok = 2;
93   }
94
95   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
96 }
97
98
99 static void
100 on_lookup_result_cname (void *cls, uint32_t rd_count,
101                  const struct GNUNET_NAMESTORE_RecordData *rd)
102 {
103   int i;
104   
105   if (rd_count == 0)
106   {
107     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
108                 "Lookup failed, rp_filtering?\n");
109     ok = 2;
110   }
111   else
112   {
113     ok = 1;
114     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
115     for (i=0; i<rd_count; i++)
116     {
117       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
118       if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_CNAME)
119       {
120         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CNAME: %s\n", rd[i].data);
121         if (0 == strcmp(rd[i].data, TEST_RECORD_CNAME_SERVER))
122         {
123           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
124                     "%s correctly resolved to %s!\n", TEST_DOMAIN_PLUS, rd[i].data);
125           ok = 0;
126         }
127       }
128       else
129       {
130         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
131       }
132     }
133   }
134   GNUNET_GNS_disconnect(gns_handle);
135   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
136   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
137 }
138
139 static void
140 on_lookup_result_dns (void *cls, uint32_t rd_count,
141                  const struct GNUNET_NAMESTORE_RecordData *rd)
142 {
143   struct in_addr a;
144   int i;
145   char* addr;
146   
147   if (rd_count == 0)
148   {
149     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150                 "Lookup failed, rp_filtering?\n");
151     ok = 2;
152   }
153   else
154   {
155     ok = 1;
156     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
157     for (i=0; i<rd_count; i++)
158     {
159       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
160       if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
161       {
162         memcpy(&a, rd[i].data, sizeof(a));
163         addr = inet_ntoa(a);
164         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
165         if (0 == strcmp(addr, TEST_IP_DNS))
166         {
167           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
168                     "%s correctly resolved to %s!\n", TEST_DOMAIN_DNS, addr);
169           ok = 0;
170         }
171       }
172       else
173       {
174         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
175       }
176     }
177   }
178
179   GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN_PLUS, GNUNET_GNS_RECORD_TYPE_CNAME,
180                      GNUNET_YES,
181                      NULL,
182                      &on_lookup_result_cname, TEST_DOMAIN_PLUS);
183 }
184
185 static void
186 on_lookup_result_zkey (void *cls, uint32_t rd_count,
187                  const struct GNUNET_NAMESTORE_RecordData *rd)
188 {
189   struct in_addr a;
190   int i;
191   char* addr;
192   
193   if (rd_count == 0)
194   {
195     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
196                 "Lookup failed, rp_filtering?\n");
197     ok = 2;
198   }
199   else
200   {
201     ok = 1;
202     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
203     for (i=0; i<rd_count; i++)
204     {
205       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
206       if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
207       {
208         memcpy(&a, rd[i].data, sizeof(a));
209         addr = inet_ntoa(a);
210         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
211         if (0 == strcmp(addr, TEST_IP_ZKEY))
212         {
213           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
214                     "%s correctly resolved to %s!\n", TEST_DOMAIN_ZKEY, addr);
215           ok = 0;
216         }
217       }
218       else
219       {
220         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
221       }
222     }
223   }
224
225   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_DNS, GNUNET_GNS_RECORD_TYPE_A,
226                     GNUNET_YES,
227                     NULL,
228                     &on_lookup_result_dns, TEST_DOMAIN_DNS);
229 }
230
231 static void
232 on_lookup_result_plus (void *cls, uint32_t rd_count,
233                  const struct GNUNET_NAMESTORE_RecordData *rd)
234 {
235   struct in_addr a;
236   int i;
237   char* addr;
238   
239   if (rd_count == 0)
240   {
241     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242                 "Lookup failed, rp_filtering?\n");
243     ok = 2;
244   }
245   else
246   {
247     ok = 1;
248     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
249     for (i=0; i<rd_count; i++)
250     {
251       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
252       if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
253       {
254         memcpy(&a, rd[i].data, sizeof(a));
255         addr = inet_ntoa(a);
256         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
257         if (0 == strcmp(addr, TEST_IP_PLUS))
258         {
259           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
260                     "%s correctly resolved to %s!\n", TEST_DOMAIN_PLUS, addr);
261           ok = 0;
262         }
263       }
264       else
265       {
266         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
267       }
268     }
269   }
270
271   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_ZKEY, GNUNET_GNS_RECORD_TYPE_A,
272                     GNUNET_YES,
273                     NULL,
274                     &on_lookup_result_zkey, TEST_DOMAIN_ZKEY);
275 }
276
277
278 /**
279  * Function scheduled to be run on the successful start of services
280  * tries to look up the dns record for TEST_DOMAIN
281  */
282 static void
283 commence_testing (void *cls, int32_t success, const char *emsg)
284 {
285   
286   
287   GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES);
288   
289   gns_handle = GNUNET_GNS_connect(cfg);
290
291   if (NULL == gns_handle)
292   {
293     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
294                 "Failed to connect to GNS!\n");
295     ok = 2;
296   }
297
298   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_PLUS, GNUNET_GNS_RECORD_TYPE_A,
299                     GNUNET_YES,
300                     NULL,
301                     &on_lookup_result_plus, TEST_DOMAIN_PLUS);
302 }
303
304
305 /**
306  * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
307  * down the peers without freeing memory associated with GET request.
308  */
309 static void
310 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
311 {
312
313   if (pg != NULL)
314     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
315   GNUNET_SCHEDULER_cancel (die_task);
316 }
317
318 /**
319  * Check if the get_handle is being used, if so stop the request.  Either
320  * way, schedule the end_badly_cont function which actually shuts down the
321  * test.
322  */
323 static void
324 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
325 {
326   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
327               (char *) cls);
328   GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
329   ok = 1;
330 }
331
332 static void
333 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
334           const struct GNUNET_CONFIGURATION_Handle *_cfg,
335           struct GNUNET_TESTING_Daemon *d, const char *emsg)
336 {
337   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
338   struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
339   struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
340   char* alice_keyfile;
341   
342   cfg = _cfg;
343
344   GNUNET_SCHEDULER_cancel (die_task);
345
346   /* put records into namestore */
347   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
348   if (NULL == namestore_handle)
349   {
350     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
351     ok = -1;
352     return;
353   }
354
355   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
356                                                           "ZONEKEY",
357                                                           &alice_keyfile))
358   {
359     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
360     ok = -1;
361     return;
362   }
363
364   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
365
366   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
367   
368   GNUNET_free(alice_keyfile);
369
370   bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
371
372   struct GNUNET_NAMESTORE_RecordData rd;
373   char* ip = TEST_IP_PLUS;
374   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
375   rd.expiration_time = UINT64_MAX;
376   GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
377   rd.data_size = sizeof(struct in_addr);
378   rd.data = web;
379   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
380   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
381
382   GNUNET_NAMESTORE_record_create (namestore_handle,
383                                   alice_key,
384                                   TEST_RECORD_NAME_SERVER,
385                                   &rd,
386                                   NULL,
387                                   NULL);
388
389   rd.data_size = strlen (TEST_RECORD_CNAME_PLUS);
390   rd.data = TEST_RECORD_CNAME_PLUS;
391   rd.record_type = GNUNET_GNS_RECORD_TYPE_CNAME;
392
393   GNUNET_NAMESTORE_record_create (namestore_handle,
394                                   alice_key,
395                                   TEST_RECORD_NAME_PLUS,
396                                   &rd,
397                                   NULL,
398                                   NULL);
399
400   rd.data_size = strlen (TEST_RECORD_CNAME_ZKEY);
401   rd.data = TEST_RECORD_CNAME_ZKEY;
402   rd.record_type = GNUNET_GNS_RECORD_TYPE_CNAME;
403
404   GNUNET_NAMESTORE_record_create (namestore_handle,
405                                   alice_key,
406                                   TEST_RECORD_NAME_ZKEY,
407                                   &rd,
408                                   NULL,
409                                   NULL);
410
411   rd.data_size = strlen (TEST_RECORD_CNAME_DNS);
412   rd.data = TEST_RECORD_CNAME_DNS;
413   rd.record_type = GNUNET_GNS_RECORD_TYPE_CNAME;
414
415   GNUNET_NAMESTORE_record_create (namestore_handle,
416                                   alice_key,
417                                   TEST_RECORD_NAME_DNS,
418                                   &rd,
419                                   NULL,
420                                   NULL);
421
422   GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP_ZKEY, web));
423   rd.data_size = sizeof(struct in_addr);
424   rd.data = web;
425   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
426   
427   GNUNET_NAMESTORE_record_create (namestore_handle,
428                                   bob_key,
429                                   TEST_RECORD_NAME_PLUS,
430                                   &rd,
431                                   &commence_testing,
432                                   NULL);
433   
434   GNUNET_CRYPTO_rsa_key_free(alice_key);
435   GNUNET_CRYPTO_rsa_key_free(bob_key);
436   GNUNET_free(web);
437
438 }
439
440 static void
441 run (void *cls, char *const *args, const char *cfgfile,
442      const struct GNUNET_CONFIGURATION_Handle *c)
443 {
444   cfg = c;
445    /* Get path from configuration file */
446   if (GNUNET_YES !=
447       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
448                                              &test_directory))
449   {
450     ok = 404;
451     return;
452   }
453
454     
455   /* Set up a task to end testing if peer start fails */
456   die_task =
457       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
458                                     "didn't start all daemons in reasonable amount of time!!!");
459   
460   /* Start alice */
461   pg = GNUNET_TESTING_daemons_start(cfg, 1, 1, 1, TIMEOUT,
462                                     NULL, NULL, &do_lookup, NULL,
463                                     NULL, NULL, NULL);
464 }
465
466 static int
467 check ()
468 {
469   int ret;
470
471   /* Arguments for GNUNET_PROGRAM_run */
472   char *const argv[] = { "test-gns-simple-lookup", /* Name to give running binary */
473     "-c",
474     "test_gns_simple_lookup.conf",       /* Config file to use */
475 #if VERBOSE
476     "-L", "DEBUG",
477 #endif
478     NULL
479   };
480   struct GNUNET_GETOPT_CommandLineOption options[] = {
481     GNUNET_GETOPT_OPTION_END
482   };
483   /* Run the run function as a new program */
484   ret =
485       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
486                           "test-gns-simple-lookup", "nohelp", options, &run,
487                           &ok);
488   if (ret != GNUNET_OK)
489   {
490     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
491                 "`test-gns-simple-lookup': Failed with error code %d\n", ret);
492   }
493   return ok;
494 }
495
496 int
497 main (int argc, char *argv[])
498 {
499   int ret;
500
501   GNUNET_log_setup ("test-gns-simple-lookup",
502 #if VERBOSE
503                     "DEBUG",
504 #else
505                     "WARNING",
506 #endif
507                     NULL);
508   ret = check ();
509   /**
510    * Need to remove base directory, subdirectories taken care
511    * of by the testing framework.
512    */
513   return ret;
514 }
515
516 /* end of test_gns_twopeer.c */