blob: 89c61b9101933007f84bfd3acc33b58492b4fdf9 [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 <netdb.h>
Roger Meier63243c62014-09-29 20:29:58 +020021#include <sys/wait.h>
Roger Meier213a6642010-10-27 12:30:11 +000022
Roger Meiere3da7682013-01-11 11:41:53 +010023#include <thrift/c_glib/transport/thrift_transport.h>
Roger Meier63243c62014-09-29 20:29:58 +020024#include <thrift/c_glib/transport/thrift_buffered_transport.h>
Roger Meiere3da7682013-01-11 11:41:53 +010025#include <thrift/c_glib/transport/thrift_server_transport.h>
26#include <thrift/c_glib/transport/thrift_server_socket.h>
Roger Meier213a6642010-10-27 12:30:11 +000027
28#define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }
29
30/* substituted functions to test failures of system and library calls */
31static int socket_error = 0;
32int
33my_socket(int domain, int type, int protocol)
34{
35 if (socket_error == 0)
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020036 {
37 return socket (domain, type, protocol);
38 }
Roger Meier213a6642010-10-27 12:30:11 +000039 return -1;
40}
41
42static int recv_error = 0;
43ssize_t
44my_recv(int socket, void *buffer, size_t length, int flags)
45{
46 if (recv_error == 0)
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020047 {
48 return recv (socket, buffer, length, flags);
49 }
Roger Meier213a6642010-10-27 12:30:11 +000050 return -1;
51}
52
53static int send_error = 0;
54ssize_t
55my_send(int socket, const void *buffer, size_t length, int flags)
56{
57 if (send_error == 0)
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020058 {
59 return send (socket, buffer, length, flags);
60 }
Roger Meier213a6642010-10-27 12:30:11 +000061 return -1;
62}
63
64#define socket my_socket
65#define recv my_recv
66#define send my_send
Roger Meiere3da7682013-01-11 11:41:53 +010067#include "../src/thrift/c_glib/transport/thrift_socket.c"
Roger Meier213a6642010-10-27 12:30:11 +000068#undef socket
69#undef recv
70#undef send
71
Roger Meier213a6642010-10-27 12:30:11 +000072static void thrift_socket_server (const int port);
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020073static void thrift_socket_server_open (const int port, int times);
Roger Meier213a6642010-10-27 12:30:11 +000074/* test object creation and destruction */
75static void
76test_create_and_destroy(void)
77{
78 gchar *hostname = NULL;
79 guint port = 0;
80
81 GObject *object = NULL;
82 object = g_object_new (THRIFT_TYPE_SOCKET, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -040083 g_assert (object != NULL);
Roger Meier213a6642010-10-27 12:30:11 +000084 g_object_get (G_OBJECT(object), "hostname", &hostname, "port", &port, NULL);
85 g_free (hostname);
86
87 g_object_unref (object);
88}
89
90static void
91test_open_and_close(void)
92{
93 ThriftSocket *tsocket = NULL;
94 ThriftTransport *transport = NULL;
95 GError *err = NULL;
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020096 int port = 51199;
97 pid_t pid;
98 int status;
Roger Meier213a6642010-10-27 12:30:11 +000099
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200100 pid = fork ();
101 g_assert ( pid >= 0 );
Roger Meier213a6642010-10-27 12:30:11 +0000102
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200103 if ( pid == 0 )
104 {
105 /* child listens */
106 thrift_socket_server_open (port, 1);
107 exit (0);
108 } else {
109 /* parent connects, wait a bit for the socket to be created */
110 sleep (1);
Roger Meier213a6642010-10-27 12:30:11 +0000111
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200112 /* open a connection and close it */
113 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
114 "port", port, NULL);
115 transport = THRIFT_TRANSPORT (tsocket);
116 thrift_socket_open (transport, NULL);
117 g_assert (thrift_socket_is_open (transport) == TRUE);
118 thrift_socket_close (transport, NULL);
119 g_assert (thrift_socket_is_open (transport) == FALSE);
Roger Meier213a6642010-10-27 12:30:11 +0000120
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200121 /* test close failure */
122 tsocket->sd = -1;
123 thrift_socket_close (transport, NULL);
124 g_object_unref (tsocket);
125
126 /* try a hostname lookup failure */
127 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
128 NULL);
129 transport = THRIFT_TRANSPORT (tsocket);
130 g_assert (thrift_socket_open (transport, &err) == FALSE);
131 g_object_unref (tsocket);
132 g_error_free (err);
133 err = NULL;
134
135 /* try an error call to socket() */
136 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", NULL);
137 transport = THRIFT_TRANSPORT (tsocket);
138 socket_error = 1;
139 g_assert (thrift_socket_open (transport, &err) == FALSE);
140 socket_error = 0;
141 g_object_unref (tsocket);
142 g_error_free (err);
143 g_assert ( wait (&status) == pid );
144 g_assert ( status == 0 );
145 }
Roger Meier213a6642010-10-27 12:30:11 +0000146}
147
148static void
149test_read_and_write(void)
150{
Roger Meier213a6642010-10-27 12:30:11 +0000151 ThriftSocket *tsocket = NULL;
152 ThriftTransport *transport = NULL;
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200153 pid_t pid;
Roger Meier213a6642010-10-27 12:30:11 +0000154 int port = 51199;
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200155 int status;
Roger Meier213a6642010-10-27 12:30:11 +0000156 guchar buf[10] = TEST_DATA; /* a buffer */
157
158 pid = fork ();
James E. King, III43f4bf22017-10-28 12:54:02 -0400159 g_assert ( pid >= 0 );
Roger Meier213a6642010-10-27 12:30:11 +0000160
161 if ( pid == 0 )
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200162 {
163 /* child listens */
164 thrift_socket_server (port);
165 exit (0);
166 } else {
167 /* parent connects, wait a bit for the socket to be created */
168 sleep (1);
Roger Meier213a6642010-10-27 12:30:11 +0000169
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200170 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
171 "port", port, NULL);
172 transport = THRIFT_TRANSPORT (tsocket);
173 g_assert (thrift_socket_open (transport, NULL) == TRUE);
174 g_assert (thrift_socket_is_open (transport));
175 thrift_socket_write (transport, buf, 10, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000176
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200177 /* write fail */
178 send_error = 1;
179 thrift_socket_write (transport, buf, 1, NULL);
180 send_error = 0;
Roger Meier213a6642010-10-27 12:30:11 +0000181
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200182 thrift_socket_write_end (transport, NULL);
183 thrift_socket_flush (transport, NULL);
184 thrift_socket_close (transport, NULL);
185 g_object_unref (tsocket);
Roger Meier213a6642010-10-27 12:30:11 +0000186
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200187 g_assert ( wait (&status) == pid );
188 g_assert ( status == 0 );
189 }
Roger Meier213a6642010-10-27 12:30:11 +0000190}
191
Roger Meier63243c62014-09-29 20:29:58 +0200192/* test ThriftSocket's peek() implementation */
193static void
194test_peek(void)
195{
196 gint status;
197 pid_t pid;
198 guint port = 51199;
199 gchar data = 'A';
200 ThriftTransport *client_transport;
201 GError *error = NULL;
202
203 client_transport = g_object_new (THRIFT_TYPE_SOCKET,
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200204 "hostname", "localhost",
205 "port", port,
206 NULL);
Roger Meier63243c62014-09-29 20:29:58 +0200207
208 /* thrift_transport_peek returns FALSE when the socket is closed */
209 g_assert (thrift_transport_is_open (client_transport) == FALSE);
210 g_assert (thrift_transport_peek (client_transport, &error) == FALSE);
211 g_assert (error == NULL);
212
213 pid = fork ();
214 g_assert (pid >= 0);
215
216 if (pid == 0)
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200217 {
218 ThriftServerTransport *server_transport = NULL;
Roger Meier63243c62014-09-29 20:29:58 +0200219
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200220 g_object_unref (client_transport);
Roger Meier63243c62014-09-29 20:29:58 +0200221
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200222 /* child listens */
223 server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
224 "port", port,
225 NULL);
226 g_assert (server_transport != NULL);
Roger Meier63243c62014-09-29 20:29:58 +0200227
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200228 thrift_server_transport_listen (server_transport, &error);
229 g_assert (error == NULL);
Roger Meier63243c62014-09-29 20:29:58 +0200230
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200231 client_transport = g_object_new
232 (THRIFT_TYPE_BUFFERED_TRANSPORT,
233 "transport", thrift_server_transport_accept (server_transport, &error),
234 "r_buf_size", 0,
235 "w_buf_size", sizeof data,
236 NULL);
237 g_assert (error == NULL);
238 g_assert (client_transport != NULL);
Roger Meier63243c62014-09-29 20:29:58 +0200239
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200240 /* write exactly one character to the client */
241 g_assert (thrift_transport_write (client_transport,
242 &data,
243 sizeof data,
244 &error) == TRUE);
Roger Meier63243c62014-09-29 20:29:58 +0200245
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200246 thrift_transport_flush (client_transport, &error);
247 thrift_transport_write_end (client_transport, &error);
248 thrift_transport_close (client_transport, &error);
Roger Meier63243c62014-09-29 20:29:58 +0200249
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200250 g_object_unref (client_transport);
251 g_object_unref (server_transport);
Roger Meier63243c62014-09-29 20:29:58 +0200252
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200253 exit (0);
254 }
Roger Meier63243c62014-09-29 20:29:58 +0200255 else {
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200256 /* parent connects, wait a bit for the socket to be created */
257 sleep (1);
Roger Meier63243c62014-09-29 20:29:58 +0200258
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200259 /* connect to the child */
260 thrift_transport_open (client_transport, &error);
261 g_assert (error == NULL);
262 g_assert (thrift_transport_is_open (client_transport) == TRUE);
Roger Meier63243c62014-09-29 20:29:58 +0200263
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200264 /* thrift_transport_peek returns TRUE when the socket is open and there is
Roger Meier63243c62014-09-29 20:29:58 +0200265 data available to be read */
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200266 g_assert (thrift_transport_peek (client_transport, &error) == TRUE);
267 g_assert (error == NULL);
Roger Meier63243c62014-09-29 20:29:58 +0200268
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200269 /* read exactly one character from the server */
270 g_assert_cmpint (thrift_transport_read (client_transport,
271 &data,
272 sizeof data,
273 &error), ==, sizeof data);
Roger Meier63243c62014-09-29 20:29:58 +0200274
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200275 /* thrift_transport_peek returns FALSE when the socket is open but there is
Roger Meier63243c62014-09-29 20:29:58 +0200276 no (more) data available to be read */
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200277 g_assert (thrift_transport_is_open (client_transport) == TRUE);
278 g_assert (thrift_transport_peek (client_transport, &error) == FALSE);
279 g_assert (error == NULL);
Roger Meier63243c62014-09-29 20:29:58 +0200280
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200281 thrift_transport_read_end (client_transport, &error);
282 thrift_transport_close (client_transport, &error);
Roger Meier63243c62014-09-29 20:29:58 +0200283
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200284 g_object_unref (client_transport);
Roger Meier63243c62014-09-29 20:29:58 +0200285
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200286 g_assert (wait (&status) == pid);
287 g_assert (status == 0);
Roger Meier63243c62014-09-29 20:29:58 +0200288 }
289}
290
Roger Meier213a6642010-10-27 12:30:11 +0000291static void
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200292thrift_socket_server_open (const int port, int times)
293{
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200294 ThriftServerTransport *transport = NULL;
295 ThriftTransport *client = NULL;
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200296 int i;
297 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
298 "port", port, NULL);
299
300 transport = THRIFT_SERVER_TRANSPORT (tsocket);
301 thrift_server_transport_listen (transport, NULL);
302 for(i=0;i<times;i++){
303 client = thrift_server_transport_accept (transport, NULL);
304 g_assert (client != NULL);
305 thrift_socket_close (client, NULL);
306 g_object_unref (client);
307 }
308 g_object_unref (tsocket);
309}
310
311
312static void
Roger Meier213a6642010-10-27 12:30:11 +0000313thrift_socket_server (const int port)
314{
315 int bytes = 0;
316 ThriftServerTransport *transport = NULL;
317 ThriftTransport *client = NULL;
318 guchar buf[10]; /* a buffer */
319 guchar match[10] = TEST_DATA;
320
321 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200322 "port", port, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000323
324 transport = THRIFT_SERVER_TRANSPORT (tsocket);
325 thrift_server_transport_listen (transport, NULL);
326 client = thrift_server_transport_accept (transport, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400327 g_assert (client != NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000328
329 /* read 10 bytes */
330 bytes = thrift_socket_read (client, buf, 10, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400331 g_assert (bytes == 10); /* make sure we've read 10 bytes */
332 g_assert ( memcmp(buf, match, 10) == 0 ); /* make sure what we got matches */
Roger Meier213a6642010-10-27 12:30:11 +0000333
334 /* failed read */
335 recv_error = 1;
336 thrift_socket_read (client, buf, 1, NULL);
337 recv_error = 0;
338
339 thrift_socket_read_end (client, NULL);
340 thrift_socket_close (client, NULL);
341 g_object_unref (tsocket);
342 g_object_unref (client);
343}
344
345int
Roger Meierc1010922010-11-26 10:17:48 +0000346main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000347{
Jens Geyer1c190272015-07-28 23:15:18 +0200348#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier213a6642010-10-27 12:30:11 +0000349 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200350#endif
351
Roger Meierc1010922010-11-26 10:17:48 +0000352 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000353
Roger Meierc1010922010-11-26 10:17:48 +0000354 g_test_add_func ("/testtransportsocket/CreateAndDestroy", test_create_and_destroy);
355 g_test_add_func ("/testtransportsocket/OpenAndClose", test_open_and_close);
356 g_test_add_func ("/testtransportsocket/ReadAndWrite", test_read_and_write);
Roger Meier63243c62014-09-29 20:29:58 +0200357 g_test_add_func ("/testtransportsocket/Peek", test_peek);
Roger Meierc1010922010-11-26 10:17:48 +0000358
359 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000360}
361