blob: d01806d6132d504ff069ccdaeea70ff591660d22 [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{
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200178 ThriftServerTransport *transport = NULL;
179 ThriftTransport *client = NULL;
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200180 int i;
181 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
182 "port", port, NULL);
183
184 transport = THRIFT_SERVER_TRANSPORT (tsocket);
185 thrift_server_transport_listen (transport, NULL);
186 for(i=0;i<times;i++){
187 client = thrift_server_transport_accept (transport, NULL);
188 g_assert (client != NULL);
189 thrift_socket_close (client, NULL);
190 g_object_unref (client);
Roger Meier213a6642010-10-27 12:30:11 +0000191 }
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200192 g_object_unref (tsocket);
Roger Meier213a6642010-10-27 12:30:11 +0000193}
194
195static void
196thrift_server (const int port)
197{
198 int bytes = 0;
199 ThriftServerTransport *transport = NULL;
200 ThriftTransport *client = NULL;
201 guchar buf[10]; /* a buffer */
202 guchar match[10] = TEST_DATA;
203
204 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200205 "port", port, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000206
207 transport = THRIFT_SERVER_TRANSPORT (tsocket);
208 thrift_server_transport_listen (transport, NULL);
209
210 /* wrap the client in a BufferedTransport */
211 client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200212 thrift_server_transport_accept (transport, NULL),
213 "r_buf_size", 5, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400214 g_assert (client != NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000215
216 /* read 10 bytes */
217 bytes = thrift_buffered_transport_read (client, buf, 10, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400218 g_assert (bytes == 10); /* make sure we've read 10 bytes */
219 g_assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
Roger Meier213a6642010-10-27 12:30:11 +0000220
221 /* read 1 byte */
222 bytes = thrift_buffered_transport_read (client, buf, 1, NULL);
223
224 bytes = thrift_buffered_transport_read (client, buf, 6, NULL);
225 bytes = thrift_buffered_transport_read (client, buf, 2, NULL);
226 bytes = thrift_buffered_transport_read (client, buf, 1, NULL);
227
228 thrift_buffered_transport_read_end (client, NULL);
229 thrift_buffered_transport_close (client, NULL);
230 g_object_unref (client);
231 g_object_unref (tsocket);
232}
233
Roger Meier5c80b562014-05-04 22:19:46 +0200234static void
235test_write_fail(void)
236{
237 int status;
238 pid_t pid;
239 ThriftSocket *tsocket = NULL;
240 ThriftTransport *transport = NULL;
241 int port = 51198;
242 guchar buf[10] = TEST_DATA; /* a buffer */
243
244 /* SIGPIPE when send to disconnected socket */
245 signal(SIGPIPE, SIG_IGN);
246
247 pid = fork ();
James E. King, III43f4bf22017-10-28 12:54:02 -0400248 g_assert ( pid >= 0 );
Roger Meier5c80b562014-05-04 22:19:46 +0200249
250 if ( pid == 0 )
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200251 {
252 /* child listens */
253 ThriftServerTransport *transport = NULL;
254 ThriftTransport *client = NULL;
Roger Meier5c80b562014-05-04 22:19:46 +0200255
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200256 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
257 "port", port, NULL);
Roger Meier5c80b562014-05-04 22:19:46 +0200258
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200259 transport = THRIFT_SERVER_TRANSPORT (tsocket);
260 thrift_server_transport_listen (transport, NULL);
Roger Meier5c80b562014-05-04 22:19:46 +0200261
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200262 /* wrap the client in a BufferedTransport */
263 client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
264 thrift_server_transport_accept (transport, NULL),
265 "r_buf_size", 5, NULL);
266 g_assert (client != NULL);
Roger Meier5c80b562014-05-04 22:19:46 +0200267
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200268 /* just close socket */
269 thrift_buffered_transport_close (client, NULL);
270 g_object_unref (client);
271 g_object_unref (tsocket);
272 exit (0);
273 } else {
274 /* parent connects, wait a bit for the socket to be created */
275 sleep (1);
Roger Meier5c80b562014-05-04 22:19:46 +0200276
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200277 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
278 "port", port, NULL);
279 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
280 "transport", THRIFT_TRANSPORT (tsocket),
281 "w_buf_size", 4, NULL);
Roger Meier5c80b562014-05-04 22:19:46 +0200282
283
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200284 g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
285 g_assert (thrift_buffered_transport_is_open (transport));
Roger Meier5c80b562014-05-04 22:19:46 +0200286
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200287 /* recognize disconnection */
288 sleep(1);
289 g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == TRUE);
290 g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
Roger Meier5c80b562014-05-04 22:19:46 +0200291
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200292 /* write and overflow buffer */
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 1 and flush */
296 g_assert (thrift_buffered_transport_write (transport, buf, 1, NULL) == TRUE);
297 g_assert (thrift_buffered_transport_flush (transport, NULL) == FALSE);
Roger Meier5c80b562014-05-04 22:19:46 +0200298
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200299 thrift_buffered_transport_close (transport, NULL);
Roger Meier5c80b562014-05-04 22:19:46 +0200300
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200301 g_object_unref (transport);
302 g_object_unref (tsocket);
Roger Meier5c80b562014-05-04 22:19:46 +0200303
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200304 g_assert ( wait (&status) == pid );
305 g_assert ( status == 0 );
306 }
Roger Meier5c80b562014-05-04 22:19:46 +0200307}
308
Roger Meier213a6642010-10-27 12:30:11 +0000309int
Roger Meierc1010922010-11-26 10:17:48 +0000310main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000311{
Jens Geyer1c190272015-07-28 23:15:18 +0200312#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier213a6642010-10-27 12:30:11 +0000313 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200314#endif
315
Roger Meierc1010922010-11-26 10:17:48 +0000316 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000317
Roger Meierc1010922010-11-26 10:17:48 +0000318 g_test_add_func ("/testbufferedtransport/CreateAndDestroy", test_create_and_destroy);
319 g_test_add_func ("/testbufferedtransport/OpenAndClose", test_open_and_close);
320 g_test_add_func ("/testbufferedtransport/ReadAndWrite", test_read_and_write);
Roger Meier5c80b562014-05-04 22:19:46 +0200321 g_test_add_func ("/testbufferedtransport/WriteFail", test_write_fail);
Roger Meierc1010922010-11-26 10:17:48 +0000322
323 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000324}
325