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 (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;
50 static const char hostname[] = "www.gnu.org"; /* any domain should do */
54 trigger_disconnect (void *cls)
56 GNUNET_ARM_disconnect (arm);
62 arm_stop_cb (void *cls,
63 enum GNUNET_ARM_RequestStatus status,
64 enum GNUNET_ARM_Result result)
66 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
67 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
68 if (result != GNUNET_ARM_RESULT_STOPPED)
73 GNUNET_SCHEDULER_add_now (&trigger_disconnect, NULL);
78 service_list (void *cls,
79 enum GNUNET_ARM_RequestStatus rs,
81 const char *const*list)
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86 "%u services are are currently running\n",
88 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
92 if (0 == strcasecmp (list[i],
93 "resolver (gnunet-service-resolver)"))
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96 "Got service list, now stopping arm\n");
102 GNUNET_ARM_request_service_stop (arm,
110 hostname_resolve_cb (void *cls,
111 const struct sockaddr *addr,
114 if ((0 == ret) || (4 == ret) || (1 == resolved_ok))
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119 "Failed to resolve hostname!\n");
122 GNUNET_ARM_request_service_stop (arm,
128 if (0 == asked_for_a_list)
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131 "Resolved hostname, now checking the service list\n");
132 GNUNET_ARM_request_service_list (arm,
135 asked_for_a_list = 1;
142 arm_start_cb (void *cls,
143 enum GNUNET_ARM_RequestStatus status,
144 enum GNUNET_ARM_Result result)
146 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
147 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Trying to resolve a hostname via the resolver service!\n");
150 /* connect to the resolver service */
152 GNUNET_RESOLVER_ip_get (hostname,
155 &hostname_resolve_cb,
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159 "Unable initiate connection to resolver service\n");
162 GNUNET_ARM_request_service_stop (arm,
174 const struct GNUNET_CONFIGURATION_Handle *c)
176 arm = GNUNET_ARM_connect (c,
179 GNUNET_ARM_request_service_start (arm,
181 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
188 main (int argc, char *av[])
190 static char *const argv[] = {
191 "test-gnunet-service-arm",
193 "test_arm_api_data.conf",
196 static struct GNUNET_GETOPT_CommandLineOption options[] = {
197 GNUNET_GETOPT_OPTION_END
200 /* trigger DNS lookup */
206 if (0 != (ret = getaddrinfo (hostname, NULL, NULL, &ai)))
209 "Failed to resolve `%s', testcase not run.\n",
215 #elif HAVE_GETHOSTBYNAME2
217 struct hostent *host;
219 host = gethostbyname2 (hostname, AF_INET);
221 host = gethostbyname2 (hostname, AF_INET6);
225 "Failed to resolve `%s', testcase not run.\n",
230 #elif HAVE_GETHOSTBYNAME
232 struct hostent *host;
234 host = gethostbyname (hostname);
238 "Failed to resolve `%s', testcase not run.\n",
245 "libc fails to have resolver function, testcase not run.\n");
248 GNUNET_log_setup ("test-gnunet-service-arm",
251 GNUNET_break (GNUNET_OK ==
252 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
253 argv, "test-gnunet-service-arm",
259 "Test failed with error code %d\n",
265 /* end of test_gnunet_service_arm.c */