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