2 This file is part of GNUnet.
3 (C) 2009, 2010 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.c
22 * @brief tests for server.c
25 #include "gnunet_common.h"
26 #include "gnunet_client_lib.h"
27 #include "gnunet_scheduler_lib.h"
28 #include "gnunet_server_lib.h"
29 #include "gnunet_time_lib.h"
31 #define VERBOSE GNUNET_EXTRA_LOGGING
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
40 static struct GNUNET_SERVER_Handle *server;
42 static struct GNUNET_CLIENT_Connection *cc;
44 static struct GNUNET_SERVER_Client *argclient;
46 static struct GNUNET_CONFIGURATION_Handle *cfg;
52 finish_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
54 GNUNET_assert (ok == 6);
56 GNUNET_SERVER_destroy (server);
57 GNUNET_CLIENT_disconnect (cc, GNUNET_NO);
58 GNUNET_CONFIGURATION_destroy (cfg);
63 recv_fin_cb (void *cls, struct GNUNET_SERVER_Client *client,
64 const struct GNUNET_MessageHeader *message)
66 GNUNET_assert (ok == 5);
68 GNUNET_SERVER_receive_done (client, GNUNET_OK);
69 GNUNET_SCHEDULER_add_now (&finish_up, NULL);
74 first_reply_handler (void *cls, const struct GNUNET_MessageHeader *msg)
76 GNUNET_assert (ok == 4);
78 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
79 GNUNET_SERVER_client_drop (argclient);
85 reply_msg (void *cls, size_t size, void *buf)
87 struct GNUNET_MessageHeader msg;
89 GNUNET_assert (ok == 3);
91 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
92 msg.type = htons (MY_TYPE);
93 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
94 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
95 return sizeof (struct GNUNET_MessageHeader);
100 recv_cb (void *cls, struct GNUNET_SERVER_Client *client,
101 const struct GNUNET_MessageHeader *message)
103 GNUNET_assert (ok == 2);
106 GNUNET_SERVER_client_keep (argclient);
107 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
108 GNUNET_assert (MY_TYPE == ntohs (message->type));
109 GNUNET_assert (NULL !=
110 GNUNET_SERVER_notify_transmit_ready (client,
111 ntohs (message->size),
117 static struct GNUNET_SERVER_MessageHandler handlers[] = {
118 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
119 {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
125 transmit_second_message (void *cls, size_t size, void *buf)
127 struct GNUNET_MessageHeader msg;
129 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
130 msg.type = htons (MY_TYPE2);
131 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
132 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
133 return sizeof (struct GNUNET_MessageHeader);
138 transmit_initial_message (void *cls, size_t size, void *buf)
140 struct GNUNET_MessageHeader msg;
142 GNUNET_assert (ok == 1);
144 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
145 msg.type = htons (MY_TYPE);
146 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
147 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
148 GNUNET_assert (NULL !=
149 GNUNET_CLIENT_notify_transmit_ready (cc,
151 GNUNET_MessageHeader),
153 &transmit_second_message,
155 GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT);
156 return sizeof (struct GNUNET_MessageHeader);
161 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
163 struct sockaddr_in sa;
164 struct sockaddr *sap[2];
167 sap[0] = (struct sockaddr *) &sa;
168 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 (NULL, NULL, sap, slens, TIMEOUT, GNUNET_NO);
178 GNUNET_assert (server != NULL);
179 GNUNET_SERVER_add_handlers (server, handlers);
180 cfg = GNUNET_CONFIGURATION_create ();
181 GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
182 GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME",
184 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
186 cc = GNUNET_CLIENT_connect ("test-server", cfg);
187 GNUNET_assert (cc != NULL);
188 GNUNET_assert (NULL !=
189 GNUNET_CLIENT_notify_transmit_ready (cc,
191 GNUNET_MessageHeader),
193 &transmit_initial_message,
199 * Main method, starts scheduler with task1,
200 * checks that "ok" is correct at the end.
206 GNUNET_SCHEDULER_run (&task, &ok);
212 main (int argc, char *argv[])
216 GNUNET_log_setup ("test_server", "WARNING", NULL);
222 /* end of test_server.c */