-fix no-dot-in-path issue on FreeBSD bot
[oweals/gnunet.git] / src / gns / test_gns_simple_lookup.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 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_lookup.c
22  * @brief base testcase for testing a local GNS record lookup
23  * @author Martin Schanzenbach
24  */
25 #include "platform.h"
26 #include "gnunet_testing_lib-new.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 "gnunet_dnsparser_lib.h"
32 #include "gnunet_gns_service.h"
33
34
35 /**
36  * Timeout for entire testcase 
37  */
38 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120)
39
40 /* test records to resolve */
41 #define TEST_DOMAIN "www.gads"
42 #define TEST_IP "127.0.0.1"
43 #define TEST_RECORD_NAME "www"
44
45 /**
46  * Task handle to use to schedule test failure 
47  */
48 static GNUNET_SCHEDULER_TaskIdentifier die_task;
49
50 /**
51  * Global return value (0 for success, anything else for failure) 
52  */
53 static int ok;
54
55 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
56
57 static struct GNUNET_GNS_Handle *gns_handle;
58
59 static const struct GNUNET_CONFIGURATION_Handle *cfg;
60
61 static struct GNUNET_GNS_LookupRequest *lr;
62
63 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
64
65
66 /**
67  * Check if the get_handle is being used, if so stop the request.  Either
68  * way, schedule the end_badly_cont function which actually shuts down the
69  * test.
70  */
71 static void
72 end_badly (void *cls, 
73            const struct GNUNET_SCHEDULER_TaskContext *tc)
74 {
75   if (NULL != nsqe)
76   {
77     GNUNET_NAMESTORE_cancel (nsqe);
78     nsqe = NULL;
79   }
80   if (NULL != lr)
81   {
82     GNUNET_GNS_cancel_lookup_request (lr);
83     lr = NULL;
84   }
85   if (NULL != gns_handle)
86   {
87     GNUNET_GNS_disconnect (gns_handle);
88     gns_handle = NULL;
89   }
90
91   if (NULL != namestore_handle)
92   {
93     GNUNET_NAMESTORE_disconnect (namestore_handle);
94     namestore_handle = NULL;
95   }
96   GNUNET_break (0);
97   GNUNET_SCHEDULER_shutdown ();
98   ok = 1;
99 }
100
101
102 static void 
103 end_badly_now ()
104 {
105   GNUNET_SCHEDULER_cancel (die_task);
106   die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
107 }
108
109
110 static void 
111 shutdown_task (void *cls,
112                const struct GNUNET_SCHEDULER_TaskContext *tc)
113 {
114   GNUNET_GNS_disconnect (gns_handle);
115   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
116   GNUNET_SCHEDULER_shutdown ();
117 }
118
119
120 /**
121  * Function called on result for a GNS lookup
122  *
123  * @param cls closure, unused
124  * @param rd_count number of records
125  * @param rd the records in reply
126  */
127 static void
128 on_lookup_result(void *cls, uint32_t rd_count,
129                  const struct GNUNET_NAMESTORE_RecordData *rd)
130 {
131   struct in_addr a;
132   uint32_t i;
133   char* addr;
134
135   lr = NULL;
136   if (GNUNET_SCHEDULER_NO_TASK != die_task)
137   {
138       GNUNET_SCHEDULER_cancel (die_task);
139       die_task = GNUNET_SCHEDULER_NO_TASK;
140   }
141   GNUNET_NAMESTORE_disconnect (namestore_handle);
142   namestore_handle = NULL;
143   if (rd_count == 0)
144   {
145     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146                 "Lookup failed, rp_filtering?\n");
147     ok = 2;
148   }
149   else
150   {
151     ok = 1;
152     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "name: %s\n", (char*)cls);
153     for (i=0; i<rd_count; i++)
154     {
155       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "type: %d\n", rd[i].record_type);
156       if (rd[i].record_type == GNUNET_GNS_RECORD_A)
157       {
158         memcpy (&a, rd[i].data, sizeof(a));
159         addr = inet_ntoa(a);
160         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
161         if (0 == strcmp (addr, TEST_IP))
162         {
163           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
164                       "%s correctly resolved to %s!\n",
165                       TEST_DOMAIN, addr);
166           ok = 0;
167         }
168       }
169       else
170       {
171         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172                     "No resolution!\n");
173       }
174     }
175   }
176   GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
177 }
178
179
180 /**
181  * Function scheduled to be run on the successful start of services
182  * tries to look up the dns record for TEST_DOMAIN
183  *
184  * @param cls closure
185  * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
186  *                GNUNET_NO if content was already there or not found
187  *                GNUNET_YES (or other positive value) on success
188  * @param emsg NULL on success, otherwise an error message
189  */
190 static void
191 commence_testing (void *cls,
192                   int32_t success,
193                   const char *emsg)
194 {
195   nsqe = NULL;
196   if (NULL != emsg)
197   {
198     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
199                 "Failed to store record in namestore: %s\n",
200                 emsg);
201     end_badly_now ();
202     return;
203   }
204   gns_handle = GNUNET_GNS_connect (cfg);
205   if (NULL == gns_handle)
206   {
207     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
208                 "Failed to connect to GNS!\n");
209     end_badly_now ();
210     return;
211   }
212   lr = GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_A,
213                           GNUNET_YES,
214                           NULL,
215                           &on_lookup_result, TEST_DOMAIN);
216 }
217
218
219 static void
220 do_check (void *cls,
221           const struct GNUNET_CONFIGURATION_Handle *ccfg,
222           struct GNUNET_TESTING_Peer *peer)
223 {
224   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded alice_pkey;
225   struct GNUNET_CRYPTO_RsaPrivateKey *alice_key;
226   struct GNUNET_NAMESTORE_RecordData rd;
227   char* alice_keyfile;
228   char* ip = TEST_IP;
229   struct in_addr web;
230
231   cfg = ccfg;
232   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
233
234   /* put records into namestore */
235   namestore_handle = GNUNET_NAMESTORE_connect(cfg);
236   if (NULL == namestore_handle)
237   {
238     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
239                 "Failed to connect to namestore\n");
240     end_badly_now ();
241     return;
242   }
243   if (GNUNET_OK != 
244       GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
245                                                "ZONEKEY",
246                                                &alice_keyfile))
247   {
248     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
249                 "Failed to get key from cfg\n");
250     end_badly_now ();
251     return;
252   }
253
254   alice_key = GNUNET_CRYPTO_rsa_key_create_from_file (alice_keyfile);
255   GNUNET_CRYPTO_rsa_key_get_public (alice_key, &alice_pkey);
256   GNUNET_free (alice_keyfile);
257   rd.expiration_time = UINT64_MAX;
258   GNUNET_assert (1 == inet_pton (AF_INET, ip, &web));
259   rd.data_size = sizeof(struct in_addr);
260   rd.data = &web;
261   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
262   rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
263   nsqe = GNUNET_NAMESTORE_record_create (namestore_handle,
264                                          alice_key,
265                                          TEST_RECORD_NAME,
266                                          &rd,
267                                          &commence_testing,
268                                          NULL);
269   GNUNET_CRYPTO_rsa_key_free (alice_key);
270 }
271
272
273 int
274 main (int argc, char *argv[])
275 {
276   ok = 1;
277   GNUNET_log_setup ("test-gns-simple-lookup",
278                     "WARNING",
279                     NULL);
280   GNUNET_TESTING_peer_run ("test-gns-simple-lookup", 
281                            "test_gns_simple_lookup.conf", 
282                            &do_check, NULL);
283   return ok;
284 }
285  
286 /* end of test_gns_simple_lookup.c */