debug code
[oweals/gnunet.git] / src / resolver / test_resolver_api.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 2, 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 resolver/test_resolver_api.c
22  * @brief testcase for resolver_api.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_os_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_resolver_service.h"
31 #include "resolver.h"
32
33 #define VERBOSE GNUNET_NO
34
35
36 static void
37 check_hostname (void *cls, const struct sockaddr *sa, socklen_t salen)
38 {
39   char buf[INET6_ADDRSTRLEN];
40   int *ok = cls;
41
42   if (salen == 0)
43     {
44       (*ok) &= ~8;
45       return;
46     }
47   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
48               _("Got IP address `%s' for our host.\n"),
49               GNUNET_a2s (sa, salen));
50 }
51
52
53 static void
54 check_localhost_num (void *cls, const char *hostname)
55 {
56   int *ok = cls;
57   if (hostname == NULL)
58     return;
59   if (0 == strcmp (hostname, "127.0.0.1"))
60     {
61 #if DEBUG_RESOLVER
62       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
63                   "Received correct hostname `%s'.\n", hostname);
64 #endif
65       (*ok) &= ~4;
66     }
67   else
68     {
69 #if DEBUG_RESOLVER
70       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
71                   "Received invalid hostname `%s'.\n", hostname);
72 #endif
73       GNUNET_break (0);
74     }
75 }
76
77 static void
78 check_localhost (void *cls, const char *hostname)
79 {
80   int *ok = cls;
81   if (hostname == NULL)
82     return;
83   if (0 == strcmp (hostname, "localhost"))
84     {
85 #if DEBUG_RESOLVER
86       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
87                   "Received correct hostname `%s'.\n", hostname);
88 #endif
89       (*ok) &= ~2;
90     }
91   else
92     {
93 #if DEBUG_RESOLVER
94       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95                   "Received invalid hostname `%s'.\n", hostname);
96 #endif
97       GNUNET_break (0);
98     }
99 }
100
101 static void
102 check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
103 {
104   int *ok = cls;
105   const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
106
107   if (sa == NULL)
108     return;
109   GNUNET_assert (sizeof (struct sockaddr_in) == salen);
110   if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
111     {
112 #if DEBUG_RESOLVER
113       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n");
114 #endif
115       (*ok) &= ~1;
116     }
117   else
118     {
119 #if DEBUG_RESOLVER
120       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n");
121 #endif
122       GNUNET_break (0);
123     }
124 }
125
126 static void
127 run (void *cls,
128      struct GNUNET_SCHEDULER_Handle *sched,
129      char *const *args,
130      const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
131 {
132   struct sockaddr_in sa;
133   struct GNUNET_TIME_Relative timeout =
134     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
135                                    2500);
136   memset (&sa, 0, sizeof (sa));
137   sa.sin_family = AF_INET;
138   sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
139   GNUNET_RESOLVER_ip_get (sched,
140                           cfg,
141                           "localhost", AF_INET, timeout, &check_127, cls);
142   GNUNET_RESOLVER_hostname_get (sched,
143                                 cfg,
144                                 (const struct sockaddr *) &sa,
145                                 sizeof (struct sockaddr),
146                                 GNUNET_YES, timeout, &check_localhost, cls);
147   GNUNET_RESOLVER_hostname_get (sched,
148                                 cfg,
149                                 (const struct sockaddr *) &sa,
150                                 sizeof (struct sockaddr),
151                                 GNUNET_NO,
152                                 timeout, &check_localhost_num, cls);
153   GNUNET_RESOLVER_hostname_resolve (sched,
154                                     cfg,
155                                     AF_UNSPEC, timeout, &check_hostname, cls);
156 }
157
158 static int
159 check ()
160 {
161   int ok = 1 + 2 + 4 + 8;
162   pid_t pid;
163   char *const argv[] = { "test-resolver-api",
164     "-c",
165     "test_resolver_api_data.conf",
166 #if VERBOSE
167     "-L", "DEBUG",
168 #endif
169     NULL
170   };
171   struct GNUNET_GETOPT_CommandLineOption options[] = {
172     GNUNET_GETOPT_OPTION_END
173   };
174   pid = GNUNET_OS_start_process ("gnunet-service-resolver",
175                                  "gnunet-service-resolver",
176 #if VERBOSE
177                                  "-L", "DEBUG",
178 #endif
179                                  "-c", "test_resolver_api_data.conf", NULL);
180   sleep (1);
181   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
182                       argv, "test-resolver-api", "nohelp",
183                       options, &run, &ok);
184   if (0 != PLIBC_KILL (pid, SIGTERM))
185     {
186       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
187       ok = 1;
188     }
189   waitpid (pid, NULL, 0);
190   if (ok != 0)
191     fprintf (stderr, "Missed some resolutions: %u\n", ok);
192   return ok;
193 }
194
195 int
196 main (int argc, char *argv[])
197 {
198   int ret;
199
200   GNUNET_log_setup ("test-resolver-api",
201 #if VERBOSE
202                     "DEBUG",
203 #else
204                     "WARNING",
205 #endif
206                     NULL);
207   ret = check ();
208
209   return ret;
210 }
211
212 /* end of test_resolver_api.c */