small API change: do no longer pass rarely needed GNUNET_SCHEDULER_TaskContext to...
[oweals/gnunet.git] / src / util / test_server.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2014 GNUnet e.V.
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file util/test_server.c
22  * @brief tests for server.c
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26
27 /**
28  * TCP port to use for the server.
29  */
30 #define PORT 12435
31
32 /**
33  * Timeout to use for operations.
34  */
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
36
37 /**
38  * Test message type.
39  */
40 #define MY_TYPE 128
41
42 /**
43  * Test message type.
44  */
45 #define MY_TYPE2 129
46
47 /**
48  * Handle for the server.
49  */
50 static struct GNUNET_SERVER_Handle *server;
51
52 /**
53  * Handle for the client.
54  */
55 static struct GNUNET_CLIENT_Connection *cc;
56
57 /**
58  * Handle of the server for the client.
59  */
60 static struct GNUNET_SERVER_Client *argclient;
61
62 /**
63  * Our configuration.
64  */
65 static struct GNUNET_CONFIGURATION_Handle *cfg;
66
67 /**
68  * Number indiciating in which phase of the test we are.
69  */
70 static int ok;
71
72
73 /**
74  * Final task invoked to clean up.
75  *
76  * @param cls NULL
77  */
78 static void
79 finish_up (void *cls)
80 {
81   GNUNET_assert (7 == ok);
82   ok = 0;
83   GNUNET_SERVER_destroy (server);
84   GNUNET_CLIENT_disconnect (cc);
85   GNUNET_CONFIGURATION_destroy (cfg);
86 }
87
88
89 /**
90  * The server has received the second message, initiate clean up.
91  *
92  * @param cls NULL
93  * @param client client we got the message from
94  * @param message the message
95  */
96 static void
97 recv_fin_cb (void *cls,
98              struct GNUNET_SERVER_Client *client,
99              const struct GNUNET_MessageHeader *message)
100 {
101   GNUNET_assert (6 == ok);
102   ok = 7;
103   GNUNET_SERVER_receive_done (client, GNUNET_OK);
104   GNUNET_SCHEDULER_add_now (&finish_up, NULL);
105 }
106
107
108 /**
109  * The client connected to the server and is now allowed
110  * to send a second message.  We send one.
111  *
112  * @param cls NULL
113  * @param size number of bytes that can be transmitted
114  * @param buf where to copy the message
115  * @return number of bytes copied to @a buf
116  */
117 static size_t
118 transmit_second_message (void *cls,
119                          size_t size,
120                          void *buf)
121 {
122   struct GNUNET_MessageHeader msg;
123
124   GNUNET_assert (5 == ok);
125   ok = 6;
126   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
127   msg.type = htons (MY_TYPE2);
128   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
129   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
130   return sizeof (struct GNUNET_MessageHeader);
131 }
132
133
134 /**
135  * We have received the reply from the server, check that we are at
136  * the right stage and queue the next message to the server.  Cleans
137  * up #argclient.
138  *
139  * @param cls NULL
140  * @param msg message we got from the server
141  */
142 static void
143 first_reply_handler (void *cls,
144                      const struct GNUNET_MessageHeader *msg)
145 {
146   GNUNET_assert (4 == ok);
147   ok = 5;
148   GNUNET_assert (NULL !=
149                  GNUNET_CLIENT_notify_transmit_ready (cc,
150                                                       sizeof (struct GNUNET_MessageHeader),
151                                                       TIMEOUT,
152                                                       GNUNET_YES,
153                                                       &transmit_second_message,
154                                                       NULL));
155 }
156
157
158 /**
159  * Send a reply of type #MY_TYPE from the server to the client.
160  * Checks that we are in the right phase and transmits the
161  * reply.  Cleans up #argclient state.
162  *
163  * @param cls NULL
164  * @param size number of bytes we are allowed to send
165  * @param buf where to copy the reply
166  * @return number of bytes written to @a buf
167  */
168 static size_t
169 reply_msg (void *cls,
170            size_t size,
171            void *buf)
172 {
173   struct GNUNET_MessageHeader msg;
174
175   GNUNET_assert (3 == ok);
176   ok = 4;
177   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
178   msg.type = htons (MY_TYPE);
179   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
180   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
181   GNUNET_assert (NULL != argclient);
182   GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
183   GNUNET_SERVER_client_drop (argclient);
184   argclient = NULL;
185   return sizeof (struct GNUNET_MessageHeader);
186 }
187
188
189 /**
190  * Function called whenever the server receives a message of
191  * type #MY_TYPE.  Checks that we are at the stage where
192  * we expect the first message, then sends a reply.  Stores
193  * the handle to the client in #argclient.
194  *
195  * @param cls NULL
196  * @param client client that sent the message
197  * @param message the message we received
198  */
199 static void
200 recv_cb (void *cls,
201          struct GNUNET_SERVER_Client *client,
202          const struct GNUNET_MessageHeader *message)
203 {
204   GNUNET_assert (2 == ok);
205   ok = 3;
206   argclient = client;
207   GNUNET_SERVER_client_keep (argclient);
208   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
209   GNUNET_assert (MY_TYPE == ntohs (message->type));
210   GNUNET_assert (NULL !=
211                  GNUNET_SERVER_notify_transmit_ready (client,
212                                                       ntohs (message->size),
213                                                       TIMEOUT, &reply_msg,
214                                                       NULL));
215 }
216
217
218 /**
219  * The client connected to the server and is now allowed
220  * to send a first message.  We transmit a simple message,
221  * ask for a second transmission and get ready to receive
222  * a response.
223  *
224  * @param cls NULL
225  * @param size number of bytes that can be transmitted
226  * @param buf where to copy the message
227  * @return number of bytes copied to @a buf
228  */
229 static size_t
230 transmit_initial_message (void *cls,
231                           size_t size,
232                           void *buf)
233 {
234   struct GNUNET_MessageHeader msg;
235
236   GNUNET_assert (1 == ok);
237   ok = 2;
238   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
239   msg.type = htons (MY_TYPE);
240   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
241   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
242   GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT);
243   return sizeof (struct GNUNET_MessageHeader);
244 }
245
246
247 /**
248  * Message handlers for the server.
249  */
250 static struct GNUNET_SERVER_MessageHandler handlers[] = {
251   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
252   {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
253   {NULL, NULL, 0, 0}
254 };
255
256
257 /**
258  * First task run by the scheduler.  Initializes the server and
259  * a client and asks for a transmission from the client to the
260  * server.
261  *
262  * @param cls NULL
263  */
264 static void
265 task (void *cls)
266 {
267   struct sockaddr_in sa;
268   struct sockaddr *sap[2];
269   socklen_t slens[2];
270
271   sap[0] = (struct sockaddr *) &sa;
272   slens[0] = sizeof (sa);
273   sap[1] = NULL;
274   slens[1] = 0;
275   memset (&sa, 0, sizeof (sa));
276 #if HAVE_SOCKADDR_IN_SIN_LEN
277   sa.sin_len = sizeof (sa);
278 #endif
279   sa.sin_family = AF_INET;
280   sa.sin_port = htons (PORT);
281   server = GNUNET_SERVER_create (NULL, NULL,
282                                  sap, slens,
283                                  TIMEOUT, GNUNET_NO);
284   GNUNET_assert (server != NULL);
285   GNUNET_SERVER_add_handlers (server, handlers);
286   cfg = GNUNET_CONFIGURATION_create ();
287   GNUNET_CONFIGURATION_set_value_number (cfg,
288                                          "test-server",
289                                          "PORT",
290                                          PORT);
291   GNUNET_CONFIGURATION_set_value_string (cfg,
292                                          "test-server",
293                                          "HOSTNAME",
294                                          "localhost");
295   GNUNET_CONFIGURATION_set_value_string (cfg,
296                                          "resolver",
297                                          "HOSTNAME",
298                                          "localhost");
299   cc = GNUNET_CLIENT_connect ("test-server", cfg);
300   GNUNET_assert (cc != NULL);
301   GNUNET_assert (NULL !=
302                  GNUNET_CLIENT_notify_transmit_ready (cc,
303                                                       sizeof (struct
304                                                               GNUNET_MessageHeader),
305                                                       TIMEOUT, GNUNET_YES,
306                                                       &transmit_initial_message,
307                                                       NULL));
308 }
309
310
311 /**
312  * Runs the test.
313  *
314  * @param argc length of @a argv
315  * @param argv command line arguments (ignored)
316  * @return 0 on success, otherwise phase of failure
317  */
318 int
319 main (int argc, char *argv[])
320 {
321   GNUNET_log_setup ("test_server",
322                     "WARNING",
323                     NULL);
324   ok = 1;
325   GNUNET_SCHEDULER_run (&task, &ok);
326   return ok;
327 }
328
329 /* end of test_server.c */