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