splitting 'struct GNUNET_CRYPTO_EccPublicKey' into one struct for signing and another...
[oweals/gnunet.git] / src / gns / test_gns_ns_lookup.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012, 2013 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
22 /**
23  * @file gns/test_gns_ns_lookup.c
24  * @brief base testcase for testing a local GNS record lookup through NS
25  * @author Martin Schanzenbach
26  */
27 #include "platform.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_core_service.h"
30 #include "block_dns.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_namestore_service.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_dnsparser_lib.h"
35 #include "gnunet_gns_service.h"
36
37 /**
38  * Timeout for entire testcase 
39  */
40 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20)
41
42 /**
43  * Name to resolve for testing.  NS record on 'homepage.gnu' redirects to
44  * DNS 'TEST_RECORD_NS' domain and thus names should be resolved within
45  * that target domain.
46  */
47 #define TEST_DOMAIN "www.homepage.gnu"
48
49 /**
50  * Name to resolve for testing.  NS record on 'homepage.gnu' redirects to
51  * DNS 'TEST_RECORD_NS' domain and thus names should be resolved within
52  * that target domain.
53  */
54 #define TEST_DOMAIN_ALT "homepage.gnu"
55
56 /**
57  * Name to resolve for testing.  NS record on 'homepage.gnu' redirects to
58  * DNS 'TEST_RECORD_NS' domain and thus names should be resolved within
59  * that target domain.
60  */
61 #define TEST_DOMAIN_ALT2 "uk.homepage.gnu"
62
63 /**
64  * Expected test value (matching TEST_DOMAIN_ALT2).
65  * Currently 'uk.gnunet.org' / 'stat.wensley.org.uk'.
66  */
67 #define TEST_IP_ALT2 "81.187.252.184"
68
69 /**
70  * Must be the IP address for TEST_RECORD_NS in DNS and TEST_DOMAIN in GADS;
71  * used to check that DNS is working as expected.  We use the IPv4
72  * address of gnunet.org.
73  */
74 #define TEST_IP "131.159.74.67"
75
76 /**
77  * DNS domain name used for testing.
78  */
79 #define TEST_RECORD_NS "gnunet.org"
80
81 /**
82  * Nameserver for 'TEST_RECORD_NS', currently 'a.ns.joker.com'.
83  */
84 #define TEST_IP_NS "184.172.157.218" 
85
86 /**
87  * Name we use within our GADS zone.
88  */
89 #define TEST_RECORD_NAME "homepage"
90
91 /**
92  * Task handle to use to schedule test failure 
93  */
94 static GNUNET_SCHEDULER_TaskIdentifier die_task;
95
96 /**
97  * Global return value (0 for success, anything else for failure) 
98  */
99 static int ok;
100
101 /**
102  * Flag we set if the DNS resolver seems to be working.
103  */
104 static int resolver_working;
105
106 /**
107  * Handle to namestore.
108  */
109 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
110
111 /**
112  * Handle to GNS resolver.
113  */
114 static struct GNUNET_GNS_Handle *gns_handle;
115
116 /**
117  * Handle for DNS request.
118  */
119 static struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
120
121 /**
122  * Our configuration.
123  */
124 static const struct GNUNET_CONFIGURATION_Handle *cfg;
125
126 /**
127  * Handle for active GNS lookup.
128  */
129 static struct GNUNET_GNS_LookupRequest *lr;
130
131 /**
132  * Queue for storing records in namestore.
133  */
134 static struct GNUNET_NAMESTORE_QueueEntry *qe;
135
136 /**
137  * Our private key for signing records.
138  */
139 static struct GNUNET_CRYPTO_EccPrivateKey *alice_key;
140
141
142 /**
143  * Check if the get_handle is being used, if so stop the request.  Either
144  * way, schedule the end_badly_cont function which actually shuts down the
145  * test.
146  */
147 static void
148 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
149 {
150   die_task = GNUNET_SCHEDULER_NO_TASK;
151   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test failed, shutting down...\n");
152   if (NULL != lr)
153   {
154     GNUNET_GNS_cancel_lookup_request (lr);
155     lr = NULL;
156   }
157   if (NULL != resolver_handle)
158   {
159     GNUNET_RESOLVER_request_cancel (resolver_handle);
160     resolver_handle = NULL;
161   }
162   if (NULL != qe)
163   {
164     GNUNET_NAMESTORE_cancel (qe);
165     qe = NULL;
166   }
167   if (NULL != gns_handle)
168   {
169     GNUNET_GNS_disconnect(gns_handle);
170     gns_handle = NULL;
171   }
172   if (NULL != namestore_handle)
173   {
174     GNUNET_NAMESTORE_disconnect (namestore_handle);
175     namestore_handle = NULL;
176   }
177   if (NULL != alice_key)
178   {
179     GNUNET_free (alice_key);
180     alice_key = NULL;
181   }
182   GNUNET_break (0);
183   GNUNET_SCHEDULER_shutdown ();
184   ok = 1;
185 }
186
187
188 /**
189  * We hit a hard failure, shutdown now.
190  */
191 static void
192 end_badly_now ()
193 {
194   GNUNET_SCHEDULER_cancel (die_task);
195   die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
196 }
197
198
199 /**
200  * Testcase is finished, terminate everything.
201  */
202 static void
203 end_now (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
204 {
205   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 
206              "Test successful, shutting down...\n");
207   if (GNUNET_SCHEDULER_NO_TASK != die_task)
208   {
209       GNUNET_SCHEDULER_cancel (die_task);
210       die_task = GNUNET_SCHEDULER_NO_TASK;
211   }
212   if (NULL != qe)
213   {
214     GNUNET_NAMESTORE_cancel (qe);
215     qe = NULL;
216   }
217   if (NULL != resolver_handle)
218   {
219     GNUNET_RESOLVER_request_cancel (resolver_handle);
220     resolver_handle = NULL;
221   }
222   if (NULL != gns_handle)
223   {
224     GNUNET_GNS_disconnect(gns_handle);
225     gns_handle = NULL;
226   }
227   if (NULL != namestore_handle)
228   {
229     GNUNET_NAMESTORE_disconnect (namestore_handle);
230     namestore_handle = NULL;
231   }
232   if (NULL != alice_key)
233   {
234     GNUNET_free (alice_key);
235     alice_key = NULL;
236   }
237   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down peer!\n");
238   GNUNET_SCHEDULER_shutdown ();
239 }
240
241
242 /**
243  * We got resolution result for 'TEST_DOMAIN_ALT2', check if
244  * they match our expectations, then finish the test with success.
245  *
246  * @param cls unused
247  * @param rd_count number of records in rd
248  * @param rd records returned from naming system for the name
249  */
250 static void
251 on_lookup_result_alt2 (void *cls, uint32_t rd_count,
252                        const struct GNUNET_NAMESTORE_RecordData *rd)
253 {
254   struct in_addr a;
255   uint32_t i;
256   char* addr;
257
258   lr = NULL;
259   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received alternative results 2\n");
260   if (0 == rd_count)
261   {
262     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
263                 "Lookup for `%s' failed\n",
264                 TEST_DOMAIN_ALT2);
265     ok = 2;
266     GNUNET_SCHEDULER_add_now (&end_now, NULL);
267     return;
268   }
269   ok = 1;
270   for (i=0; i<rd_count; i++)
271   {
272     if (rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
273     {
274       memcpy(&a, rd[i].data, sizeof(a));
275       addr = inet_ntoa(a);
276       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "address: %s\n", addr);
277       if (0 == strcmp(addr, TEST_IP_ALT2))
278       {
279         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
280                     "%s correctly resolved to %s!\n", 
281                     TEST_DOMAIN_ALT2, addr);
282         ok = 0;
283       }
284       else
285         GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
286                     "Got unexpected address %s for %s\n",
287                     addr,
288                     TEST_DOMAIN);
289     }
290   }
291   if (1 == ok)
292   {
293     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
294                 "None of the results matched the expected value %s for %s\n",
295                 TEST_IP,
296                 TEST_DOMAIN);
297     GNUNET_SCHEDULER_add_now (&end_now, NULL);
298     return;
299   }
300
301   GNUNET_SCHEDULER_add_now (&end_now, NULL);
302 }
303
304
305 /**
306  * We got resolution result for 'TEST_DOMAIN_ALT', check if
307  * they match our expectations, then move on to the next
308  * resolution.
309  *
310  * @param cls unused
311  * @param rd_count number of records in rd
312  * @param rd records returned from naming system for the name
313  */
314 static void
315 on_lookup_result_alt (void *cls, uint32_t rd_count,
316                       const struct GNUNET_NAMESTORE_RecordData *rd)
317 {
318   struct in_addr a;
319   uint32_t i;
320   char* addr;
321
322   lr = NULL;
323   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received alternative results\n");
324   if (0 == rd_count)
325   {
326     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
327                 "Lookup for `%s' failed\n",
328                 TEST_DOMAIN_ALT);
329     ok = 2;
330     GNUNET_SCHEDULER_add_now (&end_now, NULL);
331     return;
332   }
333   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
334               "Received %u results for %s\n",
335               (unsigned int) rd_count,
336               TEST_DOMAIN_ALT);
337   ok = 1;
338   for (i=0; i<rd_count; i++)
339   {
340     if (rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
341     {
342       memcpy (&a, rd[i].data, sizeof(a));
343       addr = inet_ntoa (a);
344       if (0 == strcmp(addr, TEST_IP))
345       {
346         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
347                     "%s correctly resolved to %s!\n", TEST_DOMAIN, addr);
348         ok = 0;
349       }
350       else
351         GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
352                     "Got unexpected address %s for %s\n",
353                     addr,
354                     TEST_DOMAIN_ALT);
355     }
356   }
357   if (1 == ok)
358   {
359     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
360                 "None of the results matched the expected value %s for %s\n",
361                 TEST_IP,
362                 TEST_DOMAIN_ALT);
363     GNUNET_SCHEDULER_add_now (&end_now, NULL);
364     return;
365   }
366
367   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
368               "Starting lookup for `%s'\n",
369               TEST_DOMAIN_ALT2);
370   lr = GNUNET_GNS_lookup (gns_handle, 
371                           TEST_DOMAIN_ALT2, GNUNET_DNSPARSER_TYPE_A,
372                           GNUNET_YES,
373                           NULL,
374                           &on_lookup_result_alt2, NULL);
375 }
376
377
378 /**
379  * We got resolution result for 'TEST_DOMAIN', check if
380  * they match our expectations, then move on to the next
381  * resolution.
382  *
383  * @param cls unused
384  * @param rd_count number of records in rd
385  * @param rd records returned from naming system for the name
386  */
387 static void
388 on_lookup_result (void *cls, uint32_t rd_count,
389                   const struct GNUNET_NAMESTORE_RecordData *rd)
390 {
391   struct in_addr a;
392   uint32_t i;
393   char* addr;
394
395   lr = NULL;
396   if (0 == rd_count)
397   {
398     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
399                 "Lookup for `%s' failed\n",
400                 TEST_DOMAIN);
401     ok = 2;
402     GNUNET_SCHEDULER_add_now (&end_now, NULL);
403     return;
404   }
405   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
406               "Received %u results for %s\n",
407               (unsigned int) rd_count,
408               TEST_DOMAIN);
409   ok = 1;
410   for (i=0; i<rd_count; i++)
411   {
412     if (rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
413     {
414       memcpy (&a, rd[i].data, sizeof(a));
415       addr = inet_ntoa(a);
416       if (0 == strcmp (addr, TEST_IP))
417       {
418         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
419                     "%s correctly resolved to %s!\n", 
420                     TEST_DOMAIN, addr);
421         ok = 0;
422       }
423       else
424         GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
425                     "Got unexpected address %s for %s\n",
426                     addr,
427                     TEST_DOMAIN);
428     }
429   }
430   if (1 == ok)
431   {
432     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
433                 "None of the results matched the expected value %s for %s\n",
434                 TEST_IP,
435                 TEST_DOMAIN);
436     GNUNET_SCHEDULER_add_now (&end_now, NULL);
437     return;
438   }
439   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
440               "Starting lookup for `%s'\n",
441               TEST_DOMAIN_ALT);
442
443   lr = GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN_ALT, GNUNET_DNSPARSER_TYPE_A,
444                           GNUNET_YES,
445                           NULL,
446                           &on_lookup_result_alt, NULL);
447 }
448
449
450 /**
451  * Start the actual NS-based lookup.
452  */
453 static void
454 start_lookup ()
455 {
456   gns_handle = GNUNET_GNS_connect (cfg);
457   if (NULL == gns_handle)
458   {
459     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
460                 "Failed to connect to GNS!\n");
461     end_badly_now ();
462     return;
463   }
464   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
465               "Records ready, starting lookup for `%s'\n",
466               TEST_DOMAIN);
467   lr = GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN, GNUNET_DNSPARSER_TYPE_A,
468                           GNUNET_YES,
469                           NULL,
470                           &on_lookup_result, NULL);
471 }
472
473
474 /**
475  * Function called with the result of resolving the "NS" record
476  * for TEST_RECORD_NS.  Check if the NS record is set as expected,
477  * and if so, continue with the test.
478  *
479  * @param cls closure, unused
480  * @param addr NULL for last address,
481  * @param addrlen number of bytes in addr
482  */
483 static void
484 handle_dns_test (void *cls,
485                  const struct sockaddr *addr,
486                  socklen_t addrlen)
487 {
488   struct sockaddr_in* sai;
489
490   resolver_handle = NULL;
491   if (NULL == addr)
492   {
493     /* end of results */
494     if (GNUNET_YES != resolver_working)
495     {
496       ok = 0;
497       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
498                   "System resolver not working as expected. Test inconclusive!\n");
499       GNUNET_SCHEDULER_add_now (&end_now, NULL);
500       return;
501     }
502     /* done preparing records, start lookup */
503     GNUNET_NAMESTORE_disconnect (namestore_handle);
504     namestore_handle = NULL;
505     start_lookup ();
506     return;
507   }
508   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
509               "Received DNS response\n");
510   if (addrlen == sizeof (struct sockaddr_in))
511   {
512     sai = (struct sockaddr_in*) addr;
513     if (0 == strcmp (TEST_IP, inet_ntoa (sai->sin_addr)))
514     {
515       resolver_working = GNUNET_YES;
516       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517                   "Resolver is working (returned expected A record %s for %s)\n",
518                   TEST_IP,
519                   TEST_RECORD_NS);
520     }
521   }
522 }
523
524
525 /**
526  * Function scheduled to be run on the successful start of services
527  * tries to look up the dns record for TEST_DOMAIN
528  *
529  * @param cls closure, unused
530  * @param success GNUNET_OK on success
531  * @param emsg error message, NULL on success
532  */
533 static void
534 commence_testing (void *cls, int32_t success, const char *emsg)
535 {
536   qe = NULL;
537   if (NULL != emsg)
538     FPRINTF (stderr, "Failed to create record: %s\n", emsg);
539   GNUNET_assert (GNUNET_YES == success);
540   resolver_working = GNUNET_NO;
541   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
542               "Resolving NS record for %s\n",
543               TEST_RECORD_NS);
544   GNUNET_RESOLVER_connect (cfg);
545   resolver_handle = GNUNET_RESOLVER_ip_get (TEST_RECORD_NS,
546                                             AF_INET,
547                                             TIMEOUT,
548                                             &handle_dns_test,
549                                             NULL);
550 }
551
552
553 /**
554  * Peer is ready, run the actual test.  Begins by storing
555  * a record in the namestore.
556  *
557  * @param cls closure, NULL
558  * @param ccfg our configuration
559  * @param peer handle to the peer
560  */
561 static void
562 do_check (void *cls,
563           const struct GNUNET_CONFIGURATION_Handle *ccfg,
564           struct GNUNET_TESTING_Peer *peer)
565 {
566   struct GNUNET_CRYPTO_EccPublicSignKey alice_pkey;
567   char* alice_keyfile;
568   struct GNUNET_NAMESTORE_RecordData rd[2];
569   struct in_addr ns;
570   
571   cfg = ccfg;
572   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
573
574   /* put records into namestore */
575   namestore_handle = GNUNET_NAMESTORE_connect (cfg);
576   if (NULL == namestore_handle)
577   {
578     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
579                 "Failed to connect to namestore\n");
580     end_badly_now ();
581     return;
582   }
583
584   if (GNUNET_OK !=
585       GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
586                                                "ZONEKEY",
587                                                &alice_keyfile))
588   {
589     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
590                 "Failed to get key from cfg\n");
591     end_badly_now ();
592     return;
593   }
594
595   alice_key = GNUNET_CRYPTO_ecc_key_create_from_file (alice_keyfile);
596   GNUNET_CRYPTO_ecc_key_get_public_for_signature (alice_key, &alice_pkey);
597   GNUNET_free (alice_keyfile);
598
599   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
600               "Creating NS records\n");
601   rd[0].expiration_time = UINT64_MAX;
602   GNUNET_assert(1 == inet_pton (AF_INET, TEST_IP_NS, &ns));
603   rd[0].data_size = sizeof (struct in_addr);
604   rd[0].data = &ns;
605   rd[0].record_type = GNUNET_DNSPARSER_TYPE_A;
606   rd[0].flags = GNUNET_NAMESTORE_RF_NONE;
607   
608   rd[1].expiration_time = UINT64_MAX;
609   rd[1].data_size = strlen (TEST_RECORD_NS);
610   rd[1].data = TEST_RECORD_NS;
611   rd[1].record_type = GNUNET_DNSPARSER_TYPE_NS;
612   rd[1].flags = GNUNET_NAMESTORE_RF_NONE;
613
614   qe = GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
615                                                  alice_key,
616                                                  TEST_RECORD_NAME,
617                                                  2, rd,
618                                                  &commence_testing,
619                                                  NULL);
620 }
621
622
623 int
624 main (int argc, char *argv[])
625 {
626   ok = 1;
627   GNUNET_TESTING_peer_run ("test-gns-simple-ns-lookup", "test_gns_simple_lookup.conf",
628                            &do_check, NULL);
629   return ok;
630 }
631
632 /* end of test_gns_ns_lookup.c */