Fixed compile warnings under windows
[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_SCHEDULER_Handle *sched;
41
42 static struct GNUNET_SERVICE_Context *sctx;
43
44 static int ok = 1;
45
46
47 static size_t
48 build_msg (void *cls, size_t size, void *buf)
49 {
50   struct GNUNET_CLIENT_Connection *client = cls;
51   struct GNUNET_MessageHeader *msg = buf;
52
53   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected, transmitting\n");
54   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
55   msg->type = htons (MY_TYPE);
56   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
57   GNUNET_CLIENT_disconnect (client, GNUNET_NO);
58   return sizeof (struct GNUNET_MessageHeader);
59 }
60
61
62 static void
63 ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
64 {
65   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
66   struct GNUNET_CLIENT_Connection *client;
67
68   GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
69   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service confirmed running\n");
70   sched = tc->sched;
71   client = GNUNET_CLIENT_connect (tc->sched, "test_service", cfg);
72   GNUNET_assert (client != NULL);
73   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
74               "Client connecting, waiting to transmit\n");
75   GNUNET_CLIENT_notify_transmit_ready (client,
76                                        sizeof (struct GNUNET_MessageHeader),
77                                        GNUNET_TIME_UNIT_SECONDS,
78                                        GNUNET_NO, &build_msg, client);
79 }
80
81 static void
82 do_stop (void *cls,
83          const struct GNUNET_SCHEDULER_TaskContext *tc)
84 {
85   GNUNET_SERVICE_stop (sctx);
86 }
87
88
89 static void
90 recv_cb (void *cls,
91          struct GNUNET_SERVER_Client *client,
92          const struct GNUNET_MessageHeader *message)
93 {
94   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
95   GNUNET_SERVER_receive_done (client, GNUNET_OK);
96   if (sctx != NULL)
97     GNUNET_SCHEDULER_add_now (sched, &do_stop, NULL);
98   else
99     GNUNET_SCHEDULER_shutdown (sched);
100   ok = 0;
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
109
110
111 static void
112 runner (void *cls,
113         struct GNUNET_SCHEDULER_Handle *sched,
114         struct GNUNET_SERVER_Handle *server,
115         const struct GNUNET_CONFIGURATION_Handle *cfg)
116 {
117   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n");
118   GNUNET_SERVER_add_handlers (server, myhandlers);
119   GNUNET_CLIENT_service_test (sched,
120                               "test_service",
121                               cfg, GNUNET_TIME_UNIT_SECONDS, &ready,
122                               (void *) cfg);
123 }
124
125
126 /**
127  * Main method, starts scheduler with task1,
128  * checks that "ok" is correct at the end.
129  */
130 static int
131 check ()
132 {
133   ok = 1;
134   char *const argv[] = {
135     "test_service",
136     "-c",
137     "test_service_data.conf",
138     "-L",
139 #if VERBOSE
140     "DEBUG",
141 #else
142     "WARNING",
143 #endif
144     NULL
145   };
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n");
147   GNUNET_assert (GNUNET_OK ==
148                  GNUNET_SERVICE_run (5,
149                                      argv,
150                                      "test_service",
151                                      GNUNET_SERVICE_OPTION_NONE,
152                                      &runner, &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   const 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                                        GNUNET_NO, &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          const 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,
186                               (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   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                                      GNUNET_SERVICE_OPTION_NONE,
214                                      &runner6, &ok));
215   GNUNET_assert (0 == ok);
216   return ok;
217 }
218
219
220
221 static void
222 start_stop_main (void *cls,
223                  struct GNUNET_SCHEDULER_Handle *sched,
224                  char *const *args,
225                  const char *cfgfile,
226                  const struct GNUNET_CONFIGURATION_Handle *cfg)
227 {
228   int *ret = cls;
229   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230               "Starting service using start method\n");
231   sctx = GNUNET_SERVICE_start ("test_service", sched, cfg);
232   runner (cls, sched, GNUNET_SERVICE_get_server (sctx), cfg);
233   *ret = 0;
234 }
235
236
237 static int
238 check_start_stop ()
239 {
240   char *const argv[] = {
241     "test-service-program",
242     "-c",
243     "test_service_data.conf",
244     "-L",
245 #if VERBOSE
246     "DEBUG",
247 #else
248     "WARNING",
249 #endif
250     NULL
251   };
252   const struct GNUNET_GETOPT_CommandLineOption options[] = {
253     GNUNET_GETOPT_OPTION_END
254   };
255   int ret = 1;
256   GNUNET_assert (GNUNET_OK ==
257                  GNUNET_PROGRAM_run (5,
258                                      argv,
259                                      "test-service-program",
260                                      "no help",
261                                      options, &start_stop_main, &ret));
262
263   GNUNET_break (0 == ret);
264   return ret;
265 }
266
267
268 int
269 main (int argc, char *argv[])
270 {
271   int ret = 0;
272   struct GNUNET_NETWORK_Handle *s = NULL;
273
274   GNUNET_log_setup ("test-service",
275 #if VERBOSE
276                     "DEBUG",
277 #else
278                     "WARNING",
279 #endif
280                     NULL);
281   ret += check ();
282   ret += check ();
283
284   // FIXME
285 #ifndef MINGW
286   s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
287 #endif
288   if (NULL == s)
289     {
290       if ((errno == ENOBUFS) ||
291           (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES))
292         {
293           GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
294           return 1;
295         }
296       fprintf (stderr,
297                "IPv6 support seems to not be available (%s), not testing it!\n",
298                strerror (errno));
299     }
300   else
301     {
302       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
303       ret += check6 ();
304     }
305   ret += check_start_stop ();
306
307   return ret;
308 }
309
310 /* end of test_service.c */