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