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