namestore api change: include block expiration time in record create
[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   sig = GNUNET_NAMESTORE_create_signature(bob_key, TEST_RECORD_NAME,
208                                           &rd, 1);
209   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
210   nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
211                       + sizeof(struct GNSNameRecordBlock));
212   nrb->signature = *sig;
213   nrb->public_key = bob_pkey;
214   nrb->rd_count = htonl(1);
215   memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
216   memcpy(&nrb[1], TEST_RECORD_NAME, strlen(TEST_RECORD_NAME));
217   nrb_data = (char*)&nrb[1];
218   nrb_data += strlen(TEST_RECORD_NAME) + 1;
219
220   if (-1 == GNUNET_NAMESTORE_records_serialize (1,
221                                                 &rd,
222                                                 rd_payload_length,
223                                                 nrb_data))
224   {
225     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
226     ok = 3;
227     return;
228   }
229   GNUNET_CRYPTO_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
230   GNUNET_CRYPTO_hash(&bob_pkey,
231                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
232                      &zone_hash);
233   GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
234
235   rd_payload_length += sizeof(struct GNSNameRecordBlock) +
236     strlen(TEST_RECORD_NAME) + 1;
237   GNUNET_DHT_put (dht_handle, &xor_hash,
238                   0,
239                   GNUNET_DHT_RO_NONE,
240                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
241                   rd_payload_length,
242                   (char*)nrb,
243                   rd.expiration,
244                   DHT_OPERATION_TIMEOUT,
245                   NULL,
246                   NULL);
247   GNUNET_SCHEDULER_add_delayed(TIMEOUT, &finish_testing, NULL);
248 }
249
250 static void
251 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
252           const struct GNUNET_CONFIGURATION_Handle *cfg,
253           struct GNUNET_TESTING_Daemon *d, const char *emsg)
254 {
255   
256   
257   char* alice_keyfile;
258   GNUNET_HashCode bob_hash;
259   
260
261   GNUNET_SCHEDULER_cancel (die_task);
262
263   /* put records into namestore */
264   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
265   if (NULL == namestore_handle)
266   {
267     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
268     ok = -1;
269     return;
270   }
271   
272   /* dht */
273   dht_handle = GNUNET_DHT_connect(cfg, 1);
274   if (NULL == dht_handle)
275   {
276     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
277     ok = -1;
278     return;
279   }
280
281   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "gns",
282                                                           "ZONEKEY",
283                                                           &alice_keyfile))
284   {
285     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
286     ok = -1;
287     return;
288   }
289
290   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
291   bob_key = GNUNET_CRYPTO_rsa_key_create ();
292
293   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
294   GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
295   GNUNET_CRYPTO_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
296
297   struct GNUNET_NAMESTORE_RecordData rd;
298   rd.expiration = GNUNET_TIME_absolute_get_forever ();
299   rd.data_size = sizeof(GNUNET_HashCode);
300   rd.data = &bob_hash;
301   rd.record_type = GNUNET_GNS_RECORD_PKEY;
302
303   GNUNET_NAMESTORE_record_create (namestore_handle,
304                                   alice_key,
305                                   GNUNET_TIME_absolute_get_forever(),
306                                   TEST_AUTHORITY_NAME,
307                                   &rd,
308                                   &put_dht,
309                                   NULL);
310
311   
312
313 }
314
315 static void
316 run (void *cls, char *const *args, const char *cfgfile,
317      const struct GNUNET_CONFIGURATION_Handle *c)
318 {
319   cfg = c;
320    /* Get path from configuration file */
321   if (GNUNET_YES !=
322       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
323                                              &test_directory))
324   {
325     ok = 404;
326     return;
327   }
328
329     
330   /* Set up a task to end testing if peer start fails */
331   die_task =
332       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
333                                     "didn't start all daemons in reasonable amount of time!!!");
334   
335   /* Start alice */
336   d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
337                                    NULL, NULL, NULL, &do_lookup, NULL);
338 }
339
340 static int
341 check ()
342 {
343   int ret;
344
345   /* Arguments for GNUNET_PROGRAM_run */
346   char *const argv[] = { "test-gns-simple-lookup", /* Name to give running binary */
347     "-c",
348     "test_gns_simple_lookup.conf",       /* Config file to use */
349 #if VERBOSE
350     "-L", "DEBUG",
351 #endif
352     NULL
353   };
354   struct GNUNET_GETOPT_CommandLineOption options[] = {
355     GNUNET_GETOPT_OPTION_END
356   };
357   /* Run the run function as a new program */
358   ret =
359       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
360                           "test-gns-simple-lookup", "nohelp", options, &run,
361                           &ok);
362   if (ret != GNUNET_OK)
363   {
364     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
365                 "`test-gns-simple-lookup': Failed with error code %d\n", ret);
366   }
367   return ok;
368 }
369
370 int
371 main (int argc, char *argv[])
372 {
373   int ret;
374
375   GNUNET_log_setup ("test-gns-simple-lookup",
376 #if VERBOSE
377                     "DEBUG",
378 #else
379                     "WARNING",
380 #endif
381                     NULL);
382   ret = check ();
383   /**
384    * Need to remove base directory, subdirectories taken care
385    * of by the testing framework.
386    */
387   return ret;
388 }
389
390 /* end of test_gns_twopeer.c */