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