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_util_lib.h"
29 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
34 static struct GNUNET_SERVER_Handle *server;
36 static struct GNUNET_CLIENT_Connection *cc;
38 static struct GNUNET_SERVER_Client *argclient;
40 static struct GNUNET_CONFIGURATION_Handle *cfg;
46 finish_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
48 GNUNET_assert (ok == 6);
50 GNUNET_SERVER_destroy (server);
51 GNUNET_CLIENT_disconnect (cc);
52 GNUNET_CONFIGURATION_destroy (cfg);
57 recv_fin_cb (void *cls, struct GNUNET_SERVER_Client *client,
58 const struct GNUNET_MessageHeader *message)
60 GNUNET_assert (ok == 5);
62 GNUNET_SERVER_receive_done (client, GNUNET_OK);
63 GNUNET_SCHEDULER_add_now (&finish_up, NULL);
68 first_reply_handler (void *cls, const struct GNUNET_MessageHeader *msg)
70 GNUNET_assert (ok == 4);
72 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
73 GNUNET_SERVER_client_drop (argclient);
79 reply_msg (void *cls, size_t size, void *buf)
81 struct GNUNET_MessageHeader msg;
83 GNUNET_assert (ok == 3);
85 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
86 msg.type = htons (MY_TYPE);
87 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
88 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
89 return sizeof (struct GNUNET_MessageHeader);
94 recv_cb (void *cls, struct GNUNET_SERVER_Client *client,
95 const struct GNUNET_MessageHeader *message)
97 GNUNET_assert (ok == 2);
100 GNUNET_SERVER_client_keep (argclient);
101 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
102 GNUNET_assert (MY_TYPE == ntohs (message->type));
103 GNUNET_assert (NULL !=
104 GNUNET_SERVER_notify_transmit_ready (client,
105 ntohs (message->size),
111 static struct GNUNET_SERVER_MessageHandler handlers[] = {
112 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
113 {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
119 transmit_second_message (void *cls, size_t size, void *buf)
121 struct GNUNET_MessageHeader msg;
123 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
124 msg.type = htons (MY_TYPE2);
125 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
126 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
127 return sizeof (struct GNUNET_MessageHeader);
132 transmit_initial_message (void *cls, size_t size, void *buf)
134 struct GNUNET_MessageHeader msg;
136 GNUNET_assert (ok == 1);
138 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
139 msg.type = htons (MY_TYPE);
140 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
141 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
142 GNUNET_assert (NULL !=
143 GNUNET_CLIENT_notify_transmit_ready (cc,
145 GNUNET_MessageHeader),
147 &transmit_second_message,
149 GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT);
150 return 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);
171 server = GNUNET_SERVER_create (NULL, NULL, sap, slens, TIMEOUT, GNUNET_NO);
172 GNUNET_assert (server != NULL);
173 GNUNET_SERVER_add_handlers (server, handlers);
174 cfg = GNUNET_CONFIGURATION_create ();
175 GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
176 GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME",
178 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
180 cc = GNUNET_CLIENT_connect ("test-server", cfg);
181 GNUNET_assert (cc != NULL);
182 GNUNET_assert (NULL !=
183 GNUNET_CLIENT_notify_transmit_ready (cc,
185 GNUNET_MessageHeader),
187 &transmit_initial_message,
193 main (int argc, char *argv[])
195 GNUNET_log_setup ("test_server", "WARNING", NULL);
197 GNUNET_SCHEDULER_run (&task, &ok);
201 /* end of test_server.c */