Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / test_server_with_client.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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_with_client.c
22  * @brief tests for server.c and client.c,
23  *       specifically disconnect_notify,
24  *       client_get_address and receive_done (resume processing)
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_scheduler_lib.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_server_lib.h"
31 #include "gnunet_time_lib.h"
32
33 #define VERBOSE GNUNET_NO
34
35 #define PORT 22335
36
37 #define MY_TYPE 128
38
39
40 static struct GNUNET_SERVER_Handle *server;
41
42 static struct GNUNET_CLIENT_Connection *client;
43
44 static struct GNUNET_CONFIGURATION_Handle *cfg;
45
46 static int ok;
47
48 static void
49 send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
50 {
51   struct GNUNET_SERVER_Client *argclient = cls;
52   GNUNET_assert (ok == 3);
53   ok++;
54   GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
55 }
56
57
58 static void
59 recv_cb (void *cls,
60          struct GNUNET_SERVER_Client *argclient,
61          const struct GNUNET_MessageHeader *message)
62 {
63   void *addr;
64   size_t addrlen;
65   struct sockaddr_in sa;
66   struct sockaddr_in *have;
67
68   GNUNET_assert (GNUNET_OK ==
69                  GNUNET_SERVER_client_get_address (argclient,
70                                                    &addr, &addrlen));
71
72   GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
73   have = addr;
74   memset (&sa, 0, sizeof (sa));
75 #if HAVE_SOCKADDR_IN_SIN_LEN
76   sa.sin_len = sizeof (sa);
77 #endif
78   sa.sin_family = AF_INET;
79   sa.sin_port = have->sin_port;
80   sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
81   GNUNET_assert (0 == memcmp (&sa, addr, addrlen));
82   GNUNET_free (addr);
83   switch (ok)
84     {
85     case 2:
86       ok++;
87       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
88                                     (GNUNET_TIME_UNIT_MILLISECONDS, 50),
89                                     &send_done, argclient);
90       break;
91     case 4:
92       ok++;
93       GNUNET_CLIENT_disconnect (client, GNUNET_YES);
94       GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
95       break;
96     default:
97       GNUNET_assert (0);
98     }
99
100 }
101
102
103 static void
104 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
105 {
106   GNUNET_SERVER_destroy (server);
107   server = NULL;
108   GNUNET_CONFIGURATION_destroy (cfg);
109   cfg = NULL;
110 }
111
112
113 /**
114  * Functions with this signature are called whenever a client
115  * is disconnected on the network level.
116  *
117  * @param cls closure
118  * @param client identification of the client
119  */
120 static void
121 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
122 {
123   if (client == NULL)
124     return;
125   GNUNET_assert (ok == 5);
126   ok = 0;
127   GNUNET_SCHEDULER_add_now (&clean_up, NULL);
128 }
129
130
131 static size_t
132 notify_ready (void *cls, size_t size, void *buf)
133 {
134   struct GNUNET_MessageHeader *msg;
135
136   GNUNET_assert (size >= 256);
137   GNUNET_assert (1 == ok);
138   ok++;
139   msg = buf;
140   msg->type = htons (MY_TYPE);
141   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
142   msg++;
143   msg->type = htons (MY_TYPE);
144   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
145   return 2 * sizeof (struct GNUNET_MessageHeader);
146 }
147
148
149 static struct GNUNET_SERVER_MessageHandler handlers[] = {
150   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
151   {NULL, NULL, 0, 0}
152 };
153
154
155 static void
156 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 {
158   struct sockaddr_in sa;
159   struct sockaddr * sap[2];
160   socklen_t slens[2];
161
162   sap[0] = (struct sockaddr*) &sa;
163   slens[0] = sizeof (sa);
164   sap[1] = NULL;
165   slens[1] = 0;
166   memset (&sa, 0, sizeof (sa));
167 #if HAVE_SOCKADDR_IN_SIN_LEN
168   sa.sin_len = sizeof (sa);
169 #endif
170   sa.sin_family = AF_INET;
171   sa.sin_port = htons (PORT);
172   server = GNUNET_SERVER_create (NULL,
173                                  NULL,
174                                  sap,
175                                  slens,
176                                  GNUNET_TIME_relative_multiply
177                                  (GNUNET_TIME_UNIT_MILLISECONDS, 250),
178                                  GNUNET_NO);
179   GNUNET_assert (server != NULL);
180   handlers[0].callback_cls = cls;
181   GNUNET_SERVER_add_handlers (server, handlers);
182   GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls);
183   cfg = GNUNET_CONFIGURATION_create ();
184   GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
185   GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME",
186                                          "localhost");
187   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
188                                          "localhost");
189   client = GNUNET_CLIENT_connect ("test", cfg);
190   GNUNET_assert (client != NULL);
191   GNUNET_CLIENT_notify_transmit_ready (client,
192                                        256,
193                                        GNUNET_TIME_relative_multiply
194                                        (GNUNET_TIME_UNIT_MILLISECONDS, 250),
195                                        GNUNET_NO, &notify_ready, 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
207   ok = 1;
208   GNUNET_SCHEDULER_run (&task, NULL);
209   return ok;
210 }
211
212
213 int
214 main (int argc, char *argv[])
215 {
216   int ret = 0;
217
218   GNUNET_log_setup ("test_server_with_client",
219 #if VERBOSE
220                     "DEBUG",
221 #else
222                     "WARNING",
223 #endif
224                     NULL);
225   ret += check ();
226
227   return ret;
228 }
229
230 /* end of test_server_with_client.c */