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