blob: 1c15ef2ef1c85ed5aa4aa32ae336ef3197d05350 [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 Meier5c80b562014-05-04 22:19:46 +020021#include <signal.h>
Jens Geyer1c190272015-07-28 23:15:18 +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>
25#include <thrift/c_glib/transport/thrift_socket.h>
26#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
Roger Meiere3da7682013-01-11 11:41:53 +010031#include "../src/thrift/c_glib/transport/thrift_buffered_transport.c"
Roger Meier213a6642010-10-27 12:30:11 +000032
Roger Meier213a6642010-10-27 12:30:11 +000033static void thrift_server (const int port);
34
35/* test object creation and destruction */
36static void
37test_create_and_destroy(void)
38{
39 ThriftTransport *transport = NULL;
40 guint r_buf_size = 0;
41 guint w_buf_size = 0;
42
43 GObject *object = NULL;
44 object = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -040045 g_assert (object != NULL);
Roger Meier213a6642010-10-27 12:30:11 +000046 g_object_get (G_OBJECT (object), "transport", &transport,
47 "r_buf_size", &r_buf_size,
48 "w_buf_size", &w_buf_size, NULL);
49 g_object_unref (object);
50}
51
52static void
53test_open_and_close(void)
54{
55 ThriftSocket *tsocket = NULL;
56 ThriftTransport *transport = NULL;
57 GError *err = NULL;
58
59 /* create a ThriftSocket */
60 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
61 "port", 51188, NULL);
62
63 /* create a BufferedTransport wrapper of the Socket */
64 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
65 "transport", THRIFT_TRANSPORT (tsocket), NULL);
66
67 /* this shouldn't work */
James E. King, III43f4bf22017-10-28 12:54:02 -040068 g_assert (thrift_buffered_transport_open (transport, NULL) == FALSE);
69 g_assert (thrift_buffered_transport_is_open (transport) == TRUE);
70 g_assert (thrift_buffered_transport_close (transport, NULL) == TRUE);
Roger Meier213a6642010-10-27 12:30:11 +000071 g_object_unref (transport);
72 g_object_unref (tsocket);
73
74 /* try and underlying socket failure */
75 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
76 NULL);
77
78 /* create a BufferedTransport wrapper of the Socket */
79 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
80 "transport", THRIFT_TRANSPORT (tsocket), NULL);
81
James E. King, III43f4bf22017-10-28 12:54:02 -040082 g_assert (thrift_buffered_transport_open (transport, &err) == FALSE);
Roger Meier213a6642010-10-27 12:30:11 +000083 g_object_unref (transport);
84 g_object_unref (tsocket);
85 g_error_free (err);
86 err = NULL;
87}
88
89static void
90test_read_and_write(void)
91{
92 int status;
93 pid_t pid;
94 ThriftSocket *tsocket = NULL;
95 ThriftTransport *transport = NULL;
96 int port = 51199;
97 guchar buf[10] = TEST_DATA; /* a buffer */
98
99 pid = fork ();
James E. King, III43f4bf22017-10-28 12:54:02 -0400100 g_assert ( pid >= 0 );
Roger Meier213a6642010-10-27 12:30:11 +0000101
102 if ( pid == 0 )
103 {
104 /* child listens */
105 thrift_server (port);
106 exit (0);
107 } else {
108 /* parent connects, wait a bit for the socket to be created */
109 sleep (1);
110
111 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
112 "port", port, NULL);
113 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
114 "transport", THRIFT_TRANSPORT (tsocket),
115 "w_buf_size", 4, NULL);
116
James E. King, III43f4bf22017-10-28 12:54:02 -0400117 g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
118 g_assert (thrift_buffered_transport_is_open (transport));
Roger Meier213a6642010-10-27 12:30:11 +0000119
120 /* write 10 bytes */
121 thrift_buffered_transport_write (transport, buf, 10, NULL);
122
123 /* write 1 byte at a time */
124 thrift_buffered_transport_write (transport, buf, 1, NULL);
125 thrift_buffered_transport_write (transport, buf, 1, NULL);
126 thrift_buffered_transport_write (transport, buf, 1, NULL);
127
128 /* overflow the buffer */
129 thrift_buffered_transport_write (transport, buf, 2, NULL);
130 thrift_buffered_transport_write (transport, buf, 1, NULL);
131 thrift_buffered_transport_flush (transport, NULL);
132
133 /* write 1 byte and flush */
134 thrift_buffered_transport_write (transport, buf, 1, NULL);
135 thrift_buffered_transport_flush (transport, NULL);
136
137 /* write and overflow buffer with 2 system calls */
138 thrift_buffered_transport_write (transport, buf, 1, NULL);
139 thrift_buffered_transport_write (transport, buf, 3, NULL);
140
141 /* write 10 bytes */
142 thrift_buffered_transport_write (transport, buf, 10, NULL);
143
144 thrift_buffered_transport_write_end (transport, NULL);
145 thrift_buffered_transport_flush (transport, NULL);
146 thrift_buffered_transport_close (transport, NULL);
147
148 g_object_unref (transport);
149 g_object_unref (tsocket);
150
James E. King, III43f4bf22017-10-28 12:54:02 -0400151 g_assert ( wait (&status) == pid );
152 g_assert ( status == 0 );
Roger Meier213a6642010-10-27 12:30:11 +0000153 }
154}
155
156static void
157thrift_server (const int port)
158{
159 int bytes = 0;
160 ThriftServerTransport *transport = NULL;
161 ThriftTransport *client = NULL;
162 guchar buf[10]; /* a buffer */
163 guchar match[10] = TEST_DATA;
164
165 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
166 "port", port, NULL);
167
168 transport = THRIFT_SERVER_TRANSPORT (tsocket);
169 thrift_server_transport_listen (transport, NULL);
170
171 /* wrap the client in a BufferedTransport */
172 client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
173 thrift_server_transport_accept (transport, NULL),
174 "r_buf_size", 5, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400175 g_assert (client != NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000176
177 /* read 10 bytes */
178 bytes = thrift_buffered_transport_read (client, buf, 10, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400179 g_assert (bytes == 10); /* make sure we've read 10 bytes */
180 g_assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
Roger Meier213a6642010-10-27 12:30:11 +0000181
182 /* read 1 byte */
183 bytes = thrift_buffered_transport_read (client, buf, 1, NULL);
184
185 bytes = thrift_buffered_transport_read (client, buf, 6, NULL);
186 bytes = thrift_buffered_transport_read (client, buf, 2, NULL);
187 bytes = thrift_buffered_transport_read (client, buf, 1, NULL);
188
189 thrift_buffered_transport_read_end (client, NULL);
190 thrift_buffered_transport_close (client, NULL);
191 g_object_unref (client);
192 g_object_unref (tsocket);
193}
194
Roger Meier5c80b562014-05-04 22:19:46 +0200195static void
196test_write_fail(void)
197{
198 int status;
199 pid_t pid;
200 ThriftSocket *tsocket = NULL;
201 ThriftTransport *transport = NULL;
202 int port = 51198;
203 guchar buf[10] = TEST_DATA; /* a buffer */
204
205 /* SIGPIPE when send to disconnected socket */
206 signal(SIGPIPE, SIG_IGN);
207
208 pid = fork ();
James E. King, III43f4bf22017-10-28 12:54:02 -0400209 g_assert ( pid >= 0 );
Roger Meier5c80b562014-05-04 22:19:46 +0200210
211 if ( pid == 0 )
212 {
213 /* child listens */
214 ThriftServerTransport *transport = NULL;
215 ThriftTransport *client = NULL;
216
217 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
218 "port", port, NULL);
219
220 transport = THRIFT_SERVER_TRANSPORT (tsocket);
221 thrift_server_transport_listen (transport, NULL);
222
223 /* wrap the client in a BufferedTransport */
224 client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
225 thrift_server_transport_accept (transport, NULL),
226 "r_buf_size", 5, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400227 g_assert (client != NULL);
Roger Meier5c80b562014-05-04 22:19:46 +0200228
229 /* just close socket */
230 thrift_buffered_transport_close (client, NULL);
231 g_object_unref (client);
232 g_object_unref (tsocket);
233 exit (0);
234 } else {
235 /* parent connects, wait a bit for the socket to be created */
236 sleep (1);
237
238 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
239 "port", port, NULL);
240 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
241 "transport", THRIFT_TRANSPORT (tsocket),
242 "w_buf_size", 4, NULL);
243
244
James E. King, III43f4bf22017-10-28 12:54:02 -0400245 g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
246 g_assert (thrift_buffered_transport_is_open (transport));
Roger Meier5c80b562014-05-04 22:19:46 +0200247
248 /* recognize disconnection */
249 sleep(1);
James E. King, III43f4bf22017-10-28 12:54:02 -0400250 g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == TRUE);
251 g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
Roger Meier5c80b562014-05-04 22:19:46 +0200252
253 /* write and overflow buffer */
James E. King, III43f4bf22017-10-28 12:54:02 -0400254 g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
Roger Meier5c80b562014-05-04 22:19:46 +0200255
256 /* write 1 and flush */
James E. King, III43f4bf22017-10-28 12:54:02 -0400257 g_assert (thrift_buffered_transport_write (transport, buf, 1, NULL) == TRUE);
258 g_assert (thrift_buffered_transport_flush (transport, NULL) == FALSE);
Roger Meier5c80b562014-05-04 22:19:46 +0200259
260 thrift_buffered_transport_close (transport, NULL);
261
262 g_object_unref (transport);
263 g_object_unref (tsocket);
264
James E. King, III43f4bf22017-10-28 12:54:02 -0400265 g_assert ( wait (&status) == pid );
266 g_assert ( status == 0 );
Roger Meier5c80b562014-05-04 22:19:46 +0200267 }
268}
269
Roger Meier213a6642010-10-27 12:30:11 +0000270int
Roger Meierc1010922010-11-26 10:17:48 +0000271main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000272{
Jens Geyer1c190272015-07-28 23:15:18 +0200273#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier213a6642010-10-27 12:30:11 +0000274 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200275#endif
276
Roger Meierc1010922010-11-26 10:17:48 +0000277 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000278
Roger Meierc1010922010-11-26 10:17:48 +0000279 g_test_add_func ("/testbufferedtransport/CreateAndDestroy", test_create_and_destroy);
280 g_test_add_func ("/testbufferedtransport/OpenAndClose", test_open_and_close);
281 g_test_add_func ("/testbufferedtransport/ReadAndWrite", test_read_and_write);
Roger Meier5c80b562014-05-04 22:19:46 +0200282 g_test_add_func ("/testbufferedtransport/WriteFail", test_write_fail);
Roger Meierc1010922010-11-26 10:17:48 +0000283
284 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000285}
286