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