aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/test_service.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/test_service.c')
-rw-r--r--src/lib/util/test_service.c239
1 files changed, 239 insertions, 0 deletions
diff --git a/src/lib/util/test_service.c b/src/lib/util/test_service.c
new file mode 100644
index 000000000..198ae68ec
--- /dev/null
+++ b/src/lib/util/test_service.c
@@ -0,0 +1,239 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009, 2013, 2016 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file util/test_service.c
22 * @brief tests for service.c
23 * @author Christian Grothoff
24 */
25
26#include "platform.h"
27#include "gnunet_util_lib.h"
28
29/**
30 * Message type we use for testing.
31 */
32#define MY_TYPE 256
33
34#define TIMEOUT GNUNET_TIME_UNIT_SECONDS
35
36static int global_ret = 1;
37
38static struct GNUNET_MQ_Handle *mq;
39
40/**
41 * Timeout task.
42 */
43static struct GNUNET_SCHEDULER_Task *tt;
44
45
46static void
47handle_recv (void *cls,
48 const struct GNUNET_MessageHeader *message)
49{
50 struct GNUNET_SERVICE_Client *client = cls;
51
52 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
53 "Received client message...\n");
54 GNUNET_SERVICE_client_continue (client);
55 global_ret = 2;
56 if (NULL != mq)
57 {
58 GNUNET_MQ_destroy (mq);
59 mq = NULL;
60 }
61}
62
63
64/**
65 * Function called when the client connects to the service.
66 *
67 * @param cls the name of the service
68 * @param c connecting client
69 * @param mq message queue to talk to the client
70 * @return @a c so we have the client handle in the future
71 */
72static void *
73connect_cb (void *cls,
74 struct GNUNET_SERVICE_Client *c,
75 struct GNUNET_MQ_Handle *mq)
76{
77 /* FIXME: in the future, do something with mq
78 to test sending messages to the client! */
79 return c;
80}
81
82
83/**
84 * Function called when the client disconnects.
85 *
86 * @param cls our service name
87 * @param c disconnecting client
88 * @param internal_cls must match @a c
89 */
90static void
91disconnect_cb (void *cls,
92 struct GNUNET_SERVICE_Client *c,
93 void *internal_cls)
94{
95 GNUNET_assert (c == internal_cls);
96 if (2 == global_ret)
97 {
98 GNUNET_SCHEDULER_shutdown ();
99 global_ret = 0;
100 if (NULL != tt)
101 {
102 GNUNET_SCHEDULER_cancel (tt);
103 tt = NULL;
104 }
105 }
106}
107
108
109static void
110timeout_task (void *cls)
111{
112 tt = NULL;
113 if (NULL != mq)
114 {
115 GNUNET_MQ_destroy (mq);
116 mq = NULL;
117 }
118 global_ret = 33;
119 GNUNET_SCHEDULER_shutdown ();
120}
121
122
123/**
124 * Initialization function of the service. Starts
125 * a client to connect to the service.
126 *
127 * @param cls the name of the service (const char *)
128 * @param cfg the configuration we use
129 * @param sh handle to the service
130 */
131static void
132service_init (void *cls,
133 const struct GNUNET_CONFIGURATION_Handle *cfg,
134 struct GNUNET_SERVICE_Handle *sh)
135{
136 const char *service_name = cls;
137 struct GNUNET_MQ_Envelope *env;
138 struct GNUNET_MessageHeader *msg;
139
140 GNUNET_assert (NULL == tt);
141 tt = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
142 &timeout_task,
143 NULL);
144 mq = GNUNET_CLIENT_connect (cfg,
145 service_name,
146 NULL,
147 NULL,
148 NULL);
149 GNUNET_assert (NULL != mq);
150 env = GNUNET_MQ_msg (msg,
151 MY_TYPE);
152 GNUNET_MQ_send (mq,
153 env);
154}
155
156
157/**
158 * Main method, starts the service and initiates
159 * the running of the test.
160 *
161 * @param sname name of the service to run
162 */
163static int
164check (const char *sname)
165{
166 struct GNUNET_MQ_MessageHandler myhandlers[] = {
167 GNUNET_MQ_hd_fixed_size (recv,
168 MY_TYPE,
169 struct GNUNET_MessageHeader,
170 NULL),
171 GNUNET_MQ_handler_end ()
172 };
173 char *const argv[] = {
174 (char *) sname,
175 "-c",
176 "test_service_data.conf",
177 NULL
178 };
179
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 "Starting `%s' service\n",
182 sname);
183 global_ret = 1;
184 GNUNET_assert (0 ==
185 GNUNET_SERVICE_run_ (3,
186 argv,
187 sname,
188 GNUNET_SERVICE_OPTION_NONE,
189 &service_init,
190 &connect_cb,
191 &disconnect_cb,
192 (void *) sname,
193 myhandlers));
194 return global_ret;
195}
196
197
198int
199main (int argc,
200 char *argv[])
201{
202 int ret = 0;
203 struct GNUNET_NETWORK_Handle *s = NULL;
204
205 GNUNET_log_setup ("test-service",
206 "WARNING",
207 NULL);
208 ret += check ("test_service");
209 ret += check ("test_service");
210 s = GNUNET_NETWORK_socket_create (PF_INET6,
211 SOCK_STREAM,
212 0);
213
214 if (NULL == s)
215 {
216 if ((errno == ENOBUFS) ||
217 (errno == ENOMEM) ||
218 (errno == ENFILE) ||
219 (errno == EACCES))
220 {
221 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
222 "socket");
223 return 1;
224 }
225 fprintf (stderr,
226 "IPv6 support seems to not be available (%s), not testing it!\n",
227 strerror (errno));
228 }
229 else
230 {
231 GNUNET_break (GNUNET_OK ==
232 GNUNET_NETWORK_socket_close (s));
233 ret += check ("test_service6");
234 }
235 return ret;
236}
237
238
239/* end of test_service.c */