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