blob: 4870ae8cf31d6e5ee23114add48c25521b3abd31 [file] [log] [blame]
zeshuai007c80b8bb2020-07-23 09:43:41 +08001/*
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
20#include <netdb.h>
21#include <signal.h>
22#include <sys/wait.h>
23
24#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>
28
29#define TEST_DATA { 'a', 'b', 'c' }
30
31#define MAX_MESSAGE_SIZE 3
32
33#include "../src/thrift/c_glib/transport/thrift_buffered_transport.c"
34
35static void thrift_server (const int port);
36static void thrift_socket_server_open (const int port, int times);
37
38static void
39test_open_and_close(void)
40{
zeshuai007c80b8bb2020-07-23 09:43:41 +080041 ThriftSocket *tsocket = NULL;
42 ThriftTransport *transport = NULL;
43 GError *err = NULL;
44 pid_t pid;
45 int port = 51199;
46 int status;
47
48 pid = fork ();
49 g_assert ( pid >= 0 );
50
51 if ( pid == 0 )
52 {
53 /* child listens */
54 thrift_socket_server_open (port,1);
55 exit (0);
56 } else {
57 /* parent connects, wait a bit for the socket to be created */
58 sleep (1);
59 /* create a ThriftSocket */
60 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
61 "port", port, NULL);
62
63 /* create a BufferedTransport wrapper of the Socket */
64 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
65 "transport", THRIFT_TRANSPORT (tsocket),
66 NULL);
67
68 /* this shouldn't work */
69 g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
70 g_assert (thrift_buffered_transport_is_open (transport) == TRUE);
71 g_assert (thrift_buffered_transport_close (transport, NULL) == TRUE);
72 g_object_unref (transport);
73 g_object_unref (tsocket);
74
75 /* try and underlying socket failure */
76 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
77 NULL);
78
79 /* create a BufferedTransport wrapper of the Socket */
80 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
81 "transport", THRIFT_TRANSPORT (tsocket), NULL);
82
83 g_assert (thrift_buffered_transport_open (transport, &err) == FALSE);
84 g_object_unref (transport);
85 g_object_unref (tsocket);
86 g_error_free (err);
87 err = NULL;
88 g_assert ( wait (&status) == pid );
89 g_assert ( status == 0 );
90 }
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[3] = TEST_DATA; /* a buffer */
102
103 pid = fork ();
104 g_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 g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
122 g_assert (thrift_buffered_transport_is_open (transport));
123
124 /* write 3 bytes */
125 thrift_buffered_transport_write (transport, buf, 3, NULL);
126
127 /* write 4 bytes */
128 thrift_buffered_transport_write (transport, buf, 4, NULL);
129
130 thrift_buffered_transport_write_end (transport, NULL);
131 thrift_buffered_transport_flush (transport, NULL);
132 thrift_buffered_transport_close (transport, NULL);
133
134 g_object_unref (transport);
135 g_object_unref (tsocket);
136
137 g_assert ( wait (&status) == pid );
138 g_assert ( status == 0 );
139 }
140}
141
142
143static void
144thrift_socket_server_open (const int port, int times)
145{
146 ThriftServerTransport *transport = NULL;
147 ThriftTransport *client = NULL;
148 int i;
149
150 ThriftConfiguration *tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE,
151 "max_frame_size", MAX_MESSAGE_SIZE, NULL);
152
153 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, "port", port,
154 "configuration", tconfiguration, NULL);
155
156 transport = THRIFT_SERVER_TRANSPORT (tsocket);
157 THRIFT_SERVER_TRANSPORT_GET_CLASS (tsocket)->resetConsumedMessageSize(transport, -1, NULL);
158 thrift_server_transport_listen (transport, NULL);
159 for(i=0;i<times;i++){
160 client = thrift_server_transport_accept (transport, NULL);
161 g_assert (client != NULL);
162 thrift_socket_close (client, NULL);
163 g_object_unref (client);
164 }
165 g_object_unref (tsocket);
166 g_object_unref (tconfiguration);
167 g_assert(tconfiguration != NULL);
168}
169
170static void
171thrift_server (const int port)
172{
173 int bytes = 0;
174 ThriftServerTransport *transport = NULL;
175 ThriftTransport *client = NULL;
176 guchar buf[3]; /* a buffer */
177 guchar match[3] = TEST_DATA;
178
179 ThriftConfiguration *tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE,
180 "max_frame_size", MAX_MESSAGE_SIZE, NULL);
181
182 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
183 "port", port, "configuration", tconfiguration, NULL);
184
185 transport = THRIFT_SERVER_TRANSPORT (tsocket);
186 thrift_server_transport_listen (transport, NULL);
187
188 /* wrap the client in a BufferedTransport */
189 client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
190 thrift_server_transport_accept (transport, NULL),
191 "r_buf_size", 5, NULL);
192 g_assert (client != NULL);
193
194 /* read 3 bytes */
195 bytes = thrift_buffered_transport_read (client, buf, 3, NULL);
196 g_assert (bytes == 3); /* make sure we've read 10 bytes */
197 g_assert ( memcmp (buf, match, 3) == 0 ); /* make sure what we got matches */
198
199 bytes = thrift_buffered_transport_read (client, buf, 4, NULL);
200 g_assert (bytes == -1);
201
202 thrift_buffered_transport_read_end (client, NULL);
203 thrift_buffered_transport_close (client, NULL);
204 g_object_unref (client);
205 g_object_unref (tsocket);
206 g_object_unref (tconfiguration);
207}
208
209int
210main(int argc, char *argv[])
211{
212#if (!GLIB_CHECK_VERSION (2, 36, 0))
213 g_type_init();
214#endif
215
216 g_test_init (&argc, &argv, NULL);
217
218 g_test_add_func ("/testthriftbufferedreadcheck/OpenAndClose", test_open_and_close);
219 g_test_add_func ("/testthriftbufferedreadcheck/ReadAndWrite", test_read_and_write);
220
221 return g_test_run ();
222}
223