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