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_Handle *server,
44 struct GNUNET_SERVER_Client *client,
45 const struct GNUNET_MessageHeader *message)
48 GNUNET_assert (2 == *ok);
49 GNUNET_SERVER_receive_done (client, GNUNET_OK);
53 struct SignalTimeoutContext
55 GNUNET_NETWORK_Receiver cb;
61 signal_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
63 struct SignalTimeoutContext *stctx = cls;
65 stctx->cb (stctx->cb_cls, NULL, 0, NULL, 0, 0);
70 static GNUNET_SCHEDULER_TaskIdentifier
71 my_receive (void *cls,
73 struct GNUNET_TIME_Relative timeout,
74 GNUNET_NETWORK_Receiver receiver, void *receiver_cls)
77 struct GNUNET_MessageHeader msg;
78 struct SignalTimeoutContext *stctx;
79 GNUNET_SCHEDULER_TaskIdentifier ret;
81 ret = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
85 *ok = 2; /* report success */
86 msg.type = htons (MY_TYPE2);
87 msg.size = htons (sizeof (msg));
88 receiver (receiver_cls, &msg, sizeof (msg), NULL, 0, 0);
91 /* called after first receive instantly
93 schedule receiver call with timeout
94 after timeout expires! */
96 stctx = GNUNET_malloc (sizeof (struct SignalTimeoutContext));
98 stctx->cb_cls = receiver_cls;
99 ret = GNUNET_SCHEDULER_add_delayed (sched,
101 GNUNET_SCHEDULER_PRIORITY_KEEP,
102 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
103 timeout, &signal_timeout, stctx);
113 my_cancel (void *cls, GNUNET_SCHEDULER_TaskIdentifier ti)
115 GNUNET_SCHEDULER_cancel (sched, ti);
119 my_transmit_ready_cb (void *cls,
121 struct GNUNET_TIME_Relative timeout,
122 GNUNET_NETWORK_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 (msg));
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_Handle *server,
181 struct GNUNET_SERVER_Client *argclient,
182 const struct GNUNET_MessageHeader *message)
184 struct GNUNET_SERVER_Client *client;
185 struct CopyContext *cc;
186 struct GNUNET_MessageHeader *cpy;
188 GNUNET_assert (argclient == NULL);
189 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
190 ntohs (message->size));
191 GNUNET_assert (MY_TYPE == ntohs (message->type));
192 client = GNUNET_SERVER_connect_callback (server,
196 &my_transmit_ready_cb,
197 &my_transmit_ready_cancel_cb,
198 &my_check, &my_destroy);
199 cc = GNUNET_malloc (sizeof (struct CopyContext));
201 cpy = GNUNET_malloc (ntohs (message->size));
202 memcpy (cpy, message, ntohs (message->size));
204 GNUNET_assert (NULL !=
205 GNUNET_SERVER_notify_transmit_ready (client,
206 ntohs (message->size),
207 GNUNET_TIME_UNIT_SECONDS,
209 GNUNET_SERVER_client_drop (client);
213 static struct GNUNET_SERVER_MessageHandler handlers[] = {
214 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
215 {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
221 my_destroy (void *cls)
224 GNUNET_assert (5 == *ok);
225 *ok = 0; /* report success */
226 /* this will cause us to terminate */
227 GNUNET_SERVER_destroy (server);
232 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
234 struct sockaddr_in sa;
235 struct GNUNET_MessageHeader msg;
238 memset (&sa, 0, sizeof (sa));
239 sa.sin_family = AF_INET;
240 sa.sin_port = htons (PORT);
241 server = GNUNET_SERVER_create (tc->sched,
244 (const struct sockaddr *) &sa,
247 GNUNET_TIME_relative_multiply
248 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
250 GNUNET_assert (server != NULL);
251 handlers[0].callback_cls = cls;
252 handlers[1].callback_cls = cls;
253 GNUNET_SERVER_add_handlers (server, handlers);
254 msg.type = htons (MY_TYPE);
255 msg.size = htons (sizeof (msg));
256 GNUNET_SERVER_inject (server, NULL, &msg);
257 memset (&msg, 0, sizeof (msg));
262 * Main method, starts scheduler with task1,
263 * checks that "ok" is correct at the end.
271 GNUNET_SCHEDULER_run (&task, &ok);
277 main (int argc, char *argv[])
281 GNUNET_log_setup ("test_server", "WARNING", NULL);
287 /* end of test_server.c */