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