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_NETWORK_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
70 my_receive (void *cls,
72 struct GNUNET_TIME_Relative timeout,
73 GNUNET_NETWORK_Receiver receiver, void *receiver_cls)
76 struct GNUNET_MessageHeader msg;
77 struct SignalTimeoutContext *stctx;
78 GNUNET_SCHEDULER_TaskIdentifier ret;
80 ret = GNUNET_SCHEDULER_NO_TASK;
84 *ok = 2; /* report success */
85 msg.type = htons (MY_TYPE2);
86 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
87 receiver (receiver_cls, &msg, sizeof (struct GNUNET_MessageHeader), NULL, 0, 0);
90 /* called after first receive instantly
92 schedule receiver call with timeout
93 after timeout expires! */
95 stctx = GNUNET_malloc (sizeof (struct SignalTimeoutContext));
97 stctx->cb_cls = receiver_cls;
98 ret = GNUNET_SCHEDULER_add_delayed (sched,
100 GNUNET_SCHEDULER_PRIORITY_KEEP,
101 GNUNET_SCHEDULER_NO_TASK,
102 timeout, &signal_timeout, stctx);
112 my_cancel (void *cls, GNUNET_SCHEDULER_TaskIdentifier ti)
114 GNUNET_SCHEDULER_cancel (sched, ti);
118 my_transmit_ready_cb (void *cls,
120 struct GNUNET_TIME_Relative timeout,
121 GNUNET_NETWORK_TransmitReadyNotify notify,
124 static int non_null_addr;
127 struct GNUNET_MessageHeader msg;
129 GNUNET_assert (4 == *ok);
130 GNUNET_assert (size == sizeof (struct GNUNET_MessageHeader));
131 notify (notify_cls, size, buf);
132 msg.type = htons (MY_TYPE);
133 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
134 GNUNET_assert (0 == memcmp (&msg, buf, size));
135 *ok = 5; /* report success */
136 return &non_null_addr;
141 my_transmit_ready_cancel_cb (void *cls, void *ctx)
154 static void my_destroy (void *cls);
159 struct GNUNET_SERVER_Client *client;
160 struct GNUNET_MessageHeader *cpy;
164 copy_msg (void *cls, size_t size, void *buf)
166 struct CopyContext *ctx = cls;
167 struct GNUNET_MessageHeader *cpy = ctx->cpy;
168 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (cpy->size));
169 GNUNET_assert (size >= ntohs (cpy->size));
170 memcpy (buf, cpy, ntohs (cpy->size));
173 return sizeof (struct GNUNET_MessageHeader);
179 struct GNUNET_SERVER_Client *argclient,
180 const struct GNUNET_MessageHeader *message)
182 struct GNUNET_SERVER_Client *client;
183 struct CopyContext *cc;
184 struct GNUNET_MessageHeader *cpy;
186 GNUNET_assert (argclient == NULL);
187 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
188 ntohs (message->size));
189 GNUNET_assert (MY_TYPE == ntohs (message->type));
190 client = GNUNET_SERVER_connect_callback (server,
194 &my_transmit_ready_cb,
195 &my_transmit_ready_cancel_cb,
196 &my_check, &my_destroy);
197 cc = GNUNET_malloc (sizeof (struct CopyContext));
199 cpy = GNUNET_malloc (ntohs (message->size));
200 memcpy (cpy, message, ntohs (message->size));
202 GNUNET_assert (NULL !=
203 GNUNET_SERVER_notify_transmit_ready (client,
204 ntohs (message->size),
205 GNUNET_TIME_UNIT_SECONDS,
207 GNUNET_SERVER_client_drop (client);
211 static struct GNUNET_SERVER_MessageHandler handlers[] = {
212 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
213 {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
219 my_destroy (void *cls)
222 GNUNET_assert (5 == *ok);
223 *ok = 0; /* report success */
224 /* this will cause us to terminate */
225 GNUNET_SERVER_destroy (server);
230 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
232 struct sockaddr_in sa;
233 struct GNUNET_MessageHeader msg;
236 memset (&sa, 0, sizeof (sa));
237 sa.sin_family = AF_INET;
238 sa.sin_port = htons (PORT);
239 server = GNUNET_SERVER_create (tc->sched,
242 (const struct sockaddr *) &sa,
245 GNUNET_TIME_relative_multiply
246 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
248 GNUNET_assert (server != NULL);
249 handlers[0].callback_cls = cls;
250 handlers[1].callback_cls = cls;
251 GNUNET_SERVER_add_handlers (server, handlers);
252 msg.type = htons (MY_TYPE);
253 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
254 GNUNET_SERVER_inject (server, NULL, &msg);
255 memset (&msg, 0, sizeof (struct GNUNET_MessageHeader));
260 * Main method, starts scheduler with task1,
261 * checks that "ok" is correct at the end.
269 GNUNET_SCHEDULER_run (&task, &ok);
275 main (int argc, char *argv[])
279 GNUNET_log_setup ("test_server", "WARNING", NULL);
285 /* end of test_server.c */