aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_server.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_server.c')
-rw-r--r--src/util/test_server.c302
1 files changed, 0 insertions, 302 deletions
diff --git a/src/util/test_server.c b/src/util/test_server.c
deleted file mode 100644
index 8003adbf4..000000000
--- a/src/util/test_server.c
+++ /dev/null
@@ -1,302 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2014, 2016 GNUnet e.V.
4
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.
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 General Public License for more details.
14
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.
19*/
20/**
21 * @file util/test_server.c
22 * @brief tests for server.c
23 */
24#include "platform.h"
25#include "gnunet_util_lib.h"
26
27/**
28 * TCP port to use for the server.
29 */
30#define PORT 12435
31
32/**
33 * Timeout to use for operations.
34 */
35#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
36
37/**
38 * Test message type.
39 */
40#define MY_TYPE 128
41
42/**
43 * Test message type.
44 */
45#define MY_TYPE2 129
46
47/**
48 * Handle for the server.
49 */
50static struct GNUNET_SERVER_Handle *server;
51
52/**
53 * Handle for the client.
54 */
55static struct GNUNET_MQ_Handle *mq;
56
57/**
58 * Handle of the server for the client.
59 */
60static struct GNUNET_SERVER_Client *argclient;
61
62/**
63 * Our configuration.
64 */
65static struct GNUNET_CONFIGURATION_Handle *cfg;
66
67/**
68 * Number indiciating in which phase of the test we are.
69 */
70static int ok;
71
72
73/**
74 * Final task invoked to clean up.
75 *
76 * @param cls NULL
77 */
78static void
79finish_up (void *cls)
80{
81 GNUNET_assert (7 == ok);
82 ok = 0;
83 GNUNET_SERVER_destroy (server);
84 GNUNET_MQ_destroy (mq);
85 GNUNET_CONFIGURATION_destroy (cfg);
86}
87
88
89/**
90 * The server has received the second message, initiate clean up.
91 *
92 * @param cls NULL
93 * @param client client we got the message from
94 * @param message the message
95 */
96static void
97recv_fin_cb (void *cls,
98 struct GNUNET_SERVER_Client *client,
99 const struct GNUNET_MessageHeader *message)
100{
101 GNUNET_assert (6 == ok);
102 ok = 7;
103 GNUNET_SERVER_receive_done (client, GNUNET_OK);
104 GNUNET_SCHEDULER_add_now (&finish_up, NULL);
105}
106
107
108/**
109 * We have received the reply from the server, check that we are at
110 * the right stage and queue the next message to the server. Cleans
111 * up #argclient.
112 *
113 * @param cls NULL
114 * @param msg message we got from the server
115 */
116static void
117handle_reply (void *cls,
118 const struct GNUNET_MessageHeader *msg)
119{
120 struct GNUNET_MQ_Envelope *env;
121 struct GNUNET_MessageHeader *m;
122
123 GNUNET_assert (4 == ok);
124 ok = 6;
125 env = GNUNET_MQ_msg (m,
126 MY_TYPE2);
127 GNUNET_MQ_send (mq,
128 env);
129}
130
131
132/**
133 * Send a reply of type #MY_TYPE from the server to the client.
134 * Checks that we are in the right phase and transmits the
135 * reply. Cleans up #argclient state.
136 *
137 * @param cls NULL
138 * @param size number of bytes we are allowed to send
139 * @param buf where to copy the reply
140 * @return number of bytes written to @a buf
141 */
142static size_t
143reply_msg (void *cls,
144 size_t size,
145 void *buf)
146{
147 struct GNUNET_MessageHeader msg;
148
149 GNUNET_assert (3 == ok);
150 ok = 4;
151 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
152 msg.type = htons (MY_TYPE);
153 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
154 GNUNET_memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
155 GNUNET_assert (NULL != argclient);
156 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
157 GNUNET_SERVER_client_drop (argclient);
158 argclient = NULL;
159 return sizeof (struct GNUNET_MessageHeader);
160}
161
162
163/**
164 * Function called whenever the server receives a message of
165 * type #MY_TYPE. Checks that we are at the stage where
166 * we expect the first message, then sends a reply. Stores
167 * the handle to the client in #argclient.
168 *
169 * @param cls NULL
170 * @param client client that sent the message
171 * @param message the message we received
172 */
173static void
174recv_cb (void *cls,
175 struct GNUNET_SERVER_Client *client,
176 const struct GNUNET_MessageHeader *message)
177{
178 GNUNET_assert (2 == ok);
179 ok = 3;
180 argclient = client;
181 GNUNET_SERVER_client_keep (argclient);
182 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
183 GNUNET_assert (MY_TYPE == ntohs (message->type));
184 GNUNET_assert (NULL !=
185 GNUNET_SERVER_notify_transmit_ready (client,
186 ntohs (message->size),
187 TIMEOUT,
188 &reply_msg,
189 NULL));
190}
191
192
193/**
194 * Message handlers for the server.
195 */
196static struct GNUNET_SERVER_MessageHandler handlers[] = {
197 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
198 {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
199 {NULL, NULL, 0, 0}
200};
201
202
203/**
204 * Generic error handler, called with the appropriate error code and
205 * the same closure specified at the creation of the message queue.
206 * Not every message queue implementation supports an error handler.
207 *
208 * @param cls closure with the `struct GNUNET_STATISTICS_Handle *`
209 * @param error error code
210 */
211static void
212mq_error_handler (void *cls,
213 enum GNUNET_MQ_Error error)
214{
215 GNUNET_assert (0); /* should never happen */
216}
217
218
219/**
220 * First task run by the scheduler. Initializes the server and
221 * a client and asks for a transmission from the client to the
222 * server.
223 *
224 * @param cls NULL
225 */
226static void
227task (void *cls)
228{
229 struct sockaddr_in sa;
230 struct sockaddr *sap[2];
231 socklen_t slens[2];
232 struct GNUNET_MQ_Envelope *env;
233 struct GNUNET_MessageHeader *msg;
234 struct GNUNET_MQ_MessageHandler chandlers[] = {
235 GNUNET_MQ_hd_fixed_size (reply,
236 MY_TYPE,
237 struct GNUNET_MessageHeader,
238 cls),
239 GNUNET_MQ_handler_end ()
240 };
241
242 sap[0] = (struct sockaddr *) &sa;
243 slens[0] = sizeof (sa);
244 sap[1] = NULL;
245 slens[1] = 0;
246 memset (&sa, 0, sizeof (sa));
247#if HAVE_SOCKADDR_IN_SIN_LEN
248 sa.sin_len = sizeof (sa);
249#endif
250 sa.sin_family = AF_INET;
251 sa.sin_port = htons (PORT);
252 server = GNUNET_SERVER_create (NULL, NULL,
253 sap, slens,
254 TIMEOUT, GNUNET_NO);
255 GNUNET_assert (server != NULL);
256 GNUNET_SERVER_add_handlers (server, handlers);
257 cfg = GNUNET_CONFIGURATION_create ();
258 GNUNET_CONFIGURATION_set_value_number (cfg,
259 "test-server",
260 "PORT",
261 PORT);
262 GNUNET_CONFIGURATION_set_value_string (cfg,
263 "test-server",
264 "HOSTNAME",
265 "localhost");
266 GNUNET_CONFIGURATION_set_value_string (cfg,
267 "resolver",
268 "HOSTNAME",
269 "localhost");
270 mq = GNUNET_CLIENT_connect (cfg,
271 "test-server",
272 chandlers,
273 &mq_error_handler,
274 NULL);
275 GNUNET_assert (NULL != mq);
276 ok = 2;
277 env = GNUNET_MQ_msg (msg,
278 MY_TYPE);
279 GNUNET_MQ_send (mq,
280 env);
281}
282
283
284/**
285 * Runs the test.
286 *
287 * @param argc length of @a argv
288 * @param argv command line arguments (ignored)
289 * @return 0 on success, otherwise phase of failure
290 */
291int
292main (int argc, char *argv[])
293{
294 GNUNET_log_setup ("test_server",
295 "WARNING",
296 NULL);
297 ok = 1;
298 GNUNET_SCHEDULER_run (&task, &ok);
299 return ok;
300}
301
302/* end of test_server.c */