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 3, 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_with_client.c
22 * @brief tests for server.c and client.c,
23 * specifically disconnect_notify,
24 * client_get_address and receive_done (resume processing)
27 #include "gnunet_common.h"
28 #include "gnunet_scheduler_lib.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_server_lib.h"
31 #include "gnunet_time_lib.h"
33 #define VERBOSE GNUNET_EXTRA_LOGGING
40 static struct GNUNET_SERVER_Handle *server;
42 static struct GNUNET_CLIENT_Connection *client;
44 static struct GNUNET_CONFIGURATION_Handle *cfg;
49 send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
51 struct GNUNET_SERVER_Client *argclient = cls;
53 GNUNET_assert (ok == 3);
55 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
60 recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient,
61 const struct GNUNET_MessageHeader *message)
65 struct sockaddr_in sa;
66 struct sockaddr_in *have;
68 GNUNET_assert (GNUNET_OK ==
69 GNUNET_SERVER_client_get_address (argclient, &addr, &addrlen));
71 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
73 memset (&sa, 0, sizeof (sa));
74 #if HAVE_SOCKADDR_IN_SIN_LEN
75 sa.sin_len = sizeof (sa);
77 sa.sin_family = AF_INET;
78 sa.sin_port = have->sin_port;
79 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
80 GNUNET_assert (0 == memcmp (&sa, addr, addrlen));
86 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
87 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
88 &send_done, argclient);
92 GNUNET_CLIENT_disconnect (client, GNUNET_YES);
93 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
103 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
105 GNUNET_SERVER_destroy (server);
107 GNUNET_CONFIGURATION_destroy (cfg);
113 * Functions with this signature are called whenever a client
114 * is disconnected on the network level.
117 * @param client identification of the client
120 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
124 GNUNET_assert (ok == 5);
126 GNUNET_SCHEDULER_add_now (&clean_up, NULL);
131 notify_ready (void *cls, size_t size, void *buf)
133 struct GNUNET_MessageHeader *msg;
135 GNUNET_assert (size >= 256);
136 GNUNET_assert (1 == ok);
139 msg->type = htons (MY_TYPE);
140 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
142 msg->type = htons (MY_TYPE);
143 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
144 return 2 * sizeof (struct GNUNET_MessageHeader);
148 static struct GNUNET_SERVER_MessageHandler handlers[] = {
149 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
155 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 struct sockaddr_in sa;
158 struct sockaddr *sap[2];
161 sap[0] = (struct sockaddr *) &sa;
162 slens[0] = sizeof (sa);
165 memset (&sa, 0, sizeof (sa));
166 #if HAVE_SOCKADDR_IN_SIN_LEN
167 sa.sin_len = sizeof (sa);
169 sa.sin_family = AF_INET;
170 sa.sin_port = htons (PORT);
172 GNUNET_SERVER_create (NULL, NULL, sap, slens,
173 GNUNET_TIME_relative_multiply
174 (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
175 GNUNET_assert (server != NULL);
176 handlers[0].callback_cls = cls;
177 GNUNET_SERVER_add_handlers (server, handlers);
178 GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, cls);
179 cfg = GNUNET_CONFIGURATION_create ();
180 GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
181 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", "localhost");
182 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
184 client = GNUNET_CLIENT_connect ("test", cfg);
185 GNUNET_assert (client != NULL);
186 GNUNET_CLIENT_notify_transmit_ready (client, 256,
187 GNUNET_TIME_relative_multiply
188 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
189 GNUNET_NO, ¬ify_ready, NULL);
194 * Main method, starts scheduler with task1,
195 * checks that "ok" is correct at the end.
202 GNUNET_SCHEDULER_run (&task, NULL);
208 main (int argc, char *argv[])
212 GNUNET_log_setup ("test_server_with_client",
224 /* end of test_server_with_client.c */