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