-remove trailing whitespace
[oweals/gnunet.git] / src / arm / test_gnunet_service_arm.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 static int ret = 1;
42
43 static int resolved_ok = 0;
44
45 static int asked_for_a_list = 0;
46
47 static struct GNUNET_ARM_Handle *arm;
48
49 static void
50 trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
51 {
52   GNUNET_ARM_disconnect_and_free (arm);
53   arm = NULL;
54 }
55
56
57 static void
58 arm_stop_cb (void *cls,
59              enum GNUNET_ARM_RequestStatus status,
60              const char *servicename,
61              enum GNUNET_ARM_Result result)
62 {
63   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
64   GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
65   if (result != GNUNET_ARM_RESULT_STOPPED)
66     ret = 4;
67   GNUNET_SCHEDULER_add_now (trigger_disconnect, NULL);
68 }
69
70
71 static void
72 service_list (void *cls,
73               enum GNUNET_ARM_RequestStatus rs,
74               unsigned int count, const char *const*list)
75 {
76   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
77               "%u services are are currently running\n",
78               count);
79   GNUNET_break (count == 1);
80   GNUNET_break (0 == strcasecmp (list[0], "resolver (gnunet-service-resolver)"));
81   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got service list, now stopping arm\n");
82   ret = 0;
83   GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
84 }
85
86
87 static void
88 hostNameResolveCB (void *cls, const struct sockaddr *addr, socklen_t addrlen)
89 {
90   if ((ret == 0) || (ret == 4) || (resolved_ok == 1))
91     return;
92   if (NULL == addr)
93   {
94     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Name not resolved!\n");
95     ret = 3;
96     GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
97   }
98   else if (asked_for_a_list == 0)
99   {
100     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
101         "Resolved hostname, now checking the service list\n");
102     GNUNET_ARM_request_service_list (arm, TIMEOUT, service_list, NULL);
103     asked_for_a_list = 1;
104     resolved_ok = 1;
105   }
106 }
107
108
109 static void
110 arm_start_cb (void *cls,
111               enum GNUNET_ARM_RequestStatus status,
112               const char *servicename,
113               enum GNUNET_ARM_Result result)
114 {
115   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
116   GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
117   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
118       "Trying to resolve our own hostname!\n");
119   /* connect to the resolver service */
120   if (NULL == GNUNET_RESOLVER_hostname_resolve (
121       AF_UNSPEC, TIMEOUT, &hostNameResolveCB, NULL))
122   {
123     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
124         "Unable initiate connection to resolver service\n");
125     ret = 2;
126     GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
127   }
128 }
129
130
131 static void
132 run (void *cls, char *const *args, const char *cfgfile,
133      const struct GNUNET_CONFIGURATION_Handle *c)
134 {
135   char *armconfig;
136
137   if (NULL != cfgfile)
138   {
139     if (GNUNET_OK !=
140         GNUNET_CONFIGURATION_get_value_filename (c, "arm", "CONFIG",
141                                                &armconfig))
142     {
143       GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle
144                                               *) c, "arm", "CONFIG",
145                                              cfgfile);
146     }
147     else
148       GNUNET_free (armconfig);
149   }
150   arm = GNUNET_ARM_connect (c, NULL, NULL);
151   GNUNET_ARM_request_service_start (arm, "arm",
152       GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, arm_start_cb, NULL);
153 }
154
155
156 int
157 main (int argc, char *av[])
158 {
159   static char *const argv[] = {
160     "test-gnunet-service-arm",
161     "-c", "test_arm_api_data.conf",
162     NULL
163   };
164   static struct GNUNET_GETOPT_CommandLineOption options[] = {
165     GNUNET_GETOPT_OPTION_END
166   };
167   char hostname[GNUNET_OS_get_hostname_max_length () + 1];
168
169   if (0 != gethostname (hostname, sizeof (hostname) - 1))
170     {
171       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
172                            "gethostname");
173       FPRINTF (stderr,
174                "%s", "Failed to determine my own hostname, testcase not run.\n");
175       return 0;
176     }
177   if (NULL == gethostbyname (hostname))
178     {
179       FPRINTF (stderr,
180                "Failed to resolve my hostname `%s', testcase not run.\n",
181                hostname);
182       return 0;
183     }
184   GNUNET_log_setup ("test-gnunet-service-arm",
185                     "WARNING",
186                     NULL);
187   GNUNET_break (GNUNET_OK ==
188                 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
189                                     argv, "test-gnunet-service-arm",
190                                     "nohelp", options, &run, NULL));
191   return ret;
192 }
193
194 /* end of test_gnunet_service_arm.c */