trying to port statvfs call to BSD
[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 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_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 struct GNUNET_SCHEDULER_Handle *sched;
46
47 static int ok;
48
49
50 static void
51 finish_up (void *cls,
52            const struct GNUNET_SCHEDULER_TaskContext *tc)
53 {
54   GNUNET_assert (ok == 5);
55   ok = 0;
56   GNUNET_SERVER_destroy (server);
57   GNUNET_CLIENT_disconnect (cc, GNUNET_NO);
58   GNUNET_CONFIGURATION_destroy (cfg);
59 }
60
61
62 static void
63 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *clientarg)
64 {
65   if (clientarg == NULL)
66     return;
67   GNUNET_assert (ok == 4);
68   ok = 5;
69   GNUNET_SCHEDULER_add_now (sched,
70                             &finish_up,
71                             NULL);
72 }
73
74
75 static void
76 server_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
77 {
78   struct GNUNET_SERVER_Client *argclient = cls;
79   GNUNET_assert (ok == 3);
80   ok = 4;
81   GNUNET_SERVER_client_disconnect (argclient);
82   GNUNET_SERVER_client_drop (argclient);
83 }
84
85
86 static void
87 recv_cb (void *cls,
88          struct GNUNET_SERVER_Client *client,
89          const struct GNUNET_MessageHeader *message)
90 {
91   GNUNET_assert (ok == 2);
92   ok = 3;
93   GNUNET_SERVER_client_keep (client);
94   GNUNET_SCHEDULER_add_now (sched,
95                             &server_disconnect, client);
96   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
97                  ntohs (message->size));
98   GNUNET_assert (MY_TYPE == ntohs (message->type));
99   GNUNET_SERVER_receive_done (client, GNUNET_OK);
100 }
101
102
103 static struct GNUNET_SERVER_MessageHandler handlers[] = {
104   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
105   {NULL, NULL, 0, 0}
106 };
107
108
109 static size_t
110 transmit_initial_message (void *cls,
111                           size_t size,
112                           void *buf)
113 {  
114   struct GNUNET_MessageHeader msg;
115
116   GNUNET_assert (ok == 1);
117   ok = 2;
118   GNUNET_assert (size > sizeof (struct GNUNET_MessageHeader));
119   msg.type = htons (MY_TYPE);
120   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
121   memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
122   return sizeof (struct GNUNET_MessageHeader);
123 }
124
125
126 static void
127 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
128 {
129   struct sockaddr_in sa;
130   struct sockaddr * sap[2];
131   socklen_t slens[2];
132
133   sap[0] = (struct sockaddr*) &sa;
134   slens[0] = sizeof (sa);
135   sap[1] = NULL;
136   slens[1] = 0;
137   sched = tc->sched;
138   memset (&sa, 0, sizeof (sa));
139 #if HAVE_SOCKADDR_IN_SIN_LEN
140   sa.sin_len = sizeof (sa);
141 #endif
142   sa.sin_family = AF_INET;
143   sa.sin_port = htons (PORT);
144   server = GNUNET_SERVER_create (tc->sched,
145                                  NULL,
146                                  NULL,
147                                  sap,
148                                  slens,
149                                  1024,
150                                  TIMEOUT,
151                                  GNUNET_NO);
152   GNUNET_assert (server != NULL);
153   GNUNET_SERVER_add_handlers (server, handlers);
154   GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, NULL);
155   cfg = GNUNET_CONFIGURATION_create ();
156   GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
157   GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME", "localhost");
158   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", "localhost");
159   cc = GNUNET_CLIENT_connect (tc->sched,
160                               "test-server",
161                               cfg);
162   GNUNET_assert (cc != NULL);
163   GNUNET_assert (NULL !=
164                  GNUNET_CLIENT_notify_transmit_ready (cc,
165                                                       sizeof (struct GNUNET_MessageHeader),
166                                                       TIMEOUT,
167                                                       GNUNET_YES,
168                                                       &transmit_initial_message,
169                                                       NULL));
170 }
171
172
173 /**
174  * Main method, starts scheduler with task1,
175  * checks that "ok" is correct at the end.
176  */
177 static int
178 check ()
179 {
180   ok = 1;
181   GNUNET_SCHEDULER_run (&task, &ok);
182   return ok;
183 }
184
185
186 int
187 main (int argc, char *argv[])
188 {
189   int ret = 0;
190
191   GNUNET_log_setup ("test_server_disconnect", "WARNING", NULL);
192   ret += check ();
193
194   return ret;
195 }
196
197 /* end of test_server_disconnect.c */