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