proper shutdown
[oweals/gnunet.git] / src / util / test_connection.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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_connection.c
22  * @brief tests for connection.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_connection_lib.h"
27 #include "gnunet_scheduler_lib.h"
28 #include "gnunet_time_lib.h"
29
30 #define VERBOSE GNUNET_NO
31
32 #define PORT 12435
33
34
35 static struct GNUNET_CONNECTION_Handle *csock;
36
37 static struct GNUNET_CONNECTION_Handle *asock;
38
39 static struct GNUNET_CONNECTION_Handle *lsock;
40
41 static size_t sofar;
42
43 static struct GNUNET_NETWORK_Handle *ls;
44
45 static struct GNUNET_CONFIGURATION_Handle *cfg;
46
47 /**
48  * Create and initialize a listen socket for the server.
49  *
50  * @return -1 on error, otherwise the listen socket
51  */
52 static struct GNUNET_NETWORK_Handle *
53 open_listen_socket ()
54 {
55   const static int on = 1;
56   struct sockaddr_in sa;
57   struct GNUNET_NETWORK_Handle *desc;
58
59   memset (&sa, 0, sizeof (sa));
60 #if HAVE_SOCKADDR_IN_SIN_LEN
61   sa.sin_len = sizeof (sa);
62 #endif
63   sa.sin_port = htons (PORT);
64   sa.sin_family = AF_INET;
65   desc = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
66   GNUNET_assert (desc != NULL);
67   if (GNUNET_NETWORK_socket_setsockopt
68       (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
69     GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
70                 "setsockopt");
71   GNUNET_assert (GNUNET_NETWORK_socket_bind
72                  (desc, (const struct sockaddr *) &sa,
73                   sizeof (sa)) == GNUNET_OK);
74   GNUNET_NETWORK_socket_listen (desc, 5);
75   return desc;
76 }
77
78 static void
79 receive_check (void *cls,
80                const void *buf,
81                size_t available,
82                const struct sockaddr *addr, socklen_t addrlen, int errCode)
83 {
84   int *ok = cls;
85
86 #if VERBOSE
87   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive validates incoming data\n");
88 #endif
89   GNUNET_assert (buf != NULL);  /* no timeout */
90   if (0 == memcmp (&"Hello World"[sofar], buf, available))
91     sofar += available;
92   if (sofar < 12)
93     {
94 #if VERBOSE
95       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive needs more data\n");
96 #endif
97       GNUNET_CONNECTION_receive (asock,
98                                  1024,
99                                  GNUNET_TIME_relative_multiply
100                                  (GNUNET_TIME_UNIT_SECONDS, 5),
101                                  &receive_check, cls);
102     }
103   else
104     {
105 #if VERBOSE
106       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107                   "Receive closes accepted socket\n");
108 #endif
109       *ok = 0;
110       GNUNET_CONNECTION_destroy (asock, GNUNET_YES);
111     }
112 }
113
114
115 static void
116 run_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 {
118 #if VERBOSE
119   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test accepts connection\n");
120 #endif
121   asock = GNUNET_CONNECTION_create_from_accept (NULL, NULL, ls);
122   GNUNET_assert (asock != NULL);
123   GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock));
124 #if VERBOSE
125   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test destroys listen socket\n");
126 #endif
127   GNUNET_CONNECTION_destroy (lsock, GNUNET_YES);
128 #if VERBOSE
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130               "Test asks to receive on accepted socket\n");
131 #endif
132   GNUNET_CONNECTION_receive (asock,
133                              1024,
134                              GNUNET_TIME_relative_multiply
135                              (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check,
136                              cls);
137 }
138
139 static size_t
140 make_hello (void *cls, size_t size, void *buf)
141 {
142 #if VERBOSE
143   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
144               "Test prepares to transmit on connect socket\n");
145 #endif
146   GNUNET_assert (size >= 12);
147   strcpy ((char *) buf, "Hello World");
148 #if VERBOSE
149   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test destroys client socket\n");
150 #endif
151   GNUNET_CONNECTION_destroy (csock, GNUNET_YES);
152   return 12;
153 }
154
155 static void
156 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 {
158   ls = open_listen_socket ();
159   lsock = GNUNET_CONNECTION_create_from_existing (ls);
160   GNUNET_assert (lsock != NULL);
161   csock = GNUNET_CONNECTION_create_from_connect (cfg,
162                                                  "localhost", PORT);
163   GNUNET_assert (csock != NULL);
164 #if VERBOSE
165   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test asks for write notification\n");
166 #endif
167   GNUNET_assert (NULL !=
168                  GNUNET_CONNECTION_notify_transmit_ready (csock,
169                                                           12,
170                                                           GNUNET_TIME_UNIT_SECONDS,
171                                                           &make_hello, NULL));
172 #if VERBOSE
173   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test prepares to accept\n");
174 #endif
175   GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
176                                  ls, &run_accept, cls);
177 }
178
179
180 /**
181  * Main method, starts scheduler with task ,
182  * checks that "ok" is correct at the end.
183  */
184 static int
185 check ()
186 {
187   int ok;
188
189   ok = 1;
190   cfg = GNUNET_CONFIGURATION_create ();
191   GNUNET_CONFIGURATION_set_value_string (cfg,
192                                          "resolver", "HOSTNAME", "localhost");
193   GNUNET_SCHEDULER_run (&task, &ok);
194   GNUNET_CONFIGURATION_destroy (cfg);
195   return ok;
196 }
197
198
199
200 int
201 main (int argc, char *argv[])
202 {
203   int ret = 0;
204
205   GNUNET_log_setup ("test_connection",
206 #if VERBOSE
207                     "DEBUG",
208 #else
209                     "WARNING",
210 #endif
211                     NULL);
212   ret += check ();
213   return ret;
214 }
215
216 /* end of test_connection.c */