blob: 04e368ffa930c5d611fc6eea51d4c6f42939d0da [file] [log] [blame]
zeshuai007037753e2020-11-30 11:16:10 +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 \
30 { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', \
31 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', \
32 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', \
33 '5', '6', '7', '8', '9', '0' }
34
35#include "../src/thrift/c_glib/transport/thrift_zlib_transport.c"
36
37static void thrift_server (const int port);
38static void thrift_socket_server_open (const int port, int times);
39
40/* test object creation and destruction */
41static void
42test_create_and_destroy(void)
43{
44 ThriftTransport *transport = NULL;
45 gint urbuf_size = 0;
46 gint crbuf_size = 0;
47 gint uwbuf_size = 0;
48 gint cwbuf_size = 0;
49 gint comp_level = 0;
50
51 GObject *object = NULL;
52 object = g_object_new (THRIFT_TYPE_ZLIB_TRANSPORT, NULL);
53 g_assert (object != NULL);
54 g_object_get (G_OBJECT (object), "transport", &transport,
55 "urbuf_size", &urbuf_size,
56 "crbuf_size", &crbuf_size,
57 "uwbuf_size", &uwbuf_size,
58 "cwbuf_size", &cwbuf_size,
59 "comp_level", &comp_level, NULL);
60 g_object_unref (object);
61}
62
63static void
64test_open_and_close(void)
65{
66 ThriftSocket *tsocket = NULL;
67 ThriftTransport *transport = NULL;
68 GError *err = NULL;
69 pid_t pid;
70 int port = 51199;
71 int status;
72
73 pid = fork ();
74 g_assert ( pid >= 0 );
75
76 if ( pid == 0 )
77 {
78 /* child listens */
79 thrift_socket_server_open (port,1);
80 exit (0);
81 } else {
82 /* parent connects, wait a bit for the socket to be created */
83 sleep (1);
84 /* create a ThriftSocket */
85 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
86 "port", port, NULL);
87
88 /* create a ZlibTransport wrapper of the Socket */
89 transport = g_object_new (THRIFT_TYPE_ZLIB_TRANSPORT,
90 "transport", THRIFT_TRANSPORT (tsocket), NULL);
91
92 /* this shouldn't work */
93 g_assert (thrift_zlib_transport_open (transport, NULL) == TRUE);
94 g_assert (thrift_zlib_transport_is_open (transport) == TRUE);
95 g_assert (thrift_zlib_transport_close (transport, NULL) == TRUE);
96 g_object_unref (transport);
97 g_object_unref (tsocket);
98
99 /* try and underlying socket failure */
100 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost_broken",
101 NULL);
102
103 /* create a ZlibTransport wrapper of the Socket */
104 transport = g_object_new (THRIFT_TYPE_ZLIB_TRANSPORT,
105 "transport", THRIFT_TRANSPORT (tsocket), NULL);
106
107 g_assert (thrift_zlib_transport_open (transport, &err) == FALSE);
108 g_object_unref (transport);
109 g_object_unref (tsocket);
110 g_error_free (err);
111 err = NULL;
112
113 g_assert ( wait (&status) == pid );
114 g_assert ( status == 0 );
115 }
116}
117
118static void
119test_read_and_write(void)
120{
121 int status;
122 pid_t pid;
123 ThriftSocket *tsocket = NULL;
124 ThriftTransport *transport = NULL;
125 int port = 51199;
126 gchar buf[36] = TEST_DATA;
127
128 pid = fork ();
129 g_assert ( pid >= 0 );
130
131 if ( pid == 0 )
132 {
133 /* child listens */
134 thrift_server (port);
135 exit (0);
136 } else {
137 /* parent connects, wait a bit for the socket to be created */
138 sleep (1);
139
140 tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
141 "port", port, NULL);
142 transport = g_object_new (THRIFT_TYPE_ZLIB_TRANSPORT,
143 "transport", THRIFT_TRANSPORT (tsocket), NULL);
144
145 g_assert (thrift_zlib_transport_open (transport, NULL) == TRUE);
146 g_assert (thrift_zlib_transport_is_open (transport));
147
148 thrift_zlib_transport_write (transport, buf, 36, NULL);
149 thrift_zlib_transport_flush (transport, NULL);
150 thrift_zlib_transport_write_end (transport, NULL);
151
152 g_object_unref (transport);
153 g_object_unref (tsocket);
154
155 g_assert ( wait (&status) == pid );
156 g_assert ( status == 0 );
157 }
158}
159
160static void
161thrift_socket_server_open (const int port, int times)
162{
163 ThriftServerTransport *transport = NULL;
164 ThriftTransport *client = NULL;
165 int i;
166
167 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
168 "port", port, NULL);
169
170 transport = THRIFT_SERVER_TRANSPORT (tsocket);
171 thrift_server_transport_listen (transport, NULL);
172 for(i=0;i<times;i++){
173 client = thrift_server_transport_accept (transport, NULL);
174 g_assert (client != NULL);
175 thrift_socket_close (client, NULL);
176 g_object_unref (client);
177 }
178 g_object_unref (tsocket);
179}
180
181static void
182thrift_server (const int port)
183{
184 int bytes = 0;
185 gboolean check_sum = FALSE;
186 ThriftServerTransport *transport = NULL;
187 ThriftTransport *client = NULL;
188 gchar buf[36]; /* a buffer */
189 gchar match[36] = TEST_DATA;
190
191 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
192 "port", port, NULL);
193
194 transport = THRIFT_SERVER_TRANSPORT (tsocket);
195 thrift_server_transport_listen (transport, NULL);
196
197 /* wrap the client in a ZlibTransport */
198 client = g_object_new (THRIFT_TYPE_ZLIB_TRANSPORT, "transport",
199 thrift_server_transport_accept (transport, NULL),
200 NULL);
201 g_assert (client != NULL);
202
203 /* read 36 bytes */
204 thrift_zlib_transport_read (client, buf, 36, NULL);
205 g_assert (memcmp(buf, match, 36) == 0 );
206
207 thrift_zlib_transport_read_end (client, NULL);
208
209 check_sum = thrift_zlib_transport_verify_checksum (client, NULL);
210 g_assert (!check_sum);
211
212 thrift_zlib_transport_close (client, NULL);
213 g_object_unref (client);
214 g_object_unref (tsocket);
215}
216
217int
218main(int argc, char *argv[])
219{
220#if (!GLIB_CHECK_VERSION (2, 36, 0))
221 g_type_init();
222#endif
223
224 g_test_init (&argc, &argv, NULL);
225
226 g_test_add_func ("/testzlibtransport/CreateAndDestroy", test_create_and_destroy);
227 g_test_add_func ("/testzlibtransport/OpenAndClose", test_open_and_close);
228 g_test_add_func ("/testzlibtransport/ReadAndWrite", test_read_and_write);
229
230 return g_test_run ();
231}