poll PIDs for status information
[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 (msg));
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   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                                        &build_msg, client);
91 }
92
93 static void
94 recv_cb (void *cls,
95          struct GNUNET_SERVER_Client *client,
96          const struct GNUNET_MessageHeader *message)
97 {
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
99   GNUNET_SERVER_receive_done (client, GNUNET_OK);
100 }
101
102 static struct GNUNET_SERVER_MessageHandler myhandlers[] = {
103   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
104   {NULL, NULL, 0, 0}
105 };
106
107 static void
108 runner (void *cls,
109         struct GNUNET_SCHEDULER_Handle *sched,
110         struct GNUNET_SERVER_Handle *server,
111         struct GNUNET_CONFIGURATION_Handle *cfg)
112 {
113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n");
114   GNUNET_SERVER_add_handlers (server, myhandlers);
115   GNUNET_CLIENT_service_test (sched,
116                               "test_service",
117                               cfg, GNUNET_TIME_UNIT_SECONDS, &ready, cfg);
118 }
119
120 static void
121 term (void *cls, 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   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                                        &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          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, cfg);
186 }
187
188 /**
189  * Main method, starts scheduler with task1,
190  * checks that "ok" is correct at the end.
191  */
192 static int
193 check6 ()
194 {
195   int ok = 1;
196   char *const argv[] = {
197     "test_service6",
198     "-c",
199     "test_service_data.conf",
200     "-L",
201 #if VERBOSE
202     "DEBUG",
203 #else
204     "WARNING",
205 #endif
206     NULL
207   };
208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n");
209   GNUNET_assert (GNUNET_OK ==
210                  GNUNET_SERVICE_run (5,
211                                      argv,
212                                      "test_service6",
213                                      &runner6, &ok, &term, &ok));
214   GNUNET_assert (0 == ok);
215   return ok;
216 }
217
218
219 /**
220  * Main method, starts scheduler with task1,
221  * checks that "ok" is correct at the end.
222  */
223 static int
224 check6d ()
225 {
226   int ok = 1;
227   char *const argv[] = {
228     "test_service6",
229     "-c",
230     "test_service_data.conf",
231     "-L",
232 #if VERBOSE
233     "DEBUG",
234 #else
235     "WARNING",
236 #endif
237     "-d",
238     NULL
239   };
240   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting V6 as daemon\n");
241   GNUNET_assert (GNUNET_OK ==
242                  GNUNET_SERVICE_run (6,
243                                      argv,
244                                      "test_service6",
245                                      &runner6, &ok, &term, &ok));
246   GNUNET_break (0 == ok);
247   return ok;
248 }
249
250
251 static void
252 start_stop_main (void *cls,
253                  struct GNUNET_SCHEDULER_Handle *sched,
254                  char *const *args,
255                  const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
256 {
257   int *ret = cls;
258   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259               "Starting service using start method\n");
260   sctx = GNUNET_SERVICE_start ("test_service", sched, cfg);
261   runner (cls, sched, GNUNET_SERVICE_get_server (sctx), cfg);
262   *ret = 0;
263 }
264
265
266 static int
267 check_start_stop ()
268 {
269   char *const argv[] = {
270     "test-service-program",
271     "-c",
272     "test_service_data.conf",
273     "-L",
274 #if VERBOSE
275     "DEBUG",
276 #else
277     "WARNING",
278 #endif
279     NULL
280   };
281   const struct GNUNET_GETOPT_CommandLineOption options[] = {
282     GNUNET_GETOPT_OPTION_END
283   };
284   int ret = 1;
285   GNUNET_assert (GNUNET_OK ==
286                  GNUNET_PROGRAM_run (5,
287                                      argv,
288                                      "test-service-program",
289                                      "no help",
290                                      options, &start_stop_main, &ret));
291
292   GNUNET_break (0 == ret);
293   return ret;
294 }
295
296
297 int
298 main (int argc, char *argv[])
299 {
300   int ret = 0;
301   int s;
302
303   GNUNET_log_setup ("test-service",
304 #if VERBOSE
305                     "DEBUG",
306 #else
307                     "WARNING",
308 #endif
309                     NULL);
310   ret += check ();
311   ret += check ();
312   s = SOCKET (PF_INET6, SOCK_STREAM, 0);
313   if (s == -1)
314     {
315       if ((errno == ENOBUFS) ||
316           (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES))
317         {
318           GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
319           return 1;
320         }
321       fprintf (stderr,
322                "IPv6 support seems to not be available (%s), not testing it!\n",
323                strerror (errno));
324     }
325   else
326     {
327       GNUNET_break (0 == CLOSE (s));
328       ret += check6 ();
329       ret += check6d ();        /* with daemonization */
330     }
331   ret += check_start_stop ();
332
333   return ret;
334 }
335
336 /* end of test_service.c */