2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2014, 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_server.c
22 * @brief tests for server.c
25 #include "gnunet_util_lib.h"
28 * TCP port to use for the server.
33 * Timeout to use for operations.
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
48 * Handle for the server.
50 static struct GNUNET_SERVER_Handle *server;
53 * Handle for the client.
55 static struct GNUNET_MQ_Handle *mq;
58 * Handle of the server for the client.
60 static struct GNUNET_SERVER_Client *argclient;
65 static struct GNUNET_CONFIGURATION_Handle *cfg;
68 * Number indiciating in which phase of the test we are.
74 * Final task invoked to clean up.
81 GNUNET_assert (7 == ok);
83 GNUNET_SERVER_destroy (server);
84 GNUNET_MQ_destroy (mq);
85 GNUNET_CONFIGURATION_destroy (cfg);
90 * The server has received the second message, initiate clean up.
93 * @param client client we got the message from
94 * @param message the message
97 recv_fin_cb (void *cls,
98 struct GNUNET_SERVER_Client *client,
99 const struct GNUNET_MessageHeader *message)
101 GNUNET_assert (6 == ok);
103 GNUNET_SERVER_receive_done (client, GNUNET_OK);
104 GNUNET_SCHEDULER_add_now (&finish_up, NULL);
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
114 * @param msg message we got from the server
117 handle_reply (void *cls,
118 const struct GNUNET_MessageHeader *msg)
120 struct GNUNET_MQ_Envelope *env;
121 struct GNUNET_MessageHeader *m;
123 GNUNET_assert (4 == ok);
125 env = GNUNET_MQ_msg (m,
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.
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
143 reply_msg (void *cls,
147 struct GNUNET_MessageHeader msg;
149 GNUNET_assert (3 == ok);
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);
159 return sizeof (struct GNUNET_MessageHeader);
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.
170 * @param client client that sent the message
171 * @param message the message we received
175 struct GNUNET_SERVER_Client *client,
176 const struct GNUNET_MessageHeader *message)
178 GNUNET_assert (2 == ok);
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),
194 * Message handlers for the server.
196 static 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)},
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.
208 * @param cls closure with the `struct GNUNET_STATISTICS_Handle *`
209 * @param error error code
212 mq_error_handler (void *cls,
213 enum GNUNET_MQ_Error error)
215 GNUNET_assert (0); /* should never happen */
220 * First task run by the scheduler. Initializes the server and
221 * a client and asks for a transmission from the client to the
229 struct sockaddr_in sa;
230 struct sockaddr *sap[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,
237 struct GNUNET_MessageHeader,
239 GNUNET_MQ_handler_end ()
242 sap[0] = (struct sockaddr *) &sa;
243 slens[0] = sizeof (sa);
246 memset (&sa, 0, sizeof (sa));
247 #if HAVE_SOCKADDR_IN_SIN_LEN
248 sa.sin_len = sizeof (sa);
250 sa.sin_family = AF_INET;
251 sa.sin_port = htons (PORT);
252 server = GNUNET_SERVER_create (NULL, NULL,
255 GNUNET_assert (server != NULL);
256 GNUNET_SERVER_add_handlers (server, handlers);
257 cfg = GNUNET_CONFIGURATION_create ();
258 GNUNET_CONFIGURATION_set_value_number (cfg,
262 GNUNET_CONFIGURATION_set_value_string (cfg,
266 GNUNET_CONFIGURATION_set_value_string (cfg,
270 mq = GNUNET_CLIENT_connect (cfg,
275 GNUNET_assert (NULL != mq);
277 env = GNUNET_MQ_msg (msg,
287 * @param argc length of @a argv
288 * @param argv command line arguments (ignored)
289 * @return 0 on success, otherwise phase of failure
292 main (int argc, char *argv[])
294 GNUNET_log_setup ("test_server",
298 GNUNET_SCHEDULER_run (&task, &ok);
302 /* end of test_server.c */