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