Tell core that we want to have this packet delivered
[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 int ok;
49
50
51 static void
52 finish_up (void *cls,
53            const struct GNUNET_SCHEDULER_TaskContext *tc)
54 {
55   GNUNET_assert (ok == 6);
56   ok = 0;
57   GNUNET_SERVER_destroy (server);
58   GNUNET_CLIENT_disconnect (cc, GNUNET_NO);
59   GNUNET_CONFIGURATION_destroy (cfg);
60 }
61
62
63 static void
64 recv_fin_cb (void *cls,
65              struct GNUNET_SERVER_Client *client,
66              const struct GNUNET_MessageHeader *message)
67 {
68   GNUNET_assert (ok == 5);
69   ok = 6;
70   GNUNET_SERVER_receive_done (client, GNUNET_OK);
71   GNUNET_SCHEDULER_add_now (&finish_up,
72                             NULL);
73 }
74
75
76 static void
77 first_reply_handler (void *cls,
78                      const struct GNUNET_MessageHeader *msg)
79 {
80   GNUNET_assert (ok == 4);
81   ok = 5;
82   GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
83   GNUNET_SERVER_client_drop (argclient);
84   argclient = NULL;
85 }
86
87
88 static size_t
89 reply_msg (void *cls, size_t size, void *buf)
90 {
91   struct GNUNET_MessageHeader msg;
92
93   GNUNET_assert (ok == 3);
94   ok = 4;
95   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
96   msg.type = htons (MY_TYPE);
97   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
98   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
99   return sizeof (struct GNUNET_MessageHeader);
100 }
101
102
103 static void
104 recv_cb (void *cls,
105          struct GNUNET_SERVER_Client *client,
106          const struct GNUNET_MessageHeader *message)
107 {
108   GNUNET_assert (ok == 2);
109   ok = 3;
110   argclient = client;
111   GNUNET_SERVER_client_keep (argclient);
112   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
113                  ntohs (message->size));
114   GNUNET_assert (MY_TYPE == ntohs (message->type));
115   GNUNET_assert (NULL !=
116                  GNUNET_SERVER_notify_transmit_ready (client,
117                                                       ntohs (message->size),
118                                                       TIMEOUT,
119                                                       &reply_msg, NULL));
120 }
121
122
123 static struct GNUNET_SERVER_MessageHandler handlers[] = {
124   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
125   {&recv_fin_cb, NULL, MY_TYPE2, sizeof (struct GNUNET_MessageHeader)},
126   {NULL, NULL, 0, 0}
127 };
128
129
130 static size_t
131 transmit_second_message (void *cls,
132                          size_t size,
133                          void *buf)
134 {
135   struct GNUNET_MessageHeader msg;
136
137   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
138   msg.type = htons (MY_TYPE2);
139   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
140   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
141   return sizeof (struct GNUNET_MessageHeader);
142 }
143
144
145 static size_t
146 transmit_initial_message (void *cls,
147                           size_t size,
148                           void *buf)
149 {  
150   struct GNUNET_MessageHeader msg;
151
152   GNUNET_assert (ok == 1);
153   ok = 2;
154   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
155   msg.type = htons (MY_TYPE);
156   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
157   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
158   GNUNET_assert (NULL !=
159                  GNUNET_CLIENT_notify_transmit_ready (cc,
160                                                       sizeof (struct GNUNET_MessageHeader),
161                                                       TIMEOUT,
162                                                       GNUNET_YES,
163                                                       &transmit_second_message,
164                                                       NULL));
165   GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT);
166   return sizeof (struct GNUNET_MessageHeader);
167 }
168
169
170 static void
171 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
172 {
173   struct sockaddr_in sa;
174   struct sockaddr * sap[2];
175   socklen_t slens[2];
176
177   sap[0] = (struct sockaddr*) &sa;
178   slens[0] = sizeof (sa);
179   sap[1] = NULL;
180   slens[1] = 0;
181   memset (&sa, 0, sizeof (sa));
182 #if HAVE_SOCKADDR_IN_SIN_LEN
183   sa.sin_len = sizeof (sa);
184 #endif
185   sa.sin_family = AF_INET;
186   sa.sin_port = htons (PORT);
187   server = GNUNET_SERVER_create (NULL,
188                                  NULL,
189                                  sap,
190                                  slens,
191                                  TIMEOUT,
192                                  GNUNET_NO);
193   GNUNET_assert (server != NULL);
194   GNUNET_SERVER_add_handlers (server, handlers);
195   cfg = GNUNET_CONFIGURATION_create ();
196   GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
197   GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME", "localhost");
198   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", "localhost");
199   cc = GNUNET_CLIENT_connect ("test-server",
200                               cfg);
201   GNUNET_assert (cc != NULL);
202   GNUNET_assert (NULL !=
203                  GNUNET_CLIENT_notify_transmit_ready (cc,
204                                                       sizeof (struct GNUNET_MessageHeader),
205                                                       TIMEOUT,
206                                                       GNUNET_YES,
207                                                       &transmit_initial_message,
208                                                       NULL));
209 }
210
211
212 /**
213  * Main method, starts scheduler with task1,
214  * checks that "ok" is correct at the end.
215  */
216 static int
217 check ()
218 {
219   ok = 1;
220   GNUNET_SCHEDULER_run (&task, &ok);
221   return ok;
222 }
223
224
225 int
226 main (int argc, char *argv[])
227 {
228   int ret = 0;
229
230   GNUNET_log_setup ("test_server", "WARNING", NULL);
231   ret += check ();
232
233   return ret;
234 }
235
236 /* end of test_server.c */