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;
48 end_it (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
50 struct GNUNET_CLIENT_Connection *client = cls;
52 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down service\n");
53 GNUNET_CLIENT_service_shutdown (client);
55 GNUNET_SERVICE_stop (sctx);
57 GNUNET_SCHEDULER_shutdown (sched);
63 build_msg (void *cls, size_t size, void *buf)
65 struct GNUNET_CLIENT_Connection *client = cls;
66 struct GNUNET_MessageHeader *msg = buf;
68 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected, transmitting\n");
69 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
70 msg->type = htons (MY_TYPE);
71 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
72 GNUNET_SCHEDULER_add_continuation (sched,
75 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
76 return sizeof (struct GNUNET_MessageHeader);
81 ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
84 struct GNUNET_CLIENT_Connection *client;
86 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service confirmed running\n");
89 client = GNUNET_CLIENT_connect (tc->sched, "test_service", cfg);
90 GNUNET_assert (client != NULL);
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
92 "Client connecting, waiting to transmit\n");
93 GNUNET_CLIENT_notify_transmit_ready (client,
94 sizeof (struct GNUNET_MessageHeader),
95 GNUNET_TIME_UNIT_SECONDS,
96 GNUNET_NO, &build_msg, client);
101 struct GNUNET_SERVER_Client *client,
102 const struct GNUNET_MessageHeader *message)
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
105 GNUNET_SERVER_receive_done (client, GNUNET_OK);
108 static struct GNUNET_SERVER_MessageHandler myhandlers[] = {
109 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
118 struct GNUNET_SCHEDULER_Handle *sched,
119 struct GNUNET_SERVER_Handle *server,
120 const struct GNUNET_CONFIGURATION_Handle *cfg)
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n");
123 GNUNET_SERVER_add_handlers (server, myhandlers);
124 GNUNET_CLIENT_service_test (sched,
126 cfg, GNUNET_TIME_UNIT_SECONDS, &ready,
132 * Main method, starts scheduler with task1,
133 * checks that "ok" is correct at the end.
139 char *const argv[] = {
142 "test_service_data.conf",
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n");
152 GNUNET_assert (GNUNET_OK ==
153 GNUNET_SERVICE_run (5,
156 GNUNET_SERVICE_OPTION_NONE,
158 GNUNET_assert (0 == ok);
163 ready6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
165 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
166 struct GNUNET_CLIENT_Connection *client;
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 ready\n");
170 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
171 client = GNUNET_CLIENT_connect (tc->sched, "test_service6", cfg);
172 GNUNET_assert (client != NULL);
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n");
174 GNUNET_CLIENT_notify_transmit_ready (client,
175 sizeof (struct GNUNET_MessageHeader),
176 GNUNET_TIME_UNIT_SECONDS,
177 GNUNET_NO, &build_msg, client);
182 struct GNUNET_SCHEDULER_Handle *sched,
183 struct GNUNET_SERVER_Handle *server,
184 const struct GNUNET_CONFIGURATION_Handle *cfg)
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing v6 service\n");
187 GNUNET_SERVER_add_handlers (server, myhandlers);
188 GNUNET_CLIENT_service_test (sched,
190 cfg, GNUNET_TIME_UNIT_SECONDS, &ready6,
195 * Main method, starts scheduler with task1,
196 * checks that "ok" is correct at the end.
201 char *const argv[] = {
204 "test_service_data.conf",
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n");
214 GNUNET_assert (GNUNET_OK ==
215 GNUNET_SERVICE_run (5,
218 GNUNET_SERVICE_OPTION_NONE,
220 GNUNET_assert (0 == ok);
226 * Main method, starts scheduler with task1,
227 * checks that "ok" is correct at the end.
233 char *const argv[] = {
236 "test_service_data.conf",
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting V6 as daemon\n");
247 GNUNET_assert (GNUNET_OK ==
248 GNUNET_SERVICE_run (6,
251 GNUNET_SERVICE_OPTION_NONE,
253 GNUNET_break (0 == ok);
259 start_stop_main (void *cls,
260 struct GNUNET_SCHEDULER_Handle *sched,
263 const struct GNUNET_CONFIGURATION_Handle *cfg)
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267 "Starting service using start method\n");
268 sctx = GNUNET_SERVICE_start ("test_service", sched, cfg);
269 runner (cls, sched, GNUNET_SERVICE_get_server (sctx), cfg);
277 char *const argv[] = {
278 "test-service-program",
280 "test_service_data.conf",
289 const struct GNUNET_GETOPT_CommandLineOption options[] = {
290 GNUNET_GETOPT_OPTION_END
293 GNUNET_assert (GNUNET_OK ==
294 GNUNET_PROGRAM_run (5,
296 "test-service-program",
298 options, &start_stop_main, &ret));
300 GNUNET_break (0 == ret);
306 main (int argc, char *argv[])
309 struct GNUNET_NETWORK_Handle *s = NULL;
311 GNUNET_log_setup ("test-service",
323 s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
327 if ((errno == ENOBUFS) ||
328 (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES))
330 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
334 "IPv6 support seems to not be available (%s), not testing it!\n",
339 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
341 ret += check6d (); /* with daemonization */
343 ret += check_start_stop ();
348 /* end of test_service.c */