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