fix
[oweals/gnunet.git] / src / util / test_server.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file util/test_server.c
22  * @brief tests for server.c
23  */
24 #include "platform.h"
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"
30
31 #define VERBOSE GNUNET_NO
32
33 #define PORT 12435
34
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
36
37 #define MY_TYPE 128
38 #define MY_TYPE2 129
39
40 static struct GNUNET_SERVER_Handle *server;
41
42 static struct GNUNET_CLIENT_Connection *cc;
43
44 static struct GNUNET_SERVER_Client *argclient;
45
46 static struct GNUNET_CONFIGURATION_Handle *cfg;
47
48 static struct GNUNET_SCHEDULER_Handle *sched;
49
50 static int ok;
51
52
53 static void
54 finish_up (void *cls,
55            const struct GNUNET_SCHEDULER_TaskContext *tc)
56 {
57   GNUNET_assert (ok == 6);
58   ok = 0;
59   GNUNET_SERVER_destroy (server);
60   GNUNET_CLIENT_disconnect (cc, GNUNET_NO);
61   GNUNET_CONFIGURATION_destroy (cfg);
62 }
63
64
65 static void
66 recv_fin_cb (void *cls,
67              struct GNUNET_SERVER_Client *client,
68              const struct GNUNET_MessageHeader *message)
69 {
70   GNUNET_assert (ok == 5);
71   ok = 6;
72   GNUNET_SERVER_receive_done (client, GNUNET_OK);
73   GNUNET_SCHEDULER_add_now (sched,
74                             &finish_up,
75                             NULL);
76 }
77
78
79 static void
80 first_reply_handler (void *cls,
81                      const struct GNUNET_MessageHeader *msg)
82 {
83   GNUNET_assert (ok == 4);
84   ok = 5;
85   GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
86   GNUNET_SERVER_client_drop (argclient);
87   argclient = NULL;
88 }
89
90
91 static size_t
92 reply_msg (void *cls, size_t size, void *buf)
93 {
94   struct GNUNET_MessageHeader msg;
95
96   GNUNET_assert (ok == 3);
97   ok = 4;
98   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
99   msg.type = htons (MY_TYPE);
100   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
101   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
102   return sizeof (struct GNUNET_MessageHeader);
103 }
104
105
106 static void
107 recv_cb (void *cls,
108          struct GNUNET_SERVER_Client *client,
109          const struct GNUNET_MessageHeader *message)
110 {
111   GNUNET_assert (ok == 2);
112   ok = 3;
113   argclient = client;
114   GNUNET_SERVER_client_keep (argclient);
115   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
116                  ntohs (message->size));
117   GNUNET_assert (MY_TYPE == ntohs (message->type));
118   GNUNET_assert (NULL !=
119                  GNUNET_SERVER_notify_transmit_ready (client,
120                                                       ntohs (message->size),
121                                                       TIMEOUT,
122                                                       &reply_msg, NULL));
123 }
124
125
126 static struct GNUNET_SERVER_MessageHandler handlers[] = {
127   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
128   {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
129   {NULL, NULL, 0, 0}
130 };
131
132
133 static size_t
134 transmit_second_message (void *cls,
135                          size_t size,
136                          void *buf)
137 {
138   struct GNUNET_MessageHeader msg;
139
140   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
141   msg.type = htons (MY_TYPE2);
142   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
143   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
144   return sizeof (struct GNUNET_MessageHeader);
145 }
146
147
148 static size_t
149 transmit_initial_message (void *cls,
150                           size_t size,
151                           void *buf)
152 {  
153   struct GNUNET_MessageHeader msg;
154
155   GNUNET_assert (ok == 1);
156   ok = 2;
157   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
158   msg.type = htons (MY_TYPE);
159   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
160   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
161   GNUNET_assert (NULL !=
162                  GNUNET_CLIENT_notify_transmit_ready (cc,
163                                                       sizeof (struct GNUNET_MessageHeader),
164                                                       TIMEOUT,
165                                                       GNUNET_YES,
166                                                       &transmit_second_message,
167                                                       NULL));
168   GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT);
169   return sizeof (struct GNUNET_MessageHeader);
170 }
171
172
173 static void
174 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
175 {
176   struct sockaddr_in sa;
177   struct sockaddr * sap[2];
178   socklen_t slens[2];
179
180   sap[0] = (struct sockaddr*) &sa;
181   slens[0] = sizeof (sa);
182   sap[1] = NULL;
183   slens[1] = 0;
184   sched = tc->sched;
185   memset (&sa, 0, sizeof (sa));
186 #if HAVE_SOCKADDR_IN_SIN_LEN
187   sa.sin_len = sizeof (sa);
188 #endif
189   sa.sin_family = AF_INET;
190   sa.sin_port = htons (PORT);
191   server = GNUNET_SERVER_create (tc->sched,
192                                  NULL,
193                                  NULL,
194                                  sap,
195                                  slens,
196                                  TIMEOUT,
197                                  GNUNET_NO);
198   GNUNET_assert (server != NULL);
199   GNUNET_SERVER_add_handlers (server, handlers);
200   cfg = GNUNET_CONFIGURATION_create ();
201   GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
202   GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME", "localhost");
203   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", "localhost");
204   cc = GNUNET_CLIENT_connect (tc->sched,
205                               "test-server",
206                               cfg);
207   GNUNET_assert (cc != NULL);
208   GNUNET_assert (NULL !=
209                  GNUNET_CLIENT_notify_transmit_ready (cc,
210                                                       sizeof (struct GNUNET_MessageHeader),
211                                                       TIMEOUT,
212                                                       GNUNET_YES,
213                                                       &transmit_initial_message,
214                                                       NULL));
215 }
216
217
218 /**
219  * Main method, starts scheduler with task1,
220  * checks that "ok" is correct at the end.
221  */
222 static int
223 check ()
224 {
225   ok = 1;
226   GNUNET_SCHEDULER_run (&task, &ok);
227   return ok;
228 }
229
230
231 int
232 main (int argc, char *argv[])
233 {
234   int ret = 0;
235
236   GNUNET_log_setup ("test_server", "WARNING", NULL);
237   ret += check ();
238
239   return ret;
240 }
241
242 /* end of test_server.c */