-fixes, interceptor now speaks zkey
[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   struct GNUNET_DNSPARSER_MxRecord *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 = (struct GNUNET_DNSPARSER_MxRecord*)rd[i].data;
130         mx->mxhost = (char*)&mx[1];
131         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
132                     "Got MX %s\n", mx->mxhost);
133         if (0 == strcmp(mx->mxhost, TEST_EXPECTED_MX))
134         {
135           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
136                       "%s correctly resolved to %s!\n", TEST_DOMAIN,
137                       TEST_EXPECTED_MX);
138           mx_found = 1;
139         }
140       }
141     }
142   }
143
144   if (ip_found && mx_found)
145   {
146     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
147                 "Test succeeded!\n");
148     ok = 0;
149   }
150
151   if (!ip_found && mx_found)
152   {
153     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
154   "Test partially succeeded: A record not passed along!(NOT IMPLEMENTED)\n");
155     ok = 0;
156   }
157
158   GNUNET_GNS_disconnect(gns_handle);
159   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
160   GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
161                               GNUNET_YES, GNUNET_NO);
162 }
163
164
165 /**
166  * Function scheduled to be run on the successful start of services
167  * tries to look up the dns record for TEST_DOMAIN
168  */
169 static void
170 commence_testing (void *cls, int32_t success, const char *emsg)
171 {
172   GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES);
173
174   gns_handle = GNUNET_GNS_connect(cfg);
175
176   if (NULL == gns_handle)
177   {
178     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179                 "Failed to connect to GNS!\n");
180   }
181
182   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_MX,
183                     &on_lookup_result, TEST_DOMAIN);
184 }
185
186 /**
187  * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
188  * down the peers without freeing memory associated with GET request.
189  */
190 static void
191 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
192 {
193
194   if (d1 != NULL)
195     GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
196                                 GNUNET_YES, GNUNET_NO);
197   GNUNET_SCHEDULER_cancel (die_task);
198 }
199
200 /**
201  * Check if the get_handle is being used, if so stop the request.  Either
202  * way, schedule the end_badly_cont function which actually shuts down the
203  * test.
204  */
205 static void
206 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
207 {
208   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
209               (char *) cls);
210   GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
211   ok = 1;
212 }
213
214 static void
215 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
216           const struct GNUNET_CONFIGURATION_Handle *cfg,
217           struct GNUNET_TESTING_Daemon *d, const char *emsg)
218 {
219   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
220   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
221   struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
222   struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
223   GNUNET_HashCode bob_hash;
224   struct GNUNET_CRYPTO_RsaSignature *sig;
225   char* alice_keyfile;
226
227   GNUNET_SCHEDULER_cancel (die_task);
228
229   /* put records into namestore */
230   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
231   if (NULL == namestore_handle)
232   {
233     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
234     ok = -1;
235     return;
236   }
237
238   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
239                                                           "ZONEKEY",
240                                                           &alice_keyfile))
241   {
242     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
243     ok = -1;
244     return;
245   }
246
247   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
248   bob_key = GNUNET_CRYPTO_rsa_key_create ();
249
250   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
251   GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
252
253   struct GNUNET_NAMESTORE_RecordData rd;
254   char* ip = TEST_IP;
255   struct in_addr *mail = GNUNET_malloc(sizeof(struct in_addr));
256   struct GNUNET_DNSPARSER_MxRecord *mx_record;
257   rd.expiration = GNUNET_TIME_absolute_get_forever ();
258   GNUNET_assert(1 == inet_pton (AF_INET, ip, mail));
259   
260   GNUNET_CRYPTO_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
261
262   rd.data_size = sizeof(GNUNET_HashCode);
263   rd.data = &bob_hash;
264   rd.record_type = GNUNET_GNS_RECORD_PKEY;
265
266   GNUNET_NAMESTORE_record_create (namestore_handle,
267                                   alice_key,
268                                   TEST_AUTHORITY_NAME,
269                                   &rd,
270                                   NULL,
271                                   NULL);
272
273   rd.data_size = sizeof(struct in_addr);
274   rd.data = mail;
275   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
276   sig = GNUNET_NAMESTORE_create_signature(bob_key,
277                                           GNUNET_TIME_absolute_get_forever(),
278                                           TEST_RECORD_NAME,
279                                           &rd, 1);
280
281   GNUNET_NAMESTORE_record_put (namestore_handle,
282                                &bob_pkey,
283                                TEST_RECORD_NAME,
284                                rd.expiration,
285                                1,
286                                &rd,
287                                sig,
288                                NULL,
289                                NULL);
290   
291   rd.data_size = sizeof(struct GNUNET_DNSPARSER_MxRecord)+strlen(TEST_MX_NAME)+1;
292   mx_record = GNUNET_malloc(rd.data_size);
293   mx_record->preference = 1;
294   mx_record->mxhost = (char*)&mx_record[1];
295   strcpy(mx_record->mxhost, TEST_MX_NAME);
296   rd.data = mx_record;
297   rd.record_type = GNUNET_GNS_RECORD_MX;
298   sig = GNUNET_NAMESTORE_create_signature(bob_key,
299                                           GNUNET_TIME_absolute_get_forever(),
300                                           "+",
301                                           &rd, 1);
302   GNUNET_NAMESTORE_record_put (namestore_handle,
303                                &bob_pkey,
304                                "+",
305                                rd.expiration,
306                                1,
307                                &rd,
308                                sig,
309                                &commence_testing,
310                                NULL);
311   GNUNET_free(mx_record);
312   GNUNET_free(mail);
313   GNUNET_free(sig);
314   GNUNET_CRYPTO_rsa_key_free(bob_key);
315   GNUNET_CRYPTO_rsa_key_free(alice_key);
316 }
317
318 static void
319 run (void *cls, char *const *args, const char *cfgfile,
320      const struct GNUNET_CONFIGURATION_Handle *c)
321 {
322   cfg = c;
323    /* Get path from configuration file */
324   if (GNUNET_YES !=
325       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
326                                              &test_directory))
327   {
328     ok = 404;
329     return;
330   }
331
332     
333   /* Set up a task to end testing if peer start fails */
334   die_task =
335       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
336                                     "didn't start all daemons in reasonable amount of time!!!");
337   
338   /* Start alice */
339   d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
340                                    NULL, NULL, NULL, &do_lookup, NULL);
341 }
342
343 static int
344 check ()
345 {
346   int ret;
347
348   /* Arguments for GNUNET_PROGRAM_run */
349   char *const argv[] = { "test-gns-simple-delegated-lookup", /* Name to give running binary */
350     "-c",
351     "test_gns_simple_lookup.conf",       /* Config file to use */
352 #if VERBOSE
353     "-L", "DEBUG",
354 #endif
355     NULL
356   };
357   struct GNUNET_GETOPT_CommandLineOption options[] = {
358     GNUNET_GETOPT_OPTION_END
359   };
360   /* Run the run function as a new program */
361   ret =
362       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
363                           "test-gns-simple-delegated-lookup", "nohelp", options, &run,
364                           &ok);
365   if (ret != GNUNET_OK)
366   {
367     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
368                 "`test-gns-simple-delegated-lookup': Failed with error code %d\n", ret);
369   }
370   return ok;
371 }
372
373 int
374 main (int argc, char *argv[])
375 {
376   int ret;
377
378   GNUNET_log_setup ("test-gns-simple-lookup",
379 #if VERBOSE
380                     "DEBUG",
381 #else
382                     "WARNING",
383 #endif
384                     NULL);
385   ret = check ();
386   /**
387    * Need to remove base directory, subdirectories taken care
388    * of by the testing framework.
389    */
390   return ret;
391 }
392
393 /* end of test_gns_twopeer.c */