remove 'illegal' (non-reentrant) log logic from signal handler
[oweals/gnunet.git] / src / arm / test_gnunet_service_arm.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2014 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file arm/test_gnunet_service_arm.c
22  * @brief testcase for gnunet-service-arm.c; tests ARM by making it start the resolver
23  * @author Safey
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_resolver_service.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet_program_lib.h"
31
32 /**
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)).
36  */
37 #define START_TIMEOUT GNUNET_TIME_relative_multiply ( \
38     GNUNET_TIME_UNIT_MILLISECONDS, 50)
39
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
41
42
43 static int ret = 1;
44
45 static int resolved_ok;
46
47 static int asked_for_a_list;
48
49 static struct GNUNET_ARM_Handle *arm;
50
51 static const char hostname[] = "www.gnu.org"; /* any domain should do */
52
53
54 static void
55 trigger_disconnect (void *cls)
56 {
57   GNUNET_ARM_disconnect (arm);
58   arm = NULL;
59 }
60
61
62 static void
63 arm_stop_cb (void *cls,
64              enum GNUNET_ARM_RequestStatus status,
65              enum GNUNET_ARM_Result result)
66 {
67   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
68   GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
69   if (result != GNUNET_ARM_RESULT_STOPPED)
70   {
71     GNUNET_break (0);
72     ret = 4;
73   }
74   GNUNET_SCHEDULER_add_now (&trigger_disconnect, NULL);
75 }
76
77
78 static void
79 service_list (void *cls,
80               enum GNUNET_ARM_RequestStatus rs,
81               unsigned int count,
82               const struct GNUNET_ARM_ServiceInfo *list)
83 {
84   unsigned int i;
85
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
87               "%u services are are currently running\n",
88               count);
89   if (GNUNET_ARM_REQUEST_SENT_OK != rs)
90     goto stop_arm;
91   for (i = 0; i < count; i++)
92   {
93     if ((0 == strcasecmp (list[i].name, "resolver")) &&
94         (0 == strcasecmp (list[i].binary, "gnunet-service-resolver")))
95     {
96       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97                   "Got service list, now stopping arm\n");
98       ret = 0;
99     }
100   }
101
102 stop_arm:
103   GNUNET_ARM_request_service_stop (arm,
104                                    "arm",
105                                    &arm_stop_cb,
106                                    NULL);
107 }
108
109
110 static void
111 hostname_resolve_cb (void *cls,
112                      const struct sockaddr *addr,
113                      socklen_t addrlen)
114 {
115   if ((0 == ret) || (4 == ret) || (1 == resolved_ok))
116     return;
117   if (NULL == addr)
118   {
119     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
120                 "Failed to resolve hostname!\n");
121     GNUNET_break (0);
122     ret = 3;
123     GNUNET_ARM_request_service_stop (arm,
124                                      "arm",
125                                      &arm_stop_cb,
126                                      NULL);
127     return;
128   }
129   if (0 == asked_for_a_list)
130   {
131     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132                 "Resolved hostname, now checking the service list\n");
133     GNUNET_ARM_request_service_list (arm,
134                                      &service_list,
135                                      NULL);
136     asked_for_a_list = 1;
137     resolved_ok = 1;
138   }
139 }
140
141
142 static void
143 arm_start_cb (void *cls,
144               enum GNUNET_ARM_RequestStatus status,
145               enum GNUNET_ARM_Result result)
146 {
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 */
152   if (NULL ==
153       GNUNET_RESOLVER_ip_get (hostname,
154                               AF_UNSPEC,
155                               TIMEOUT,
156                               &hostname_resolve_cb,
157                               NULL))
158   {
159     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160                 "Unable initiate connection to resolver service\n");
161     GNUNET_break (0);
162     ret = 2;
163     GNUNET_ARM_request_service_stop (arm,
164                                      "arm",
165                                      &arm_stop_cb,
166                                      NULL);
167   }
168 }
169
170
171 static void
172 run (void *cls,
173      char *const *args,
174      const char *cfgfile,
175      const struct GNUNET_CONFIGURATION_Handle *c)
176 {
177   arm = GNUNET_ARM_connect (c,
178                             NULL,
179                             NULL);
180   GNUNET_ARM_request_service_start (arm,
181                                     "arm",
182                                     GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
183                                     &arm_start_cb,
184                                     NULL);
185 }
186
187
188 int
189 main (int argc, char *av[])
190 {
191   static char *const argv[] = {
192     "test-gnunet-service-arm",
193     "-c",
194     "test_arm_api_data.conf",
195     NULL
196   };
197   static struct GNUNET_GETOPT_CommandLineOption options[] = {
198     GNUNET_GETOPT_OPTION_END
199   };
200
201   /* trigger DNS lookup */
202 #if HAVE_GETADDRINFO
203   {
204     struct addrinfo *ai;
205     int ret;
206
207     if (0 != (ret = getaddrinfo (hostname, NULL, NULL, &ai)))
208     {
209       fprintf (stderr,
210                "Failed to resolve `%s', testcase not run.\n",
211                hostname);
212       return 77;
213     }
214     freeaddrinfo (ai);
215   }
216 #elif HAVE_GETHOSTBYNAME2
217   {
218     struct hostent *host;
219
220     host = gethostbyname2 (hostname, AF_INET);
221     if (NULL == host)
222       host = gethostbyname2 (hostname, AF_INET6);
223     if (NULL == host)
224     {
225       fprintf (stderr,
226                "Failed to resolve `%s', testcase not run.\n",
227                hostname);
228       return 77;
229     }
230   }
231 #elif HAVE_GETHOSTBYNAME
232   {
233     struct hostent *host;
234
235     host = gethostbyname (hostname);
236     if (NULL == host)
237     {
238       fprintf (stderr,
239                "Failed to resolve `%s', testcase not run.\n",
240                hostname);
241       return 77;
242     }
243   }
244 #else
245   fprintf (stderr,
246            "libc fails to have resolver function, testcase not run.\n");
247   return 77;
248 #endif
249   GNUNET_log_setup ("test-gnunet-service-arm",
250                     "WARNING",
251                     NULL);
252   GNUNET_break (GNUNET_OK ==
253                 GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1,
254                                     argv, "test-gnunet-service-arm",
255                                     "nohelp", options,
256                                     &run, NULL));
257   if (0 != ret)
258   {
259     fprintf (stderr,
260              "Test failed with error code %d\n",
261              ret);
262   }
263   return ret;
264 }
265
266
267 /* end of test_gnunet_service_arm.c */