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_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_NO
40 static struct GNUNET_SERVER_Handle *server;
42 static struct GNUNET_CLIENT_Connection *client;
44 static struct GNUNET_SCHEDULER_Handle *sched;
46 static struct GNUNET_CONFIGURATION_Handle *cfg;
51 send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
53 struct GNUNET_SERVER_Client *argclient = cls;
54 GNUNET_assert (ok == 3);
56 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
62 struct GNUNET_SERVER_Client *argclient,
63 const struct GNUNET_MessageHeader *message)
67 struct sockaddr_in sa;
68 struct sockaddr_in *have;
70 GNUNET_assert (GNUNET_OK ==
71 GNUNET_SERVER_client_get_address (argclient,
74 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
76 memset (&sa, 0, sizeof (sa));
77 #if HAVE_SOCKADDR_IN_SIN_LEN
78 sa.sin_len = sizeof (sa);
80 sa.sin_family = AF_INET;
81 sa.sin_port = have->sin_port;
82 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
83 GNUNET_assert (0 == memcmp (&sa, addr, addrlen));
89 GNUNET_SCHEDULER_add_delayed (sched,
90 GNUNET_TIME_relative_multiply
91 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
92 &send_done, argclient);
96 GNUNET_CLIENT_disconnect (client, GNUNET_YES);
97 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
107 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 GNUNET_SERVER_destroy (server);
111 GNUNET_CONFIGURATION_destroy (cfg);
117 * Functions with this signature are called whenever a client
118 * is disconnected on the network level.
121 * @param client identification of the client
124 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
128 GNUNET_assert (ok == 5);
130 GNUNET_SCHEDULER_add_now (sched,
136 notify_ready (void *cls, size_t size, void *buf)
138 struct GNUNET_MessageHeader *msg;
140 GNUNET_assert (size >= 256);
141 GNUNET_assert (1 == ok);
144 msg->type = htons (MY_TYPE);
145 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
147 msg->type = htons (MY_TYPE);
148 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
149 return 2 * sizeof (struct GNUNET_MessageHeader);
153 static struct GNUNET_SERVER_MessageHandler handlers[] = {
154 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
160 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
162 struct sockaddr_in sa;
163 struct sockaddr * sap[2];
166 sap[0] = (struct sockaddr*) &sa;
167 slens[0] = sizeof (sa);
171 memset (&sa, 0, sizeof (sa));
172 #if HAVE_SOCKADDR_IN_SIN_LEN
173 sa.sin_len = sizeof (sa);
175 sa.sin_family = AF_INET;
176 sa.sin_port = htons (PORT);
177 server = GNUNET_SERVER_create (tc->sched,
183 GNUNET_TIME_relative_multiply
184 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
186 GNUNET_assert (server != NULL);
187 handlers[0].callback_cls = cls;
188 GNUNET_SERVER_add_handlers (server, handlers);
189 GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, cls);
190 cfg = GNUNET_CONFIGURATION_create ();
191 GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
192 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME",
194 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
196 client = GNUNET_CLIENT_connect (tc->sched, "test", cfg);
197 GNUNET_assert (client != NULL);
198 GNUNET_CLIENT_notify_transmit_ready (client,
200 GNUNET_TIME_relative_multiply
201 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
202 GNUNET_NO, ¬ify_ready, NULL);
207 * Main method, starts scheduler with task1,
208 * checks that "ok" is correct at the end.
215 GNUNET_SCHEDULER_run (&task, NULL);
221 main (int argc, char *argv[])
225 GNUNET_log_setup ("test_server_with_client",
237 /* end of test_server_with_client.c */