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