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,
129 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
131 struct sockaddr_in sa;
132 struct GNUNET_TIME_Relative timeout =
133 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
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,
140 "localhost", AF_INET, timeout, &check_127, cls);
141 GNUNET_RESOLVER_hostname_get (sched,
143 (const struct sockaddr *) &sa,
144 sizeof (struct sockaddr),
145 GNUNET_YES, timeout, &check_localhost, cls);
146 GNUNET_RESOLVER_hostname_get (sched,
148 (const struct sockaddr *) &sa,
149 sizeof (struct sockaddr),
151 timeout, &check_localhost_num, cls);
152 GNUNET_RESOLVER_hostname_resolve (sched,
154 AF_UNSPEC, timeout, &check_hostname, cls);
160 int ok = 1 + 2 + 4 + 8;
162 char *const argv[] = { "test-resolver-api",
164 "test_resolver_api_data.conf",
170 struct GNUNET_GETOPT_CommandLineOption options[] = {
171 GNUNET_GETOPT_OPTION_END
173 pid = GNUNET_OS_start_process ("gnunet-service-resolver",
174 "gnunet-service-resolver",
178 "-c", "test_resolver_api_data.conf", NULL);
179 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
180 argv, "test-resolver-api", "nohelp",
182 if (0 != PLIBC_KILL (pid, SIGTERM))
184 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
187 GNUNET_OS_process_wait (pid);
189 fprintf (stderr, "Missed some resolutions: %u\n", ok);
194 main (int argc, char *argv[])
198 GNUNET_log_setup ("test-resolver-api",
210 /* end of test_resolver_api.c */