Merge branch 'master'
[oweals/gnunet.git] / src / util / test_server_disconnect.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2016 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_MQ_Handle *mq;
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_MQ_destroy (mq);
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,
80          struct GNUNET_SERVER_Client *client,
81          const struct GNUNET_MessageHeader *message)
82 {
83   GNUNET_assert (ok == 2);
84   ok = 3;
85   GNUNET_SERVER_client_keep (client);
86   GNUNET_SCHEDULER_add_now (&server_disconnect, client);
87   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
88   GNUNET_assert (MY_TYPE == ntohs (message->type));
89   GNUNET_SERVER_receive_done (client, GNUNET_OK);
90 }
91
92
93 static struct GNUNET_SERVER_MessageHandler handlers[] = {
94   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
95   {NULL, NULL, 0, 0}
96 };
97
98
99 static void
100 task (void *cls)
101 {
102   struct sockaddr_in sa;
103   struct sockaddr *sap[2];
104   socklen_t slens[2];
105   struct GNUNET_MQ_Envelope *env;
106   struct GNUNET_MessageHeader *msg;
107
108   sap[0] = (struct sockaddr *) &sa;
109   slens[0] = sizeof (sa);
110   sap[1] = NULL;
111   slens[1] = 0;
112   memset (&sa, 0, sizeof (sa));
113 #if HAVE_SOCKADDR_IN_SIN_LEN
114   sa.sin_len = sizeof (sa);
115 #endif
116   sa.sin_family = AF_INET;
117   sa.sin_port = htons (PORT);
118   server = GNUNET_SERVER_create (NULL, NULL, sap, slens, TIMEOUT, GNUNET_NO);
119   GNUNET_assert (server != NULL);
120   GNUNET_SERVER_add_handlers (server, handlers);
121   GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, NULL);
122   cfg = GNUNET_CONFIGURATION_create ();
123   GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
124   GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME",
125                                          "localhost");
126   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
127                                          "localhost");
128   mq = GNUNET_CLIENT_connect (cfg,
129                               "test-server",
130                               NULL,
131                               NULL,
132                               NULL);
133   GNUNET_assert (NULL != mq);
134   ok = 2;
135   env = GNUNET_MQ_msg (msg,
136                        MY_TYPE);
137   GNUNET_MQ_send (mq,
138                   env);
139 }
140
141
142 /**
143  * Main method, starts scheduler with task1,
144  * checks that "ok" is correct at the end.
145  */
146 static int
147 check ()
148 {
149   ok = 1;
150   GNUNET_SCHEDULER_run (&task, &ok);
151   return ok;
152 }
153
154
155 int
156 main (int argc, char *argv[])
157 {
158   int ret = 0;
159
160   GNUNET_log_setup ("test_server_disconnect", "WARNING", NULL);
161   ret += check ();
162
163   return ret;
164 }
165
166 /* end of test_server_disconnect.c */