-mx,cname,ns postprocessing + test
[oweals/gnunet.git] / src / gns / test_gns_simple_mx_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_simple_mx_lookup.c
22  * @brief base testcase for testing GNS MX lookups
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 "../namestore/namestore.h"
32 #include "gnunet_dnsparser_lib.h"
33 #include "gnunet_gns_service.h"
34
35 /* DEFINES */
36 #define VERBOSE GNUNET_YES
37
38 /* Timeout for entire testcase */
39 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
40
41 /* If number of peers not in config file, use this number */
42 #define DEFAULT_NUM_PEERS 2
43
44 /* test records to resolve */
45 #define TEST_DOMAIN "bob.gnunet"
46 #define TEST_IP "127.0.0.1"
47 #define TEST_RECORD_NAME "mail"
48 #define TEST_MX_NAME "mail.+"
49 #define TEST_EXPECTED_MX "mail.bob.gnunet"
50
51 #define TEST_AUTHORITY_NAME "bob"
52
53 /* Globals */
54
55 /**
56  * Directory to store temp data in, defined in config file
57  */
58 static char *test_directory;
59
60 struct GNUNET_TESTING_Daemon *d1;
61
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 whether peers successfully shut down.
77  */
78 void
79 shutdown_callback (void *cls, const char *emsg)
80 {
81   if (emsg != NULL)
82   {
83     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
84     if (ok == 0)
85       ok = 2;
86   }
87
88   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
89 }
90
91 static void
92 on_lookup_result(void *cls, uint32_t rd_count,
93                  const struct GNUNET_NAMESTORE_RecordData *rd)
94 {
95   struct in_addr a;
96   int i;
97   char* addr;
98   int mx_found = 0;
99   int ip_found = 0;
100   char* mx;
101   
102   if (rd_count == 0)
103   {
104     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
105                 "Lookup failed, rp_filtering?\n");
106     ok = 2;
107   }
108   else
109   {
110     ok = 1;
111     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
112     for (i=0; i<rd_count; i++)
113     {
114       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
115       if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
116       {
117         memcpy(&a, rd[i].data, sizeof(a));
118         addr = inet_ntoa(a);
119         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
120         if (0 == strcmp(addr, TEST_IP))
121         {
122           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
123                     "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
124           ip_found = 1;
125         }
126       }
127       else if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
128       {
129         mx = (char*)rd[i].data;
130         if (0 == strcmp(mx, TEST_EXPECTED_MX))
131         {
132           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
133                       "%s correctly resolved to %s!\n", TEST_DOMAIN,
134                       TEST_EXPECTED_MX);
135           mx_found = 1;
136         }
137       }
138     }
139   }
140
141   if (ip_found && mx_found)
142   {
143     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
144                 "Test succeeded!\n");
145     ok = 0;
146   }
147
148   if (!ip_found && mx_found)
149   {
150     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
151   "Test partially succeeded: A record not passed along!(NOT IMPLEMENTED)\n");
152     ok = 0;
153   }
154
155   GNUNET_GNS_disconnect(gns_handle);
156   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
157   GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
158                               GNUNET_YES, GNUNET_NO);
159 }
160
161
162 /**
163  * Function scheduled to be run on the successful start of services
164  * tries to look up the dns record for TEST_DOMAIN
165  */
166 static void
167 commence_testing (void *cls, int32_t success, const char *emsg)
168 {
169   GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES);
170
171   gns_handle = GNUNET_GNS_connect(cfg);
172
173   if (NULL == gns_handle)
174   {
175     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176                 "Failed to connect to GNS!\n");
177   }
178
179   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_MX,
180                     &on_lookup_result, TEST_DOMAIN);
181 }
182
183 /**
184  * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
185  * down the peers without freeing memory associated with GET request.
186  */
187 static void
188 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
189 {
190
191   if (d1 != NULL)
192     GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
193                                 GNUNET_YES, GNUNET_NO);
194   GNUNET_SCHEDULER_cancel (die_task);
195 }
196
197 /**
198  * Check if the get_handle is being used, if so stop the request.  Either
199  * way, schedule the end_badly_cont function which actually shuts down the
200  * test.
201  */
202 static void
203 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
204 {
205   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
206               (char *) cls);
207   GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
208   ok = 1;
209 }
210
211 static void
212 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
213           const struct GNUNET_CONFIGURATION_Handle *cfg,
214           struct GNUNET_TESTING_Daemon *d, const char *emsg)
215 {
216   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
217   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
218   struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
219   struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
220   GNUNET_HashCode bob_hash;
221   struct GNUNET_CRYPTO_RsaSignature *sig;
222   char* alice_keyfile;
223
224   GNUNET_SCHEDULER_cancel (die_task);
225
226   /* put records into namestore */
227   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
228   if (NULL == namestore_handle)
229   {
230     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
231     ok = -1;
232     return;
233   }
234
235   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
236                                                           "ZONEKEY",
237                                                           &alice_keyfile))
238   {
239     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
240     ok = -1;
241     return;
242   }
243
244   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
245   bob_key = GNUNET_CRYPTO_rsa_key_create ();
246
247   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
248   GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
249
250   struct GNUNET_NAMESTORE_RecordData rd;
251   char* ip = TEST_IP;
252   struct in_addr *mail = GNUNET_malloc(sizeof(struct in_addr));
253   rd.expiration = GNUNET_TIME_absolute_get_forever ();
254   GNUNET_assert(1 == inet_pton (AF_INET, ip, mail));
255   
256   GNUNET_CRYPTO_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
257
258   rd.data_size = sizeof(GNUNET_HashCode);
259   rd.data = &bob_hash;
260   rd.record_type = GNUNET_GNS_RECORD_PKEY;
261
262   GNUNET_NAMESTORE_record_create (namestore_handle,
263                                   alice_key,
264                                   TEST_AUTHORITY_NAME,
265                                   &rd,
266                                   NULL,
267                                   NULL);
268
269   rd.data_size = sizeof(struct in_addr);
270   rd.data = mail;
271   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
272   sig = GNUNET_NAMESTORE_create_signature(bob_key,
273                                           GNUNET_TIME_absolute_get_forever(),
274                                           TEST_RECORD_NAME,
275                                           &rd, 1);
276
277   GNUNET_NAMESTORE_record_put (namestore_handle,
278                                &bob_pkey,
279                                TEST_RECORD_NAME,
280                                rd.expiration,
281                                1,
282                                &rd,
283                                sig,
284                                NULL,
285                                NULL);
286
287   rd.data_size = strlen(TEST_MX_NAME)+1;
288   rd.data = TEST_MX_NAME;
289   rd.record_type = GNUNET_GNS_RECORD_MX;
290   sig = GNUNET_NAMESTORE_create_signature(bob_key,
291                                           GNUNET_TIME_absolute_get_forever(),
292                                           "+",
293                                           &rd, 1);
294   GNUNET_NAMESTORE_record_put (namestore_handle,
295                                &bob_pkey,
296                                "+",
297                                rd.expiration,
298                                1,
299                                &rd,
300                                sig,
301                                &commence_testing,
302                                NULL);
303   GNUNET_free(mail);
304   GNUNET_free(sig);
305   GNUNET_CRYPTO_rsa_key_free(bob_key);
306   GNUNET_CRYPTO_rsa_key_free(alice_key);
307 }
308
309 static void
310 run (void *cls, char *const *args, const char *cfgfile,
311      const struct GNUNET_CONFIGURATION_Handle *c)
312 {
313   cfg = c;
314    /* Get path from configuration file */
315   if (GNUNET_YES !=
316       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
317                                              &test_directory))
318   {
319     ok = 404;
320     return;
321   }
322
323     
324   /* Set up a task to end testing if peer start fails */
325   die_task =
326       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
327                                     "didn't start all daemons in reasonable amount of time!!!");
328   
329   /* Start alice */
330   d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
331                                    NULL, NULL, NULL, &do_lookup, NULL);
332 }
333
334 static int
335 check ()
336 {
337   int ret;
338
339   /* Arguments for GNUNET_PROGRAM_run */
340   char *const argv[] = { "test-gns-simple-delegated-lookup", /* Name to give running binary */
341     "-c",
342     "test_gns_simple_lookup.conf",       /* Config file to use */
343 #if VERBOSE
344     "-L", "DEBUG",
345 #endif
346     NULL
347   };
348   struct GNUNET_GETOPT_CommandLineOption options[] = {
349     GNUNET_GETOPT_OPTION_END
350   };
351   /* Run the run function as a new program */
352   ret =
353       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
354                           "test-gns-simple-delegated-lookup", "nohelp", options, &run,
355                           &ok);
356   if (ret != GNUNET_OK)
357   {
358     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
359                 "`test-gns-simple-delegated-lookup': Failed with error code %d\n", ret);
360   }
361   return ok;
362 }
363
364 int
365 main (int argc, char *argv[])
366 {
367   int ret;
368
369   GNUNET_log_setup ("test-gns-simple-lookup",
370 #if VERBOSE
371                     "DEBUG",
372 #else
373                     "WARNING",
374 #endif
375                     NULL);
376   ret = check ();
377   /**
378    * Need to remove base directory, subdirectories taken care
379    * of by the testing framework.
380    */
381   return ret;
382 }
383
384 /* end of test_gns_twopeer.c */