blob: d50ff23c77dfda64aa09d5f73bc9557a9be53209 [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>
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_framed_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_FRAMED_TRANSPORT, NULL);
45 assert (object != NULL);
46 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_FRAMED_TRANSPORT,
65 "transport", THRIFT_TRANSPORT (tsocket), NULL);
66
67 /* this shouldn't work */
68 assert (thrift_framed_transport_open (transport, NULL) == FALSE);
69 assert (thrift_framed_transport_is_open (transport) == TRUE);
70 assert (thrift_framed_transport_close (transport, NULL) == TRUE);
71 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_FRAMED_TRANSPORT,
80 "transport", THRIFT_TRANSPORT (tsocket), NULL);
81
82 assert (thrift_framed_transport_open (transport, &err) == FALSE);
83 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 ();
100 assert ( pid >= 0 );
101
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_FRAMED_TRANSPORT,
114 "transport", THRIFT_TRANSPORT (tsocket),
115 "w_buf_size", 4, NULL);
116
117 assert (thrift_framed_transport_open (transport, NULL) == TRUE);
118 assert (thrift_framed_transport_is_open (transport));
119
120 /* write 10 bytes */
121 thrift_framed_transport_write (transport, buf, 10, NULL);
122 thrift_framed_transport_flush (transport, NULL);
123
124 thrift_framed_transport_write (transport, buf, 1, NULL);
125 thrift_framed_transport_flush (transport, NULL);
126
127 thrift_framed_transport_write (transport, buf, 10, NULL);
128 thrift_framed_transport_flush (transport, NULL);
129
130 thrift_framed_transport_write (transport, buf, 10, NULL);
131 thrift_framed_transport_flush (transport, NULL);
132
133 thrift_framed_transport_write_end (transport, NULL);
134 thrift_framed_transport_flush (transport, NULL);
135 thrift_framed_transport_close (transport, NULL);
136
137 g_object_unref (transport);
138 g_object_unref (tsocket);
139
140 assert ( wait (&status) == pid );
141 assert ( status == 0 );
142 }
143}
144
Roger Meier7fa98482014-09-01 20:21:33 +0200145/* test reading from the transport after the peer has unexpectedly
146 closed the connection */
147static void
148test_read_after_peer_close(void)
149{
150 int status;
151 pid_t pid;
152 int port = 51199;
153 GError *err = NULL;
154
155 pid = fork ();
156 g_assert (pid >= 0);
157
158 if (pid == 0)
159 {
160 ThriftServerTransport *server_transport = NULL;
161 ThriftTransport *client_transport = NULL;
162
163 /* child listens */
164 server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
165 "port", port,
166 NULL);
167 g_assert (server_transport != NULL);
168
169 thrift_server_transport_listen (server_transport, &err);
170 g_assert (err == NULL);
171
172 /* wrap the client transport in a ThriftFramedTransport */
173 client_transport = g_object_new
174 (THRIFT_TYPE_FRAMED_TRANSPORT,
175 "transport", thrift_server_transport_accept (server_transport, &err),
176 "r_buf_size", 0,
177 NULL);
178 g_assert (err == NULL);
179 g_assert (client_transport != NULL);
180
181 /* close the connection immediately after the client connects */
182 thrift_transport_close (client_transport, NULL);
183
184 g_object_unref (client_transport);
185 g_object_unref (server_transport);
186
187 exit (0);
188 } else {
189 ThriftSocket *tsocket = NULL;
190 ThriftTransport *transport = NULL;
191 guchar buf[10]; /* a buffer */
192
193 /* parent connects, wait a bit for the socket to be created */
194 sleep (1);
195
196 tsocket = g_object_new (THRIFT_TYPE_SOCKET,
197 "hostname", "localhost",
198 "port", port,
199 NULL);
200 transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
201 "transport", THRIFT_TRANSPORT (tsocket),
202 "w_buf_size", 0,
203 NULL);
204
205 g_assert (thrift_transport_open (transport, NULL) == TRUE);
206 g_assert (thrift_transport_is_open (transport));
207
208 /* attempting to read from the transport after the peer has closed
209 the connection fails gracefully without generating a critical
210 warning or segmentation fault */
211 thrift_transport_read (transport, buf, 10, &err);
212 g_assert (err != NULL);
213
214 g_error_free (err);
215 err = NULL;
216
217 thrift_transport_read_end (transport, &err);
218 g_assert (err == NULL);
219
220 thrift_transport_close (transport, &err);
221 g_assert (err == NULL);
222
223 g_object_unref (transport);
224 g_object_unref (tsocket);
225
226 g_assert (wait (&status) == pid);
227 g_assert (status == 0);
228 }
229}
230
Roger Meier213a6642010-10-27 12:30:11 +0000231static void
232thrift_server (const int port)
233{
234 int bytes = 0;
235 ThriftServerTransport *transport = NULL;
236 ThriftTransport *client = NULL;
Roger Meierc75797d2012-04-28 11:33:58 +0000237 guchar buf[12]; /* a buffer */
Roger Meier213a6642010-10-27 12:30:11 +0000238 guchar match[10] = TEST_DATA;
239
240 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
241 "port", port, NULL);
242
243 transport = THRIFT_SERVER_TRANSPORT (tsocket);
244 thrift_server_transport_listen (transport, NULL);
245
246 /* wrap the client in a BufferedTransport */
247 client = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport",
248 thrift_server_transport_accept (transport, NULL),
249 "r_buf_size", 5, NULL);
250 assert (client != NULL);
251
252 /* read 10 bytes */
253 bytes = thrift_framed_transport_read (client, buf, 10, NULL);
254 assert (bytes == 10); /* make sure we've read 10 bytes */
255 assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
256
257 bytes = thrift_framed_transport_read (client, buf, 6, NULL);
258 bytes = thrift_framed_transport_read (client, buf, 5, NULL);
259 bytes = thrift_framed_transport_read (client, buf, 1, NULL);
260
261 bytes = thrift_framed_transport_read (client, buf, 12, NULL);
262
263 thrift_framed_transport_read_end (client, NULL);
264 thrift_framed_transport_close (client, NULL);
265 g_object_unref (client);
266 g_object_unref (tsocket);
267}
268
269int
Roger Meierc1010922010-11-26 10:17:48 +0000270main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000271{
Jens Geyer1c190272015-07-28 23:15:18 +0200272#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier213a6642010-10-27 12:30:11 +0000273 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200274#endif
275
Roger Meierc1010922010-11-26 10:17:48 +0000276 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000277
Roger Meierc1010922010-11-26 10:17:48 +0000278 g_test_add_func ("/testframedtransport/CreateAndDestroy", test_create_and_destroy);
279 g_test_add_func ("/testframedtransport/OpenAndClose", test_open_and_close);
280 g_test_add_func ("/testframedtransport/ReadAndWrite", test_read_and_write);
Roger Meier7fa98482014-09-01 20:21:33 +0200281 g_test_add_func ("/testframedtransport/ReadAfterPeerClose", test_read_after_peer_close);
Roger Meierc1010922010-11-26 10:17:48 +0000282
283 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000284}