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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file arm/test_gnunet_service_arm.c
20 * @brief testcase for gnunet-service-arm.c; tests ARM by making it start the resolver
22 * @author Christian Grothoff
25 #include "gnunet_arm_service.h"
26 #include "gnunet_resolver_service.h"
27 #include "gnunet_os_lib.h"
28 #include "gnunet_program_lib.h"
31 * Timeout for starting services, very short because of the strange way start works
32 * (by checking if running before starting, so really this time is always waited on
33 * startup (annoying)).
35 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50)
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
42 static int resolved_ok;
44 static int asked_for_a_list;
46 static struct GNUNET_ARM_Handle *arm;
50 trigger_disconnect (void *cls)
52 GNUNET_ARM_disconnect (arm);
58 arm_stop_cb (void *cls,
59 enum GNUNET_ARM_RequestStatus status,
60 enum GNUNET_ARM_Result result)
62 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
63 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
64 if (result != GNUNET_ARM_RESULT_STOPPED)
69 GNUNET_SCHEDULER_add_now (&trigger_disconnect, NULL);
74 service_list (void *cls,
75 enum GNUNET_ARM_RequestStatus rs,
77 const char *const*list)
81 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
82 "%u services are are currently running\n",
84 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
88 if (0 == strcasecmp (list[i],
89 "resolver (gnunet-service-resolver)"))
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
92 "Got service list, now stopping arm\n");
98 GNUNET_ARM_request_service_stop (arm,
106 hostname_resolve_cb (void *cls,
107 const struct sockaddr *addr,
110 if ((0 == ret) || (4 == ret) || (1 == resolved_ok))
114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115 "Name not resolved!\n");
118 GNUNET_ARM_request_service_stop (arm,
124 if (0 == asked_for_a_list)
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "Resolved hostname, now checking the service list\n");
128 GNUNET_ARM_request_service_list (arm,
131 asked_for_a_list = 1;
138 arm_start_cb (void *cls,
139 enum GNUNET_ARM_RequestStatus status,
140 enum GNUNET_ARM_Result result)
142 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
143 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145 "Trying to resolve our own hostname!\n");
146 /* connect to the resolver service */
148 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
150 &hostname_resolve_cb,
153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
154 "Unable initiate connection to resolver service\n");
157 GNUNET_ARM_request_service_stop (arm,
169 const struct GNUNET_CONFIGURATION_Handle *c)
171 arm = GNUNET_ARM_connect (c,
174 GNUNET_ARM_request_service_start (arm,
176 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
183 main (int argc, char *av[])
185 static char *const argv[] = {
186 "test-gnunet-service-arm",
188 "test_arm_api_data.conf",
191 static struct GNUNET_GETOPT_CommandLineOption options[] = {
192 GNUNET_GETOPT_OPTION_END
194 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
196 if (0 != gethostname (hostname, sizeof (hostname) - 1))
198 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
202 "Failed to determine my own hostname, testcase not run.\n");
205 if ( (0 == strcmp (hostname,
207 (0 == strcmp (hostname,
210 /* we cannot use 'localhost' as this would not trigger the
211 resolver service (see resolver_api.c); so in this case,
212 we fall back to (ab)using gnu.org. */
216 /* trigger DNS lookup */
222 if (0 != (ret = getaddrinfo (hostname, NULL, NULL, &ai)))
225 "Failed to resolve my hostname `%s', testcase not run.\n",
231 #elif HAVE_GETHOSTBYNAME2
233 struct hostent *host;
235 host = gethostbyname2 (hostname, AF_INET);
237 host = gethostbyname2 (hostname, AF_INET6);
241 "Failed to resolve my hostname `%s', testcase not run.\n",
246 #elif HAVE_GETHOSTBYNAME
248 struct hostent *host;
250 host = gethostbyname (hostname);
254 "Failed to resolve my hostname `%s', testcase not run.\n",
261 "libc fails to have resolver function, testcase not run.\n");
264 GNUNET_log_setup ("test-gnunet-service-arm",
267 GNUNET_break (GNUNET_OK ==
268 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
269 argv, "test-gnunet-service-arm",
275 "Test failed with error code %d\n",
281 /* end of test_gnunet_service_arm.c */