2 This file is part of GNUnet.
3 Copyright (C) 2009, 2014 GNUnet e.V.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file arm/test_gnunet_service_arm.c
22 * @brief testcase for gnunet-service-arm.c; tests ARM by making it start the resolver
24 * @author Christian Grothoff
27 #include "gnunet_arm_service.h"
28 #include "gnunet_resolver_service.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet_program_lib.h"
33 * Timeout for starting services, very short because of the strange way start works
34 * (by checking if running before starting, so really this time is always waited on
35 * startup (annoying)).
37 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50)
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
44 static int resolved_ok;
46 static int asked_for_a_list;
48 static struct GNUNET_ARM_Handle *arm;
52 trigger_disconnect (void *cls)
54 GNUNET_ARM_disconnect (arm);
60 arm_stop_cb (void *cls,
61 enum GNUNET_ARM_RequestStatus status,
62 enum GNUNET_ARM_Result result)
64 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
65 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
66 if (result != GNUNET_ARM_RESULT_STOPPED)
71 GNUNET_SCHEDULER_add_now (&trigger_disconnect, NULL);
76 service_list (void *cls,
77 enum GNUNET_ARM_RequestStatus rs,
79 const char *const*list)
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84 "%u services are are currently running\n",
86 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
90 if (0 == strcasecmp (list[i],
91 "resolver (gnunet-service-resolver)"))
93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94 "Got service list, now stopping arm\n");
100 GNUNET_ARM_request_service_stop (arm,
108 hostname_resolve_cb (void *cls,
109 const struct sockaddr *addr,
112 if ((0 == ret) || (4 == ret) || (1 == resolved_ok))
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
117 "Name not resolved!\n");
120 GNUNET_ARM_request_service_stop (arm,
126 if (0 == asked_for_a_list)
128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129 "Resolved hostname, now checking the service list\n");
130 GNUNET_ARM_request_service_list (arm,
133 asked_for_a_list = 1;
140 arm_start_cb (void *cls,
141 enum GNUNET_ARM_RequestStatus status,
142 enum GNUNET_ARM_Result result)
144 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
145 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147 "Trying to resolve our own hostname!\n");
148 /* connect to the resolver service */
150 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
152 &hostname_resolve_cb,
155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
156 "Unable initiate connection to resolver service\n");
159 GNUNET_ARM_request_service_stop (arm,
171 const struct GNUNET_CONFIGURATION_Handle *c)
173 arm = GNUNET_ARM_connect (c,
176 GNUNET_ARM_request_service_start (arm,
178 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
185 main (int argc, char *av[])
187 static char *const argv[] = {
188 "test-gnunet-service-arm",
190 "test_arm_api_data.conf",
193 static struct GNUNET_GETOPT_CommandLineOption options[] = {
194 GNUNET_GETOPT_OPTION_END
196 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
198 if (0 != gethostname (hostname, sizeof (hostname) - 1))
200 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
204 "Failed to determine my own hostname, testcase not run.\n");
207 if ( (0 == strcmp (hostname,
209 (0 == strcmp (hostname,
212 /* we cannot use 'localhost' as this would not trigger the
213 resolver service (see resolver_api.c); so in this case,
214 we fall back to (ab)using gnu.org. */
218 /* trigger DNS lookup */
224 if (0 != (ret = getaddrinfo (hostname, NULL, NULL, &ai)))
227 "Failed to resolve my hostname `%s', testcase not run.\n",
233 #elif HAVE_GETHOSTBYNAME2
235 struct hostent *host;
237 host = gethostbyname2 (hostname, AF_INET);
239 host = gethostbyname2 (hostname, AF_INET6);
243 "Failed to resolve my hostname `%s', testcase not run.\n",
248 #elif HAVE_GETHOSTBYNAME
250 struct hostent *host;
252 host = gethostbyname (hostname);
256 "Failed to resolve my hostname `%s', testcase not run.\n",
263 "libc fails to have resolver function, testcase not run.\n");
266 GNUNET_log_setup ("test-gnunet-service-arm",
269 GNUNET_break (GNUNET_OK ==
270 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
271 argv, "test-gnunet-service-arm",
277 "Test failed with error code %d\n",
283 /* end of test_gnunet_service_arm.c */