blob: 1472072949ba62de52048ac577156f897bbf9faa [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{
41 ThriftConfiguration *tconfiguration = NULL;
42 ThriftSocket *tsocket = NULL;
43 ThriftTransport *transport = NULL;
44 GError *err = NULL;
45 pid_t pid;
46 int port = 51199;
47 int status;
48
49 pid = fork ();
50 g_assert ( pid >= 0 );
51
52 if ( pid == 0 )
53 {
54 /* child listens */
55 thrift_socket_server_open (port,1);
56 exit (0);
57 } else {
58 /* parent connects, wait a bit for the socket to be created */
59 sleep (1);
60 /* create a ThriftSocket */
61 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
62 "port", port, NULL);
63
64 /* create a BufferedTransport wrapper of the Socket */
65 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
66 "transport", THRIFT_TRANSPORT (tsocket),
67 NULL);
68
69 /* this shouldn't work */
70 g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
71 g_assert (thrift_buffered_transport_is_open (transport) == TRUE);
72 g_assert (thrift_buffered_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_BUFFERED_TRANSPORT,
82 "transport", THRIFT_TRANSPORT (tsocket), NULL);
83
84 g_assert (thrift_buffered_transport_open (transport, &err) == FALSE);
85 g_object_unref (transport);
86 g_object_unref (tsocket);
87 g_error_free (err);
88 err = NULL;
89 g_assert ( wait (&status) == pid );
90 g_assert ( status == 0 );
91 }
92}
93
94static void
95test_read_and_write(void)
96{
97 int status;
98 pid_t pid;
99 ThriftSocket *tsocket = NULL;
100 ThriftTransport *transport = NULL;
101 int port = 51199;
102 guchar buf[3] = TEST_DATA; /* a buffer */
103
104 pid = fork ();
105 g_assert ( pid >= 0 );
106
107 if ( pid == 0 )
108 {
109 /* child listens */
110 thrift_server (port);
111 exit (0);
112 } else {
113 /* parent connects, wait a bit for the socket to be created */
114 sleep (1);
115
116 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
117 "port", port, NULL);
118 transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
119 "transport", THRIFT_TRANSPORT (tsocket),
120 "w_buf_size", 4, NULL);
121
122 g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
123 g_assert (thrift_buffered_transport_is_open (transport));
124
125 /* write 3 bytes */
126 thrift_buffered_transport_write (transport, buf, 3, NULL);
127
128 /* write 4 bytes */
129 thrift_buffered_transport_write (transport, buf, 4, NULL);
130
131 thrift_buffered_transport_write_end (transport, NULL);
132 thrift_buffered_transport_flush (transport, NULL);
133 thrift_buffered_transport_close (transport, NULL);
134
135 g_object_unref (transport);
136 g_object_unref (tsocket);
137
138 g_assert ( wait (&status) == pid );
139 g_assert ( status == 0 );
140 }
141}
142
143
144static void
145thrift_socket_server_open (const int port, int times)
146{
147 ThriftServerTransport *transport = NULL;
148 ThriftTransport *client = NULL;
149 int i;
150
151 ThriftConfiguration *tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE,
152 "max_frame_size", MAX_MESSAGE_SIZE, NULL);
153
154 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, "port", port,
155 "configuration", tconfiguration, NULL);
156
157 transport = THRIFT_SERVER_TRANSPORT (tsocket);
158 THRIFT_SERVER_TRANSPORT_GET_CLASS (tsocket)->resetConsumedMessageSize(transport, -1, NULL);
159 thrift_server_transport_listen (transport, NULL);
160 for(i=0;i<times;i++){
161 client = thrift_server_transport_accept (transport, NULL);
162 g_assert (client != NULL);
163 thrift_socket_close (client, NULL);
164 g_object_unref (client);
165 }
166 g_object_unref (tsocket);
167 g_object_unref (tconfiguration);
168 g_assert(tconfiguration != NULL);
169}
170
171static void
172thrift_server (const int port)
173{
174 int bytes = 0;
175 ThriftServerTransport *transport = NULL;
176 ThriftTransport *client = NULL;
177 guchar buf[3]; /* a buffer */
178 guchar match[3] = TEST_DATA;
179
180 ThriftConfiguration *tconfiguration = g_object_new (THRIFT_TYPE_CONFIGURATION, "max_message_size", MAX_MESSAGE_SIZE,
181 "max_frame_size", MAX_MESSAGE_SIZE, NULL);
182
183 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
184 "port", port, "configuration", tconfiguration, NULL);
185
186 transport = THRIFT_SERVER_TRANSPORT (tsocket);
187 thrift_server_transport_listen (transport, NULL);
188
189 /* wrap the client in a BufferedTransport */
190 client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
191 thrift_server_transport_accept (transport, NULL),
192 "r_buf_size", 5, NULL);
193 g_assert (client != NULL);
194
195 /* read 3 bytes */
196 bytes = thrift_buffered_transport_read (client, buf, 3, NULL);
197 g_assert (bytes == 3); /* make sure we've read 10 bytes */
198 g_assert ( memcmp (buf, match, 3) == 0 ); /* make sure what we got matches */
199
200 bytes = thrift_buffered_transport_read (client, buf, 4, NULL);
201 g_assert (bytes == -1);
202
203 thrift_buffered_transport_read_end (client, NULL);
204 thrift_buffered_transport_close (client, NULL);
205 g_object_unref (client);
206 g_object_unref (tsocket);
207 g_object_unref (tconfiguration);
208}
209
210int
211main(int argc, char *argv[])
212{
213#if (!GLIB_CHECK_VERSION (2, 36, 0))
214 g_type_init();
215#endif
216
217 g_test_init (&argc, &argv, NULL);
218
219 g_test_add_func ("/testthriftbufferedreadcheck/OpenAndClose", test_open_and_close);
220 g_test_add_func ("/testthriftbufferedreadcheck/ReadAndWrite", test_read_and_write);
221
222 return g_test_run ();
223}
224