2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file resolver/test_resolver_api.c
22 * @brief testcase for resolver_api.c
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"
33 #define VERBOSE GNUNET_NO
37 check_hostname (void *cls, const struct sockaddr *sa, socklen_t salen)
46 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
47 _("Got IP address `%s' for our host.\n"),
48 GNUNET_a2s (sa, salen));
53 check_localhost_num (void *cls, const char *hostname)
58 if (0 == strcmp (hostname, "127.0.0.1"))
61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62 "Received correct hostname `%s'.\n", hostname);
69 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
70 "Received invalid hostname `%s'.\n", hostname);
77 check_localhost (void *cls, const char *hostname)
82 if (0 == strcmp (hostname, "localhost"))
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86 "Received correct hostname `%s'.\n", hostname);
93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94 "Received invalid hostname `%s'.\n", hostname);
101 check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
104 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
108 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
109 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n");
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n");
127 struct GNUNET_SCHEDULER_Handle *sched,
130 const struct GNUNET_CONFIGURATION_Handle *cfg)
132 struct sockaddr_in sa;
133 struct GNUNET_TIME_Relative timeout =
134 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
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,
141 "localhost", AF_INET, timeout, &check_127, cls);
142 GNUNET_RESOLVER_hostname_get (sched,
144 (const struct sockaddr *) &sa,
145 sizeof (struct sockaddr),
146 GNUNET_YES, timeout, &check_localhost, cls);
147 GNUNET_RESOLVER_hostname_get (sched,
149 (const struct sockaddr *) &sa,
150 sizeof (struct sockaddr),
152 timeout, &check_localhost_num, cls);
153 GNUNET_RESOLVER_hostname_resolve (sched,
155 AF_UNSPEC, timeout, &check_hostname, cls);
161 int ok = 1 + 2 + 4 + 8;
163 char *const argv[] = { "test-resolver-api",
165 "test_resolver_api_data.conf",
171 struct GNUNET_GETOPT_CommandLineOption options[] = {
172 GNUNET_GETOPT_OPTION_END
174 pid = GNUNET_OS_start_process ("gnunet-service-resolver",
175 "gnunet-service-resolver",
179 "-c", "test_resolver_api_data.conf", NULL);
181 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
182 argv, "test-resolver-api", "nohelp",
184 if (0 != PLIBC_KILL (pid, SIGTERM))
186 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
189 GNUNET_OS_process_wait(pid);
191 fprintf (stderr, "Missed some resolutions: %u\n", ok);
196 main (int argc, char *argv[])
200 GNUNET_log_setup ("test-resolver-api",
212 /* end of test_resolver_api.c */