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