blob: e4bee69b82ec6a1dab50a720d3b17b1313a636ef [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>
Roger Meier5c80b562014-05-04 22:19:46 +020022#include <signal.h>
Jens Geyer1c190272015-07-28 23:15:18 +020023#include <sys/wait.h>
Roger Meier213a6642010-10-27 12:30:11 +000024
Roger Meiere3da7682013-01-11 11:41:53 +010025#include <thrift/c_glib/transport/thrift_transport.h>
26#include <thrift/c_glib/transport/thrift_socket.h>
27#include <thrift/c_glib/transport/thrift_server_transport.h>
28#include <thrift/c_glib/transport/thrift_server_socket.h>
Roger Meier213a6642010-10-27 12:30:11 +000029
30#define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }
31
Roger Meiere3da7682013-01-11 11:41:53 +010032#include "../src/thrift/c_glib/transport/thrift_buffered_transport.c"
Roger Meier213a6642010-10-27 12:30:11 +000033
34static const char TEST_ADDRESS[] = "localhost";
35static const short TEST_PORT = 64444;
36
37static void thrift_server (const int port);
38
39/* test object creation and destruction */
40static void
41test_create_and_destroy(void)
42{
43 ThriftTransport *transport = NULL;
44 guint r_buf_size = 0;
45 guint w_buf_size = 0;
46
47 GObject *object = NULL;
48 object = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, NULL);
49 assert (object != NULL);
50 g_object_get (G_OBJECT (object), "transport", &transport,
51 "r_buf_size", &r_buf_size,
52 "w_buf_size", &w_buf_size, NULL);
53 g_object_unref (object);
54}
55
56static void
57test_open_and_close(void)
58{
59 ThriftSocket *tsocket = NULL;
60 ThriftTransport *transport = NULL;
61 GError *err = NULL;
62
63 /* create a ThriftSocket */
64 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
65 "port", 51188, NULL);
66
67 /* create a BufferedTransport wrapper of the Socket */
68 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
69 "transport", THRIFT_TRANSPORT (tsocket), NULL);
70
71 /* this shouldn't work */
72 assert (thrift_buffered_transport_open (transport, NULL) == FALSE);
73 assert (thrift_buffered_transport_is_open (transport) == TRUE);
74 assert (thrift_buffered_transport_close (transport, NULL) == TRUE);
75 g_object_unref (transport);
76 g_object_unref (tsocket);
77
78 /* try and underlying socket failure */
79 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
80 NULL);
81
82 /* create a BufferedTransport wrapper of the Socket */
83 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
84 "transport", THRIFT_TRANSPORT (tsocket), NULL);
85
86 assert (thrift_buffered_transport_open (transport, &err) == FALSE);
87 g_object_unref (transport);
88 g_object_unref (tsocket);
89 g_error_free (err);
90 err = NULL;
91}
92
93static void
94test_read_and_write(void)
95{
96 int status;
97 pid_t pid;
98 ThriftSocket *tsocket = NULL;
99 ThriftTransport *transport = NULL;
100 int port = 51199;
101 guchar buf[10] = TEST_DATA; /* a buffer */
102
103 pid = fork ();
104 assert ( pid >= 0 );
105
106 if ( pid == 0 )
107 {
108 /* child listens */
109 thrift_server (port);
110 exit (0);
111 } else {
112 /* parent connects, wait a bit for the socket to be created */
113 sleep (1);
114
115 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
116 "port", port, NULL);
117 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
118 "transport", THRIFT_TRANSPORT (tsocket),
119 "w_buf_size", 4, NULL);
120
121 assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
122 assert (thrift_buffered_transport_is_open (transport));
123
124 /* write 10 bytes */
125 thrift_buffered_transport_write (transport, buf, 10, NULL);
126
127 /* write 1 byte at a time */
128 thrift_buffered_transport_write (transport, buf, 1, NULL);
129 thrift_buffered_transport_write (transport, buf, 1, NULL);
130 thrift_buffered_transport_write (transport, buf, 1, NULL);
131
132 /* overflow the buffer */
133 thrift_buffered_transport_write (transport, buf, 2, NULL);
134 thrift_buffered_transport_write (transport, buf, 1, NULL);
135 thrift_buffered_transport_flush (transport, NULL);
136
137 /* write 1 byte and flush */
138 thrift_buffered_transport_write (transport, buf, 1, NULL);
139 thrift_buffered_transport_flush (transport, NULL);
140
141 /* write and overflow buffer with 2 system calls */
142 thrift_buffered_transport_write (transport, buf, 1, NULL);
143 thrift_buffered_transport_write (transport, buf, 3, NULL);
144
145 /* write 10 bytes */
146 thrift_buffered_transport_write (transport, buf, 10, NULL);
147
148 thrift_buffered_transport_write_end (transport, NULL);
149 thrift_buffered_transport_flush (transport, NULL);
150 thrift_buffered_transport_close (transport, NULL);
151
152 g_object_unref (transport);
153 g_object_unref (tsocket);
154
155 assert ( wait (&status) == pid );
156 assert ( status == 0 );
157 }
158}
159
160static void
161thrift_server (const int port)
162{
163 int bytes = 0;
164 ThriftServerTransport *transport = NULL;
165 ThriftTransport *client = NULL;
166 guchar buf[10]; /* a buffer */
167 guchar match[10] = TEST_DATA;
168
169 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
170 "port", port, NULL);
171
172 transport = THRIFT_SERVER_TRANSPORT (tsocket);
173 thrift_server_transport_listen (transport, NULL);
174
175 /* wrap the client in a BufferedTransport */
176 client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
177 thrift_server_transport_accept (transport, NULL),
178 "r_buf_size", 5, NULL);
179 assert (client != NULL);
180
181 /* read 10 bytes */
182 bytes = thrift_buffered_transport_read (client, buf, 10, NULL);
183 assert (bytes == 10); /* make sure we've read 10 bytes */
184 assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
185
186 /* read 1 byte */
187 bytes = thrift_buffered_transport_read (client, buf, 1, NULL);
188
189 bytes = thrift_buffered_transport_read (client, buf, 6, NULL);
190 bytes = thrift_buffered_transport_read (client, buf, 2, NULL);
191 bytes = thrift_buffered_transport_read (client, buf, 1, NULL);
192
193 thrift_buffered_transport_read_end (client, NULL);
194 thrift_buffered_transport_close (client, NULL);
195 g_object_unref (client);
196 g_object_unref (tsocket);
197}
198
Roger Meier5c80b562014-05-04 22:19:46 +0200199static void
200test_write_fail(void)
201{
202 int status;
203 pid_t pid;
204 ThriftSocket *tsocket = NULL;
205 ThriftTransport *transport = NULL;
206 int port = 51198;
207 guchar buf[10] = TEST_DATA; /* a buffer */
208
209 /* SIGPIPE when send to disconnected socket */
210 signal(SIGPIPE, SIG_IGN);
211
212 pid = fork ();
213 assert ( pid >= 0 );
214
215 if ( pid == 0 )
216 {
217 /* child listens */
218 ThriftServerTransport *transport = NULL;
219 ThriftTransport *client = NULL;
220
221 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
222 "port", port, NULL);
223
224 transport = THRIFT_SERVER_TRANSPORT (tsocket);
225 thrift_server_transport_listen (transport, NULL);
226
227 /* wrap the client in a BufferedTransport */
228 client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
229 thrift_server_transport_accept (transport, NULL),
230 "r_buf_size", 5, NULL);
231 assert (client != NULL);
232
233 /* just close socket */
234 thrift_buffered_transport_close (client, NULL);
235 g_object_unref (client);
236 g_object_unref (tsocket);
237 exit (0);
238 } else {
239 /* parent connects, wait a bit for the socket to be created */
240 sleep (1);
241
242 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
243 "port", port, NULL);
244 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
245 "transport", THRIFT_TRANSPORT (tsocket),
246 "w_buf_size", 4, NULL);
247
248
249 assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
250 assert (thrift_buffered_transport_is_open (transport));
251
252 /* recognize disconnection */
253 sleep(1);
254 assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == TRUE);
255 assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
256
257 /* write and overflow buffer */
258 assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
259
260 /* write 1 and flush */
261 assert (thrift_buffered_transport_write (transport, buf, 1, NULL) == TRUE);
262 assert (thrift_buffered_transport_flush (transport, NULL) == FALSE);
263
264 thrift_buffered_transport_close (transport, NULL);
265
266 g_object_unref (transport);
267 g_object_unref (tsocket);
268
269 assert ( wait (&status) == pid );
270 assert ( status == 0 );
271 }
272}
273
Roger Meier213a6642010-10-27 12:30:11 +0000274int
Roger Meierc1010922010-11-26 10:17:48 +0000275main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000276{
Jens Geyer1c190272015-07-28 23:15:18 +0200277#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier213a6642010-10-27 12:30:11 +0000278 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200279#endif
280
Roger Meierc1010922010-11-26 10:17:48 +0000281 g_test_init (&argc, &argv, NULL);
Roger Meier213a6642010-10-27 12:30:11 +0000282
Roger Meierc1010922010-11-26 10:17:48 +0000283 g_test_add_func ("/testbufferedtransport/CreateAndDestroy", test_create_and_destroy);
284 g_test_add_func ("/testbufferedtransport/OpenAndClose", test_open_and_close);
285 g_test_add_func ("/testbufferedtransport/ReadAndWrite", test_read_and_write);
Roger Meier5c80b562014-05-04 22:19:46 +0200286 g_test_add_func ("/testbufferedtransport/WriteFail", test_write_fail);
Roger Meierc1010922010-11-26 10:17:48 +0000287
288 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000289}
290