2 This file is part of GNUnet.
3 (C) 2009 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 2, 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_common.h"
26 #include "gnunet_scheduler_lib.h"
27 #include "gnunet_server_lib.h"
28 #include "gnunet_time_lib.h"
30 #define VERBOSE GNUNET_NO
37 static struct GNUNET_SERVER_Handle *server;
39 static struct GNUNET_SCHEDULER_Handle *sched;
42 recv_fin_cb (void *cls,
43 struct GNUNET_SERVER_Client *client,
44 const struct GNUNET_MessageHeader *message)
47 GNUNET_assert (2 == *ok);
48 GNUNET_SERVER_receive_done (client, GNUNET_OK);
52 struct SignalTimeoutContext
54 GNUNET_CONNECTION_Receiver cb;
60 signal_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
62 struct SignalTimeoutContext *stctx = cls;
64 stctx->cb (stctx->cb_cls, NULL, 0, NULL, 0, 0);
69 static GNUNET_SCHEDULER_TaskIdentifier ti;
73 my_receive (void *cls,
75 struct GNUNET_TIME_Relative timeout,
76 GNUNET_CONNECTION_Receiver receiver, void *receiver_cls)
79 struct GNUNET_MessageHeader msg;
80 struct SignalTimeoutContext *stctx;
81 GNUNET_SCHEDULER_TaskIdentifier ret;
83 ret = GNUNET_SCHEDULER_NO_TASK;
87 *ok = 2; /* report success */
88 msg.type = htons (MY_TYPE2);
89 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
90 receiver (receiver_cls, &msg, sizeof (struct GNUNET_MessageHeader), NULL, 0, 0);
93 /* called after first receive instantly
95 schedule receiver call with timeout
96 after timeout expires! */
98 stctx = GNUNET_malloc (sizeof (struct SignalTimeoutContext));
100 stctx->cb_cls = receiver_cls;
101 ret = GNUNET_SCHEDULER_add_delayed (sched,
103 GNUNET_SCHEDULER_PRIORITY_KEEP,
104 GNUNET_SCHEDULER_NO_TASK,
105 timeout, &signal_timeout, stctx);
115 my_cancel (void *cls)
117 GNUNET_SCHEDULER_cancel (sched, ti);
121 my_transmit_ready_cb (void *cls,
123 struct GNUNET_TIME_Relative timeout,
124 GNUNET_CONNECTION_TransmitReadyNotify notify,
127 static int non_null_addr;
130 struct GNUNET_MessageHeader msg;
132 GNUNET_assert (4 == *ok);
133 GNUNET_assert (size == sizeof (struct GNUNET_MessageHeader));
134 notify (notify_cls, size, buf);
135 msg.type = htons (MY_TYPE);
136 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
137 GNUNET_assert (0 == memcmp (&msg, buf, size));
138 *ok = 5; /* report success */
139 return &non_null_addr;
144 my_transmit_ready_cancel_cb (void *cls, void *ctx)
157 static void my_destroy (void *cls);
162 struct GNUNET_SERVER_Client *client;
163 struct GNUNET_MessageHeader *cpy;
167 copy_msg (void *cls, size_t size, void *buf)
169 struct CopyContext *ctx = cls;
170 struct GNUNET_MessageHeader *cpy = ctx->cpy;
171 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (cpy->size));
172 GNUNET_assert (size >= ntohs (cpy->size));
173 memcpy (buf, cpy, ntohs (cpy->size));
176 return sizeof (struct GNUNET_MessageHeader);
182 struct GNUNET_SERVER_Client *argclient,
183 const struct GNUNET_MessageHeader *message)
185 struct GNUNET_SERVER_Client *client;
186 struct CopyContext *cc;
187 struct GNUNET_MessageHeader *cpy;
189 GNUNET_assert (argclient == NULL);
190 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
191 ntohs (message->size));
192 GNUNET_assert (MY_TYPE == ntohs (message->type));
193 client = GNUNET_SERVER_connect_callback (server,
197 &my_transmit_ready_cb,
198 &my_transmit_ready_cancel_cb,
199 &my_check, &my_destroy);
200 cc = GNUNET_malloc (sizeof (struct CopyContext));
202 cpy = GNUNET_malloc (ntohs (message->size));
203 memcpy (cpy, message, ntohs (message->size));
205 GNUNET_assert (NULL !=
206 GNUNET_SERVER_notify_transmit_ready (client,
207 ntohs (message->size),
208 GNUNET_TIME_UNIT_SECONDS,
210 GNUNET_SERVER_client_drop (client);
214 static struct GNUNET_SERVER_MessageHandler handlers[] = {
215 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
216 {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
222 my_destroy (void *cls)
225 GNUNET_assert (5 == *ok);
226 *ok = 0; /* report success */
227 /* this will cause us to terminate */
228 GNUNET_SERVER_destroy (server);
233 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
235 struct sockaddr_in sa;
236 struct GNUNET_MessageHeader msg;
239 memset (&sa, 0, sizeof (sa));
240 #if HAVE_SOCKADDR_IN_SIN_LEN
241 sa.sin_len = sizeof (sa);
243 sa.sin_family = AF_INET;
244 sa.sin_port = htons (PORT);
245 server = GNUNET_SERVER_create (tc->sched,
248 (const struct sockaddr *) &sa,
251 GNUNET_TIME_relative_multiply
252 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
254 GNUNET_assert (server != NULL);
255 handlers[0].callback_cls = cls;
256 handlers[1].callback_cls = cls;
257 GNUNET_SERVER_add_handlers (server, handlers);
258 msg.type = htons (MY_TYPE);
259 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
260 GNUNET_SERVER_inject (server, NULL, &msg);
261 memset (&msg, 0, sizeof (struct GNUNET_MessageHeader));
266 * Main method, starts scheduler with task1,
267 * checks that "ok" is correct at the end.
275 GNUNET_SCHEDULER_run (&task, &ok);
281 main (int argc, char *argv[])
285 GNUNET_log_setup ("test_server", "WARNING", NULL);
291 /* end of test_server.c */