700e7ca19412f2e6d7e6dc55a89ea4d66dbc075c
[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 (GNUNET_TIME_UNIT_MILLISECONDS, 50)
38
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
40
41
42 static int ret = 1;
43
44 static int resolved_ok;
45
46 static int asked_for_a_list;
47
48 static struct GNUNET_ARM_Handle *arm;
49
50
51 static void
52 trigger_disconnect (void *cls)
53 {
54   GNUNET_ARM_disconnect (arm);
55   arm = NULL;
56 }
57
58
59 static void
60 arm_stop_cb (void *cls,
61              enum GNUNET_ARM_RequestStatus status,
62              enum GNUNET_ARM_Result result)
63 {
64   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
65   GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
66   if (result != GNUNET_ARM_RESULT_STOPPED)
67   {
68     GNUNET_break (0);
69     ret = 4;
70   }
71   GNUNET_SCHEDULER_add_now (&trigger_disconnect, NULL);
72 }
73
74
75 static void
76 service_list (void *cls,
77               enum GNUNET_ARM_RequestStatus rs,
78               unsigned int count,
79               const char *const*list)
80 {
81   unsigned int i;
82
83   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84               "%u services are are currently running\n",
85               count);
86   if (GNUNET_ARM_REQUEST_SENT_OK != rs)
87     goto stop_arm;
88   for (i=0;i<count;i++)
89   {
90     if (0 == strcasecmp (list[i],
91                          "resolver (gnunet-service-resolver)"))
92     {
93       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94                   "Got service list, now stopping arm\n");
95       ret = 0;
96     }
97   }
98
99  stop_arm:
100   GNUNET_ARM_request_service_stop (arm,
101                                    "arm",
102                                    &arm_stop_cb,
103                                    NULL);
104 }
105
106
107 static void
108 hostname_resolve_cb (void *cls,
109                      const struct sockaddr *addr,
110                      socklen_t addrlen)
111 {
112   if ((0 == ret) || (4 == ret) || (1 == resolved_ok))
113     return;
114   if (NULL == addr)
115   {
116     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
117                 "Failed to resolve our own hostname!\n");
118     GNUNET_break (0);
119     ret = 3;
120     GNUNET_ARM_request_service_stop (arm,
121                                      "arm",
122                                      &arm_stop_cb,
123                                      NULL);
124     return;
125   }
126   if (0 == asked_for_a_list)
127   {
128     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129                 "Resolved hostname, now checking the service list\n");
130     GNUNET_ARM_request_service_list (arm,
131                                      &service_list,
132                                      NULL);
133     asked_for_a_list = 1;
134     resolved_ok = 1;
135   }
136 }
137
138
139 static void
140 arm_start_cb (void *cls,
141               enum GNUNET_ARM_RequestStatus status,
142               enum GNUNET_ARM_Result result)
143 {
144   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
145   GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147               "Trying to resolve our own hostname!\n");
148   /* connect to the resolver service */
149   if (NULL ==
150       GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
151                                         TIMEOUT,
152                                         &hostname_resolve_cb,
153                                         NULL))
154   {
155     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
156                 "Unable initiate connection to resolver service\n");
157     GNUNET_break (0);
158     ret = 2;
159     GNUNET_ARM_request_service_stop (arm,
160                                      "arm",
161                                      &arm_stop_cb,
162                                      NULL);
163   }
164 }
165
166
167 static void
168 run (void *cls,
169      char *const *args,
170      const char *cfgfile,
171      const struct GNUNET_CONFIGURATION_Handle *c)
172 {
173   arm = GNUNET_ARM_connect (c,
174                             NULL,
175                             NULL);
176   GNUNET_ARM_request_service_start (arm,
177                                     "arm",
178                                     GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
179                                     &arm_start_cb,
180                                     NULL);
181 }
182
183
184 int
185 main (int argc, char *av[])
186 {
187   static char *const argv[] = {
188     "test-gnunet-service-arm",
189     "-c",
190     "test_arm_api_data.conf",
191     NULL
192   };
193   static struct GNUNET_GETOPT_CommandLineOption options[] = {
194     GNUNET_GETOPT_OPTION_END
195   };
196   char hostname[GNUNET_OS_get_hostname_max_length () + 1];
197
198   if (0 != gethostname (hostname, sizeof (hostname) - 1))
199   {
200     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
201                          "gethostname");
202     FPRINTF (stderr,
203              "%s",
204              "Failed to determine my own hostname, testcase not run.\n");
205     return 77;
206   }
207   if ( (0 == strcmp (hostname,
208                      "localhost")) ||
209        (0 == strcmp (hostname,
210                      "ipv6-localnet")) )
211   {
212     /* we cannot use 'localhost' as this would not trigger the
213        resolver service (see resolver_api.c); so in this case,
214        we fall back to (ab)using gnu.org. */
215     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
216                 "Falling back to `www.gnu.org'\n");
217     strcpy (hostname,
218             "www.gnu.org");
219   }
220   /* trigger DNS lookup */
221 #if HAVE_GETADDRINFO
222   {
223     struct addrinfo *ai;
224     int ret;
225
226     if (0 != (ret = getaddrinfo (hostname, NULL, NULL, &ai)))
227     {
228       FPRINTF (stderr,
229                "Failed to resolve my hostname `%s', testcase not run.\n",
230                hostname);
231       return 77;
232     }
233     freeaddrinfo (ai);
234   }
235 #elif HAVE_GETHOSTBYNAME2
236   {
237     struct hostent *host;
238
239     host = gethostbyname2 (hostname, AF_INET);
240     if (NULL == host)
241       host = gethostbyname2 (hostname, AF_INET6);
242     if (NULL == host)
243       {
244         FPRINTF (stderr,
245                  "Failed to resolve my hostname `%s', testcase not run.\n",
246                  hostname);
247         return 77;
248       }
249   }
250 #elif HAVE_GETHOSTBYNAME
251   {
252     struct hostent *host;
253
254     host = gethostbyname (hostname);
255     if (NULL == host)
256       {
257         FPRINTF (stderr,
258                  "Failed to resolve my hostname `%s', testcase not run.\n",
259                  hostname);
260         return 77;
261       }
262   }
263 #else
264   FPRINTF (stderr,
265            "libc fails to have resolver function, testcase not run.\n");
266   return 77;
267 #endif
268   GNUNET_log_setup ("test-gnunet-service-arm",
269                     "WARNING",
270                     NULL);
271   GNUNET_break (GNUNET_OK ==
272                 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
273                                     argv, "test-gnunet-service-arm",
274                                     "nohelp", options,
275                                     &run, NULL));
276   if (0 != ret)
277   {
278     fprintf (stderr,
279              "Test failed with error code %d\n",
280              ret);
281   }
282   return ret;
283 }
284
285 /* end of test_gnunet_service_arm.c */