blob: 0328737896633a1cb4083a8e558af55b9f0272d8 [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>
Jens Geyer1c190272015-07-28 23:15:18 +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>
24#include <thrift/c_glib/transport/thrift_socket.h>
25#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
Roger Meiere3da7682013-01-11 11:41:53 +010030#include "../src/thrift/c_glib/transport/thrift_framed_transport.c"
Roger Meier213a6642010-10-27 12:30:11 +000031
Roger Meier213a6642010-10-27 12:30:11 +000032static void thrift_server (const int port);
33
34/* test object creation and destruction */
35static void
36test_create_and_destroy(void)
37{
38 ThriftTransport *transport = NULL;
39 guint r_buf_size = 0;
40 guint w_buf_size = 0;
41
42 GObject *object = NULL;
43 object = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -040044 g_assert (object != NULL);
Roger Meier213a6642010-10-27 12:30:11 +000045 g_object_get (G_OBJECT (object), "transport", &transport,
46 "r_buf_size", &r_buf_size,
47 "w_buf_size", &w_buf_size, NULL);
48 g_object_unref (object);
49}
50
51static void
52test_open_and_close(void)
53{
54 ThriftSocket *tsocket = NULL;
55 ThriftTransport *transport = NULL;
56 GError *err = NULL;
57
58 /* create a ThriftSocket */
59 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
60 "port", 51188, NULL);
61
62 /* create a BufferedTransport wrapper of the Socket */
63 transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
64 "transport", THRIFT_TRANSPORT (tsocket), NULL);
65
66 /* this shouldn't work */
James E. King, III43f4bf22017-10-28 12:54:02 -040067 g_assert (thrift_framed_transport_open (transport, NULL) == FALSE);
68 g_assert (thrift_framed_transport_is_open (transport) == TRUE);
69 g_assert (thrift_framed_transport_close (transport, NULL) == TRUE);
Roger Meier213a6642010-10-27 12:30:11 +000070 g_object_unref (transport);
71 g_object_unref (tsocket);
72
73 /* try and underlying socket failure */
74 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
75 NULL);
76
77 /* create a BufferedTransport wrapper of the Socket */
78 transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
79 "transport", THRIFT_TRANSPORT (tsocket), NULL);
80
James E. King, III43f4bf22017-10-28 12:54:02 -040081 g_assert (thrift_framed_transport_open (transport, &err) == FALSE);
Roger Meier213a6642010-10-27 12:30:11 +000082 g_object_unref (transport);
83 g_object_unref (tsocket);
84 g_error_free (err);
85 err = NULL;
86}
87
88static void
89test_read_and_write(void)
90{
91 int status;
92 pid_t pid;
93 ThriftSocket *tsocket = NULL;
94 ThriftTransport *transport = NULL;
95 int port = 51199;
96 guchar buf[10] = TEST_DATA; /* a buffer */
97
98 pid = fork ();
James E. King, III43f4bf22017-10-28 12:54:02 -040099 g_assert ( pid >= 0 );
Roger Meier213a6642010-10-27 12:30:11 +0000100
101 if ( pid == 0 )
102 {
103 /* child listens */
104 thrift_server (port);
105 exit (0);
106 } else {
107 /* parent connects, wait a bit for the socket to be created */
108 sleep (1);
109
110 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
111 "port", port, NULL);
112 transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
113 "transport", THRIFT_TRANSPORT (tsocket),
114 "w_buf_size", 4, NULL);
115
James E. King, III43f4bf22017-10-28 12:54:02 -0400116 g_assert (thrift_framed_transport_open (transport, NULL) == TRUE);
117 g_assert (thrift_framed_transport_is_open (transport));
Roger Meier213a6642010-10-27 12:30:11 +0000118
119 /* write 10 bytes */
120 thrift_framed_transport_write (transport, buf, 10, NULL);
121 thrift_framed_transport_flush (transport, NULL);
122
123 thrift_framed_transport_write (transport, buf, 1, NULL);
124 thrift_framed_transport_flush (transport, NULL);
125
126 thrift_framed_transport_write (transport, buf, 10, NULL);
127 thrift_framed_transport_flush (transport, NULL);
128
129 thrift_framed_transport_write (transport, buf, 10, NULL);
130 thrift_framed_transport_flush (transport, NULL);
131
132 thrift_framed_transport_write_end (transport, NULL);
133 thrift_framed_transport_flush (transport, NULL);
134 thrift_framed_transport_close (transport, NULL);
135
136 g_object_unref (transport);
137 g_object_unref (tsocket);
138
James E. King, III43f4bf22017-10-28 12:54:02 -0400139 g_assert ( wait (&status) == pid );
140 g_assert ( status == 0 );
Roger Meier213a6642010-10-27 12:30:11 +0000141 }
142}
143
Roger Meier7fa98482014-09-01 20:21:33 +0200144/* test reading from the transport after the peer has unexpectedly
145 closed the connection */
146static void
147test_read_after_peer_close(void)
148{
149 int status;
150 pid_t pid;
151 int port = 51199;
152 GError *err = NULL;
153
154 pid = fork ();
155 g_assert (pid >= 0);
156
157 if (pid == 0)
158 {
159 ThriftServerTransport *server_transport = NULL;
160 ThriftTransport *client_transport = NULL;
161
162 /* child listens */
163 server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
164 "port", port,
165 NULL);
166 g_assert (server_transport != NULL);
167
168 thrift_server_transport_listen (server_transport, &err);
169 g_assert (err == NULL);
170
171 /* wrap the client transport in a ThriftFramedTransport */
172 client_transport = g_object_new
173 (THRIFT_TYPE_FRAMED_TRANSPORT,
174 "transport", thrift_server_transport_accept (server_transport, &err),
175 "r_buf_size", 0,
176 NULL);
177 g_assert (err == NULL);
178 g_assert (client_transport != NULL);
179
180 /* close the connection immediately after the client connects */
181 thrift_transport_close (client_transport, NULL);
182
183 g_object_unref (client_transport);
184 g_object_unref (server_transport);
185
186 exit (0);
187 } else {
188 ThriftSocket *tsocket = NULL;
189 ThriftTransport *transport = NULL;
190 guchar buf[10]; /* a buffer */
191
192 /* parent connects, wait a bit for the socket to be created */
193 sleep (1);
194
195 tsocket = g_object_new (THRIFT_TYPE_SOCKET,
196 "hostname", "localhost",
197 "port", port,
198 NULL);
199 transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
200 "transport", THRIFT_TRANSPORT (tsocket),
201 "w_buf_size", 0,
202 NULL);
203
204 g_assert (thrift_transport_open (transport, NULL) == TRUE);
205 g_assert (thrift_transport_is_open (transport));
206
207 /* attempting to read from the transport after the peer has closed
208 the connection fails gracefully without generating a critical
209 warning or segmentation fault */
210 thrift_transport_read (transport, buf, 10, &err);
211 g_assert (err != NULL);
212
213 g_error_free (err);
214 err = NULL;
215
216 thrift_transport_read_end (transport, &err);
217 g_assert (err == NULL);
218
219 thrift_transport_close (transport, &err);
220 g_assert (err == NULL);
221
222 g_object_unref (transport);
223 g_object_unref (tsocket);
224
225 g_assert (wait (&status) == pid);
226 g_assert (status == 0);
227 }
228}
229
Roger Meier213a6642010-10-27 12:30:11 +0000230static void
231thrift_server (const int port)
232{
233 int bytes = 0;
234 ThriftServerTransport *transport = NULL;
235 ThriftTransport *client = NULL;
Roger Meierc75797d2012-04-28 11:33:58 +0000236 guchar buf[12]; /* a buffer */
Roger Meier213a6642010-10-27 12:30:11 +0000237 guchar match[10] = TEST_DATA;
238
239 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
240 "port", port, NULL);
241
242 transport = THRIFT_SERVER_TRANSPORT (tsocket);
243 thrift_server_transport_listen (transport, NULL);
244
245 /* wrap the client in a BufferedTransport */
246 client = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport",
247 thrift_server_transport_accept (transport, NULL),
248 "r_buf_size", 5, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400249 g_assert (client != NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000250
251 /* read 10 bytes */
252 bytes = thrift_framed_transport_read (client, buf, 10, NULL);
James E. King, III43f4bf22017-10-28 12:54:02 -0400253 g_assert (bytes == 10); /* make sure we've read 10 bytes */
254 g_assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
Roger Meier213a6642010-10-27 12:30:11 +0000255
256 bytes = thrift_framed_transport_read (client, buf, 6, NULL);
257 bytes = thrift_framed_transport_read (client, buf, 5, NULL);
258 bytes = thrift_framed_transport_read (client, buf, 1, NULL);
259
260 bytes = thrift_framed_transport_read (client, buf, 12, NULL);
261
262 thrift_framed_transport_read_end (client, NULL);
263 thrift_framed_transport_close (client, NULL);
264 g_object_unref (client);
265 g_object_unref (tsocket);
266}
267
268int
Roger Meierc1010922010-11-26 10:17:48 +0000269main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000270{
Jens Geyer1c190272015-07-28 23:15:18 +0200271#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier213a6642010-10-27 12:30:11 +0000272 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200273#endif
274
Roger Meierc1010922010-11-26 10:17:48 +0000275 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000276
Roger Meierc1010922010-11-26 10:17:48 +0000277 g_test_add_func ("/testframedtransport/CreateAndDestroy", test_create_and_destroy);
278 g_test_add_func ("/testframedtransport/OpenAndClose", test_open_and_close);
279 g_test_add_func ("/testframedtransport/ReadAndWrite", test_read_and_write);
Roger Meier7fa98482014-09-01 20:21:33 +0200280 g_test_add_func ("/testframedtransport/ReadAfterPeerClose", test_read_after_peer_close);
Roger Meierc1010922010-11-26 10:17:48 +0000281
282 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000283}