blob: c6e6b581d33460859a90a284492dd00d23b5aeca [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);
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020034static void thrift_socket_server_open (const int port, int times);
Roger Meier213a6642010-10-27 12:30:11 +000035
36/* test object creation and destruction */
37static void
38test_create_and_destroy(void)
39{
40 ThriftTransport *transport = NULL;
41 guint r_buf_size = 0;
42 guint w_buf_size = 0;
43
44 GObject *object = NULL;
45 object = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -040046 g_assert (object != NULL);
Roger Meier213a6642010-10-27 12:30:11 +000047 g_object_get (G_OBJECT (object), "transport", &transport,
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020048 "r_buf_size", &r_buf_size,
49 "w_buf_size", &w_buf_size, NULL);
Roger Meier213a6642010-10-27 12:30:11 +000050 g_object_unref (object);
51}
52
53static void
54test_open_and_close(void)
55{
56 ThriftSocket *tsocket = NULL;
57 ThriftTransport *transport = NULL;
58 GError *err = NULL;
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020059 pid_t pid;
60 int port = 51199;
61 int status;
Roger Meier213a6642010-10-27 12:30:11 +000062
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020063 pid = fork ();
64 g_assert ( pid >= 0 );
Roger Meier213a6642010-10-27 12:30:11 +000065
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020066 if ( pid == 0 )
67 {
68 /* child listens */
69 thrift_socket_server_open (port,1);
70 exit (0);
71 } else {
72 /* parent connects, wait a bit for the socket to be created */
73 sleep (1);
74 /* create a ThriftSocket */
75 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
76 "port", port, NULL);
Roger Meier213a6642010-10-27 12:30:11 +000077
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020078 /* create a BufferedTransport wrapper of the Socket */
79 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
80 "transport", THRIFT_TRANSPORT (tsocket), NULL);
Roger Meier213a6642010-10-27 12:30:11 +000081
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020082 /* this shouldn't work */
83 g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
84 g_assert (thrift_buffered_transport_is_open (transport) == TRUE);
85 g_assert (thrift_buffered_transport_close (transport, NULL) == TRUE);
86 g_object_unref (transport);
87 g_object_unref (tsocket);
Roger Meier213a6642010-10-27 12:30:11 +000088
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020089 /* try and underlying socket failure */
90 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
91 NULL);
Roger Meier213a6642010-10-27 12:30:11 +000092
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020093 /* create a BufferedTransport wrapper of the Socket */
94 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
95 "transport", THRIFT_TRANSPORT (tsocket), NULL);
96
97 g_assert (thrift_buffered_transport_open (transport, &err) == FALSE);
98 g_object_unref (transport);
99 g_object_unref (tsocket);
100 g_error_free (err);
101 err = NULL;
102 g_assert ( wait (&status) == pid );
103 g_assert ( status == 0 );
104 }
Roger Meier213a6642010-10-27 12:30:11 +0000105}
106
107static void
108test_read_and_write(void)
109{
110 int status;
111 pid_t pid;
112 ThriftSocket *tsocket = NULL;
113 ThriftTransport *transport = NULL;
114 int port = 51199;
115 guchar buf[10] = TEST_DATA; /* a buffer */
116
117 pid = fork ();
James E. King, III43f4bf22017-10-28 12:54:02 -0400118 g_assert ( pid >= 0 );
Roger Meier213a6642010-10-27 12:30:11 +0000119
120 if ( pid == 0 )
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200121 {
122 /* child listens */
123 thrift_server (port);
124 exit (0);
125 } else {
126 /* parent connects, wait a bit for the socket to be created */
127 sleep (1);
Roger Meier213a6642010-10-27 12:30:11 +0000128
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200129 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
130 "port", port, NULL);
131 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
132 "transport", THRIFT_TRANSPORT (tsocket),
133 "w_buf_size", 4, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000134
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200135 g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
136 g_assert (thrift_buffered_transport_is_open (transport));
Roger Meier213a6642010-10-27 12:30:11 +0000137
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200138 /* write 10 bytes */
139 thrift_buffered_transport_write (transport, buf, 10, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000140
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200141 /* write 1 byte at a time */
142 thrift_buffered_transport_write (transport, buf, 1, NULL);
143 thrift_buffered_transport_write (transport, buf, 1, NULL);
144 thrift_buffered_transport_write (transport, buf, 1, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000145
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200146 /* overflow the buffer */
147 thrift_buffered_transport_write (transport, buf, 2, NULL);
148 thrift_buffered_transport_write (transport, buf, 1, NULL);
149 thrift_buffered_transport_flush (transport, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000150
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200151 /* write 1 byte and flush */
152 thrift_buffered_transport_write (transport, buf, 1, NULL);
153 thrift_buffered_transport_flush (transport, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000154
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200155 /* write and overflow buffer with 2 system calls */
156 thrift_buffered_transport_write (transport, buf, 1, NULL);
157 thrift_buffered_transport_write (transport, buf, 3, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000158
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200159 /* write 10 bytes */
160 thrift_buffered_transport_write (transport, buf, 10, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000161
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200162 thrift_buffered_transport_write_end (transport, NULL);
163 thrift_buffered_transport_flush (transport, NULL);
164 thrift_buffered_transport_close (transport, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000165
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200166 g_object_unref (transport);
167 g_object_unref (tsocket);
Roger Meier213a6642010-10-27 12:30:11 +0000168
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200169 g_assert ( wait (&status) == pid );
170 g_assert ( status == 0 );
171 }
172}
173
174
175static void
176thrift_socket_server_open (const int port, int times)
177{
178 int bytes = 0;
179 ThriftServerTransport *transport = NULL;
180 ThriftTransport *client = NULL;
181 guchar buf[10]; /* a buffer */
182 guchar match[10] = TEST_DATA;
183 int i;
184 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
185 "port", port, NULL);
186
187 transport = THRIFT_SERVER_TRANSPORT (tsocket);
188 thrift_server_transport_listen (transport, NULL);
189 for(i=0;i<times;i++){
190 client = thrift_server_transport_accept (transport, NULL);
191 g_assert (client != NULL);
192 thrift_socket_close (client, NULL);
193 g_object_unref (client);
Roger Meier213a6642010-10-27 12:30:11 +0000194 }
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200195 g_object_unref (tsocket);
Roger Meier213a6642010-10-27 12:30:11 +0000196}
197
198static void
199thrift_server (const int port)
200{
201 int bytes = 0;
202 ThriftServerTransport *transport = NULL;
203 ThriftTransport *client = NULL;
204 guchar buf[10]; /* a buffer */
205 guchar match[10] = TEST_DATA;
206
207 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200208 "port", port, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000209
210 transport = THRIFT_SERVER_TRANSPORT (tsocket);
211 thrift_server_transport_listen (transport, NULL);
212
213 /* wrap the client in a BufferedTransport */
214 client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200215 thrift_server_transport_accept (transport, NULL),
216 "r_buf_size", 5, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400217 g_assert (client != NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000218
219 /* read 10 bytes */
220 bytes = thrift_buffered_transport_read (client, buf, 10, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400221 g_assert (bytes == 10); /* make sure we've read 10 bytes */
222 g_assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
Roger Meier213a6642010-10-27 12:30:11 +0000223
224 /* read 1 byte */
225 bytes = thrift_buffered_transport_read (client, buf, 1, NULL);
226
227 bytes = thrift_buffered_transport_read (client, buf, 6, NULL);
228 bytes = thrift_buffered_transport_read (client, buf, 2, NULL);
229 bytes = thrift_buffered_transport_read (client, buf, 1, NULL);
230
231 thrift_buffered_transport_read_end (client, NULL);
232 thrift_buffered_transport_close (client, NULL);
233 g_object_unref (client);
234 g_object_unref (tsocket);
235}
236
Roger Meier5c80b562014-05-04 22:19:46 +0200237static void
238test_write_fail(void)
239{
240 int status;
241 pid_t pid;
242 ThriftSocket *tsocket = NULL;
243 ThriftTransport *transport = NULL;
244 int port = 51198;
245 guchar buf[10] = TEST_DATA; /* a buffer */
246
247 /* SIGPIPE when send to disconnected socket */
248 signal(SIGPIPE, SIG_IGN);
249
250 pid = fork ();
James E. King, III43f4bf22017-10-28 12:54:02 -0400251 g_assert ( pid >= 0 );
Roger Meier5c80b562014-05-04 22:19:46 +0200252
253 if ( pid == 0 )
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200254 {
255 /* child listens */
256 ThriftServerTransport *transport = NULL;
257 ThriftTransport *client = NULL;
Roger Meier5c80b562014-05-04 22:19:46 +0200258
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200259 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
260 "port", port, NULL);
Roger Meier5c80b562014-05-04 22:19:46 +0200261
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200262 transport = THRIFT_SERVER_TRANSPORT (tsocket);
263 thrift_server_transport_listen (transport, NULL);
Roger Meier5c80b562014-05-04 22:19:46 +0200264
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200265 /* wrap the client in a BufferedTransport */
266 client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
267 thrift_server_transport_accept (transport, NULL),
268 "r_buf_size", 5, NULL);
269 g_assert (client != NULL);
Roger Meier5c80b562014-05-04 22:19:46 +0200270
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200271 /* just close socket */
272 thrift_buffered_transport_close (client, NULL);
273 g_object_unref (client);
274 g_object_unref (tsocket);
275 exit (0);
276 } else {
277 /* parent connects, wait a bit for the socket to be created */
278 sleep (1);
Roger Meier5c80b562014-05-04 22:19:46 +0200279
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200280 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
281 "port", port, NULL);
282 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
283 "transport", THRIFT_TRANSPORT (tsocket),
284 "w_buf_size", 4, NULL);
Roger Meier5c80b562014-05-04 22:19:46 +0200285
286
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200287 g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
288 g_assert (thrift_buffered_transport_is_open (transport));
Roger Meier5c80b562014-05-04 22:19:46 +0200289
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200290 /* recognize disconnection */
291 sleep(1);
292 g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == TRUE);
293 g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
Roger Meier5c80b562014-05-04 22:19:46 +0200294
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200295 /* write and overflow buffer */
296 g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
Roger Meier5c80b562014-05-04 22:19:46 +0200297
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200298 /* write 1 and flush */
299 g_assert (thrift_buffered_transport_write (transport, buf, 1, NULL) == TRUE);
300 g_assert (thrift_buffered_transport_flush (transport, NULL) == FALSE);
Roger Meier5c80b562014-05-04 22:19:46 +0200301
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200302 thrift_buffered_transport_close (transport, NULL);
Roger Meier5c80b562014-05-04 22:19:46 +0200303
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200304 g_object_unref (transport);
305 g_object_unref (tsocket);
Roger Meier5c80b562014-05-04 22:19:46 +0200306
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200307 g_assert ( wait (&status) == pid );
308 g_assert ( status == 0 );
309 }
Roger Meier5c80b562014-05-04 22:19:46 +0200310}
311
Roger Meier213a6642010-10-27 12:30:11 +0000312int
Roger Meierc1010922010-11-26 10:17:48 +0000313main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000314{
Jens Geyer1c190272015-07-28 23:15:18 +0200315#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier213a6642010-10-27 12:30:11 +0000316 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200317#endif
318
Roger Meierc1010922010-11-26 10:17:48 +0000319 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000320
Roger Meierc1010922010-11-26 10:17:48 +0000321 g_test_add_func ("/testbufferedtransport/CreateAndDestroy", test_create_and_destroy);
322 g_test_add_func ("/testbufferedtransport/OpenAndClose", test_open_and_close);
323 g_test_add_func ("/testbufferedtransport/ReadAndWrite", test_read_and_write);
Roger Meier5c80b562014-05-04 22:19:46 +0200324 g_test_add_func ("/testbufferedtransport/WriteFail", test_write_fail);
Roger Meierc1010922010-11-26 10:17:48 +0000325
326 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000327}
328