2 This file is part of GNUnet.
3 Copyright (C) 2009, 2013, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file util/test_service.c
22 * @brief tests for service.c
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
32 * Message type we use for testing.
36 static struct GNUNET_SERVICE_Context *sctx;
40 static struct GNUNET_MQ_Handle *mq;
48 GNUNET_MQ_destroy (mq);
53 GNUNET_SERVICE_stop (sctx);
58 GNUNET_SCHEDULER_shutdown ();
67 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
68 struct GNUNET_MQ_Envelope *env;
69 struct GNUNET_MessageHeader *msg;
71 GNUNET_assert (GNUNET_YES == result);
72 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
73 "Service confirmed running\n");
74 mq = GNUNET_CLIENT_connecT (cfg,
79 GNUNET_assert (NULL != mq);
80 env = GNUNET_MQ_msg (msg,
89 struct GNUNET_SERVER_Client *sc,
90 const struct GNUNET_MessageHeader *message)
92 GNUNET_assert (NULL != message);
93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
94 GNUNET_SERVER_receive_done (sc, GNUNET_OK);
95 GNUNET_SCHEDULER_add_now (&do_stop, NULL);
100 static struct GNUNET_SERVER_MessageHandler myhandlers[] = {
101 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
108 struct GNUNET_SERVER_Handle *server,
109 const struct GNUNET_CONFIGURATION_Handle *cfg)
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112 "Service initializing\n");
113 GNUNET_SERVER_add_handlers (server,
115 GNUNET_CLIENT_service_test ("test_service", cfg, GNUNET_TIME_UNIT_SECONDS,
116 &ready, (void *) cfg);
121 * Main method, starts scheduler with task1,
122 * checks that "ok" is correct at the end.
128 char *const argv[] = {
131 "test_service_data.conf",
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting service\n");
135 GNUNET_assert (GNUNET_OK ==
136 GNUNET_SERVICE_run (3, argv, "test_service",
137 GNUNET_SERVICE_OPTION_NONE, &runner, &ok));
138 GNUNET_assert (0 == ok);
147 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
148 struct GNUNET_MQ_Envelope *env;
149 struct GNUNET_MessageHeader *msg;
151 GNUNET_assert (GNUNET_YES == result);
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154 mq = GNUNET_CLIENT_connecT (cfg,
159 GNUNET_assert (NULL != mq);
160 env = GNUNET_MQ_msg (msg,
169 struct GNUNET_SERVER_Handle *server,
170 const struct GNUNET_CONFIGURATION_Handle *cfg)
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "Initializing v6 service\n");
174 GNUNET_SERVER_add_handlers (server,
176 GNUNET_CLIENT_service_test ("test_service6", cfg, GNUNET_TIME_UNIT_SECONDS,
177 &ready6, (void *) cfg);
182 * Main method, starts scheduler with task1,
183 * checks that "ok" is correct at the end.
188 char *const argv[] = {
191 "test_service_data.conf",
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting v6 service\n");
195 GNUNET_assert (GNUNET_OK ==
196 GNUNET_SERVICE_run (3, argv, "test_service6",
197 GNUNET_SERVICE_OPTION_NONE, &runner6,
199 GNUNET_assert (0 == ok);
205 start_stop_main (void *cls,
208 const struct GNUNET_CONFIGURATION_Handle *cfg)
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Starting service using start method\n");
214 sctx = GNUNET_SERVICE_start ("test_service",
216 GNUNET_SERVICE_OPTION_NONE);
217 GNUNET_assert (NULL != sctx);
218 runner (cls, GNUNET_SERVICE_get_server (sctx), cfg);
226 char *const argv[] = {
227 "test-service-program",
229 "test_service_data.conf",
232 const struct GNUNET_GETOPT_CommandLineOption options[] = {
233 GNUNET_GETOPT_OPTION_END
237 GNUNET_assert (GNUNET_OK ==
238 GNUNET_PROGRAM_run (3, argv, "test-service-program", "no help",
239 options, &start_stop_main, &ret));
241 GNUNET_break (0 == ret);
247 main (int argc, char *argv[])
250 struct GNUNET_NETWORK_Handle *s = NULL;
252 GNUNET_log_setup ("test-service",
259 s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
263 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
266 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
270 "IPv6 support seems to not be available (%s), not testing it!\n",
275 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
278 ret += check_start_stop ();
282 /* end of test_service.c */