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