2 This file is part of GNUnet.
3 (C) 2009, 2010, 2014 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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.
77 * @param tc scheduler context
81 const struct GNUNET_SCHEDULER_TaskContext *tc)
83 GNUNET_assert (7 == ok);
85 GNUNET_SERVER_destroy (server);
86 GNUNET_CLIENT_disconnect (cc);
87 GNUNET_CONFIGURATION_destroy (cfg);
92 * The server has received the second message, initiate clean up.
95 * @param client client we got the message from
96 * @param message the message
99 recv_fin_cb (void *cls,
100 struct GNUNET_SERVER_Client *client,
101 const struct GNUNET_MessageHeader *message)
103 GNUNET_assert (6 == ok);
105 GNUNET_SERVER_receive_done (client, GNUNET_OK);
106 GNUNET_SCHEDULER_add_now (&finish_up, NULL);
111 * The client connected to the server and is now allowed
112 * to send a second message. We send one.
115 * @param size number of bytes that can be transmitted
116 * @param buf where to copy the message
117 * @return number of bytes copied to @a buf
120 transmit_second_message (void *cls,
124 struct GNUNET_MessageHeader msg;
126 GNUNET_assert (5 == ok);
128 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
129 msg.type = htons (MY_TYPE2);
130 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
131 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
132 return sizeof (struct GNUNET_MessageHeader);
137 * We have received the reply from the server, check that we are at
138 * the right stage and queue the next message to the server. Cleans
142 * @param msg message we got from the server
145 first_reply_handler (void *cls,
146 const struct GNUNET_MessageHeader *msg)
148 GNUNET_assert (4 == ok);
150 GNUNET_assert (NULL !=
151 GNUNET_CLIENT_notify_transmit_ready (cc,
152 sizeof (struct GNUNET_MessageHeader),
155 &transmit_second_message,
161 * Send a reply of type #MY_TYPE from the server to the client.
162 * Checks that we are in the right phase and transmits the
163 * reply. Cleans up #argclient state.
166 * @param size number of bytes we are allowed to send
167 * @param buf where to copy the reply
168 * @return number of bytes written to @a buf
171 reply_msg (void *cls,
175 struct GNUNET_MessageHeader msg;
177 GNUNET_assert (3 == ok);
179 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
180 msg.type = htons (MY_TYPE);
181 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
182 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
183 GNUNET_assert (NULL != argclient);
184 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
185 GNUNET_SERVER_client_drop (argclient);
187 return sizeof (struct GNUNET_MessageHeader);
192 * Function called whenever the server receives a message of
193 * type #MY_TYPE. Checks that we are at the stage where
194 * we expect the first message, then sends a reply. Stores
195 * the handle to the client in #argclient.
198 * @param client client that sent the message
199 * @param message the message we received
203 struct GNUNET_SERVER_Client *client,
204 const struct GNUNET_MessageHeader *message)
206 GNUNET_assert (2 == ok);
209 GNUNET_SERVER_client_keep (argclient);
210 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
211 GNUNET_assert (MY_TYPE == ntohs (message->type));
212 GNUNET_assert (NULL !=
213 GNUNET_SERVER_notify_transmit_ready (client,
214 ntohs (message->size),
221 * The client connected to the server and is now allowed
222 * to send a first message. We transmit a simple message,
223 * ask for a second transmission and get ready to receive
227 * @param size number of bytes that can be transmitted
228 * @param buf where to copy the message
229 * @return number of bytes copied to @a buf
232 transmit_initial_message (void *cls,
236 struct GNUNET_MessageHeader msg;
238 GNUNET_assert (1 == ok);
240 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
241 msg.type = htons (MY_TYPE);
242 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
243 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
244 GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT);
245 return sizeof (struct GNUNET_MessageHeader);
250 * Message handlers for the server.
252 static struct GNUNET_SERVER_MessageHandler handlers[] = {
253 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
254 {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
260 * First task run by the scheduler. Initializes the server and
261 * a client and asks for a transmission from the client to the
265 * @param tc scheduler context
269 const struct GNUNET_SCHEDULER_TaskContext *tc)
271 struct sockaddr_in sa;
272 struct sockaddr *sap[2];
275 sap[0] = (struct sockaddr *) &sa;
276 slens[0] = sizeof (sa);
279 memset (&sa, 0, sizeof (sa));
280 #if HAVE_SOCKADDR_IN_SIN_LEN
281 sa.sin_len = sizeof (sa);
283 sa.sin_family = AF_INET;
284 sa.sin_port = htons (PORT);
285 server = GNUNET_SERVER_create (NULL, NULL,
288 GNUNET_assert (server != NULL);
289 GNUNET_SERVER_add_handlers (server, handlers);
290 cfg = GNUNET_CONFIGURATION_create ();
291 GNUNET_CONFIGURATION_set_value_number (cfg,
295 GNUNET_CONFIGURATION_set_value_string (cfg,
299 GNUNET_CONFIGURATION_set_value_string (cfg,
303 cc = GNUNET_CLIENT_connect ("test-server", cfg);
304 GNUNET_assert (cc != NULL);
305 GNUNET_assert (NULL !=
306 GNUNET_CLIENT_notify_transmit_ready (cc,
308 GNUNET_MessageHeader),
310 &transmit_initial_message,
318 * @param argc length of @a argv
319 * @param argv command line arguments (ignored)
320 * @return 0 on success, otherwise phase of failure
323 main (int argc, char *argv[])
325 GNUNET_log_setup ("test_server",
329 GNUNET_SCHEDULER_run (&task, &ok);
333 /* end of test_server.c */