diff options
Diffstat (limited to 'src/util/test_service.c')
-rw-r--r-- | src/util/test_service.c | 337 |
1 files changed, 337 insertions, 0 deletions
diff --git a/src/util/test_service.c b/src/util/test_service.c new file mode 100644 index 000000000..b5cf0805e --- /dev/null +++ b/src/util/test_service.c | |||
@@ -0,0 +1,337 @@ | |||
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 2, 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 | #define VERBOSE GNUNET_NO | ||
34 | |||
35 | #define PORT 12435 | ||
36 | |||
37 | #define MY_TYPE 256 | ||
38 | |||
39 | static struct GNUNET_SCHEDULER_Handle *sched; | ||
40 | |||
41 | static struct GNUNET_SERVICE_Context *sctx; | ||
42 | |||
43 | static void | ||
44 | end_it (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
45 | { | ||
46 | struct GNUNET_CLIENT_Connection *client = cls; | ||
47 | |||
48 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down service\n"); | ||
49 | GNUNET_CLIENT_service_shutdown (client); | ||
50 | GNUNET_CLIENT_disconnect (client); | ||
51 | if (sctx != NULL) | ||
52 | GNUNET_SERVICE_stop (sctx); | ||
53 | } | ||
54 | |||
55 | |||
56 | static size_t | ||
57 | build_msg (void *cls, size_t size, void *buf) | ||
58 | { | ||
59 | struct GNUNET_CLIENT_Connection *client = cls; | ||
60 | struct GNUNET_MessageHeader *msg = buf; | ||
61 | |||
62 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected, transmitting\n"); | ||
63 | GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); | ||
64 | msg->type = htons (MY_TYPE); | ||
65 | msg->size = htons (sizeof (msg)); | ||
66 | GNUNET_SCHEDULER_add_continuation (sched, | ||
67 | GNUNET_YES, | ||
68 | &end_it, | ||
69 | client, | ||
70 | GNUNET_SCHEDULER_REASON_PREREQ_DONE); | ||
71 | return sizeof (struct GNUNET_MessageHeader); | ||
72 | } | ||
73 | |||
74 | static void | ||
75 | ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
76 | { | ||
77 | struct GNUNET_CONFIGURATION_Handle *cfg = cls; | ||
78 | struct GNUNET_CLIENT_Connection *client; | ||
79 | |||
80 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service confirmed running\n"); | ||
81 | sched = tc->sched; | ||
82 | GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)); | ||
83 | client = GNUNET_CLIENT_connect (tc->sched, "test_service", cfg); | ||
84 | GNUNET_assert (client != NULL); | ||
85 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
86 | "Client connecting, waiting to transmit\n"); | ||
87 | GNUNET_CLIENT_notify_transmit_ready (client, | ||
88 | sizeof (struct GNUNET_MessageHeader), | ||
89 | GNUNET_TIME_UNIT_SECONDS, | ||
90 | &build_msg, client); | ||
91 | } | ||
92 | |||
93 | static void | ||
94 | recv_cb (void *cls, | ||
95 | struct GNUNET_SERVER_Handle *server, | ||
96 | struct GNUNET_SERVER_Client *client, | ||
97 | const struct GNUNET_MessageHeader *message) | ||
98 | { | ||
99 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n"); | ||
100 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
101 | } | ||
102 | |||
103 | static struct GNUNET_SERVER_MessageHandler myhandlers[] = { | ||
104 | {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)}, | ||
105 | {NULL, NULL, 0, 0} | ||
106 | }; | ||
107 | |||
108 | static void | ||
109 | runner (void *cls, | ||
110 | struct GNUNET_SCHEDULER_Handle *sched, | ||
111 | struct GNUNET_SERVER_Handle *server, | ||
112 | struct GNUNET_CONFIGURATION_Handle *cfg) | ||
113 | { | ||
114 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n"); | ||
115 | GNUNET_SERVER_add_handlers (server, myhandlers); | ||
116 | GNUNET_CLIENT_service_test (sched, | ||
117 | "test_service", | ||
118 | cfg, GNUNET_TIME_UNIT_SECONDS, &ready, cfg); | ||
119 | } | ||
120 | |||
121 | static void | ||
122 | term (void *cls, struct GNUNET_CONFIGURATION_Handle *cfg) | ||
123 | { | ||
124 | int *ok = cls; | ||
125 | *ok = 0; | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * Main method, starts scheduler with task1, | ||
130 | * checks that "ok" is correct at the end. | ||
131 | */ | ||
132 | static int | ||
133 | check () | ||
134 | { | ||
135 | int ok = 1; | ||
136 | char *const argv[] = { | ||
137 | "test_service", | ||
138 | "-c", | ||
139 | "test_service_data.conf", | ||
140 | "-L", | ||
141 | #if VERBOSE | ||
142 | "DEBUG", | ||
143 | #else | ||
144 | "WARNING", | ||
145 | #endif | ||
146 | NULL | ||
147 | }; | ||
148 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n"); | ||
149 | GNUNET_assert (GNUNET_OK == | ||
150 | GNUNET_SERVICE_run (5, | ||
151 | argv, | ||
152 | "test_service", | ||
153 | &runner, &ok, &term, &ok)); | ||
154 | GNUNET_assert (0 == ok); | ||
155 | return ok; | ||
156 | } | ||
157 | |||
158 | static void | ||
159 | ready6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
160 | { | ||
161 | struct GNUNET_CONFIGURATION_Handle *cfg = cls; | ||
162 | struct GNUNET_CLIENT_Connection *client; | ||
163 | |||
164 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 ready\n"); | ||
165 | sched = tc->sched; | ||
166 | GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)); | ||
167 | client = GNUNET_CLIENT_connect (tc->sched, "test_service6", cfg); | ||
168 | GNUNET_assert (client != NULL); | ||
169 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n"); | ||
170 | GNUNET_CLIENT_notify_transmit_ready (client, | ||
171 | sizeof (struct GNUNET_MessageHeader), | ||
172 | GNUNET_TIME_UNIT_SECONDS, | ||
173 | &build_msg, client); | ||
174 | } | ||
175 | |||
176 | static void | ||
177 | runner6 (void *cls, | ||
178 | struct GNUNET_SCHEDULER_Handle *sched, | ||
179 | struct GNUNET_SERVER_Handle *server, | ||
180 | 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 (sched, | ||
185 | "test_service6", | ||
186 | cfg, GNUNET_TIME_UNIT_SECONDS, &ready6, cfg); | ||
187 | } | ||
188 | |||
189 | /** | ||
190 | * Main method, starts scheduler with task1, | ||
191 | * checks that "ok" is correct at the end. | ||
192 | */ | ||
193 | static int | ||
194 | check6 () | ||
195 | { | ||
196 | int ok = 1; | ||
197 | char *const argv[] = { | ||
198 | "test_service6", | ||
199 | "-c", | ||
200 | "test_service_data.conf", | ||
201 | "-L", | ||
202 | #if VERBOSE | ||
203 | "DEBUG", | ||
204 | #else | ||
205 | "WARNING", | ||
206 | #endif | ||
207 | NULL | ||
208 | }; | ||
209 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n"); | ||
210 | GNUNET_assert (GNUNET_OK == | ||
211 | GNUNET_SERVICE_run (5, | ||
212 | argv, | ||
213 | "test_service6", | ||
214 | &runner6, &ok, &term, &ok)); | ||
215 | GNUNET_assert (0 == ok); | ||
216 | return ok; | ||
217 | } | ||
218 | |||
219 | |||
220 | /** | ||
221 | * Main method, starts scheduler with task1, | ||
222 | * checks that "ok" is correct at the end. | ||
223 | */ | ||
224 | static int | ||
225 | check6d () | ||
226 | { | ||
227 | int ok = 1; | ||
228 | char *const argv[] = { | ||
229 | "test_service6", | ||
230 | "-c", | ||
231 | "test_service_data.conf", | ||
232 | "-L", | ||
233 | #if VERBOSE | ||
234 | "DEBUG", | ||
235 | #else | ||
236 | "WARNING", | ||
237 | #endif | ||
238 | "-d", | ||
239 | NULL | ||
240 | }; | ||
241 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting V6 as daemon\n"); | ||
242 | GNUNET_assert (GNUNET_OK == | ||
243 | GNUNET_SERVICE_run (6, | ||
244 | argv, | ||
245 | "test_service6", | ||
246 | &runner6, &ok, &term, &ok)); | ||
247 | GNUNET_break (0 == ok); | ||
248 | return ok; | ||
249 | } | ||
250 | |||
251 | |||
252 | static void | ||
253 | start_stop_main (void *cls, | ||
254 | struct GNUNET_SCHEDULER_Handle *sched, | ||
255 | char *const *args, | ||
256 | const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg) | ||
257 | { | ||
258 | int *ret = cls; | ||
259 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
260 | "Starting service using start method\n"); | ||
261 | sctx = GNUNET_SERVICE_start ("test_service", sched, cfg); | ||
262 | runner (cls, sched, GNUNET_SERVICE_get_server (sctx), cfg); | ||
263 | *ret = 0; | ||
264 | } | ||
265 | |||
266 | |||
267 | static int | ||
268 | check_start_stop () | ||
269 | { | ||
270 | char *const argv[] = { | ||
271 | "test-service-program", | ||
272 | "-c", | ||
273 | "test_service_data.conf", | ||
274 | "-L", | ||
275 | #if VERBOSE | ||
276 | "DEBUG", | ||
277 | #else | ||
278 | "WARNING", | ||
279 | #endif | ||
280 | NULL | ||
281 | }; | ||
282 | const struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
283 | GNUNET_GETOPT_OPTION_END | ||
284 | }; | ||
285 | int ret = 1; | ||
286 | GNUNET_assert (GNUNET_OK == | ||
287 | GNUNET_PROGRAM_run (5, | ||
288 | argv, | ||
289 | "test-service-program", | ||
290 | "no help", | ||
291 | options, &start_stop_main, &ret)); | ||
292 | |||
293 | GNUNET_break (0 == ret); | ||
294 | return ret; | ||
295 | } | ||
296 | |||
297 | |||
298 | int | ||
299 | main (int argc, char *argv[]) | ||
300 | { | ||
301 | int ret = 0; | ||
302 | int s; | ||
303 | |||
304 | GNUNET_log_setup ("test-service", | ||
305 | #if VERBOSE | ||
306 | "DEBUG", | ||
307 | #else | ||
308 | "WARNING", | ||
309 | #endif | ||
310 | NULL); | ||
311 | ret += check (); | ||
312 | ret += check (); | ||
313 | s = SOCKET (PF_INET6, SOCK_STREAM, 0); | ||
314 | if (s == -1) | ||
315 | { | ||
316 | if ((errno == ENOBUFS) || | ||
317 | (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES)) | ||
318 | { | ||
319 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); | ||
320 | return 1; | ||
321 | } | ||
322 | fprintf (stderr, | ||
323 | "IPv6 support seems to not be available (%s), not testing it!\n", | ||
324 | strerror (errno)); | ||
325 | } | ||
326 | else | ||
327 | { | ||
328 | GNUNET_break (0 == CLOSE (s)); | ||
329 | ret += check6 (); | ||
330 | ret += check6d (); /* with daemonization */ | ||
331 | } | ||
332 | ret += check_start_stop (); | ||
333 | |||
334 | return ret; | ||
335 | } | ||
336 | |||
337 | /* end of test_service.c */ | ||