93cafb4d2883c2d168e369f468d8bfedea1a56c1
[oweals/gnunet.git] / src / util / test_service.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 2, 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 util/test_service.c
22  * @brief tests for service.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_client_lib.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_service_lib.h"
30 #include "gnunet_scheduler_lib.h"
31 #include "gnunet_time_lib.h"
32
33 #define VERBOSE GNUNET_NO
34
35 #define PORT 12435
36
37 #define MY_TYPE 256
38
39 static struct GNUNET_SCHEDULER_Handle *sched;
40
41 static struct GNUNET_SERVICE_Context *sctx;
42
43 static void
44 end_it (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
45 {
46   struct GNUNET_CLIENT_Connection *client = cls;
47
48   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down service\n");
49   GNUNET_CLIENT_service_shutdown (client);
50   if (sctx != NULL)
51     GNUNET_SERVICE_stop (sctx);
52 }
53
54
55 static size_t
56 build_msg (void *cls, size_t size, void *buf)
57 {
58   struct GNUNET_CLIENT_Connection *client = cls;
59   struct GNUNET_MessageHeader *msg = buf;
60
61   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected, transmitting\n");
62   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
63   msg->type = htons (MY_TYPE);
64   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
65   GNUNET_SCHEDULER_add_continuation (sched,
66                                      GNUNET_YES,
67                                      &end_it,
68                                      client,
69                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
70   return sizeof (struct GNUNET_MessageHeader);
71 }
72
73 static void
74 ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
75 {
76   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
77   struct GNUNET_CLIENT_Connection *client;
78
79   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service confirmed running\n");
80   sched = tc->sched;
81   GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
82   client = GNUNET_CLIENT_connect (tc->sched, "test_service", cfg);
83   GNUNET_assert (client != NULL);
84   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85               "Client connecting, waiting to transmit\n");
86   GNUNET_CLIENT_notify_transmit_ready (client,
87                                        sizeof (struct GNUNET_MessageHeader),
88                                        GNUNET_TIME_UNIT_SECONDS,
89                                        GNUNET_NO, &build_msg, client);
90 }
91
92 static void
93 recv_cb (void *cls,
94          struct GNUNET_SERVER_Client *client,
95          const struct GNUNET_MessageHeader *message)
96 {
97   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
98   GNUNET_SERVER_receive_done (client, GNUNET_OK);
99 }
100
101 static struct GNUNET_SERVER_MessageHandler myhandlers[] = {
102   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
103   {NULL, NULL, 0, 0}
104 };
105
106 static void
107 runner (void *cls,
108         struct GNUNET_SCHEDULER_Handle *sched,
109         struct GNUNET_SERVER_Handle *server,
110         const struct GNUNET_CONFIGURATION_Handle *cfg)
111 {
112   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n");
113   GNUNET_SERVER_add_handlers (server, myhandlers);
114   GNUNET_CLIENT_service_test (sched,
115                               "test_service",
116                               cfg, GNUNET_TIME_UNIT_SECONDS, &ready,
117                               (void *) cfg);
118 }
119
120 static void
121 term (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
122 {
123   int *ok = cls;
124   *ok = 0;
125 }
126
127 /**
128  * Main method, starts scheduler with task1,
129  * checks that "ok" is correct at the end.
130  */
131 static int
132 check ()
133 {
134   int ok = 1;
135   char *const argv[] = {
136     "test_service",
137     "-c",
138     "test_service_data.conf",
139     "-L",
140 #if VERBOSE
141     "DEBUG",
142 #else
143     "WARNING",
144 #endif
145     NULL
146   };
147   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n");
148   GNUNET_assert (GNUNET_OK ==
149                  GNUNET_SERVICE_run (5,
150                                      argv,
151                                      "test_service",
152                                      &runner, &ok, &term, &ok));
153   GNUNET_assert (0 == ok);
154   return ok;
155 }
156
157 static void
158 ready6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 {
160   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
161   struct GNUNET_CLIENT_Connection *client;
162
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 ready\n");
164   sched = tc->sched;
165   GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
166   client = GNUNET_CLIENT_connect (tc->sched, "test_service6", cfg);
167   GNUNET_assert (client != NULL);
168   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n");
169   GNUNET_CLIENT_notify_transmit_ready (client,
170                                        sizeof (struct GNUNET_MessageHeader),
171                                        GNUNET_TIME_UNIT_SECONDS,
172                                        GNUNET_NO, &build_msg, client);
173 }
174
175 static void
176 runner6 (void *cls,
177          struct GNUNET_SCHEDULER_Handle *sched,
178          struct GNUNET_SERVER_Handle *server,
179          const struct GNUNET_CONFIGURATION_Handle *cfg)
180 {
181   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing v6 service\n");
182   GNUNET_SERVER_add_handlers (server, myhandlers);
183   GNUNET_CLIENT_service_test (sched,
184                               "test_service6",
185                               cfg, GNUNET_TIME_UNIT_SECONDS, &ready6,
186                               (void *) cfg);
187 }
188
189 /**
190  * Main method, starts scheduler with task1,
191  * checks that "ok" is correct at the end.
192  */
193 static int
194 check6 ()
195 {
196   int ok = 1;
197   char *const argv[] = {
198     "test_service6",
199     "-c",
200     "test_service_data.conf",
201     "-L",
202 #if VERBOSE
203     "DEBUG",
204 #else
205     "WARNING",
206 #endif
207     NULL
208   };
209   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n");
210   GNUNET_assert (GNUNET_OK ==
211                  GNUNET_SERVICE_run (5,
212                                      argv,
213                                      "test_service6",
214                                      &runner6, &ok, &term, &ok));
215   GNUNET_assert (0 == ok);
216   return ok;
217 }
218
219
220 /**
221  * Main method, starts scheduler with task1,
222  * checks that "ok" is correct at the end.
223  */
224 static int
225 check6d ()
226 {
227   int ok = 1;
228   char *const argv[] = {
229     "test_service6",
230     "-c",
231     "test_service_data.conf",
232     "-L",
233 #if VERBOSE
234     "DEBUG",
235 #else
236     "WARNING",
237 #endif
238     "-d",
239     NULL
240   };
241   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting V6 as daemon\n");
242   GNUNET_assert (GNUNET_OK ==
243                  GNUNET_SERVICE_run (6,
244                                      argv,
245                                      "test_service6",
246                                      &runner6, &ok, &term, &ok));
247   GNUNET_break (0 == ok);
248   return ok;
249 }
250
251
252 static void
253 start_stop_main (void *cls,
254                  struct GNUNET_SCHEDULER_Handle *sched,
255                  char *const *args,
256                  const char *cfgfile,
257                  const struct GNUNET_CONFIGURATION_Handle *cfg)
258 {
259   int *ret = cls;
260   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261               "Starting service using start method\n");
262   sctx = GNUNET_SERVICE_start ("test_service", sched, cfg);
263   runner (cls, sched, GNUNET_SERVICE_get_server (sctx), cfg);
264   *ret = 0;
265 }
266
267
268 static int
269 check_start_stop ()
270 {
271   char *const argv[] = {
272     "test-service-program",
273     "-c",
274     "test_service_data.conf",
275     "-L",
276 #if VERBOSE
277     "DEBUG",
278 #else
279     "WARNING",
280 #endif
281     NULL
282   };
283   const struct GNUNET_GETOPT_CommandLineOption options[] = {
284     GNUNET_GETOPT_OPTION_END
285   };
286   int ret = 1;
287   GNUNET_assert (GNUNET_OK ==
288                  GNUNET_PROGRAM_run (5,
289                                      argv,
290                                      "test-service-program",
291                                      "no help",
292                                      options, &start_stop_main, &ret));
293
294   GNUNET_break (0 == ret);
295   return ret;
296 }
297
298
299 int
300 main (int argc, char *argv[])
301 {
302   int ret = 0;
303   struct GNUNET_NETWORK_Handle *s;
304
305   GNUNET_log_setup ("test-service",
306 #if VERBOSE
307                     "DEBUG",
308 #else
309                     "WARNING",
310 #endif
311                     NULL);
312   ret += check ();
313   ret += check ();
314   s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
315   if (NULL == s)
316     {
317       if ((errno == ENOBUFS) ||
318           (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES))
319         {
320           GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
321           return 1;
322         }
323       fprintf (stderr,
324                "IPv6 support seems to not be available (%s), not testing it!\n",
325                strerror (errno));
326     }
327   else
328     {
329       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
330       ret += check6 ();
331       ret += check6d ();        /* with daemonization */
332     }
333   ret += check_start_stop ();
334
335   return ret;
336 }
337
338 /* end of test_service.c */