-use precomp keys
[oweals/gnunet.git] / src / gns / test_gns_simple_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_simple_shorten.c
22  * @brief basic shorten test for gns api
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, 20)
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 "www.alice.bob.gnunet"
46 #define TEST_IP "127.0.0.1"
47 #define TEST_RECORD_NAME "www"
48
49 #define TEST_AUTHORITY_BOB "bob"
50 #define TEST_AUTHORITY_ALICE "alice"
51 #define TEST_ALICE_PSEU "carol"
52 #define TEST_EXPECTED_RESULT "www.carol.gnunet"
53
54 #define KEYFILE_BOB "../namestore/zonefiles/HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"
55 #define KEYFILE_ALICE "../namestore/zonefiles/N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"
56
57 /* Globals */
58
59 /**
60  * Directory to store temp data in, defined in config file
61  */
62 static char *test_directory;
63
64 struct GNUNET_TESTING_Daemon *d1;
65
66
67 /* Task handle to use to schedule test failure */
68 GNUNET_SCHEDULER_TaskIdentifier die_task;
69
70 /* Global return value (0 for success, anything else for failure) */
71 static int ok;
72
73 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
74
75 static struct GNUNET_GNS_Handle *gns_handle;
76
77 const struct GNUNET_CONFIGURATION_Handle *cfg;
78
79 /**
80  * Check whether peers successfully shut down.
81  */
82 static void
83 shutdown_callback (void *cls, const char *emsg)
84 {
85   if (emsg != NULL)
86   {
87     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
88     if (ok == 0)
89       ok = 2;
90   }
91
92   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
93 }
94
95 /**
96  * Called when gns shorten finishes
97  */
98 static void
99 process_shorten_result(void* cls, const char* sname)
100 {
101   GNUNET_GNS_disconnect(gns_handle);
102
103   ok = 0;
104
105   if (sname == NULL)
106   {
107     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
108                 "shorten test failed!\n");
109     ok = 1;
110   }
111   else
112   {
113     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114                 "%s shortened to %s\n", (char*)cls, sname);
115     if (0 != strcmp(sname, TEST_EXPECTED_RESULT))
116     {
117       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118                   "shorten test failed! (wanted: %s got: %s\n",
119                   (char*)cls, sname);
120       ok = 1;
121     }
122
123     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shorten test succeeded!\n");
124
125   }
126
127   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
128   GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
129                               GNUNET_YES, GNUNET_NO);
130 }
131
132 /**
133  * Function scheduled to be run on the successful start of services
134  * tries to shorten the name TEST_DOMAIN using gns
135  */
136 static void
137 commence_testing (void *cls, int32_t success, const char *emsg)
138 {
139   
140   
141
142   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143               "disconnecting from namestore\n");
144   GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES);
145   
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147               "connecting to gns\n");
148   gns_handle = GNUNET_GNS_connect(cfg);
149
150   if (NULL == gns_handle)
151   {
152     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153                 "failed to connect to gns\n");
154     ok = 1;
155     return;
156   }
157
158   GNUNET_GNS_shorten(gns_handle, TEST_DOMAIN, &process_shorten_result,
159                      TEST_DOMAIN);
160   
161 }
162
163 /**
164  * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
165  * down the peers without freeing memory associated with GET request.
166  */
167 static void
168 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
169 {
170
171   if (d1 != NULL)
172     GNUNET_TESTING_daemon_stop (d1, TIMEOUT, &shutdown_callback, NULL,
173                                 GNUNET_YES, GNUNET_NO);
174   GNUNET_SCHEDULER_cancel (die_task);
175 }
176
177 /**
178  * Check if the get_handle is being used, if so stop the request.  Either
179  * way, schedule the end_badly_cont function which actually shuts down the
180  * test.
181  */
182 static void
183 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
184 {
185   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
186               (char *) cls);
187   GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
188   ok = 1;
189 }
190
191 static void
192 do_shorten(void *cls, const struct GNUNET_PeerIdentity *id,
193           const struct GNUNET_CONFIGURATION_Handle *cfg,
194           struct GNUNET_TESTING_Daemon *d, const char *emsg)
195 {
196   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded our_pkey;
197   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
198   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
199   struct GNUNET_CRYPTO_RsaPrivateKey *our_key;
200   struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
201   struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
202   struct GNUNET_CRYPTO_ShortHashCode bob_hash;
203   struct GNUNET_CRYPTO_ShortHashCode alice_hash;
204   struct GNUNET_CRYPTO_RsaSignature *sig;
205   char* our_keyfile;
206
207   GNUNET_SCHEDULER_cancel (die_task);
208
209   /* put records into namestore */
210   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
211   if (NULL == namestore_handle)
212   {
213     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
214     ok = -1;
215     return;
216   }
217
218   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
219                                                           "ZONEKEY",
220                                                           &our_keyfile))
221   {
222     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
223     ok = -1;
224     return;
225   }
226
227   our_key = GNUNET_CRYPTO_rsa_key_create_from_file (our_keyfile);
228   GNUNET_free(our_keyfile);
229
230   bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
231   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_ALICE);
232   
233   GNUNET_CRYPTO_rsa_key_get_public (our_key, &our_pkey);
234   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
235   GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
236
237   struct GNUNET_NAMESTORE_RecordData rd;
238   char* ip = TEST_IP;
239   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
240   rd.expiration = GNUNET_TIME_absolute_get_forever ();
241   GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
242   
243   GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
244
245   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
246   rd.data = &bob_hash;
247   rd.record_type = GNUNET_GNS_RECORD_PKEY;
248   
249   /* put bob into our zone */
250   GNUNET_NAMESTORE_record_create (namestore_handle,
251                                   our_key,
252                                   TEST_AUTHORITY_BOB,
253                                   &rd,
254                                   NULL,
255                                   NULL);
256   
257   /* put alice into bobs zone */
258   GNUNET_CRYPTO_short_hash(&alice_pkey, sizeof(alice_pkey), &alice_hash);
259   rd.data = &alice_hash;
260   sig = GNUNET_NAMESTORE_create_signature(bob_key,
261                                           GNUNET_TIME_absolute_get_forever(),
262                                           TEST_AUTHORITY_ALICE,
263                                           &rd, 1);
264
265   GNUNET_NAMESTORE_record_put (namestore_handle,
266                                &bob_pkey,
267                                TEST_AUTHORITY_ALICE,
268                                GNUNET_TIME_absolute_get_forever(),
269                                1,
270                                &rd,
271                                sig,
272                                NULL,
273                                NULL);
274   GNUNET_free(sig);
275   /* put www A record and PSEU into alice's zone */
276
277   rd.data_size = sizeof(struct in_addr);
278   rd.data = web;
279   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
280   sig = GNUNET_NAMESTORE_create_signature(alice_key,
281                                           GNUNET_TIME_absolute_get_forever(),
282                                           TEST_RECORD_NAME,
283                                           &rd, 1);
284
285   GNUNET_NAMESTORE_record_put (namestore_handle,
286                                &alice_pkey,
287                                TEST_RECORD_NAME,
288                                GNUNET_TIME_absolute_get_forever(),
289                                1,
290                                &rd,
291                                sig,
292                                NULL,
293                                NULL);
294   
295   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
296   rd.data = &alice_hash;
297   rd.record_type = GNUNET_GNS_RECORD_PKEY;
298   GNUNET_free(sig);
299
300   GNUNET_NAMESTORE_record_create (namestore_handle,
301                                our_key,
302                                TEST_ALICE_PSEU,
303                                &rd,
304                                &commence_testing,
305                                NULL);
306
307   GNUNET_free(web);
308   GNUNET_CRYPTO_rsa_key_free(our_key);
309   GNUNET_CRYPTO_rsa_key_free(bob_key);
310   GNUNET_CRYPTO_rsa_key_free(alice_key);
311 }
312
313 static void
314 run (void *cls, char *const *args, const char *cfgfile,
315      const struct GNUNET_CONFIGURATION_Handle *c)
316 {
317   cfg = c;
318    /* Get path from configuration file */
319   if (GNUNET_YES !=
320       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
321                                              &test_directory))
322   {
323     ok = 404;
324     return;
325   }
326
327     
328   /* Set up a task to end testing if peer start fails */
329   die_task =
330       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
331                                     "didn't start all daemons in reasonable amount of time!!!");
332   
333   /* Start alice */
334   d1 = GNUNET_TESTING_daemon_start(cfg, TIMEOUT, GNUNET_NO, NULL, NULL, 0,
335                                    NULL, NULL, NULL, &do_shorten, NULL);
336 }
337
338 static int
339 check ()
340 {
341   int ret;
342
343   /* Arguments for GNUNET_PROGRAM_run */
344   char *const argv[] = { "test-gns-simple-shorten", /* Name to give running binary */
345     "-c",
346     "test_gns_simple_lookup.conf",       /* Config file to use */
347 #if VERBOSE
348     "-L", "DEBUG",
349 #endif
350     NULL
351   };
352   struct GNUNET_GETOPT_CommandLineOption options[] = {
353     GNUNET_GETOPT_OPTION_END
354   };
355   /* Run the run function as a new program */
356   ret =
357       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
358                           "test-gns-simple-shorten", "nohelp", options, &run,
359                           &ok);
360   if (ret != GNUNET_OK)
361   {
362     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
363                 "`test-gns-simple-shorten': Failed with error code %d\n", ret);
364   }
365   return ok;
366 }
367
368 int
369 main (int argc, char *argv[])
370 {
371   int ret;
372
373   GNUNET_log_setup ("test-gns-simple-shorten",
374 #if VERBOSE
375                     "DEBUG",
376 #else
377                     "WARNING",
378 #endif
379                     NULL);
380   ret = check ();
381   /**
382    * Need to remove base directory, subdirectories taken care
383    * of by the testing framework.
384    */
385   return ret;
386 }
387
388 /* end of test_gns_twopeer.c */