Added proper SRV handling and test
[oweals/gnunet.git] / src / gns / test_gns_simple_srv_lookup.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_srv_lookup.c
22  * @brief base testcase for testing GNS SRV lookups
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 "_sip._tcp.bob.gnunet"
46 #define TEST_IP "127.0.0.1"
47 #define TEST_RECORD_NAME "sipserver"
48 #define TEST_RECORD_NAME_SRV "_sip._tcp"
49 #define TEST_SRV_NAME "sipserver.+"
50 #define TEST_EXPECTED_SRV "sipserver.bob.gnunet"
51
52 #define TEST_AUTHORITY_NAME "bob"
53
54 #define KEYFILE_BOB "../namestore/zonefiles/HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"
55
56 /* Globals */
57
58 /**
59  * Directory to store temp data in, defined in config file
60  */
61 static char *test_directory;
62
63 static struct GNUNET_TESTING_PeerGroup *pg;
64
65 /* Task handle to use to schedule test failure */
66 GNUNET_SCHEDULER_TaskIdentifier die_task;
67
68 /* Global return value (0 for success, anything else for failure) */
69 static int ok;
70
71 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
72
73 static struct GNUNET_GNS_Handle *gns_handle;
74
75 const struct GNUNET_CONFIGURATION_Handle *cfg;
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(void *cls, uint32_t rd_count,
95                  const struct GNUNET_NAMESTORE_RecordData *rd)
96 {
97   int i;
98   uint16_t *srv_data;
99   char* srv;
100   
101   if (rd_count == 0)
102   {
103     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
104                 "Lookup failed, rp_filtering?\n");
105     ok = 2;
106   }
107   else
108   {
109     ok = 1;
110     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
111     for (i=0; i<rd_count; i++)
112     {
113       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
114       if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_SRV)
115       {
116         srv_data = (uint16_t*)rd[i].data;
117         srv = (char*)&srv_data[3];
118         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
119                     "Got SRV %s with p=%d,w=%d,port=%d\n",
120                     srv, srv_data, &srv_data[1], &srv_data[2]);
121         if (0 == strcmp(srv, TEST_EXPECTED_SRV))
122         {
123           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
124                       "%s correctly resolved to %s!\n", TEST_DOMAIN,
125                       TEST_EXPECTED_SRV);
126           ok = 0;
127         }
128       }
129     }
130   }
131
132   GNUNET_GNS_disconnect(gns_handle);
133   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer1!\n");
134   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
135 }
136
137
138 /**
139  * Function scheduled to be run on the successful start of services
140  * tries to look up the dns record for TEST_DOMAIN
141  */
142 static void
143 commence_testing (void *cls, int32_t success, const char *emsg)
144 {
145   GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES);
146
147   gns_handle = GNUNET_GNS_connect(cfg);
148
149   if (NULL == gns_handle)
150   {
151     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
152                 "Failed to connect to GNS!\n");
153   }
154
155   GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_TYPE_SRV,
156                     GNUNET_NO,
157                     NULL,
158                     &on_lookup_result, TEST_DOMAIN);
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 GNUNET_NETWORK_STRUCT_BEGIN
189 struct srv_data
190 {
191   uint16_t prio GNUNET_PACKED;
192   uint16_t weight GNUNET_PACKED;
193   uint16_t port GNUNET_PACKED;
194 };
195 GNUNET_NETWORK_STRUCT_END
196
197 static void
198 do_lookup(void *cls, const struct GNUNET_PeerIdentity *id,
199           const struct GNUNET_CONFIGURATION_Handle *_cfg,
200           struct GNUNET_TESTING_Daemon *d, const char *emsg)
201 {
202   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
203   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded bob_pkey;
204   struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
205   struct GNUNET_CRYPTO_RsaPrivateKey *bob_key;
206   struct GNUNET_CRYPTO_ShortHashCode bob_hash;
207   struct GNUNET_CRYPTO_RsaSignature *sig;
208   char* alice_keyfile;
209   struct srv_data *srv_data;
210   struct GNUNET_TIME_Absolute et;
211
212   cfg = _cfg;
213
214   GNUNET_SCHEDULER_cancel (die_task);
215
216   /* put records into namestore */
217   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
218   if (NULL == namestore_handle)
219   {
220     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
221     ok = -1;
222     return;
223   }
224
225   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
226                                                           "ZONEKEY",
227                                                           &alice_keyfile))
228   {
229     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
230     ok = -1;
231     return;
232   }
233
234   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
235   bob_key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE_BOB);
236
237   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
238   GNUNET_CRYPTO_rsa_key_get_public (bob_key, &bob_pkey);
239
240   struct GNUNET_NAMESTORE_RecordData rd;
241   char* ip = TEST_IP;
242   struct in_addr *sipserver = GNUNET_malloc (sizeof (struct in_addr));
243   srv_data = GNUNET_malloc (sizeof (struct srv_data) + strlen (TEST_SRV_NAME) + 1);
244   uint16_t srv_weight = 60;
245   uint16_t srv_prio = 50;
246   uint16_t srv_port = 5060;
247
248   rd.expiration_time = UINT64_MAX;
249   GNUNET_assert(1 == inet_pton (AF_INET, ip, sipserver));
250   
251   GNUNET_CRYPTO_short_hash(&bob_pkey, sizeof(bob_pkey), &bob_hash);
252
253   rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
254   rd.data = &bob_hash;
255   rd.record_type = GNUNET_GNS_RECORD_PKEY;
256   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
257
258   GNUNET_NAMESTORE_record_create (namestore_handle,
259                                   alice_key,
260                                   TEST_AUTHORITY_NAME,
261                                   &rd,
262                                   NULL,
263                                   NULL);
264
265   rd.data_size = sizeof (struct in_addr);
266   rd.data = sipserver;
267   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
268   sig = GNUNET_NAMESTORE_create_signature(bob_key,
269                                           GNUNET_TIME_UNIT_FOREVER_ABS,
270                                           TEST_RECORD_NAME,
271                                           &rd, 1);
272   et.abs_value = rd.expiration_time;
273   GNUNET_NAMESTORE_record_put (namestore_handle,
274                                &bob_pkey,
275                                TEST_RECORD_NAME,
276                                et,
277                                1,
278                                &rd,
279                                sig,
280                                NULL,
281                                NULL);
282   
283   rd.data_size = sizeof (struct srv_data)+strlen(TEST_SRV_NAME)+1;
284   srv_data->port = srv_port;
285   srv_data->prio = srv_prio;
286   srv_data->weight = srv_weight;
287   strcpy((char*)&srv_data[1], TEST_SRV_NAME);
288   rd.data = srv_data;
289   rd.record_type = GNUNET_GNS_RECORD_TYPE_SRV;
290   sig = GNUNET_NAMESTORE_create_signature(bob_key,
291                                           GNUNET_TIME_UNIT_FOREVER_ABS,
292                                           TEST_RECORD_NAME_SRV,
293                                           &rd, 1);
294   et.abs_value = rd.expiration_time;
295   GNUNET_NAMESTORE_record_put (namestore_handle,
296                                &bob_pkey,
297                                TEST_RECORD_NAME_SRV,
298                                et,
299                                1,
300                                &rd,
301                                sig,
302                                &commence_testing,
303                                NULL);
304   GNUNET_free(srv_data);
305   GNUNET_free(sipserver);
306   GNUNET_free(sig);
307   GNUNET_CRYPTO_rsa_key_free(bob_key);
308   GNUNET_CRYPTO_rsa_key_free(alice_key);
309 }
310
311 static void
312 run (void *cls, char *const *args, const char *cfgfile,
313      const struct GNUNET_CONFIGURATION_Handle *c)
314 {
315   cfg = c;
316    /* Get path from configuration file */
317   if (GNUNET_YES !=
318       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
319                                              &test_directory))
320   {
321     ok = 404;
322     return;
323   }
324
325     
326   /* Set up a task to end testing if peer start fails */
327   die_task =
328       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
329                                     "didn't start all daemons in reasonable amount of time!!!");
330   
331   /* Start alice */
332   pg = GNUNET_TESTING_daemons_start(cfg, 1, 1, 1, TIMEOUT,
333                                     NULL, NULL, &do_lookup, NULL,
334                                     NULL, NULL, NULL);
335 }
336
337 static int
338 check ()
339 {
340   int ret;
341
342   /* Arguments for GNUNET_PROGRAM_run */
343   char *const argv[] = { "test-gns-simple-mx-lookup", /* Name to give running binary */
344     "-c",
345     "test_gns_simple_lookup.conf",       /* Config file to use */
346 #if VERBOSE
347     "-L", "DEBUG",
348 #endif
349     NULL
350   };
351   struct GNUNET_GETOPT_CommandLineOption options[] = {
352     GNUNET_GETOPT_OPTION_END
353   };
354   /* Run the run function as a new program */
355   ret =
356       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
357                           "test-gns-simple-mx-lookup", "nohelp", options, &run,
358                           &ok);
359   if (ret != GNUNET_OK)
360   {
361     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
362                 "`test-gns-simple-mx-lookup': Failed with error code %d\n", ret);
363   }
364   return ok;
365 }
366
367 int
368 main (int argc, char *argv[])
369 {
370   int ret;
371
372   GNUNET_log_setup ("test-gns-simple-mx-lookup",
373 #if VERBOSE
374                     "DEBUG",
375 #else
376                     "WARNING",
377 #endif
378                     NULL);
379   ret = check ();
380   /**
381    * Need to remove base directory, subdirectories taken care
382    * of by the testing framework.
383    */
384   return ret;
385 }
386
387 /* end of test_gns_simple_mx_lookup.c */