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