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