expand GNUNET_OS_ProjectData API to also enable de-duplcation of logic for --help
[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  * @param tc scheduler context
78  */
79 static void
80 finish_up (void *cls,
81            const struct GNUNET_SCHEDULER_TaskContext *tc)
82 {
83   GNUNET_assert (7 == ok);
84   ok = 0;
85   GNUNET_SERVER_destroy (server);
86   GNUNET_CLIENT_disconnect (cc);
87   GNUNET_CONFIGURATION_destroy (cfg);
88 }
89
90
91 /**
92  * The server has received the second message, initiate clean up.
93  *
94  * @param cls NULL
95  * @param client client we got the message from
96  * @param message the message
97  */
98 static void
99 recv_fin_cb (void *cls,
100              struct GNUNET_SERVER_Client *client,
101              const struct GNUNET_MessageHeader *message)
102 {
103   GNUNET_assert (6 == ok);
104   ok = 7;
105   GNUNET_SERVER_receive_done (client, GNUNET_OK);
106   GNUNET_SCHEDULER_add_now (&finish_up, NULL);
107 }
108
109
110 /**
111  * The client connected to the server and is now allowed
112  * to send a second message.  We send one.
113  *
114  * @param cls NULL
115  * @param size number of bytes that can be transmitted
116  * @param buf where to copy the message
117  * @return number of bytes copied to @a buf
118  */
119 static size_t
120 transmit_second_message (void *cls,
121                          size_t size,
122                          void *buf)
123 {
124   struct GNUNET_MessageHeader msg;
125
126   GNUNET_assert (5 == ok);
127   ok = 6;
128   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
129   msg.type = htons (MY_TYPE2);
130   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
131   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
132   return sizeof (struct GNUNET_MessageHeader);
133 }
134
135
136 /**
137  * We have received the reply from the server, check that we are at
138  * the right stage and queue the next message to the server.  Cleans
139  * up #argclient.
140  *
141  * @param cls NULL
142  * @param msg message we got from the server
143  */
144 static void
145 first_reply_handler (void *cls,
146                      const struct GNUNET_MessageHeader *msg)
147 {
148   GNUNET_assert (4 == ok);
149   ok = 5;
150   GNUNET_assert (NULL !=
151                  GNUNET_CLIENT_notify_transmit_ready (cc,
152                                                       sizeof (struct GNUNET_MessageHeader),
153                                                       TIMEOUT,
154                                                       GNUNET_YES,
155                                                       &transmit_second_message,
156                                                       NULL));
157 }
158
159
160 /**
161  * Send a reply of type #MY_TYPE from the server to the client.
162  * Checks that we are in the right phase and transmits the
163  * reply.  Cleans up #argclient state.
164  *
165  * @param cls NULL
166  * @param size number of bytes we are allowed to send
167  * @param buf where to copy the reply
168  * @return number of bytes written to @a buf
169  */
170 static size_t
171 reply_msg (void *cls,
172            size_t size,
173            void *buf)
174 {
175   struct GNUNET_MessageHeader msg;
176
177   GNUNET_assert (3 == ok);
178   ok = 4;
179   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
180   msg.type = htons (MY_TYPE);
181   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
182   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
183   GNUNET_assert (NULL != argclient);
184   GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
185   GNUNET_SERVER_client_drop (argclient);
186   argclient = NULL;
187   return sizeof (struct GNUNET_MessageHeader);
188 }
189
190
191 /**
192  * Function called whenever the server receives a message of
193  * type #MY_TYPE.  Checks that we are at the stage where
194  * we expect the first message, then sends a reply.  Stores
195  * the handle to the client in #argclient.
196  *
197  * @param cls NULL
198  * @param client client that sent the message
199  * @param message the message we received
200  */
201 static void
202 recv_cb (void *cls,
203          struct GNUNET_SERVER_Client *client,
204          const struct GNUNET_MessageHeader *message)
205 {
206   GNUNET_assert (2 == ok);
207   ok = 3;
208   argclient = client;
209   GNUNET_SERVER_client_keep (argclient);
210   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
211   GNUNET_assert (MY_TYPE == ntohs (message->type));
212   GNUNET_assert (NULL !=
213                  GNUNET_SERVER_notify_transmit_ready (client,
214                                                       ntohs (message->size),
215                                                       TIMEOUT, &reply_msg,
216                                                       NULL));
217 }
218
219
220 /**
221  * The client connected to the server and is now allowed
222  * to send a first message.  We transmit a simple message,
223  * ask for a second transmission and get ready to receive
224  * a response.
225  *
226  * @param cls NULL
227  * @param size number of bytes that can be transmitted
228  * @param buf where to copy the message
229  * @return number of bytes copied to @a buf
230  */
231 static size_t
232 transmit_initial_message (void *cls,
233                           size_t size,
234                           void *buf)
235 {
236   struct GNUNET_MessageHeader msg;
237
238   GNUNET_assert (1 == ok);
239   ok = 2;
240   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
241   msg.type = htons (MY_TYPE);
242   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
243   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
244   GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT);
245   return sizeof (struct GNUNET_MessageHeader);
246 }
247
248
249 /**
250  * Message handlers for the server.
251  */
252 static struct GNUNET_SERVER_MessageHandler handlers[] = {
253   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
254   {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
255   {NULL, NULL, 0, 0}
256 };
257
258
259 /**
260  * First task run by the scheduler.  Initializes the server and
261  * a client and asks for a transmission from the client to the
262  * server.
263  *
264  * @param cls NULL
265  * @param tc scheduler context
266  */
267 static void
268 task (void *cls,
269       const struct GNUNET_SCHEDULER_TaskContext *tc)
270 {
271   struct sockaddr_in sa;
272   struct sockaddr *sap[2];
273   socklen_t slens[2];
274
275   sap[0] = (struct sockaddr *) &sa;
276   slens[0] = sizeof (sa);
277   sap[1] = NULL;
278   slens[1] = 0;
279   memset (&sa, 0, sizeof (sa));
280 #if HAVE_SOCKADDR_IN_SIN_LEN
281   sa.sin_len = sizeof (sa);
282 #endif
283   sa.sin_family = AF_INET;
284   sa.sin_port = htons (PORT);
285   server = GNUNET_SERVER_create (NULL, NULL,
286                                  sap, slens,
287                                  TIMEOUT, GNUNET_NO);
288   GNUNET_assert (server != NULL);
289   GNUNET_SERVER_add_handlers (server, handlers);
290   cfg = GNUNET_CONFIGURATION_create ();
291   GNUNET_CONFIGURATION_set_value_number (cfg,
292                                          "test-server",
293                                          "PORT",
294                                          PORT);
295   GNUNET_CONFIGURATION_set_value_string (cfg,
296                                          "test-server",
297                                          "HOSTNAME",
298                                          "localhost");
299   GNUNET_CONFIGURATION_set_value_string (cfg,
300                                          "resolver",
301                                          "HOSTNAME",
302                                          "localhost");
303   cc = GNUNET_CLIENT_connect ("test-server", cfg);
304   GNUNET_assert (cc != NULL);
305   GNUNET_assert (NULL !=
306                  GNUNET_CLIENT_notify_transmit_ready (cc,
307                                                       sizeof (struct
308                                                               GNUNET_MessageHeader),
309                                                       TIMEOUT, GNUNET_YES,
310                                                       &transmit_initial_message,
311                                                       NULL));
312 }
313
314
315 /**
316  * Runs the test.
317  *
318  * @param argc length of @a argv
319  * @param argv command line arguments (ignored)
320  * @return 0 on success, otherwise phase of failure
321  */
322 int
323 main (int argc, char *argv[])
324 {
325   GNUNET_log_setup ("test_server",
326                     "WARNING",
327                     NULL);
328   ok = 1;
329   GNUNET_SCHEDULER_run (&task, &ok);
330   return ok;
331 }
332
333 /* end of test_server.c */