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_disconnect.c
22 * @brief tests for server.c and client.c,
23 * specifically client_disconnect
26 #include "gnunet_common.h"
27 #include "gnunet_scheduler_lib.h"
28 #include "gnunet_client_lib.h"
29 #include "gnunet_server_lib.h"
30 #include "gnunet_time_lib.h"
32 #define VERBOSE GNUNET_NO
39 static struct GNUNET_SERVER_Handle *server;
41 static struct GNUNET_CLIENT_Connection *client;
43 static struct GNUNET_SCHEDULER_Handle *sched;
45 static struct GNUNET_CONFIGURATION_Handle *cfg;
50 send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
52 struct GNUNET_SERVER_Client *argclient = cls;
53 GNUNET_assert (ok == 3);
55 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
59 server_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
61 struct GNUNET_SERVER_Client *argclient = cls;
62 GNUNET_assert (ok == 5);
64 GNUNET_SERVER_client_disconnect (argclient);
70 struct GNUNET_SERVER_Client *argclient,
71 const struct GNUNET_MessageHeader *message)
75 struct sockaddr_in sa;
76 struct sockaddr_in *have;
78 GNUNET_assert (GNUNET_OK ==
79 GNUNET_SERVER_client_get_address (argclient,
82 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
84 memset (&sa, 0, sizeof (sa));
85 #if HAVE_SOCKADDR_IN_SIN_LEN
86 sa.sin_len = sizeof (sa);
88 sa.sin_family = AF_INET;
89 sa.sin_port = have->sin_port;
90 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
91 GNUNET_assert (0 == memcmp (&sa, addr, addrlen));
97 GNUNET_SCHEDULER_add_delayed (sched,
98 GNUNET_TIME_relative_multiply
99 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
100 &send_done, argclient);
104 GNUNET_SCHEDULER_add_delayed (sched,
105 GNUNET_TIME_relative_multiply
106 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
107 &server_disconnect, argclient);
108 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
117 disconnect_notify (void *cls, const struct GNUNET_MessageHeader *msg)
119 GNUNET_assert (msg == NULL);
120 GNUNET_assert (ok == 7);
122 GNUNET_CLIENT_disconnect (client, GNUNET_YES);
124 GNUNET_SERVER_destroy (server);
126 GNUNET_CONFIGURATION_destroy (cfg);
132 * Functions with this signature are called whenever a client
133 * is disconnected on the network level.
136 * @param client identification of the client
139 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *clientarg)
141 if (clientarg == NULL)
143 GNUNET_assert (ok == 6);
145 GNUNET_CLIENT_receive (client,
147 NULL, GNUNET_TIME_UNIT_FOREVER_REL);
152 notify_ready (void *cls, size_t size, void *buf)
154 struct GNUNET_MessageHeader *msg;
156 GNUNET_assert (size >= 256);
157 GNUNET_assert (1 == ok);
160 msg->type = htons (MY_TYPE);
161 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
163 msg->type = htons (MY_TYPE);
164 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
165 return 2 * sizeof (struct GNUNET_MessageHeader);
169 static struct GNUNET_SERVER_MessageHandler handlers[] = {
170 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
176 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
178 struct sockaddr_in sa;
179 struct sockaddr * sap[2];
182 sap[0] = (struct sockaddr*) &sa;
183 slens[0] = sizeof (sa);
187 memset (&sa, 0, sizeof (sa));
188 #if HAVE_SOCKADDR_IN_SIN_LEN
189 sa.sin_len = sizeof (sa);
191 sa.sin_family = AF_INET;
192 sa.sin_port = htons (PORT);
193 server = GNUNET_SERVER_create (tc->sched,
199 GNUNET_TIME_relative_multiply
200 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
202 GNUNET_assert (server != NULL);
203 handlers[0].callback_cls = cls;
204 GNUNET_SERVER_add_handlers (server, handlers);
205 GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, cls);
206 cfg = GNUNET_CONFIGURATION_create ();
207 GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
208 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME",
210 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
212 client = GNUNET_CLIENT_connect (tc->sched, "test", cfg);
213 GNUNET_assert (client != NULL);
214 GNUNET_CLIENT_notify_transmit_ready (client,
216 GNUNET_TIME_relative_multiply
217 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
218 GNUNET_NO, ¬ify_ready, NULL);
223 * Main method, starts scheduler with task1,
224 * checks that "ok" is correct at the end.
231 GNUNET_SCHEDULER_run (&task, NULL);
237 main (int argc, char *argv[])
241 GNUNET_log_setup ("test_server_disconnect",
253 /* end of test_server_disconnect.c */