blob: bb1d47cec52d7e238ce31ee132f095bc4129e089 [file] [log] [blame]
Roger Meierc1010922010-11-26 10:17:48 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Roger Meier213a6642010-10-27 12:30:11 +000020#include <assert.h>
21#include <netdb.h>
Roger Meier63243c62014-09-29 20:29:58 +020022#include <sys/wait.h>
Roger Meier213a6642010-10-27 12:30:11 +000023
Roger Meiere3da7682013-01-11 11:41:53 +010024#include <thrift/c_glib/transport/thrift_transport.h>
Roger Meier63243c62014-09-29 20:29:58 +020025#include <thrift/c_glib/transport/thrift_buffered_transport.h>
Roger Meiere3da7682013-01-11 11:41:53 +010026#include <thrift/c_glib/transport/thrift_server_transport.h>
27#include <thrift/c_glib/transport/thrift_server_socket.h>
Roger Meier213a6642010-10-27 12:30:11 +000028
29#define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }
30
31/* substituted functions to test failures of system and library calls */
32static int socket_error = 0;
33int
34my_socket(int domain, int type, int protocol)
35{
36 if (socket_error == 0)
37 {
38 return socket (domain, type, protocol);
39 }
40 return -1;
41}
42
43static int recv_error = 0;
44ssize_t
45my_recv(int socket, void *buffer, size_t length, int flags)
46{
47 if (recv_error == 0)
48 {
49 return recv (socket, buffer, length, flags);
50 }
51 return -1;
52}
53
54static int send_error = 0;
55ssize_t
56my_send(int socket, const void *buffer, size_t length, int flags)
57{
58 if (send_error == 0)
59 {
60 return send (socket, buffer, length, flags);
61 }
62 return -1;
63}
64
65#define socket my_socket
66#define recv my_recv
67#define send my_send
Roger Meiere3da7682013-01-11 11:41:53 +010068#include "../src/thrift/c_glib/transport/thrift_socket.c"
Roger Meier213a6642010-10-27 12:30:11 +000069#undef socket
70#undef recv
71#undef send
72
73static const char TEST_ADDRESS[] = "localhost";
74static const short TEST_PORT = 64444;
75
76static void thrift_socket_server (const int port);
77
78/* test object creation and destruction */
79static void
80test_create_and_destroy(void)
81{
82 gchar *hostname = NULL;
83 guint port = 0;
84
85 GObject *object = NULL;
86 object = g_object_new (THRIFT_TYPE_SOCKET, NULL);
87 assert (object != NULL);
88 g_object_get (G_OBJECT(object), "hostname", &hostname, "port", &port, NULL);
89 g_free (hostname);
90
91 g_object_unref (object);
92}
93
94static void
95test_open_and_close(void)
96{
97 ThriftSocket *tsocket = NULL;
98 ThriftTransport *transport = NULL;
99 GError *err = NULL;
100
101 /* open a connection and close it */
102 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
103 "port", 51188, NULL);
104 transport = THRIFT_TRANSPORT (tsocket);
105 thrift_socket_open (transport, NULL);
106 assert (thrift_socket_is_open (transport) == TRUE);
107 thrift_socket_close (transport, NULL);
108 assert (thrift_socket_is_open (transport) == FALSE);
109
110 /* test close failure */
111 tsocket->sd = -1;
112 thrift_socket_close (transport, NULL);
113 g_object_unref (tsocket);
114
115 /* try a hostname lookup failure */
116 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
117 NULL);
118 transport = THRIFT_TRANSPORT (tsocket);
119 assert (thrift_socket_open (transport, &err) == FALSE);
120 g_object_unref (tsocket);
121 g_error_free (err);
122 err = NULL;
123
124 /* try an error call to socket() */
125 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", NULL);
126 transport = THRIFT_TRANSPORT (tsocket);
127 socket_error = 1;
128 assert (thrift_socket_open (transport, &err) == FALSE);
129 socket_error = 0;
130 g_object_unref (tsocket);
131 g_error_free (err);
132}
133
134static void
135test_read_and_write(void)
136{
137 int status;
138 pid_t pid;
139 ThriftSocket *tsocket = NULL;
140 ThriftTransport *transport = NULL;
141 int port = 51199;
142 guchar buf[10] = TEST_DATA; /* a buffer */
143
144 pid = fork ();
145 assert ( pid >= 0 );
146
147 if ( pid == 0 )
148 {
149 /* child listens */
150 thrift_socket_server (port);
151 exit (0);
152 } else {
153 /* parent connects, wait a bit for the socket to be created */
154 sleep (1);
155
156 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
157 "port", port, NULL);
158 transport = THRIFT_TRANSPORT (tsocket);
159 assert (thrift_socket_open (transport, NULL) == TRUE);
160 assert (thrift_socket_is_open (transport));
161 thrift_socket_write (transport, buf, 10, NULL);
162
163 /* write fail */
164 send_error = 1;
165 thrift_socket_write (transport, buf, 1, NULL);
166 send_error = 0;
167
168 thrift_socket_write_end (transport, NULL);
169 thrift_socket_flush (transport, NULL);
170 thrift_socket_close (transport, NULL);
171 g_object_unref (tsocket);
172
173 assert ( wait (&status) == pid );
174 assert ( status == 0 );
175 }
176}
177
Roger Meier63243c62014-09-29 20:29:58 +0200178/* test ThriftSocket's peek() implementation */
179static void
180test_peek(void)
181{
182 gint status;
183 pid_t pid;
184 guint port = 51199;
185 gchar data = 'A';
186 ThriftTransport *client_transport;
187 GError *error = NULL;
188
189 client_transport = g_object_new (THRIFT_TYPE_SOCKET,
190 "hostname", "localhost",
191 "port", port,
192 NULL);
193
194 /* thrift_transport_peek returns FALSE when the socket is closed */
195 g_assert (thrift_transport_is_open (client_transport) == FALSE);
196 g_assert (thrift_transport_peek (client_transport, &error) == FALSE);
197 g_assert (error == NULL);
198
199 pid = fork ();
200 g_assert (pid >= 0);
201
202 if (pid == 0)
203 {
204 ThriftServerTransport *server_transport = NULL;
205
206 g_object_unref (client_transport);
207
208 /* child listens */
209 server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
210 "port", port,
211 NULL);
212 g_assert (server_transport != NULL);
213
214 thrift_server_transport_listen (server_transport, &error);
215 g_assert (error == NULL);
216
217 client_transport = g_object_new
218 (THRIFT_TYPE_BUFFERED_TRANSPORT,
219 "transport", thrift_server_transport_accept (server_transport, &error),
220 "r_buf_size", 0,
221 "w_buf_size", sizeof data,
222 NULL);
223 g_assert (error == NULL);
224 g_assert (client_transport != NULL);
225
226 /* write exactly one character to the client */
227 g_assert (thrift_transport_write (client_transport,
228 &data,
229 sizeof data,
230 &error) == TRUE);
231
232 thrift_transport_flush (client_transport, &error);
233 thrift_transport_write_end (client_transport, &error);
234 thrift_transport_close (client_transport, &error);
235
236 g_object_unref (client_transport);
237 g_object_unref (server_transport);
238
239 exit (0);
240 }
241 else {
242 /* parent connects, wait a bit for the socket to be created */
243 sleep (1);
244
245 /* connect to the child */
246 thrift_transport_open (client_transport, &error);
247 g_assert (error == NULL);
248 g_assert (thrift_transport_is_open (client_transport) == TRUE);
249
250 /* thrift_transport_peek returns TRUE when the socket is open and there is
251 data available to be read */
252 g_assert (thrift_transport_peek (client_transport, &error) == TRUE);
253 g_assert (error == NULL);
254
255 /* read exactly one character from the server */
256 g_assert_cmpint (thrift_transport_read (client_transport,
257 &data,
258 sizeof data,
259 &error), ==, sizeof data);
260
261 /* thrift_transport_peek returns FALSE when the socket is open but there is
262 no (more) data available to be read */
263 g_assert (thrift_transport_is_open (client_transport) == TRUE);
264 g_assert (thrift_transport_peek (client_transport, &error) == FALSE);
265 g_assert (error == NULL);
266
267 thrift_transport_read_end (client_transport, &error);
268 thrift_transport_close (client_transport, &error);
269
270 g_object_unref (client_transport);
271
272 g_assert (wait (&status) == pid);
273 g_assert (status == 0);
274 }
275}
276
Roger Meier213a6642010-10-27 12:30:11 +0000277static void
278thrift_socket_server (const int port)
279{
280 int bytes = 0;
281 ThriftServerTransport *transport = NULL;
282 ThriftTransport *client = NULL;
283 guchar buf[10]; /* a buffer */
284 guchar match[10] = TEST_DATA;
285
286 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
287 "port", port, NULL);
288
289 transport = THRIFT_SERVER_TRANSPORT (tsocket);
290 thrift_server_transport_listen (transport, NULL);
291 client = thrift_server_transport_accept (transport, NULL);
292 assert (client != NULL);
293
294 /* read 10 bytes */
295 bytes = thrift_socket_read (client, buf, 10, NULL);
296 assert (bytes == 10); /* make sure we've read 10 bytes */
297 assert ( memcmp(buf, match, 10) == 0 ); /* make sure what we got matches */
298
299 /* failed read */
300 recv_error = 1;
301 thrift_socket_read (client, buf, 1, NULL);
302 recv_error = 0;
303
304 thrift_socket_read_end (client, NULL);
305 thrift_socket_close (client, NULL);
306 g_object_unref (tsocket);
307 g_object_unref (client);
308}
309
310int
Roger Meierc1010922010-11-26 10:17:48 +0000311main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000312{
Jens Geyer1c190272015-07-28 23:15:18 +0200313#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier213a6642010-10-27 12:30:11 +0000314 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200315#endif
316
Roger Meierc1010922010-11-26 10:17:48 +0000317 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000318
Roger Meierc1010922010-11-26 10:17:48 +0000319 g_test_add_func ("/testtransportsocket/CreateAndDestroy", test_create_and_destroy);
320 g_test_add_func ("/testtransportsocket/OpenAndClose", test_open_and_close);
321 g_test_add_func ("/testtransportsocket/ReadAndWrite", test_read_and_write);
Roger Meier63243c62014-09-29 20:29:58 +0200322 g_test_add_func ("/testtransportsocket/Peek", test_peek);
Roger Meierc1010922010-11-26 10:17:48 +0000323
324 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000325}
326