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