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