2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2014 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_CLIENT_Connection *cc;
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_CLIENT_disconnect (cc);
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 * The client connected to the server and is now allowed
110 * to send a second message. We send one.
113 * @param size number of bytes that can be transmitted
114 * @param buf where to copy the message
115 * @return number of bytes copied to @a buf
118 transmit_second_message (void *cls,
122 struct GNUNET_MessageHeader msg;
124 GNUNET_assert (5 == ok);
126 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
127 msg.type = htons (MY_TYPE2);
128 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
129 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
130 return sizeof (struct GNUNET_MessageHeader);
135 * We have received the reply from the server, check that we are at
136 * the right stage and queue the next message to the server. Cleans
140 * @param msg message we got from the server
143 first_reply_handler (void *cls,
144 const struct GNUNET_MessageHeader *msg)
146 GNUNET_assert (4 == ok);
148 GNUNET_assert (NULL !=
149 GNUNET_CLIENT_notify_transmit_ready (cc,
150 sizeof (struct GNUNET_MessageHeader),
153 &transmit_second_message,
159 * Send a reply of type #MY_TYPE from the server to the client.
160 * Checks that we are in the right phase and transmits the
161 * reply. Cleans up #argclient state.
164 * @param size number of bytes we are allowed to send
165 * @param buf where to copy the reply
166 * @return number of bytes written to @a buf
169 reply_msg (void *cls,
173 struct GNUNET_MessageHeader msg;
175 GNUNET_assert (3 == ok);
177 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
178 msg.type = htons (MY_TYPE);
179 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
180 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
181 GNUNET_assert (NULL != argclient);
182 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
183 GNUNET_SERVER_client_drop (argclient);
185 return sizeof (struct GNUNET_MessageHeader);
190 * Function called whenever the server receives a message of
191 * type #MY_TYPE. Checks that we are at the stage where
192 * we expect the first message, then sends a reply. Stores
193 * the handle to the client in #argclient.
196 * @param client client that sent the message
197 * @param message the message we received
201 struct GNUNET_SERVER_Client *client,
202 const struct GNUNET_MessageHeader *message)
204 GNUNET_assert (2 == ok);
207 GNUNET_SERVER_client_keep (argclient);
208 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
209 GNUNET_assert (MY_TYPE == ntohs (message->type));
210 GNUNET_assert (NULL !=
211 GNUNET_SERVER_notify_transmit_ready (client,
212 ntohs (message->size),
219 * The client connected to the server and is now allowed
220 * to send a first message. We transmit a simple message,
221 * ask for a second transmission and get ready to receive
225 * @param size number of bytes that can be transmitted
226 * @param buf where to copy the message
227 * @return number of bytes copied to @a buf
230 transmit_initial_message (void *cls,
234 struct GNUNET_MessageHeader msg;
236 GNUNET_assert (1 == ok);
238 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
239 msg.type = htons (MY_TYPE);
240 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
241 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
242 GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT);
243 return sizeof (struct GNUNET_MessageHeader);
248 * Message handlers for the server.
250 static struct GNUNET_SERVER_MessageHandler handlers[] = {
251 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
252 {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
258 * First task run by the scheduler. Initializes the server and
259 * a client and asks for a transmission from the client to the
267 struct sockaddr_in sa;
268 struct sockaddr *sap[2];
271 sap[0] = (struct sockaddr *) &sa;
272 slens[0] = sizeof (sa);
275 memset (&sa, 0, sizeof (sa));
276 #if HAVE_SOCKADDR_IN_SIN_LEN
277 sa.sin_len = sizeof (sa);
279 sa.sin_family = AF_INET;
280 sa.sin_port = htons (PORT);
281 server = GNUNET_SERVER_create (NULL, NULL,
284 GNUNET_assert (server != NULL);
285 GNUNET_SERVER_add_handlers (server, handlers);
286 cfg = GNUNET_CONFIGURATION_create ();
287 GNUNET_CONFIGURATION_set_value_number (cfg,
291 GNUNET_CONFIGURATION_set_value_string (cfg,
295 GNUNET_CONFIGURATION_set_value_string (cfg,
299 cc = GNUNET_CLIENT_connect ("test-server", cfg);
300 GNUNET_assert (cc != NULL);
301 GNUNET_assert (NULL !=
302 GNUNET_CLIENT_notify_transmit_ready (cc,
304 GNUNET_MessageHeader),
306 &transmit_initial_message,
314 * @param argc length of @a argv
315 * @param argv command line arguments (ignored)
316 * @return 0 on success, otherwise phase of failure
319 main (int argc, char *argv[])
321 GNUNET_log_setup ("test_server",
325 GNUNET_SCHEDULER_run (&task, &ok);
329 /* end of test_server.c */