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),
94 /* called after first receive instantly
96 schedule receiver call with timeout
97 after timeout expires! */
99 stctx = GNUNET_malloc (sizeof (struct SignalTimeoutContext));
100 stctx->cb = receiver;
101 stctx->cb_cls = receiver_cls;
102 ret = GNUNET_SCHEDULER_add_delayed (sched,
103 timeout, &signal_timeout, stctx);
113 my_cancel (void *cls)
115 GNUNET_SCHEDULER_cancel (sched, ti);
119 my_transmit_ready_cb (void *cls,
121 struct GNUNET_TIME_Relative timeout,
122 GNUNET_CONNECTION_TransmitReadyNotify notify,
125 static int non_null_addr;
128 struct GNUNET_MessageHeader msg;
130 GNUNET_assert (4 == *ok);
131 GNUNET_assert (size == sizeof (struct GNUNET_MessageHeader));
132 notify (notify_cls, size, buf);
133 msg.type = htons (MY_TYPE);
134 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
135 GNUNET_assert (0 == memcmp (&msg, buf, size));
136 *ok = 5; /* report success */
137 return &non_null_addr;
142 my_transmit_ready_cancel_cb (void *cls, void *ctx)
155 static void my_destroy (void *cls);
160 struct GNUNET_SERVER_Client *client;
161 struct GNUNET_MessageHeader *cpy;
165 copy_msg (void *cls, size_t size, void *buf)
167 struct CopyContext *ctx = cls;
168 struct GNUNET_MessageHeader *cpy = ctx->cpy;
169 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (cpy->size));
170 GNUNET_assert (size >= ntohs (cpy->size));
171 memcpy (buf, cpy, ntohs (cpy->size));
174 return sizeof (struct GNUNET_MessageHeader);
180 struct GNUNET_SERVER_Client *argclient,
181 const struct GNUNET_MessageHeader *message)
183 struct GNUNET_SERVER_Client *client;
184 struct CopyContext *cc;
185 struct GNUNET_MessageHeader *cpy;
187 GNUNET_assert (argclient == NULL);
188 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
189 ntohs (message->size));
190 GNUNET_assert (MY_TYPE == ntohs (message->type));
191 client = GNUNET_SERVER_connect_callback (server,
195 &my_transmit_ready_cb,
196 &my_transmit_ready_cancel_cb,
197 &my_check, &my_destroy);
198 cc = GNUNET_malloc (sizeof (struct CopyContext));
200 cpy = GNUNET_malloc (ntohs (message->size));
201 memcpy (cpy, message, ntohs (message->size));
203 GNUNET_assert (NULL !=
204 GNUNET_SERVER_notify_transmit_ready (client,
205 ntohs (message->size),
206 GNUNET_TIME_UNIT_SECONDS,
208 GNUNET_SERVER_client_drop (client);
212 static struct GNUNET_SERVER_MessageHandler handlers[] = {
213 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
214 {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
220 my_destroy (void *cls)
223 GNUNET_assert (5 == *ok);
224 *ok = 0; /* report success */
225 /* this will cause us to terminate */
226 GNUNET_SERVER_destroy (server);
231 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
233 struct sockaddr_in sa;
234 struct GNUNET_MessageHeader msg;
237 memset (&sa, 0, sizeof (sa));
238 #if HAVE_SOCKADDR_IN_SIN_LEN
239 sa.sin_len = sizeof (sa);
241 sa.sin_family = AF_INET;
242 sa.sin_port = htons (PORT);
243 server = GNUNET_SERVER_create (tc->sched,
246 (const struct sockaddr *) &sa,
249 GNUNET_TIME_relative_multiply
250 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
252 GNUNET_assert (server != NULL);
253 handlers[0].callback_cls = cls;
254 handlers[1].callback_cls = cls;
255 GNUNET_SERVER_add_handlers (server, handlers);
256 msg.type = htons (MY_TYPE);
257 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
258 GNUNET_SERVER_inject (server, NULL, &msg);
259 memset (&msg, 0, sizeof (struct GNUNET_MessageHeader));
264 * Main method, starts scheduler with task1,
265 * checks that "ok" is correct at the end.
273 GNUNET_SCHEDULER_run (&task, &ok);
279 main (int argc, char *argv[])
283 GNUNET_log_setup ("test_server", "WARNING", NULL);
289 /* end of test_server.c */