- changes to signing verfifying: includes block expiration
[oweals/gnunet.git] / src / gns / test_gns_dht_delegated_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_twopeer.c
22  * @brief base testcase for testing DHT service with
23  *        two running peers.
24  *
25  * This testcase starts peers using the GNUNET_TESTING_daemons_start
26  * function call.  On peer start, connects to the peers DHT service
27  * by calling GNUNET_DHT_connected.  Once notified about all peers
28  * being started (by the peers_started_callback function), calls
29  * GNUNET_TESTING_connect_topology, which connects the peers in a
30  * "straight line" topology.  On notification that all peers have
31  * been properly connected, calls the do_get function which initiates
32  * a GNUNET_DHT_get from the *second* peer. Once the GNUNET_DHT_get
33  * function starts, runs the do_put function to insert data at the first peer.
34  *   If the GET is successful, schedules finish_testing
35  * to stop the test and shut down peers.  If GET is unsuccessful
36  * after GET_TIMEOUT seconds, prints an error message and shuts down
37  * the peers.
38  */
39 #include "platform.h"
40 #include "gnunet_testing_lib.h"
41 #include "gnunet_core_service.h"
42 #include "block_gns.h"
43 #include "gnunet_signatures.h"
44 #include "gnunet_namestore_service.h"
45 #include "../namestore/namestore.h"
46 #include "gnunet_dnsparser_lib.h"
47 #include "gnunet_dht_service.h"
48 #include "gnunet_gns_service.h"
49
50 /* DEFINES */
51 #define VERBOSE GNUNET_YES
52
53 /* Timeout for entire testcase */
54 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
55
56 /* If number of peers not in config file, use this number */
57 #define DEFAULT_NUM_PEERS 2
58
59 /* test records to resolve */
60 #define TEST_DOMAIN "www.bob.gnunet"
61 #define TEST_IP "127.0.0.1"
62 #define TEST_RECORD_NAME "www"
63
64 #define TEST_AUTHORITY_NAME "bob"
65
66 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
67
68 /* Globals */
69
70 /**
71  * Directory to store temp data in, defined in config file
72  */
73 static char *test_directory;
74
75 struct GNUNET_TESTING_Daemon *d1;
76
77
78 /* Task handle to use to schedule test failure */
79 GNUNET_SCHEDULER_TaskIdentifier die_task;
80
81 /* Global return value (0 for success, anything else for failure) */
82 static int ok;
83
84 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
85
86 static struct GNUNET_DHT_Handle *dht_handle;
87
88 const struct GNUNET_CONFIGURATION_Handle *cfg;
89
90 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
91 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
92 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
93 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
94
95 /**
96  * Check whether peers successfully shut down.
97  */
98 void
99 shutdown_callback (void *cls, const char *emsg)
100 {
101   if (emsg != NULL)
102   {
103     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
104     if (ok == 0)
105       ok = 2;
106   }
107
108   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
109 }
110
111 /**
112  * Function scheduled to be run on the successful start of services
113  * tries to look up the dns record for TEST_DOMAIN
114  */
115 static void
116 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 {
118   struct hostent *he;
119   struct in_addr a;
120   char* addr;
121  
122   GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES);
123   GNUNET_DHT_disconnect(dht_handle);
124
125   he = gethostbyname (TEST_DOMAIN);
126
127   if (!he)
128   {
129     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
130                 "gethostbyname failed, rp_filtering?\n");
131     ok = 2;
132   }
133   else
134   {
135     ok = 1;
136     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", he->h_name);
137     while (*he->h_addr_list)
138     {
139       memcpy(&a, *he->h_addr_list++, sizeof(a));
140       addr = inet_ntoa(a);
141       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
142       if (0 == strcmp(addr, TEST_IP))
143       {
144         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
145                     "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
146         ok = 0;
147       }
148       else
149       {
150         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No resolution!\n");
151       }
152     }
153   }
154   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
155   GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
156                               GNUNET_YES, GNUNET_NO);
157 }
158
159 /**
160  * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
161  * down the peers without freeing memory associated with GET request.
162  */
163 static void
164 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
165 {
166
167   if (d1 != NULL)
168     GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
169                                 GNUNET_YES, GNUNET_NO);
170   GNUNET_SCHEDULER_cancel (die_task);
171 }
172
173 /**
174  * Check if the get_handle is being used, if so stop the request.  Either
175  * way, schedule the end_badly_cont function which actually shuts down the
176  * test.
177  */
178 static void
179 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
180 {
181   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
182               (char *) cls);
183   GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
184   ok = 1;
185 }
186
187
188 static void
189 put_dht(void *cls, int32_t success, const char *emsg)
190 {
191   struct GNSNameRecordBlock *nrb;
192   GNUNET_HashCode name_hash;
193   GNUNET_HashCode xor_hash;
194   GNUNET_HashCode zone_hash;
195   uint32_t rd_payload_length;
196   char* nrb_data = NULL;
197   struct GNUNET_CRYPTO_RsaSignature *sig;
198   struct GNUNET_NAMESTORE_RecordData rd;
199   char* ip = TEST_IP;
200   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
201   
202   rd.expiration = GNUNET_TIME_absolute_get_forever ();
203   GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
204   rd.data_size = sizeof(struct in_addr);
205   rd.data = web;
206   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
207
208   sig = GNUNET_NAMESTORE_create_signature(bob_key, GNUNET_TIME_absolute_get_forever(), TEST_RECORD_NAME,
209                                           &rd, 1);
210   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
211   nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
212                       + sizeof(struct GNSNameRecordBlock));
213   nrb->signature = *sig;
214   nrb->public_key = bob_pkey;
215   nrb->rd_count = htonl(1);
216   memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
217   memcpy(&nrb[1], TEST_RECORD_NAME, strlen(TEST_RECORD_NAME));
218   nrb_data = (char*)&nrb[1];
219   nrb_data += strlen(TEST_RECORD_NAME) + 1;
220
221   if (-1 == GNUNET_NAMESTORE_records_serialize (1,
222                                                 &rd,
223                                                 rd_payload_length,
224                                                 nrb_data))
225   {
226     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
227     ok = 3;
228     GNUNET_free (nrb);
229     return;
230   }
231   GNUNET_CRYPTO_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
232   GNUNET_CRYPTO_hash(&bob_pkey,
233                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
234                      &zone_hash);
235   GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
236
237   rd_payload_length += sizeof(struct GNSNameRecordBlock) +
238     strlen(TEST_RECORD_NAME) + 1;
239   GNUNET_DHT_put (dht_handle, &xor_hash,
240                   0,
241                   GNUNET_DHT_RO_NONE,
242                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
243                   rd_payload_length,
244                   (char*)nrb,
245                   rd.expiration,
246                   DHT_OPERATION_TIMEOUT,
247                   NULL,
248                   NULL);
249   GNUNET_free (nrb);
250   GNUNET_SCHEDULER_add_delayed(TIMEOUT, &finish_testing, NULL);
251 }
252
253 static void
254 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
255           const struct GNUNET_CONFIGURATION_Handle *cfg,
256           struct GNUNET_TESTING_Daemon *d, const char *emsg)
257 {
258   
259   
260   char* alice_keyfile;
261   GNUNET_HashCode bob_hash;
262   
263
264   GNUNET_SCHEDULER_cancel (die_task);
265
266   /* put records into namestore */
267   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
268   if (NULL == namestore_handle)
269   {
270     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
271     ok = -1;
272     return;
273   }
274   
275   /* dht */
276   dht_handle = GNUNET_DHT_connect(cfg, 1);
277   if (NULL == dht_handle)
278   {
279     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
280     ok = -1;
281     return;
282   }
283
284   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "gns",
285                                                           "ZONEKEY",
286                                                           &alice_keyfile))
287   {
288     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
289     ok = -1;
290     return;
291   }
292
293   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
294   bob_key = GNUNET_CRYPTO_rsa_key_create ();
295
296   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
297   GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
298   GNUNET_CRYPTO_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
299
300   struct GNUNET_NAMESTORE_RecordData rd;
301   rd.expiration = GNUNET_TIME_absolute_get_forever ();
302   rd.data_size = sizeof(GNUNET_HashCode);
303   rd.data = &bob_hash;
304   rd.record_type = GNUNET_GNS_RECORD_PKEY;
305
306   GNUNET_NAMESTORE_record_create (namestore_handle,
307                                   alice_key,
308                                   TEST_AUTHORITY_NAME,
309                                   &rd,
310                                   &put_dht,
311                                   NULL);
312
313   
314
315 }
316
317 static void
318 run (void *cls, char *const *args, const char *cfgfile,
319      const struct GNUNET_CONFIGURATION_Handle *c)
320 {
321   cfg = c;
322    /* Get path from configuration file */
323   if (GNUNET_YES !=
324       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
325                                              &test_directory))
326   {
327     ok = 404;
328     return;
329   }
330
331     
332   /* Set up a task to end testing if peer start fails */
333   die_task =
334       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
335                                     "didn't start all daemons in reasonable amount of time!!!");
336   
337   /* Start alice */
338   d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
339                                    NULL, NULL, NULL, &do_lookup, NULL);
340 }
341
342 static int
343 check ()
344 {
345   int ret;
346
347   /* Arguments for GNUNET_PROGRAM_run */
348   char *const argv[] = { "test-gns-simple-lookup", /* Name to give running binary */
349     "-c",
350     "test_gns_simple_lookup.conf",       /* Config file to use */
351 #if VERBOSE
352     "-L", "DEBUG",
353 #endif
354     NULL
355   };
356   struct GNUNET_GETOPT_CommandLineOption options[] = {
357     GNUNET_GETOPT_OPTION_END
358   };
359   /* Run the run function as a new program */
360   ret =
361       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
362                           "test-gns-simple-lookup", "nohelp", options, &run,
363                           &ok);
364   if (ret != GNUNET_OK)
365   {
366     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
367                 "`test-gns-simple-lookup': Failed with error code %d\n", ret);
368   }
369   return ok;
370 }
371
372 int
373 main (int argc, char *argv[])
374 {
375   int ret;
376
377   GNUNET_log_setup ("test-gns-simple-lookup",
378 #if VERBOSE
379                     "DEBUG",
380 #else
381                     "WARNING",
382 #endif
383                     NULL);
384   ret = check ();
385   /**
386    * Need to remove base directory, subdirectories taken care
387    * of by the testing framework.
388    */
389   return ret;
390 }
391
392 /* end of test_gns_twopeer.c */