fix
[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_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_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_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_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_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_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_A,
272                     GNUNET_YES,
273                     NULL,
274                     &on_lookup_result_zkey, TEST_DOMAIN_ZKEY);
275 }
276
277
278 static void
279 disco_ns (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
280 {
281   GNUNET_NAMESTORE_disconnect (namestore_handle);
282 }
283
284 /**
285  * Function scheduled to be run on the successful start of services
286  * tries to look up the dns record for TEST_DOMAIN
287  */
288 static void
289 commence_testing (void *cls, int32_t success, const char *emsg)
290 {
291   
292   GNUNET_SCHEDULER_add_now (disco_ns, NULL);
293   
294   gns_handle = GNUNET_GNS_connect(cfg);
295
296   if (NULL == gns_handle)
297   {
298     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299                 "Failed to connect to GNS!\n");
300     ok = 2;
301   }
302
303   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN_PLUS, GNUNET_GNS_RECORD_A,
304                     GNUNET_YES,
305                     NULL,
306                     &on_lookup_result_plus, TEST_DOMAIN_PLUS);
307 }
308
309
310 /**
311  * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
312  * down the peers without freeing memory associated with GET request.
313  */
314 static void
315 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
316 {
317
318   if (pg != NULL)
319     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
320   GNUNET_SCHEDULER_cancel (die_task);
321 }
322
323 /**
324  * Check if the get_handle is being used, if so stop the request.  Either
325  * way, schedule the end_badly_cont function which actually shuts down the
326  * test.
327  */
328 static void
329 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
330 {
331   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
332               (char *) cls);
333   GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
334   ok = 1;
335 }
336
337 static void
338 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
339           const struct GNUNET_CONFIGURATION_Handle *_cfg,
340           struct GNUNET_TESTING_Daemon *d, const char *emsg)
341 {
342   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
343   struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
344   struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
345   char* alice_keyfile;
346   
347   cfg = _cfg;
348
349   GNUNET_SCHEDULER_cancel (die_task);
350
351   /* put records into namestore */
352   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
353   if (NULL == namestore_handle)
354   {
355     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
356     ok = -1;
357     return;
358   }
359
360   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
361                                                           "ZONEKEY",
362                                                           &alice_keyfile))
363   {
364     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
365     ok = -1;
366     return;
367   }
368
369   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
370
371   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
372   
373   GNUNET_free(alice_keyfile);
374
375   bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
376
377   struct GNUNET_NAMESTORE_RecordData rd;
378   char* ip = TEST_IP_PLUS;
379   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
380   rd.expiration_time = UINT64_MAX;
381   GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
382   rd.data_size = sizeof(struct in_addr);
383   rd.data = web;
384   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
385   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
386
387   GNUNET_NAMESTORE_record_create (namestore_handle,
388                                   alice_key,
389                                   TEST_RECORD_NAME_SERVER,
390                                   &rd,
391                                   NULL,
392                                   NULL);
393
394   rd.data_size = strlen (TEST_RECORD_CNAME_PLUS);
395   rd.data = TEST_RECORD_CNAME_PLUS;
396   rd.record_type = GNUNET_GNS_RECORD_CNAME;
397
398   GNUNET_NAMESTORE_record_create (namestore_handle,
399                                   alice_key,
400                                   TEST_RECORD_NAME_PLUS,
401                                   &rd,
402                                   NULL,
403                                   NULL);
404
405   rd.data_size = strlen (TEST_RECORD_CNAME_ZKEY);
406   rd.data = TEST_RECORD_CNAME_ZKEY;
407   rd.record_type = GNUNET_GNS_RECORD_CNAME;
408
409   GNUNET_NAMESTORE_record_create (namestore_handle,
410                                   alice_key,
411                                   TEST_RECORD_NAME_ZKEY,
412                                   &rd,
413                                   NULL,
414                                   NULL);
415
416   rd.data_size = strlen (TEST_RECORD_CNAME_DNS);
417   rd.data = TEST_RECORD_CNAME_DNS;
418   rd.record_type = GNUNET_GNS_RECORD_CNAME;
419
420   GNUNET_NAMESTORE_record_create (namestore_handle,
421                                   alice_key,
422                                   TEST_RECORD_NAME_DNS,
423                                   &rd,
424                                   NULL,
425                                   NULL);
426
427   GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP_ZKEY, web));
428   rd.data_size = sizeof(struct in_addr);
429   rd.data = web;
430   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
431   
432   GNUNET_NAMESTORE_record_create (namestore_handle,
433                                   bob_key,
434                                   TEST_RECORD_NAME_PLUS,
435                                   &rd,
436                                   &commence_testing,
437                                   NULL);
438   
439   GNUNET_CRYPTO_rsa_key_free(alice_key);
440   GNUNET_CRYPTO_rsa_key_free(bob_key);
441   GNUNET_free(web);
442
443 }
444
445 static void
446 run (void *cls, char *const *args, const char *cfgfile,
447      const struct GNUNET_CONFIGURATION_Handle *c)
448 {
449   cfg = c;
450    /* Get path from configuration file */
451   if (GNUNET_YES !=
452       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
453                                              &test_directory))
454   {
455     ok = 404;
456     return;
457   }
458
459     
460   /* Set up a task to end testing if peer start fails */
461   die_task =
462       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
463                                     "didn't start all daemons in reasonable amount of time!!!");
464   
465   /* Start alice */
466   pg = GNUNET_TESTING_daemons_start(cfg, 1, 1, 1, TIMEOUT,
467                                     NULL, NULL, &do_lookup, NULL,
468                                     NULL, NULL, NULL);
469 }
470
471 static int
472 check ()
473 {
474   int ret;
475
476   /* Arguments for GNUNET_PROGRAM_run */
477   char *const argv[] = { "test-gns-simple-lookup", /* Name to give running binary */
478     "-c",
479     "test_gns_simple_lookup.conf",       /* Config file to use */
480 #if VERBOSE
481     "-L", "DEBUG",
482 #endif
483     NULL
484   };
485   struct GNUNET_GETOPT_CommandLineOption options[] = {
486     GNUNET_GETOPT_OPTION_END
487   };
488   /* Run the run function as a new program */
489   ret =
490       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
491                           "test-gns-simple-lookup", "nohelp", options, &run,
492                           &ok);
493   if (ret != GNUNET_OK)
494   {
495     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
496                 "`test-gns-simple-lookup': Failed with error code %d\n", ret);
497   }
498   return ok;
499 }
500
501 int
502 main (int argc, char *argv[])
503 {
504   int ret;
505
506   GNUNET_log_setup ("test-gns-simple-lookup",
507 #if VERBOSE
508                     "DEBUG",
509 #else
510                     "WARNING",
511 #endif
512                     NULL);
513   ret = check ();
514   /**
515    * Need to remove base directory, subdirectories taken care
516    * of by the testing framework.
517    */
518   return ret;
519 }
520
521 /* end of test_gns_twopeer.c */