blob: 8e963754eef0a0875f0f65ec42d325f42ac5f642 [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)
36 {
37 return socket (domain, type, protocol);
38 }
39 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)
47 {
48 return recv (socket, buffer, length, flags);
49 }
50 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)
58 {
59 return send (socket, buffer, length, flags);
60 }
61 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);
73
74/* 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;
96
97 /* open a connection and close it */
98 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
99 "port", 51188, NULL);
100 transport = THRIFT_TRANSPORT (tsocket);
101 thrift_socket_open (transport, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400102 g_assert (thrift_socket_is_open (transport) == TRUE);
Roger Meier213a6642010-10-27 12:30:11 +0000103 thrift_socket_close (transport, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400104 g_assert (thrift_socket_is_open (transport) == FALSE);
Roger Meier213a6642010-10-27 12:30:11 +0000105
106 /* test close failure */
107 tsocket->sd = -1;
108 thrift_socket_close (transport, NULL);
109 g_object_unref (tsocket);
110
111 /* try a hostname lookup failure */
112 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
113 NULL);
114 transport = THRIFT_TRANSPORT (tsocket);
James E. King, III43f4bf22017-10-28 12:54:02 -0400115 g_assert (thrift_socket_open (transport, &err) == FALSE);
Roger Meier213a6642010-10-27 12:30:11 +0000116 g_object_unref (tsocket);
117 g_error_free (err);
118 err = NULL;
119
120 /* try an error call to socket() */
121 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", NULL);
122 transport = THRIFT_TRANSPORT (tsocket);
123 socket_error = 1;
James E. King, III43f4bf22017-10-28 12:54:02 -0400124 g_assert (thrift_socket_open (transport, &err) == FALSE);
Roger Meier213a6642010-10-27 12:30:11 +0000125 socket_error = 0;
126 g_object_unref (tsocket);
127 g_error_free (err);
128}
129
130static void
131test_read_and_write(void)
132{
133 int status;
134 pid_t pid;
135 ThriftSocket *tsocket = NULL;
136 ThriftTransport *transport = NULL;
137 int port = 51199;
138 guchar buf[10] = TEST_DATA; /* a buffer */
139
140 pid = fork ();
James E. King, III43f4bf22017-10-28 12:54:02 -0400141 g_assert ( pid >= 0 );
Roger Meier213a6642010-10-27 12:30:11 +0000142
143 if ( pid == 0 )
144 {
145 /* child listens */
146 thrift_socket_server (port);
147 exit (0);
148 } else {
149 /* parent connects, wait a bit for the socket to be created */
150 sleep (1);
151
152 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
153 "port", port, NULL);
154 transport = THRIFT_TRANSPORT (tsocket);
James E. King, III43f4bf22017-10-28 12:54:02 -0400155 g_assert (thrift_socket_open (transport, NULL) == TRUE);
156 g_assert (thrift_socket_is_open (transport));
Roger Meier213a6642010-10-27 12:30:11 +0000157 thrift_socket_write (transport, buf, 10, NULL);
158
159 /* write fail */
160 send_error = 1;
161 thrift_socket_write (transport, buf, 1, NULL);
162 send_error = 0;
163
164 thrift_socket_write_end (transport, NULL);
165 thrift_socket_flush (transport, NULL);
166 thrift_socket_close (transport, NULL);
167 g_object_unref (tsocket);
168
James E. King, III43f4bf22017-10-28 12:54:02 -0400169 g_assert ( wait (&status) == pid );
170 g_assert ( status == 0 );
Roger Meier213a6642010-10-27 12:30:11 +0000171 }
172}
173
Roger Meier63243c62014-09-29 20:29:58 +0200174/* test ThriftSocket's peek() implementation */
175static void
176test_peek(void)
177{
178 gint status;
179 pid_t pid;
180 guint port = 51199;
181 gchar data = 'A';
182 ThriftTransport *client_transport;
183 GError *error = NULL;
184
185 client_transport = g_object_new (THRIFT_TYPE_SOCKET,
186 "hostname", "localhost",
187 "port", port,
188 NULL);
189
190 /* thrift_transport_peek returns FALSE when the socket is closed */
191 g_assert (thrift_transport_is_open (client_transport) == FALSE);
192 g_assert (thrift_transport_peek (client_transport, &error) == FALSE);
193 g_assert (error == NULL);
194
195 pid = fork ();
196 g_assert (pid >= 0);
197
198 if (pid == 0)
199 {
200 ThriftServerTransport *server_transport = NULL;
201
202 g_object_unref (client_transport);
203
204 /* child listens */
205 server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
206 "port", port,
207 NULL);
208 g_assert (server_transport != NULL);
209
210 thrift_server_transport_listen (server_transport, &error);
211 g_assert (error == NULL);
212
213 client_transport = g_object_new
214 (THRIFT_TYPE_BUFFERED_TRANSPORT,
215 "transport", thrift_server_transport_accept (server_transport, &error),
216 "r_buf_size", 0,
217 "w_buf_size", sizeof data,
218 NULL);
219 g_assert (error == NULL);
220 g_assert (client_transport != NULL);
221
222 /* write exactly one character to the client */
223 g_assert (thrift_transport_write (client_transport,
224 &data,
225 sizeof data,
226 &error) == TRUE);
227
228 thrift_transport_flush (client_transport, &error);
229 thrift_transport_write_end (client_transport, &error);
230 thrift_transport_close (client_transport, &error);
231
232 g_object_unref (client_transport);
233 g_object_unref (server_transport);
234
235 exit (0);
236 }
237 else {
238 /* parent connects, wait a bit for the socket to be created */
239 sleep (1);
240
241 /* connect to the child */
242 thrift_transport_open (client_transport, &error);
243 g_assert (error == NULL);
244 g_assert (thrift_transport_is_open (client_transport) == TRUE);
245
246 /* thrift_transport_peek returns TRUE when the socket is open and there is
247 data available to be read */
248 g_assert (thrift_transport_peek (client_transport, &error) == TRUE);
249 g_assert (error == NULL);
250
251 /* read exactly one character from the server */
252 g_assert_cmpint (thrift_transport_read (client_transport,
253 &data,
254 sizeof data,
255 &error), ==, sizeof data);
256
257 /* thrift_transport_peek returns FALSE when the socket is open but there is
258 no (more) data available to be read */
259 g_assert (thrift_transport_is_open (client_transport) == TRUE);
260 g_assert (thrift_transport_peek (client_transport, &error) == FALSE);
261 g_assert (error == NULL);
262
263 thrift_transport_read_end (client_transport, &error);
264 thrift_transport_close (client_transport, &error);
265
266 g_object_unref (client_transport);
267
268 g_assert (wait (&status) == pid);
269 g_assert (status == 0);
270 }
271}
272
Roger Meier213a6642010-10-27 12:30:11 +0000273static void
274thrift_socket_server (const int port)
275{
276 int bytes = 0;
277 ThriftServerTransport *transport = NULL;
278 ThriftTransport *client = NULL;
279 guchar buf[10]; /* a buffer */
280 guchar match[10] = TEST_DATA;
281
282 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
283 "port", port, NULL);
284
285 transport = THRIFT_SERVER_TRANSPORT (tsocket);
286 thrift_server_transport_listen (transport, NULL);
287 client = thrift_server_transport_accept (transport, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400288 g_assert (client != NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000289
290 /* read 10 bytes */
291 bytes = thrift_socket_read (client, buf, 10, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400292 g_assert (bytes == 10); /* make sure we've read 10 bytes */
293 g_assert ( memcmp(buf, match, 10) == 0 ); /* make sure what we got matches */
Roger Meier213a6642010-10-27 12:30:11 +0000294
295 /* failed read */
296 recv_error = 1;
297 thrift_socket_read (client, buf, 1, NULL);
298 recv_error = 0;
299
300 thrift_socket_read_end (client, NULL);
301 thrift_socket_close (client, NULL);
302 g_object_unref (tsocket);
303 g_object_unref (client);
304}
305
306int
Roger Meierc1010922010-11-26 10:17:48 +0000307main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000308{
Jens Geyer1c190272015-07-28 23:15:18 +0200309#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier213a6642010-10-27 12:30:11 +0000310 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200311#endif
312
Roger Meierc1010922010-11-26 10:17:48 +0000313 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000314
Roger Meierc1010922010-11-26 10:17:48 +0000315 g_test_add_func ("/testtransportsocket/CreateAndDestroy", test_create_and_destroy);
316 g_test_add_func ("/testtransportsocket/OpenAndClose", test_open_and_close);
317 g_test_add_func ("/testtransportsocket/ReadAndWrite", test_read_and_write);
Roger Meier63243c62014-09-29 20:29:58 +0200318 g_test_add_func ("/testtransportsocket/Peek", test_peek);
Roger Meierc1010922010-11-26 10:17:48 +0000319
320 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000321}
322