towards adding mq destruction notification
[oweals/gnunet.git] / src / util / test_server_with_client.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2016 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_MQ_Handle *mq;
37
38 static struct GNUNET_CONFIGURATION_Handle *cfg;
39
40 static int ok;
41
42
43 static void
44 send_done (void *cls)
45 {
46   struct GNUNET_SERVER_Client *argclient = cls;
47
48   GNUNET_assert (ok == 3);
49   ok++;
50   GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
51 }
52
53
54 static void
55 recv_cb (void *cls,
56          struct GNUNET_SERVER_Client *argclient,
57          const struct GNUNET_MessageHeader *message)
58 {
59   void *addr;
60   size_t addrlen;
61   struct sockaddr_in sa;
62   struct sockaddr_in *have;
63
64   GNUNET_assert (GNUNET_OK ==
65                  GNUNET_SERVER_client_get_address (argclient,
66                                                    &addr,
67                                                    &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,
87                                   argclient);
88     break;
89   case 4:
90     ok++;
91     GNUNET_MQ_destroy (mq);
92     GNUNET_SERVER_receive_done (argclient,
93                                 GNUNET_OK);
94     break;
95   default:
96     GNUNET_assert (0);
97   }
98
99 }
100
101
102 static void
103 clean_up (void *cls)
104 {
105   GNUNET_SERVER_destroy (server);
106   server = NULL;
107   GNUNET_CONFIGURATION_destroy (cfg);
108   cfg = NULL;
109 }
110
111
112 /**
113  * Functions with this signature are called whenever a client
114  * is disconnected on the network level.
115  *
116  * @param cls closure
117  * @param client identification of the client
118  */
119 static void
120 notify_disconnect (void *cls,
121                    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 struct GNUNET_SERVER_MessageHandler handlers[] = {
132   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
133   {NULL, NULL, 0, 0}
134 };
135
136
137 static void
138 task (void *cls)
139 {
140   struct sockaddr_in sa;
141   struct sockaddr *sap[2];
142   socklen_t slens[2];
143   struct GNUNET_MQ_Envelope *env;
144   struct GNUNET_MessageHeader *msg;
145
146   sap[0] = (struct sockaddr *) &sa;
147   slens[0] = sizeof (sa);
148   sap[1] = NULL;
149   slens[1] = 0;
150   memset (&sa, 0, sizeof (sa));
151 #if HAVE_SOCKADDR_IN_SIN_LEN
152   sa.sin_len = sizeof (sa);
153 #endif
154   sa.sin_family = AF_INET;
155   sa.sin_port = htons (PORT);
156   server =
157       GNUNET_SERVER_create (NULL, NULL, sap, slens,
158                             GNUNET_TIME_relative_multiply
159                             (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
160   GNUNET_assert (server != NULL);
161   handlers[0].callback_cls = cls;
162   GNUNET_SERVER_add_handlers (server, handlers);
163   GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls);
164   cfg = GNUNET_CONFIGURATION_create ();
165   GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
166   GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", "localhost");
167   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
168                                          "localhost");
169   mq = GNUNET_CLIENT_connecT (cfg,
170                               "test",
171                               NULL,
172                               NULL,
173                               NULL);
174   GNUNET_assert (NULL != mq);
175   ok = 2;
176   env = GNUNET_MQ_msg (msg,
177                        MY_TYPE);
178   GNUNET_MQ_send (mq,
179                   env);
180   env = GNUNET_MQ_msg (msg,
181                        MY_TYPE);
182   GNUNET_MQ_send (mq,
183                   env);
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 */