Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / test_server_disconnect.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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_disconnect.c
22  * @brief tests for server.c,  specifically GNUNET_SERVER_client_disconnect
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_client_lib.h"
27 #include "gnunet_scheduler_lib.h"
28 #include "gnunet_server_lib.h"
29 #include "gnunet_time_lib.h"
30
31 #define VERBOSE GNUNET_NO
32
33 #define PORT 12435
34
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
36
37 #define MY_TYPE 128
38
39 static struct GNUNET_SERVER_Handle *server;
40
41 static struct GNUNET_CLIENT_Connection *cc;
42
43 static struct GNUNET_CONFIGURATION_Handle *cfg;
44
45 static int ok;
46
47
48 static void
49 finish_up (void *cls,
50            const struct GNUNET_SCHEDULER_TaskContext *tc)
51 {
52   GNUNET_assert (ok == 5);
53   ok = 0;
54   GNUNET_SERVER_destroy (server);
55   GNUNET_CLIENT_disconnect (cc, GNUNET_NO);
56   GNUNET_CONFIGURATION_destroy (cfg);
57 }
58
59
60 static void
61 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *clientarg)
62 {
63   if (clientarg == NULL)
64     return;
65   GNUNET_assert (ok == 4);
66   ok = 5;
67   GNUNET_SCHEDULER_add_now (&finish_up,
68                             NULL);
69 }
70
71
72 static void
73 server_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
74 {
75   struct GNUNET_SERVER_Client *argclient = cls;
76   GNUNET_assert (ok == 3);
77   ok = 4;
78   GNUNET_SERVER_client_disconnect (argclient);
79   GNUNET_SERVER_client_drop (argclient);
80 }
81
82
83 static void
84 recv_cb (void *cls,
85          struct GNUNET_SERVER_Client *client,
86          const struct GNUNET_MessageHeader *message)
87 {
88   GNUNET_assert (ok == 2);
89   ok = 3;
90   GNUNET_SERVER_client_keep (client);
91   GNUNET_SCHEDULER_add_now (&server_disconnect, client);
92   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
93                  ntohs (message->size));
94   GNUNET_assert (MY_TYPE == ntohs (message->type));
95   GNUNET_SERVER_receive_done (client, GNUNET_OK);
96 }
97
98
99 static struct GNUNET_SERVER_MessageHandler handlers[] = {
100   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
101   {NULL, NULL, 0, 0}
102 };
103
104
105 static size_t
106 transmit_initial_message (void *cls,
107                           size_t size,
108                           void *buf)
109 {  
110   struct GNUNET_MessageHeader msg;
111
112   GNUNET_assert (ok == 1);
113   ok = 2;
114   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
115   msg.type = htons (MY_TYPE);
116   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
117   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
118   return sizeof (struct GNUNET_MessageHeader);
119 }
120
121
122 static void
123 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
124 {
125   struct sockaddr_in sa;
126   struct sockaddr * sap[2];
127   socklen_t slens[2];
128
129   sap[0] = (struct sockaddr*) &sa;
130   slens[0] = sizeof (sa);
131   sap[1] = NULL;
132   slens[1] = 0;
133   memset (&sa, 0, sizeof (sa));
134 #if HAVE_SOCKADDR_IN_SIN_LEN
135   sa.sin_len = sizeof (sa);
136 #endif
137   sa.sin_family = AF_INET;
138   sa.sin_port = htons (PORT);
139   server = GNUNET_SERVER_create (NULL,
140                                  NULL,
141                                  sap,
142                                  slens,
143                                  TIMEOUT,
144                                  GNUNET_NO);
145   GNUNET_assert (server != NULL);
146   GNUNET_SERVER_add_handlers (server, handlers);
147   GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, NULL);
148   cfg = GNUNET_CONFIGURATION_create ();
149   GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
150   GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME", "localhost");
151   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", "localhost");
152   cc = GNUNET_CLIENT_connect ("test-server",
153                               cfg);
154   GNUNET_assert (cc != NULL);
155   GNUNET_assert (NULL !=
156                  GNUNET_CLIENT_notify_transmit_ready (cc,
157                                                       sizeof (struct GNUNET_MessageHeader),
158                                                       TIMEOUT,
159                                                       GNUNET_YES,
160                                                       &transmit_initial_message,
161                                                       NULL));
162 }
163
164
165 /**
166  * Main method, starts scheduler with task1,
167  * checks that "ok" is correct at the end.
168  */
169 static int
170 check ()
171 {
172   ok = 1;
173   GNUNET_SCHEDULER_run (&task, &ok);
174   return ok;
175 }
176
177
178 int
179 main (int argc, char *argv[])
180 {
181   int ret = 0;
182
183   GNUNET_log_setup ("test_server_disconnect", "WARNING", NULL);
184   ret += check ();
185
186   return ret;
187 }
188
189 /* end of test_server_disconnect.c */