2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file util/test_server_disconnect.c
22 * @brief tests for server.c, specifically GNUNET_SERVER_client_disconnect
25 #include "gnunet_util_lib.h"
30 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
34 static struct GNUNET_SERVER_Handle *server;
36 static struct GNUNET_CLIENT_Connection *cc;
38 static struct GNUNET_CONFIGURATION_Handle *cfg;
44 finish_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
46 GNUNET_assert (ok == 5);
48 GNUNET_SERVER_destroy (server);
49 GNUNET_CLIENT_disconnect (cc);
50 GNUNET_CONFIGURATION_destroy (cfg);
55 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *clientarg)
57 if (clientarg == NULL)
59 GNUNET_assert (ok == 4);
61 GNUNET_SCHEDULER_add_now (&finish_up, NULL);
66 server_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
68 struct GNUNET_SERVER_Client *argclient = cls;
70 GNUNET_assert (ok == 3);
72 GNUNET_SERVER_client_disconnect (argclient);
73 GNUNET_SERVER_client_drop (argclient);
78 recv_cb (void *cls, struct GNUNET_SERVER_Client *client,
79 const struct GNUNET_MessageHeader *message)
81 GNUNET_assert (ok == 2);
83 GNUNET_SERVER_client_keep (client);
84 GNUNET_SCHEDULER_add_now (&server_disconnect, client);
85 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
86 GNUNET_assert (MY_TYPE == ntohs (message->type));
87 GNUNET_SERVER_receive_done (client, GNUNET_OK);
91 static struct GNUNET_SERVER_MessageHandler handlers[] = {
92 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
98 transmit_initial_message (void *cls, size_t size, void *buf)
100 struct GNUNET_MessageHeader msg;
102 GNUNET_assert (ok == 1);
104 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
105 msg.type = htons (MY_TYPE);
106 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
107 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
108 return sizeof (struct GNUNET_MessageHeader);
113 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 struct sockaddr_in sa;
116 struct sockaddr *sap[2];
119 sap[0] = (struct sockaddr *) &sa;
120 slens[0] = sizeof (sa);
123 memset (&sa, 0, sizeof (sa));
124 #if HAVE_SOCKADDR_IN_SIN_LEN
125 sa.sin_len = sizeof (sa);
127 sa.sin_family = AF_INET;
128 sa.sin_port = htons (PORT);
129 server = GNUNET_SERVER_create (NULL, NULL, sap, slens, TIMEOUT, GNUNET_NO);
130 GNUNET_assert (server != NULL);
131 GNUNET_SERVER_add_handlers (server, handlers);
132 GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, NULL);
133 cfg = GNUNET_CONFIGURATION_create ();
134 GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
135 GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME",
137 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
139 cc = GNUNET_CLIENT_connect ("test-server", cfg);
140 GNUNET_assert (cc != NULL);
141 GNUNET_assert (NULL !=
142 GNUNET_CLIENT_notify_transmit_ready (cc,
144 GNUNET_MessageHeader),
146 &transmit_initial_message,
152 * Main method, starts scheduler with task1,
153 * checks that "ok" is correct at the end.
159 GNUNET_SCHEDULER_run (&task, &ok);
165 main (int argc, char *argv[])
169 GNUNET_log_setup ("test_server_disconnect", "WARNING", NULL);
175 /* end of test_server_disconnect.c */