f22c15300918eff64e884c786536ebcc1cd3a113
[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 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_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_SCHEDULER_Handle *sched;
45
46 static struct GNUNET_CONFIGURATION_Handle *cfg;
47
48 static int ok;
49
50 static void
51 send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
52 {
53   struct GNUNET_SERVER_Client *argclient = cls;
54   GNUNET_assert (ok == 3);
55   ok++;
56   GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
57 }
58
59
60 static void
61 recv_cb (void *cls,
62          struct GNUNET_SERVER_Client *argclient,
63          const struct GNUNET_MessageHeader *message)
64 {
65   void *addr;
66   size_t addrlen;
67   struct sockaddr_in sa;
68   struct sockaddr_in *have;
69
70   GNUNET_assert (GNUNET_OK ==
71                  GNUNET_SERVER_client_get_address (argclient,
72                                                    &addr, &addrlen));
73
74   GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
75   have = addr;
76   memset (&sa, 0, sizeof (sa));
77 #if HAVE_SOCKADDR_IN_SIN_LEN
78   sa.sin_len = sizeof (sa);
79 #endif
80   sa.sin_family = AF_INET;
81   sa.sin_port = have->sin_port;
82   sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
83   GNUNET_assert (0 == memcmp (&sa, addr, addrlen));
84   GNUNET_free (addr);
85   switch (ok)
86     {
87     case 2:
88       ok++;
89       GNUNET_SCHEDULER_add_delayed (sched,
90                                     GNUNET_TIME_relative_multiply
91                                     (GNUNET_TIME_UNIT_MILLISECONDS, 50),
92                                     &send_done, argclient);
93       break;
94     case 4:
95       ok++;
96       GNUNET_CLIENT_disconnect (client);
97       GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
98       break;
99     default:
100       GNUNET_assert (0);
101     }
102
103 }
104
105
106 static void
107 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
108 {
109   GNUNET_SERVER_destroy (server);
110   server = NULL;
111   GNUNET_CONFIGURATION_destroy (cfg);
112   cfg = NULL;
113 }
114
115
116 /**
117  * Functions with this signature are called whenever a client
118  * is disconnected on the network level.
119  *
120  * @param cls closure
121  * @param client identification of the client
122  */
123 static void
124 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
125 {
126   GNUNET_assert (ok == 5);
127   ok = 0;
128   GNUNET_SCHEDULER_add_now (sched,
129                             &clean_up, NULL);
130 }
131
132
133 static size_t
134 notify_ready (void *cls, size_t size, void *buf)
135 {
136   struct GNUNET_MessageHeader *msg;
137
138   GNUNET_assert (size >= 256);
139   GNUNET_assert (1 == ok);
140   ok++;
141   msg = buf;
142   msg->type = htons (MY_TYPE);
143   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
144   msg++;
145   msg->type = htons (MY_TYPE);
146   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
147   return 2 * sizeof (struct GNUNET_MessageHeader);
148 }
149
150
151 static struct GNUNET_SERVER_MessageHandler handlers[] = {
152   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
153   {NULL, NULL, 0, 0}
154 };
155
156
157 static void
158 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 {
160   struct sockaddr_in sa;
161
162   sched = tc->sched;
163   memset (&sa, 0, sizeof (sa));
164 #if HAVE_SOCKADDR_IN_SIN_LEN
165   sa.sin_len = sizeof (sa);
166 #endif
167   sa.sin_family = AF_INET;
168   sa.sin_port = htons (PORT);
169   server = GNUNET_SERVER_create (tc->sched,
170                                  NULL,
171                                  NULL,
172                                  (const struct sockaddr *) &sa,
173                                  sizeof (sa),
174                                  1024,
175                                  GNUNET_TIME_relative_multiply
176                                  (GNUNET_TIME_UNIT_MILLISECONDS, 250),
177                                  GNUNET_NO);
178   GNUNET_assert (server != NULL);
179   handlers[0].callback_cls = cls;
180   GNUNET_SERVER_add_handlers (server, handlers);
181   GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls);
182   cfg = GNUNET_CONFIGURATION_create ();
183   GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
184   GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME",
185                                          "localhost");
186   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
187                                          "localhost");
188   client = GNUNET_CLIENT_connect (tc->sched, "test", cfg);
189   GNUNET_assert (client != NULL);
190   GNUNET_CLIENT_notify_transmit_ready (client,
191                                        256,
192                                        GNUNET_TIME_relative_multiply
193                                        (GNUNET_TIME_UNIT_MILLISECONDS, 250),
194                                        GNUNET_NO, &notify_ready, NULL);
195 }
196
197
198 /**
199  * Main method, starts scheduler with task1,
200  * checks that "ok" is correct at the end.
201  */
202 static int
203 check ()
204 {
205
206   ok = 1;
207   GNUNET_SCHEDULER_run (&task, NULL);
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_with_client",
218 #if VERBOSE
219                     "DEBUG",
220 #else
221                     "WARNING",
222 #endif
223                     NULL);
224   ret += check ();
225
226   return ret;
227 }
228
229 /* end of test_server_with_client.c */