fix
[oweals/gnunet.git] / src / gns / test_gns_simple_get_authority.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 "alice.bob.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 /**
79  * Check whether peers successfully shut down.
80  */
81 static void
82 shutdown_callback (void *cls, const char *emsg)
83 {
84   if (emsg != NULL)
85   {
86     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error on shutdown! ret=%d\n", ok);
87     if (ok == 0)
88       ok = 2;
89   }
90
91   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "done(ret=%d)!\n", ok);
92 }
93
94 /**
95  * Called when gns_get_authority finishes
96  */
97 static void
98 process_auth_result(void* cls, const char* aname)
99 {
100   GNUNET_GNS_disconnect(gns_handle);
101
102
103   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
104               "disconnecting from namestore\n");
105   GNUNET_NAMESTORE_disconnect (namestore_handle);
106   ok = 0;
107
108   if (aname == NULL)
109   {
110     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111                 "get_authority test failed!\n");
112     ok = 1;
113   }
114   else
115   {
116     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117                 "%s authority is %s\n", (char*)cls, aname);
118     if (0 != strcmp(aname, TEST_EXPECTED_RESULT))
119     {
120       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
121                   "get_authority test failed! (wanted: %s got: %s\n",
122                   TEST_EXPECTED_RESULT, aname);
123       ok = 1;
124     }
125
126     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "get_authority test finished!\n");
127
128   }
129
130   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
131   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
132 }
133
134 /**
135  * Function scheduled to be run on the successful start of services
136  * tries to shorten the name TEST_DOMAIN using gns
137  */
138 static void
139 commence_testing (void *cls, int32_t success, const char *emsg)
140 {
141   
142   
143   
144   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145               "connecting to gns\n");
146   gns_handle = GNUNET_GNS_connect(cfg);
147
148   if (NULL == gns_handle)
149   {
150     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151                 "failed to connect to gns\n");
152     ok = 1;
153     return;
154   }
155
156   GNUNET_GNS_get_authority(gns_handle, TEST_DOMAIN, &process_auth_result,
157                      TEST_DOMAIN);
158   
159 }
160
161 /**
162  * Continuation for the GNUNET_DHT_get_stop call, so that we don't shut
163  * down the peers without freeing memory associated with GET request.
164  */
165 static void
166 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
167 {
168
169   if (pg != NULL)
170     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
171   GNUNET_SCHEDULER_cancel (die_task);
172 }
173
174 /**
175  * Check if the get_handle is being used, if so stop the request.  Either
176  * way, schedule the end_badly_cont function which actually shuts down the
177  * test.
178  */
179 static void
180 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
181 {
182   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test with error: `%s'!\n",
183               (char *) cls);
184   GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
185   ok = 1;
186 }
187
188 static void
189 do_shorten(void *cls, const struct GNUNET_PeerIdentity *id,
190           const struct GNUNET_CONFIGURATION_Handle *_cfg,
191           struct GNUNET_TESTING_Daemon *d, const char *emsg)
192 {
193   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded our_pkey;
194   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
195   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
196   struct GNUNET_CRYPTO_RsaPrivateKey *our_key;
197   struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
198   struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
199   struct GNUNET_CRYPTO_ShortHashCode bob_hash;
200   struct GNUNET_CRYPTO_ShortHashCode alice_hash;
201   struct GNUNET_CRYPTO_RsaSignature *sig;
202   char* our_keyfile;
203
204   cfg = _cfg;
205
206   GNUNET_SCHEDULER_cancel (die_task);
207
208   /* put records into namestore */
209   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
210   if (NULL == namestore_handle)
211   {
212     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
213     ok = -1;
214     return;
215   }
216
217   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
218                                                           "ZONEKEY",
219                                                           &our_keyfile))
220   {
221     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
222     ok = -1;
223     return;
224   }
225
226   our_key = GNUNET_CRYPTO_rsa_key_create_from_file (our_keyfile);
227   GNUNET_free(our_keyfile);
228
229   bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
230   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_ALICE);
231   
232   GNUNET_CRYPTO_rsa_key_get_public (our_key, &our_pkey);
233   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
234   GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
235
236   struct GNUNET_NAMESTORE_RecordData rd;
237   char* ip = TEST_IP;
238   struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
239   rd.expiration_time = UINT64_MAX;
240   GNUNET_assert(1 == inet_pton (AF_INET, ip, web));
241   
242   GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
243
244   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
245   rd.data = &bob_hash;
246   rd.record_type = GNUNET_GNS_RECORD_PKEY;
247   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
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, GNUNET_TIME_UNIT_FOREVER_ABS, TEST_AUTHORITY_ALICE,
261                                           &rd, 1);
262
263   GNUNET_NAMESTORE_record_put (namestore_handle,
264                                &bob_pkey,
265                                TEST_AUTHORITY_ALICE,
266                                GNUNET_TIME_UNIT_FOREVER_ABS,
267                                1,
268                                &rd,
269                                sig,
270                                NULL,
271                                NULL);
272
273   /* put www A record and PSEU into alice's zone */
274
275   rd.data_size = sizeof(struct in_addr);
276   rd.data = web;
277   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
278   sig = GNUNET_NAMESTORE_create_signature(alice_key,GNUNET_TIME_UNIT_FOREVER_ABS,  TEST_RECORD_NAME,
279                                           &rd, 1);
280
281   GNUNET_NAMESTORE_record_put (namestore_handle,
282                                &alice_pkey,
283                                TEST_RECORD_NAME,
284                                GNUNET_TIME_UNIT_FOREVER_ABS,
285                                1,
286                                &rd,
287                                sig,
288                                NULL,
289                                NULL);
290
291   rd.data_size = strlen(TEST_ALICE_PSEU);
292   rd.data = TEST_ALICE_PSEU;
293   rd.record_type = GNUNET_GNS_RECORD_PSEU;
294   GNUNET_free(sig);
295
296   sig = GNUNET_NAMESTORE_create_signature(alice_key,GNUNET_TIME_UNIT_FOREVER_ABS,  "",
297                                           &rd, 1);
298
299   GNUNET_NAMESTORE_record_put (namestore_handle,
300                                &alice_pkey,
301                                "",
302                                GNUNET_TIME_UNIT_FOREVER_ABS,
303                                1,
304                                &rd,
305                                sig,
306                                &commence_testing,
307                                NULL);
308
309   GNUNET_free(sig);
310   GNUNET_CRYPTO_rsa_key_free (alice_key);
311   GNUNET_CRYPTO_rsa_key_free (bob_key);
312   GNUNET_CRYPTO_rsa_key_free (our_key);
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   pg = GNUNET_TESTING_daemons_start(cfg, 1, 1, 1, TIMEOUT,
337                                     NULL, NULL, &do_shorten, NULL,
338                                     NULL, NULL, NULL);
339 }
340
341 static int
342 check ()
343 {
344   int ret;
345
346   /* Arguments for GNUNET_PROGRAM_run */
347   char *const argv[] = { "test-gns-simple-get-authority", /* Name to give running binary */
348     "-c",
349     "test_gns_simple_lookup.conf",       /* Config file to use */
350 #if VERBOSE
351     "-L", "DEBUG",
352 #endif
353     NULL
354   };
355   struct GNUNET_GETOPT_CommandLineOption options[] = {
356     GNUNET_GETOPT_OPTION_END
357   };
358   /* Run the run function as a new program */
359   ret =
360       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
361                           "test-gns-simple-get-authority", "nohelp", options, &run,
362                           &ok);
363   if (ret != GNUNET_OK)
364   {
365     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
366                 "`test-gns-simple-get-authority': Failed with error code %d\n", ret);
367   }
368   return ok;
369 }
370
371 int
372 main (int argc, char *argv[])
373 {
374   int ret;
375
376   GNUNET_log_setup ("test-gns-simple-lookup",
377 #if VERBOSE
378                     "DEBUG",
379 #else
380                     "WARNING",
381 #endif
382                     NULL);
383   ret = check ();
384   /**
385    * Need to remove base directory, subdirectories taken care
386    * of by the testing framework.
387    */
388   return ret;
389 }
390
391 /* end of test_gns_twopeer.c */