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