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 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.
16 * @file arm/test_gnunet_service_arm.c
17 * @brief testcase for gnunet-service-arm.c; tests ARM by making it start the resolver
19 * @author Christian Grothoff
22 #include "gnunet_arm_service.h"
23 #include "gnunet_resolver_service.h"
24 #include "gnunet_os_lib.h"
25 #include "gnunet_program_lib.h"
28 * Timeout for starting services, very short because of the strange way start works
29 * (by checking if running before starting, so really this time is always waited on
30 * startup (annoying)).
32 #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50)
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
39 static int resolved_ok;
41 static int asked_for_a_list;
43 static struct GNUNET_ARM_Handle *arm;
47 trigger_disconnect (void *cls)
49 GNUNET_ARM_disconnect (arm);
55 arm_stop_cb (void *cls,
56 enum GNUNET_ARM_RequestStatus status,
57 enum GNUNET_ARM_Result result)
59 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
60 GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
61 if (result != GNUNET_ARM_RESULT_STOPPED)
66 GNUNET_SCHEDULER_add_now (&trigger_disconnect, NULL);
71 service_list (void *cls,
72 enum GNUNET_ARM_RequestStatus rs,
74 const char *const*list)
78 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
79 "%u services are are currently running\n",
81 if (GNUNET_ARM_REQUEST_SENT_OK != rs)
85 if (0 == strcasecmp (list[i],
86 "resolver (gnunet-service-resolver)"))
88 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
89 "Got service list, now stopping arm\n");
95 GNUNET_ARM_request_service_stop (arm,
103 hostname_resolve_cb (void *cls,
104 const struct sockaddr *addr,
107 if ((0 == ret) || (4 == ret) || (1 == resolved_ok))
111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
112 "Name not resolved!\n");
115 GNUNET_ARM_request_service_stop (arm,
121 if (0 == asked_for_a_list)
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
124 "Resolved hostname, now checking the service list\n");
125 GNUNET_ARM_request_service_list (arm,
128 asked_for_a_list = 1;
135 arm_start_cb (void *cls,
136 enum GNUNET_ARM_RequestStatus status,
137 enum GNUNET_ARM_Result result)
139 GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
140 GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142 "Trying to resolve our own hostname!\n");
143 /* connect to the resolver service */
145 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
147 &hostname_resolve_cb,
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151 "Unable initiate connection to resolver service\n");
154 GNUNET_ARM_request_service_stop (arm,
166 const struct GNUNET_CONFIGURATION_Handle *c)
168 arm = GNUNET_ARM_connect (c,
171 GNUNET_ARM_request_service_start (arm,
173 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
180 main (int argc, char *av[])
182 static char *const argv[] = {
183 "test-gnunet-service-arm",
185 "test_arm_api_data.conf",
188 static struct GNUNET_GETOPT_CommandLineOption options[] = {
189 GNUNET_GETOPT_OPTION_END
191 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
193 if (0 != gethostname (hostname, sizeof (hostname) - 1))
195 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
199 "Failed to determine my own hostname, testcase not run.\n");
202 if ( (0 == strcmp (hostname,
204 (0 == strcmp (hostname,
207 /* we cannot use 'localhost' as this would not trigger the
208 resolver service (see resolver_api.c); so in this case,
209 we fall back to (ab)using gnu.org. */
213 /* trigger DNS lookup */
219 if (0 != (ret = getaddrinfo (hostname, NULL, NULL, &ai)))
222 "Failed to resolve my hostname `%s', testcase not run.\n",
228 #elif HAVE_GETHOSTBYNAME2
230 struct hostent *host;
232 host = gethostbyname2 (hostname, AF_INET);
234 host = gethostbyname2 (hostname, AF_INET6);
238 "Failed to resolve my hostname `%s', testcase not run.\n",
243 #elif HAVE_GETHOSTBYNAME
245 struct hostent *host;
247 host = gethostbyname (hostname);
251 "Failed to resolve my hostname `%s', testcase not run.\n",
258 "libc fails to have resolver function, testcase not run.\n");
261 GNUNET_log_setup ("test-gnunet-service-arm",
264 GNUNET_break (GNUNET_OK ==
265 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
266 argv, "test-gnunet-service-arm",
272 "Test failed with error code %d\n",
278 /* end of test_gnunet_service_arm.c */