blob: 843ad938392c8a4c77d83ead1b0480f0cd605fbd [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 <assert.h>
21#include <netdb.h>
22
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
32static const char TEST_ADDRESS[] = "localhost";
33static const short TEST_PORT = 64444;
34
35static void thrift_server (const int port);
36
37/* test object creation and destruction */
38static void
39test_create_and_destroy(void)
40{
41 ThriftTransport *transport = NULL;
42 guint r_buf_size = 0;
43 guint w_buf_size = 0;
44
45 GObject *object = NULL;
46 object = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, NULL);
47 assert (object != NULL);
48 g_object_get (G_OBJECT (object), "transport", &transport,
49 "r_buf_size", &r_buf_size,
50 "w_buf_size", &w_buf_size, NULL);
51 g_object_unref (object);
52}
53
54static void
55test_open_and_close(void)
56{
57 ThriftSocket *tsocket = NULL;
58 ThriftTransport *transport = NULL;
59 GError *err = NULL;
60
61 /* create a ThriftSocket */
62 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
63 "port", 51188, NULL);
64
65 /* create a BufferedTransport wrapper of the Socket */
66 transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
67 "transport", THRIFT_TRANSPORT (tsocket), NULL);
68
69 /* this shouldn't work */
70 assert (thrift_framed_transport_open (transport, NULL) == FALSE);
71 assert (thrift_framed_transport_is_open (transport) == TRUE);
72 assert (thrift_framed_transport_close (transport, NULL) == TRUE);
73 g_object_unref (transport);
74 g_object_unref (tsocket);
75
76 /* try and underlying socket failure */
77 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
78 NULL);
79
80 /* create a BufferedTransport wrapper of the Socket */
81 transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
82 "transport", THRIFT_TRANSPORT (tsocket), NULL);
83
84 assert (thrift_framed_transport_open (transport, &err) == FALSE);
85 g_object_unref (transport);
86 g_object_unref (tsocket);
87 g_error_free (err);
88 err = NULL;
89}
90
91static void
92test_read_and_write(void)
93{
94 int status;
95 pid_t pid;
96 ThriftSocket *tsocket = NULL;
97 ThriftTransport *transport = NULL;
98 int port = 51199;
99 guchar buf[10] = TEST_DATA; /* a buffer */
100
101 pid = fork ();
102 assert ( pid >= 0 );
103
104 if ( pid == 0 )
105 {
106 /* child listens */
107 thrift_server (port);
108 exit (0);
109 } else {
110 /* parent connects, wait a bit for the socket to be created */
111 sleep (1);
112
113 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
114 "port", port, NULL);
115 transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
116 "transport", THRIFT_TRANSPORT (tsocket),
117 "w_buf_size", 4, NULL);
118
119 assert (thrift_framed_transport_open (transport, NULL) == TRUE);
120 assert (thrift_framed_transport_is_open (transport));
121
122 /* write 10 bytes */
123 thrift_framed_transport_write (transport, buf, 10, NULL);
124 thrift_framed_transport_flush (transport, NULL);
125
126 thrift_framed_transport_write (transport, buf, 1, 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 (transport, buf, 10, NULL);
133 thrift_framed_transport_flush (transport, NULL);
134
135 thrift_framed_transport_write_end (transport, NULL);
136 thrift_framed_transport_flush (transport, NULL);
137 thrift_framed_transport_close (transport, NULL);
138
139 g_object_unref (transport);
140 g_object_unref (tsocket);
141
142 assert ( wait (&status) == pid );
143 assert ( status == 0 );
144 }
145}
146
Roger Meier7fa98482014-09-01 20:21:33 +0200147/* test reading from the transport after the peer has unexpectedly
148 closed the connection */
149static void
150test_read_after_peer_close(void)
151{
152 int status;
153 pid_t pid;
154 int port = 51199;
155 GError *err = NULL;
156
157 pid = fork ();
158 g_assert (pid >= 0);
159
160 if (pid == 0)
161 {
162 ThriftServerTransport *server_transport = NULL;
163 ThriftTransport *client_transport = NULL;
164
165 /* child listens */
166 server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
167 "port", port,
168 NULL);
169 g_assert (server_transport != NULL);
170
171 thrift_server_transport_listen (server_transport, &err);
172 g_assert (err == NULL);
173
174 /* wrap the client transport in a ThriftFramedTransport */
175 client_transport = g_object_new
176 (THRIFT_TYPE_FRAMED_TRANSPORT,
177 "transport", thrift_server_transport_accept (server_transport, &err),
178 "r_buf_size", 0,
179 NULL);
180 g_assert (err == NULL);
181 g_assert (client_transport != NULL);
182
183 /* close the connection immediately after the client connects */
184 thrift_transport_close (client_transport, NULL);
185
186 g_object_unref (client_transport);
187 g_object_unref (server_transport);
188
189 exit (0);
190 } else {
191 ThriftSocket *tsocket = NULL;
192 ThriftTransport *transport = NULL;
193 guchar buf[10]; /* a buffer */
194
195 /* parent connects, wait a bit for the socket to be created */
196 sleep (1);
197
198 tsocket = g_object_new (THRIFT_TYPE_SOCKET,
199 "hostname", "localhost",
200 "port", port,
201 NULL);
202 transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
203 "transport", THRIFT_TRANSPORT (tsocket),
204 "w_buf_size", 0,
205 NULL);
206
207 g_assert (thrift_transport_open (transport, NULL) == TRUE);
208 g_assert (thrift_transport_is_open (transport));
209
210 /* attempting to read from the transport after the peer has closed
211 the connection fails gracefully without generating a critical
212 warning or segmentation fault */
213 thrift_transport_read (transport, buf, 10, &err);
214 g_assert (err != NULL);
215
216 g_error_free (err);
217 err = NULL;
218
219 thrift_transport_read_end (transport, &err);
220 g_assert (err == NULL);
221
222 thrift_transport_close (transport, &err);
223 g_assert (err == NULL);
224
225 g_object_unref (transport);
226 g_object_unref (tsocket);
227
228 g_assert (wait (&status) == pid);
229 g_assert (status == 0);
230 }
231}
232
Roger Meier213a6642010-10-27 12:30:11 +0000233static void
234thrift_server (const int port)
235{
236 int bytes = 0;
237 ThriftServerTransport *transport = NULL;
238 ThriftTransport *client = NULL;
Roger Meierc75797d2012-04-28 11:33:58 +0000239 guchar buf[12]; /* a buffer */
Roger Meier213a6642010-10-27 12:30:11 +0000240 guchar match[10] = TEST_DATA;
241
242 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
243 "port", port, NULL);
244
245 transport = THRIFT_SERVER_TRANSPORT (tsocket);
246 thrift_server_transport_listen (transport, NULL);
247
248 /* wrap the client in a BufferedTransport */
249 client = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport",
250 thrift_server_transport_accept (transport, NULL),
251 "r_buf_size", 5, NULL);
252 assert (client != NULL);
253
254 /* read 10 bytes */
255 bytes = thrift_framed_transport_read (client, buf, 10, NULL);
256 assert (bytes == 10); /* make sure we've read 10 bytes */
257 assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
258
259 bytes = thrift_framed_transport_read (client, buf, 6, NULL);
260 bytes = thrift_framed_transport_read (client, buf, 5, NULL);
261 bytes = thrift_framed_transport_read (client, buf, 1, NULL);
262
263 bytes = thrift_framed_transport_read (client, buf, 12, NULL);
264
265 thrift_framed_transport_read_end (client, NULL);
266 thrift_framed_transport_close (client, NULL);
267 g_object_unref (client);
268 g_object_unref (tsocket);
269}
270
271int
Roger Meierc1010922010-11-26 10:17:48 +0000272main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000273{
274 g_type_init();
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}