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