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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 ( \
38 GNUNET_TIME_UNIT_MILLISECONDS, 50)
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
45 static int resolved_ok;
47 static int asked_for_a_list;
49 static struct GNUNET_ARM_Handle *arm;
51 static const char hostname[] = "www.gnu.org"; /* any domain should do */
55 trigger_disconnect (void *cls)
57 GNUNET_ARM_disconnect (arm);
63 arm_stop_cb (void *cls,
64 enum GNUNET_ARM_RequestStatus status,
65 enum GNUNET_ARM_Result result)
67 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
68 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
69 if (result != GNUNET_ARM_RESULT_STOPPED)
74 GNUNET_SCHEDULER_add_now (&trigger_disconnect, NULL);
79 service_list (void *cls,
80 enum GNUNET_ARM_RequestStatus rs,
82 const struct GNUNET_ARM_ServiceInfo *list)
86 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
87 "%u services are are currently running\n",
89 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
91 for (i = 0; i < count; i++)
93 if ((0 == strcasecmp (list[i].name, "resolver")) &&
94 (0 == strcasecmp (list[i].binary, "gnunet-service-resolver")))
96 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97 "Got service list, now stopping arm\n");
103 GNUNET_ARM_request_service_stop (arm,
111 hostname_resolve_cb (void *cls,
112 const struct sockaddr *addr,
115 if ((0 == ret) || (4 == ret) || (1 == resolved_ok))
119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
120 "Failed to resolve hostname!\n");
123 GNUNET_ARM_request_service_stop (arm,
129 if (0 == asked_for_a_list)
131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132 "Resolved hostname, now checking the service list\n");
133 GNUNET_ARM_request_service_list (arm,
136 asked_for_a_list = 1;
143 arm_start_cb (void *cls,
144 enum GNUNET_ARM_RequestStatus status,
145 enum GNUNET_ARM_Result result)
147 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
148 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150 "Trying to resolve a hostname via the resolver service!\n");
151 /* connect to the resolver service */
153 GNUNET_RESOLVER_ip_get (hostname,
156 &hostname_resolve_cb,
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160 "Unable initiate connection to resolver service\n");
163 GNUNET_ARM_request_service_stop (arm,
175 const struct GNUNET_CONFIGURATION_Handle *c)
177 arm = GNUNET_ARM_connect (c,
180 GNUNET_ARM_request_service_start (arm,
182 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
189 main (int argc, char *av[])
191 static char *const argv[] = {
192 "test-gnunet-service-arm",
194 "test_arm_api_data.conf",
197 static struct GNUNET_GETOPT_CommandLineOption options[] = {
198 GNUNET_GETOPT_OPTION_END
201 /* trigger DNS lookup */
207 if (0 != (ret = getaddrinfo (hostname, NULL, NULL, &ai)))
210 "Failed to resolve `%s', testcase not run.\n",
216 #elif HAVE_GETHOSTBYNAME2
218 struct hostent *host;
220 host = gethostbyname2 (hostname, AF_INET);
222 host = gethostbyname2 (hostname, AF_INET6);
226 "Failed to resolve `%s', testcase not run.\n",
231 #elif HAVE_GETHOSTBYNAME
233 struct hostent *host;
235 host = gethostbyname (hostname);
239 "Failed to resolve `%s', testcase not run.\n",
246 "libc fails to have resolver function, testcase not run.\n");
249 GNUNET_log_setup ("test-gnunet-service-arm",
252 GNUNET_break (GNUNET_OK ==
253 GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1,
254 argv, "test-gnunet-service-arm",
260 "Test failed with error code %d\n",
267 /* end of test_gnunet_service_arm.c */