2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file util/test_service.c
22 * @brief tests for service.c
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"
34 #define VERBOSE GNUNET_NO
40 static struct GNUNET_SCHEDULER_Handle *sched;
42 static struct GNUNET_SERVICE_Context *sctx;
47 end_it (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
49 struct GNUNET_CLIENT_Connection *client = cls;
51 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down service\n");
52 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
55 GNUNET_SERVICE_stop (sctx);
62 build_msg (void *cls, size_t size, void *buf)
64 struct GNUNET_CLIENT_Connection *client = cls;
65 struct GNUNET_MessageHeader *msg = buf;
67 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected, transmitting\n");
68 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
69 msg->type = htons (MY_TYPE);
70 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
71 GNUNET_SCHEDULER_add_continuation (sched,
74 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
75 return sizeof (struct GNUNET_MessageHeader);
80 ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
83 struct GNUNET_CLIENT_Connection *client;
85 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
86 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service confirmed running\n");
88 client = GNUNET_CLIENT_connect (tc->sched, "test_service", cfg);
89 GNUNET_assert (client != NULL);
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91 "Client connecting, waiting to transmit\n");
92 GNUNET_CLIENT_notify_transmit_ready (client,
93 sizeof (struct GNUNET_MessageHeader),
94 GNUNET_TIME_UNIT_SECONDS,
95 GNUNET_NO, &build_msg, client);
100 struct GNUNET_SERVER_Client *client,
101 const struct GNUNET_MessageHeader *message)
103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
104 GNUNET_SERVER_receive_done (client, GNUNET_OK);
107 static struct GNUNET_SERVER_MessageHandler myhandlers[] = {
108 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
117 struct GNUNET_SCHEDULER_Handle *sched,
118 struct GNUNET_SERVER_Handle *server,
119 const struct GNUNET_CONFIGURATION_Handle *cfg)
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n");
122 GNUNET_SERVER_add_handlers (server, myhandlers);
123 GNUNET_CLIENT_service_test (sched,
125 cfg, GNUNET_TIME_UNIT_SECONDS, &ready,
131 * Main method, starts scheduler with task1,
132 * checks that "ok" is correct at the end.
138 char *const argv[] = {
141 "test_service_data.conf",
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n");
151 GNUNET_assert (GNUNET_OK ==
152 GNUNET_SERVICE_run (5,
155 GNUNET_SERVICE_OPTION_NONE,
157 GNUNET_assert (0 == ok);
162 ready6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
164 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
165 struct GNUNET_CLIENT_Connection *client;
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 ready\n");
169 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
170 client = GNUNET_CLIENT_connect (tc->sched, "test_service6", cfg);
171 GNUNET_assert (client != NULL);
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n");
173 GNUNET_CLIENT_notify_transmit_ready (client,
174 sizeof (struct GNUNET_MessageHeader),
175 GNUNET_TIME_UNIT_SECONDS,
176 GNUNET_NO, &build_msg, client);
181 struct GNUNET_SCHEDULER_Handle *sched,
182 struct GNUNET_SERVER_Handle *server,
183 const struct GNUNET_CONFIGURATION_Handle *cfg)
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing v6 service\n");
186 GNUNET_SERVER_add_handlers (server, myhandlers);
187 GNUNET_CLIENT_service_test (sched,
189 cfg, GNUNET_TIME_UNIT_SECONDS, &ready6,
194 * Main method, starts scheduler with task1,
195 * checks that "ok" is correct at the end.
200 char *const argv[] = {
203 "test_service_data.conf",
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n");
213 GNUNET_assert (GNUNET_OK ==
214 GNUNET_SERVICE_run (5,
217 GNUNET_SERVICE_OPTION_NONE,
219 GNUNET_assert (0 == ok);
225 * Main method, starts scheduler with task1,
226 * checks that "ok" is correct at the end.
232 char *const argv[] = {
235 "test_service_data.conf",
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting V6 as daemon\n");
246 GNUNET_assert (GNUNET_OK ==
247 GNUNET_SERVICE_run (6,
250 GNUNET_SERVICE_OPTION_NONE,
252 GNUNET_break (0 == ok);
258 start_stop_main (void *cls,
259 struct GNUNET_SCHEDULER_Handle *sched,
262 const struct GNUNET_CONFIGURATION_Handle *cfg)
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 "Starting service using start method\n");
267 sctx = GNUNET_SERVICE_start ("test_service", sched, cfg);
268 runner (cls, sched, GNUNET_SERVICE_get_server (sctx), cfg);
276 char *const argv[] = {
277 "test-service-program",
279 "test_service_data.conf",
288 const struct GNUNET_GETOPT_CommandLineOption options[] = {
289 GNUNET_GETOPT_OPTION_END
292 GNUNET_assert (GNUNET_OK ==
293 GNUNET_PROGRAM_run (5,
295 "test-service-program",
297 options, &start_stop_main, &ret));
299 GNUNET_break (0 == ret);
305 main (int argc, char *argv[])
308 struct GNUNET_NETWORK_Handle *s = NULL;
310 GNUNET_log_setup ("test-service",
322 s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
326 if ((errno == ENOBUFS) ||
327 (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES))
329 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
333 "IPv6 support seems to not be available (%s), not testing it!\n",
338 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
340 ret += check6d (); /* with daemonization */
342 ret += check_start_stop ();
347 /* end of test_service.c */