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"
33 #define VERBOSE GNUNET_NO
39 static struct GNUNET_SCHEDULER_Handle *sched;
41 static struct GNUNET_SERVICE_Context *sctx;
44 end_it (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
46 struct GNUNET_CLIENT_Connection *client = cls;
48 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down service\n");
49 GNUNET_CLIENT_service_shutdown (client);
51 GNUNET_SERVICE_stop (sctx);
56 build_msg (void *cls, size_t size, void *buf)
58 struct GNUNET_CLIENT_Connection *client = cls;
59 struct GNUNET_MessageHeader *msg = buf;
61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected, transmitting\n");
62 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
63 msg->type = htons (MY_TYPE);
64 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
65 GNUNET_SCHEDULER_add_continuation (sched,
69 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
70 return sizeof (struct GNUNET_MessageHeader);
74 ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
76 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
77 struct GNUNET_CLIENT_Connection *client;
79 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service confirmed running\n");
81 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
82 client = GNUNET_CLIENT_connect (tc->sched, "test_service", cfg);
83 GNUNET_assert (client != NULL);
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 "Client connecting, waiting to transmit\n");
86 GNUNET_CLIENT_notify_transmit_ready (client,
87 sizeof (struct GNUNET_MessageHeader),
88 GNUNET_TIME_UNIT_SECONDS,
89 GNUNET_NO, &build_msg, client);
94 struct GNUNET_SERVER_Client *client,
95 const struct GNUNET_MessageHeader *message)
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
98 GNUNET_SERVER_receive_done (client, GNUNET_OK);
101 static struct GNUNET_SERVER_MessageHandler myhandlers[] = {
102 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
108 struct GNUNET_SCHEDULER_Handle *sched,
109 struct GNUNET_SERVER_Handle *server,
110 const struct GNUNET_CONFIGURATION_Handle *cfg)
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n");
113 GNUNET_SERVER_add_handlers (server, myhandlers);
114 GNUNET_CLIENT_service_test (sched,
116 cfg, GNUNET_TIME_UNIT_SECONDS, &ready,
121 term (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
128 * Main method, starts scheduler with task1,
129 * checks that "ok" is correct at the end.
135 char *const argv[] = {
138 "test_service_data.conf",
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n");
148 GNUNET_assert (GNUNET_OK ==
149 GNUNET_SERVICE_run (5,
152 &runner, &ok, &term, &ok));
153 GNUNET_assert (0 == ok);
158 ready6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
161 struct GNUNET_CLIENT_Connection *client;
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 ready\n");
165 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
166 client = GNUNET_CLIENT_connect (tc->sched, "test_service6", cfg);
167 GNUNET_assert (client != NULL);
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n");
169 GNUNET_CLIENT_notify_transmit_ready (client,
170 sizeof (struct GNUNET_MessageHeader),
171 GNUNET_TIME_UNIT_SECONDS,
172 GNUNET_NO, &build_msg, client);
177 struct GNUNET_SCHEDULER_Handle *sched,
178 struct GNUNET_SERVER_Handle *server,
179 const struct GNUNET_CONFIGURATION_Handle *cfg)
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing v6 service\n");
182 GNUNET_SERVER_add_handlers (server, myhandlers);
183 GNUNET_CLIENT_service_test (sched,
185 cfg, GNUNET_TIME_UNIT_SECONDS, &ready6,
190 * Main method, starts scheduler with task1,
191 * checks that "ok" is correct at the end.
197 char *const argv[] = {
200 "test_service_data.conf",
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n");
210 GNUNET_assert (GNUNET_OK ==
211 GNUNET_SERVICE_run (5,
214 &runner6, &ok, &term, &ok));
215 GNUNET_assert (0 == ok);
221 * Main method, starts scheduler with task1,
222 * checks that "ok" is correct at the end.
228 char *const argv[] = {
231 "test_service_data.conf",
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting V6 as daemon\n");
242 GNUNET_assert (GNUNET_OK ==
243 GNUNET_SERVICE_run (6,
246 &runner6, &ok, &term, &ok));
247 GNUNET_break (0 == ok);
253 start_stop_main (void *cls,
254 struct GNUNET_SCHEDULER_Handle *sched,
257 const struct GNUNET_CONFIGURATION_Handle *cfg)
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Starting service using start method\n");
262 sctx = GNUNET_SERVICE_start ("test_service", sched, cfg);
263 runner (cls, sched, GNUNET_SERVICE_get_server (sctx), cfg);
271 char *const argv[] = {
272 "test-service-program",
274 "test_service_data.conf",
283 const struct GNUNET_GETOPT_CommandLineOption options[] = {
284 GNUNET_GETOPT_OPTION_END
287 GNUNET_assert (GNUNET_OK ==
288 GNUNET_PROGRAM_run (5,
290 "test-service-program",
292 options, &start_stop_main, &ret));
294 GNUNET_break (0 == ret);
300 main (int argc, char *argv[])
303 struct GNUNET_NETWORK_Handle *s;
305 GNUNET_log_setup ("test-service",
314 s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
317 if ((errno == ENOBUFS) ||
318 (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES))
320 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
324 "IPv6 support seems to not be available (%s), not testing it!\n",
329 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
331 ret += check6d (); /* with daemonization */
333 ret += check_start_stop ();
338 /* end of test_service.c */