-support truncated HELLO names to work with 99-character per filename limitations...
[oweals/gnunet.git] / src / gns / test_gns_dht_shorten.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.alice.bob.gnunet"
61 #define TEST_IP "127.0.0.1"
62 #define TEST_RECORD_NAME "www"
63
64 #define TEST_AUTHORITY_NAME "bob"
65 #define TEST_AUTHORITY_ALICE "alice"
66 #define TEST_ALICE_PSEU "carol"
67 #define TEST_EXPECTED_RESULT "www.carol.gnunet"
68
69 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
70
71 /* Globals */
72
73 /**
74  * Directory to store temp data in, defined in config file
75  */
76 static char *test_directory;
77
78 struct GNUNET_TESTING_Daemon *d1;
79
80
81 /* Task handle to use to schedule test failure */
82 GNUNET_SCHEDULER_TaskIdentifier die_task;
83
84 /* Global return value (0 for success, anything else for failure) */
85 static int ok;
86
87 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
88
89 static struct GNUNET_GNS_Handle *gns_handle;
90
91 static struct GNUNET_DHT_Handle *dht_handle;
92
93 const struct GNUNET_CONFIGURATION_Handle *cfg;
94
95 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
96 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
97 struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
98 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
99
100 /**
101  * Check whether peers successfully shut down.
102  */
103 void
104 shutdown_callback (void *cls, const char *emsg)
105 {
106   if (emsg != NULL)
107   {
108     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
109     if (ok == 0)
110       ok = 2;
111   }
112
113   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
114 }
115
116
117 /**
118  * Called when gns shorten finishes
119  */
120 static void
121 process_shorten_result(void* cls, const char* sname)
122 {
123   GNUNET_GNS_disconnect(gns_handle);
124
125   ok = 0;
126
127   if (sname == NULL)
128   {
129     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
130                 "shorten test failed!\n");
131     ok = 1;
132   }
133   else
134   {
135     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136                 "%s shortened to %s\n", (char*)cls, sname);
137     if (0 != strcmp(sname, TEST_EXPECTED_RESULT))
138     {
139       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
140                   "shorten test failed! (wanted: %s got: %s\n",
141                   (char*)cls, sname);
142       ok = 1;
143     }
144
145     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shorten test succeeded!\n");
146
147   }
148
149   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
150   GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
151                               GNUNET_YES, GNUNET_NO);
152 }
153
154 /**
155  * Function scheduled to be run on the successful start of services
156  * tries to look up the dns record for TEST_DOMAIN
157  */
158 static void
159 commence_testing (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 {
161   GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES);
162
163   gns_handle = GNUNET_GNS_connect(cfg);
164
165   if (NULL == gns_handle)
166   {
167     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168                 "Failed to connect to GNS!\n");
169     ok = 1;
170   }
171
172   GNUNET_GNS_shorten(gns_handle, TEST_DOMAIN, &process_shorten_result,
173                      TEST_DOMAIN);
174 }
175
176 /**
177  * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
178  * down the peers without freeing memory associated with GET request.
179  */
180 static void
181 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
182 {
183
184   if (d1 != NULL)
185     GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
186                                 GNUNET_YES, GNUNET_NO);
187   GNUNET_SCHEDULER_cancel (die_task);
188 }
189
190 /**
191  * Check if the get_handle is being used, if so stop the request.  Either
192  * way, schedule the end_badly_cont function which actually shuts down the
193  * test.
194  */
195 static void
196 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
197 {
198   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
199               (char *) cls);
200   GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
201   ok = 1;
202 }
203
204
205 static void
206 put_www_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
207 {
208   struct GNSNameRecordBlock *nrb;
209   GNUNET_HashCode name_hash;
210   GNUNET_HashCode xor_hash;
211   GNUNET_HashCode zone_hash;
212   uint32_t rd_payload_length;
213   char* nrb_data = NULL;
214   struct GNUNET_CRYPTO_RsaSignature *sig;
215   struct GNUNET_NAMESTORE_RecordData rd;
216   char* ip = TEST_IP;
217   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
218   
219   rd.expiration = GNUNET_TIME_absolute_get_forever ();
220   GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
221   rd.data_size = sizeof(struct in_addr);
222   rd.data = web;
223   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
224
225   sig = GNUNET_NAMESTORE_create_signature(alice_key,
226                                           GNUNET_TIME_absolute_get_forever(),
227                                           TEST_RECORD_NAME,
228                                           &rd, 1);
229   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
230   nrb = GNUNET_malloc(rd_payload_length + strlen(TEST_RECORD_NAME) + 1
231                       + sizeof(struct GNSNameRecordBlock));
232   nrb->signature = *sig;
233   nrb->public_key = alice_pkey;
234   nrb->rd_count = htonl(1);
235   memset(&nrb[1], 0, strlen(TEST_RECORD_NAME) + 1);
236   memcpy(&nrb[1], TEST_RECORD_NAME, strlen(TEST_RECORD_NAME));
237   nrb_data = (char*)&nrb[1];
238   nrb_data += strlen(TEST_RECORD_NAME) + 1;
239
240   if (-1 == GNUNET_NAMESTORE_records_serialize (1,
241                                                 &rd,
242                                                 rd_payload_length,
243                                                 nrb_data))
244   {
245     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
246     ok = 3;
247     GNUNET_free (nrb);
248     return;
249   }
250   GNUNET_CRYPTO_hash(TEST_RECORD_NAME, strlen(TEST_RECORD_NAME), &name_hash);
251   GNUNET_CRYPTO_hash(&alice_pkey,
252                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
253                      &zone_hash);
254   GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
255
256   rd_payload_length += sizeof(struct GNSNameRecordBlock) +
257     strlen(TEST_RECORD_NAME) + 1;
258   GNUNET_DHT_put (dht_handle, &xor_hash,
259                   0,
260                   GNUNET_DHT_RO_NONE,
261                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
262                   rd_payload_length,
263                   (char*)nrb,
264                   rd.expiration,
265                   DHT_OPERATION_TIMEOUT,
266                   NULL,
267                   NULL);
268   GNUNET_free (nrb);
269   GNUNET_SCHEDULER_add_delayed(TIMEOUT, &commence_testing, NULL);
270 }
271
272
273 static void
274 put_alice_pseu_dht(void *cls, int32_t success, const char *emsg)
275 {
276   struct GNSNameRecordBlock *nrb;
277   GNUNET_HashCode name_hash;
278   GNUNET_HashCode xor_hash;
279   GNUNET_HashCode zone_hash;
280   uint32_t rd_payload_length;
281   char* nrb_data = NULL;
282   struct GNUNET_CRYPTO_RsaSignature *sig;
283   struct GNUNET_NAMESTORE_RecordData rd;
284   
285   rd.expiration = GNUNET_TIME_absolute_get_forever ();
286   rd.data_size = strlen(TEST_ALICE_PSEU);
287   rd.data = TEST_ALICE_PSEU;
288   rd.record_type = GNUNET_GNS_RECORD_PSEU;
289
290   sig = GNUNET_NAMESTORE_create_signature(alice_key,
291                                           GNUNET_TIME_absolute_get_forever(),
292                                           "+", //empty name for pseu
293                                           &rd, 1);
294   rd_payload_length = GNUNET_NAMESTORE_records_get_size (1, &rd);
295   nrb = GNUNET_malloc(rd_payload_length + strlen("") + 1
296                       + sizeof(struct GNSNameRecordBlock));
297   nrb->signature = *sig;
298   nrb->public_key = alice_pkey;
299   nrb->rd_count = htonl(1);
300   memset(&nrb[1], 0, strlen("+") + 1);
301   memcpy(&nrb[1], "+", strlen("+"));
302   nrb_data = (char*)&nrb[1];
303   nrb_data += strlen("+") + 1;
304
305   if (-1 == GNUNET_NAMESTORE_records_serialize (1,
306                                                 &rd,
307                                                 rd_payload_length,
308                                                 nrb_data))
309   {
310     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
311     ok = 3;
312     GNUNET_free (nrb);
313     return;
314   }
315   GNUNET_CRYPTO_hash("+", strlen("+"), &name_hash);
316   GNUNET_CRYPTO_hash(&alice_pkey,
317                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
318                      &zone_hash);
319   GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
320
321   rd_payload_length += sizeof(struct GNSNameRecordBlock) +
322     strlen("+") + 1;
323   GNUNET_DHT_put (dht_handle, &xor_hash,
324                   0,
325                   GNUNET_DHT_RO_NONE,
326                   GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
327                   rd_payload_length,
328                   (char*)nrb,
329                   rd.expiration,
330                   DHT_OPERATION_TIMEOUT,
331                   NULL,
332                   NULL);
333   GNUNET_free (nrb);
334   GNUNET_SCHEDULER_add_delayed(TIMEOUT, &put_www_dht, NULL);
335 }
336
337 static void
338 do_shorten(void *cls, const struct GNUNET_PeerIdentity *id,
339           const struct GNUNET_CONFIGURATION_Handle *cfg,
340           struct GNUNET_TESTING_Daemon *d, const char *emsg)
341 {
342   
343   char* my_keyfile;
344   struct GNUNET_CRYPTO_RsaPrivateKey *my_key;
345   GNUNET_HashCode bob_hash;
346   GNUNET_HashCode alice_hash;
347   struct GNUNET_CRYPTO_RsaSignature *sig;
348   
349
350   GNUNET_SCHEDULER_cancel (die_task);
351
352   /* put records into namestore */
353   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
354   if (NULL == namestore_handle)
355   {
356     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
357     ok = -1;
358     return;
359   }
360   
361   /* dht */
362   dht_handle = GNUNET_DHT_connect(cfg, 1);
363   if (NULL == dht_handle)
364   {
365     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to dht\n");
366     ok = -1;
367     return;
368   }
369
370   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "gns",
371                                                           "ZONEKEY",
372                                                           &my_keyfile))
373   {
374     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
375     ok = -1;
376     return;
377   }
378
379   my_key = GNUNET_CRYPTO_rsa_key_create_from_file (my_keyfile);
380   alice_key = GNUNET_CRYPTO_rsa_key_create ();
381   bob_key = GNUNET_CRYPTO_rsa_key_create ();
382
383   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
384   GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
385   GNUNET_CRYPTO_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
386   GNUNET_CRYPTO_hash(&alice_pkey, sizeof(alice_pkey), &alice_hash);
387
388   struct GNUNET_NAMESTORE_RecordData rd;
389   rd.expiration = GNUNET_TIME_absolute_get_forever ();
390   rd.data_size = sizeof(GNUNET_HashCode);
391   rd.data = &bob_hash;
392   rd.record_type = GNUNET_GNS_RECORD_PKEY;
393
394   GNUNET_NAMESTORE_record_create (namestore_handle,
395                                   my_key,
396                                   TEST_AUTHORITY_NAME,
397                                   &rd,
398                                   NULL,
399                                   NULL);
400   
401   rd.data = &alice_hash;
402
403   sig = GNUNET_NAMESTORE_create_signature(bob_key,
404                                           GNUNET_TIME_absolute_get_forever(),
405                                           TEST_AUTHORITY_ALICE,
406                                           &rd,
407                                           1);
408
409   GNUNET_NAMESTORE_record_put (namestore_handle,
410                                &bob_pkey,
411                                TEST_AUTHORITY_ALICE,
412                                GNUNET_TIME_absolute_get_forever(),
413                                1,
414                                &rd,
415                                sig,
416                                &put_alice_pseu_dht,
417                                NULL);
418
419
420
421   
422
423 }
424
425 static void
426 run (void *cls, char *const *args, const char *cfgfile,
427      const struct GNUNET_CONFIGURATION_Handle *c)
428 {
429   cfg = c;
430    /* Get path from configuration file */
431   if (GNUNET_YES !=
432       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
433                                              &test_directory))
434   {
435     ok = 404;
436     return;
437   }
438
439     
440   /* Set up a task to end testing if peer start fails */
441   die_task =
442       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
443                                     "didn't start all daemons in reasonable amount of time!!!");
444   
445   /* Start alice */
446   d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
447                                    NULL, NULL, NULL, &do_shorten, NULL);
448 }
449
450 static int
451 check ()
452 {
453   int ret;
454
455   /* Arguments for GNUNET_PROGRAM_run */
456   char *const argv[] = { "test-gns-dht-delegated-lookup", /* Name to give running binary */
457     "-c",
458     "test_gns_simple_lookup.conf",       /* Config file to use */
459 #if VERBOSE
460     "-L", "DEBUG",
461 #endif
462     NULL
463   };
464   struct GNUNET_GETOPT_CommandLineOption options[] = {
465     GNUNET_GETOPT_OPTION_END
466   };
467   /* Run the run function as a new program */
468   ret =
469       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
470                           "test-gns-dht-delegated-lookup", "nohelp", options, &run,
471                           &ok);
472   if (ret != GNUNET_OK)
473   {
474     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
475                 "`test-gns-dht-delegated-lookup': Failed with error code %d\n", ret);
476   }
477   return ok;
478 }
479
480 int
481 main (int argc, char *argv[])
482 {
483   int ret;
484
485   GNUNET_log_setup ("test-gns-simple-lookup",
486 #if VERBOSE
487                     "DEBUG",
488 #else
489                     "WARNING",
490 #endif
491                     NULL);
492   ret = check ();
493   /**
494    * Need to remove base directory, subdirectories taken care
495    * of by the testing framework.
496    */
497   return ret;
498 }
499
500 /* end of test_gns_twopeer.c */