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 3, 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_SERVICE_Context *sctx;
44 static struct GNUNET_CLIENT_Connection *client;
50 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
54 GNUNET_CLIENT_disconnect (client);
59 GNUNET_SERVICE_stop (sctx);
66 build_msg (void *cls, size_t size, void *buf)
68 struct GNUNET_MessageHeader *msg = buf;
70 if (size < sizeof (struct GNUNET_MessageHeader))
73 GNUNET_SCHEDULER_add_now (&do_stop, NULL);
78 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected, transmitting\n");
79 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
80 msg->type = htons (MY_TYPE);
81 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
82 return sizeof (struct GNUNET_MessageHeader);
87 ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
89 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
91 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service confirmed running\n");
93 client = GNUNET_CLIENT_connect ("test_service", cfg);
94 GNUNET_assert (client != NULL);
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96 "Client connecting, waiting to transmit\n");
97 GNUNET_CLIENT_notify_transmit_ready (client,
98 sizeof (struct GNUNET_MessageHeader),
99 GNUNET_TIME_UNIT_SECONDS, GNUNET_NO,
105 recv_cb (void *cls, struct GNUNET_SERVER_Client *sc,
106 const struct GNUNET_MessageHeader *message)
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
111 GNUNET_SERVER_receive_done (sc, GNUNET_OK);
112 GNUNET_SCHEDULER_add_now (&do_stop, NULL);
117 static struct GNUNET_SERVER_MessageHandler myhandlers[] = {
118 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
124 runner (void *cls, struct GNUNET_SERVER_Handle *server,
125 const struct GNUNET_CONFIGURATION_Handle *cfg)
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n");
128 GNUNET_SERVER_add_handlers (server, myhandlers);
129 GNUNET_CLIENT_service_test ("test_service", cfg, GNUNET_TIME_UNIT_SECONDS,
130 &ready, (void *) cfg);
135 * Main method, starts scheduler with task1,
136 * checks that "ok" is correct at the end.
142 char *const argv[] = {
145 "test_service_data.conf",
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n");
155 GNUNET_assert (GNUNET_OK ==
156 GNUNET_SERVICE_run (5, argv, "test_service",
157 GNUNET_SERVICE_OPTION_NONE, &runner, &ok));
158 GNUNET_assert (0 == ok);
163 ready6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
165 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 ready\n");
168 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
169 client = GNUNET_CLIENT_connect ("test_service6", cfg);
170 GNUNET_assert (client != NULL);
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n");
172 GNUNET_CLIENT_notify_transmit_ready (client,
173 sizeof (struct GNUNET_MessageHeader),
174 GNUNET_TIME_UNIT_SECONDS, GNUNET_NO,
179 runner6 (void *cls, struct GNUNET_SERVER_Handle *server,
180 const struct GNUNET_CONFIGURATION_Handle *cfg)
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing v6 service\n");
183 GNUNET_SERVER_add_handlers (server, myhandlers);
184 GNUNET_CLIENT_service_test ("test_service6", cfg, GNUNET_TIME_UNIT_SECONDS,
185 &ready6, (void *) cfg);
189 * Main method, starts scheduler with task1,
190 * checks that "ok" is correct at the end.
195 char *const argv[] = {
198 "test_service_data.conf",
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n");
208 GNUNET_assert (GNUNET_OK ==
209 GNUNET_SERVICE_run (5, argv, "test_service6",
210 GNUNET_SERVICE_OPTION_NONE, &runner6,
212 GNUNET_assert (0 == ok);
219 start_stop_main (void *cls, char *const *args, const char *cfgfile,
220 const struct GNUNET_CONFIGURATION_Handle *cfg)
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service using start method\n");
225 sctx = GNUNET_SERVICE_start ("test_service", cfg, GNUNET_SERVICE_OPTION_NONE);
226 GNUNET_assert (NULL != sctx);
227 runner (cls, GNUNET_SERVICE_get_server (sctx), cfg);
235 char *const argv[] = {
236 "test-service-program",
238 "test_service_data.conf",
247 const struct GNUNET_GETOPT_CommandLineOption options[] = {
248 GNUNET_GETOPT_OPTION_END
252 GNUNET_assert (GNUNET_OK ==
253 GNUNET_PROGRAM_run (5, argv, "test-service-program", "no help",
254 options, &start_stop_main, &ret));
256 GNUNET_break (0 == ret);
262 main (int argc, char *argv[])
265 struct GNUNET_NETWORK_Handle *s = NULL;
267 GNUNET_log_setup ("test-service",
279 s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
283 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
286 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
290 "IPv6 support seems to not be available (%s), not testing it!\n",
295 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
298 ret += check_start_stop ();
303 /* end of test_service.c */