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