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