blob: 09dffe228d3d7af11017018adfc3b227b6a901c3 [file] [log] [blame]
zeshuai00786352b42020-06-15 17:00:33 +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#define MAX_MESSAGE_SIZE 2
21
zeshuai00786352b42020-06-15 17:00:33 +080022#include <boost/test/unit_test.hpp>
23#include <iostream>
24#include <climits>
25#include <vector>
26#include <thrift/TConfiguration.h>
27#include <thrift/protocol/TBinaryProtocol.h>
28#include <thrift/protocol/TCompactProtocol.h>
29#include <thrift/protocol/TJSONProtocol.h>
30#include <thrift/Thrift.h>
31#include <memory>
32#include <thrift/transport/TTransportUtils.h>
33#include <thrift/transport/TBufferTransports.h>
34#include <thrift/transport/TSimpleFileTransport.h>
35#include <thrift/transport/TFileTransport.h>
36#include <thrift/protocol/TEnum.h>
37#include <thrift/protocol/TList.h>
38#include <thrift/protocol/TSet.h>
39#include <thrift/protocol/TMap.h>
40
41BOOST_AUTO_TEST_SUITE(ThriftReadCheckExceptionTest)
42
43using apache::thrift::TConfiguration;
44using apache::thrift::protocol::TBinaryProtocol;
45using apache::thrift::protocol::TCompactProtocol;
46using apache::thrift::protocol::TJSONProtocol;
47using apache::thrift::protocol::TType;
48using apache::thrift::transport::TPipedTransport;
49using apache::thrift::transport::TMemoryBuffer;
50using apache::thrift::transport::TSimpleFileTransport;
51using apache::thrift::transport::TFileTransport;
52using apache::thrift::transport::TFDTransport;
53using apache::thrift::transport::TTransportException;
54using apache::thrift::transport::TBufferedTransport;
55using apache::thrift::transport::TFramedTransport;
56using std::shared_ptr;
zeshuai00786352b42020-06-15 17:00:33 +080057using std::string;
58using std::memset;
59using namespace apache::thrift;
60using namespace apache::thrift::protocol;
61
62
63BOOST_AUTO_TEST_CASE(test_tmemorybuffer_read_check_exception) {
64 std::shared_ptr<TConfiguration> config(new TConfiguration(MAX_MESSAGE_SIZE));
65 TMemoryBuffer trans_out(config);
66 uint8_t buffer[6] = {1, 2, 3, 4, 5, 6};
67 trans_out.write((const uint8_t*)buffer, sizeof(buffer));
68 trans_out.close();
69
70 TMemoryBuffer trans_in(config);
71 memset(buffer, 0, sizeof(buffer));
Jens Geyer1f734552021-01-28 08:48:24 +010072 BOOST_CHECK_THROW(trans_in.read(buffer, sizeof(buffer)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +080073 trans_in.close();
74}
75
76BOOST_AUTO_TEST_CASE(test_tpipedtransport_read_check_exception) {
77 std::shared_ptr<TConfiguration> config(new TConfiguration(MAX_MESSAGE_SIZE));
78 std::shared_ptr<TMemoryBuffer> pipe(new TMemoryBuffer);
79 std::shared_ptr<TMemoryBuffer> underlying(new TMemoryBuffer);
80 std::shared_ptr<TPipedTransport> trans(new TPipedTransport(underlying, pipe, config));
81
82 uint8_t buffer[4];
83
84 underlying->write((uint8_t*)"abcd", 4);
Jens Geyer1f734552021-01-28 08:48:24 +010085 BOOST_CHECK_THROW(trans->read(buffer, sizeof(buffer)), TTransportException);
86 BOOST_CHECK_THROW(trans->readAll(buffer, sizeof(buffer)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +080087 trans->readEnd();
88 pipe->resetBuffer();
89 underlying->write((uint8_t*)"ef", 2);
Jens Geyer1f734552021-01-28 08:48:24 +010090 BOOST_CHECK_THROW(trans->read(buffer, sizeof(buffer)), TTransportException);
91 BOOST_CHECK_THROW(trans->readAll(buffer, sizeof(buffer)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +080092 trans->readEnd();
93}
94
95BOOST_AUTO_TEST_CASE(test_tsimplefiletransport_read_check_exception) {
96 std::shared_ptr<TConfiguration> config(new TConfiguration(MAX_MESSAGE_SIZE));
97 TSimpleFileTransport trans_out("data", false, true, config);
98 uint8_t buffer[6] = {1, 2, 3, 4, 5, 6};
99 trans_out.write((const uint8_t*)buffer, sizeof(buffer));
100 trans_out.close();
101
102 TSimpleFileTransport trans_in("data",true, false, config);
103 memset(buffer, 0, sizeof(buffer));
Jens Geyer1f734552021-01-28 08:48:24 +0100104 BOOST_CHECK_THROW(trans_in.read(buffer, sizeof(buffer)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800105 trans_in.close();
106
107 remove("./data");
108}
109
110BOOST_AUTO_TEST_CASE(test_tfiletransport_read_check_exception) {
111 std::shared_ptr<TConfiguration> config(new TConfiguration(MAX_MESSAGE_SIZE));
112 TFileTransport trans_out("data", false, config);
113 uint8_t buffer[6] = {1, 2, 3, 4, 5, 6};
114 trans_out.write((const uint8_t*)buffer, sizeof(buffer));
115
116 TFileTransport trans_in("data", false, config);
117 memset(buffer, 0, sizeof(buffer));
Jens Geyer1f734552021-01-28 08:48:24 +0100118 BOOST_CHECK_THROW(trans_in.read(buffer, sizeof(buffer)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800119
120 remove("./data");
121}
122
123BOOST_AUTO_TEST_CASE(test_tbufferedtransport_read_check_exception) {
124 uint8_t arr[4] = {1, 2, 3, 4};
125 std::shared_ptr<TMemoryBuffer> buffer (new TMemoryBuffer(arr, sizeof(arr)));
126 std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE));
127 std::shared_ptr<TBufferedTransport> trans (new TBufferedTransport(buffer, config));
128
129 trans->write((const uint8_t*)arr, sizeof(arr));
Jens Geyer1f734552021-01-28 08:48:24 +0100130 BOOST_CHECK_THROW(trans->read(arr, sizeof(arr)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800131}
132
133BOOST_AUTO_TEST_CASE(test_tframedtransport_read_check_exception) {
134 uint8_t arr[4] = {1, 2, 3, 4};
135 std::shared_ptr<TMemoryBuffer> buffer (new TMemoryBuffer(arr, sizeof(arr)));
136 std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE));
137 std::shared_ptr<TFramedTransport> trans (new TFramedTransport(buffer, config));
138
139 trans->write((const uint8_t*)arr, sizeof(arr));
Jens Geyer1f734552021-01-28 08:48:24 +0100140 BOOST_CHECK_THROW(trans->read(arr, sizeof(arr)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800141}
142
143BOOST_AUTO_TEST_CASE(test_tthriftbinaryprotocol_read_check_exception) {
144 std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE));
145 std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config));
146 std::shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport));
147
148 uint32_t val = 0;
149 TType elemType = apache::thrift::protocol::T_STOP;
150 TType elemType1 = apache::thrift::protocol::T_STOP;
151 TList list(T_I32, 8);
152 protocol->writeListBegin(list.elemType_, list.size_);
153 protocol->writeListEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100154 BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800155 protocol->readListEnd();
156
157 TSet set(T_I32, 8);
158 protocol->writeSetBegin(set.elemType_, set.size_);
159 protocol->writeSetEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100160 BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800161 protocol->readSetEnd();
162
163 TMap map(T_I32, T_I32, 8);
164 protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_);
165 protocol->writeMapEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100166 BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800167 protocol->readMapEnd();
168}
169
170BOOST_AUTO_TEST_CASE(test_tthriftcompactprotocol_read_check_exception) {
Maximilian Bandle5c088932025-02-07 10:00:56 +0100171 // Set Max Message Size to 11 since all structs are 12B long
172 std::shared_ptr<TConfiguration> config (new TConfiguration(11));
zeshuai00786352b42020-06-15 17:00:33 +0800173 std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config));
174 std::shared_ptr<TCompactProtocol> protocol(new TCompactProtocol(transport));
175
176 uint32_t val = 0;
177 TType elemType = apache::thrift::protocol::T_STOP;
178 TType elemType1 = apache::thrift::protocol::T_STOP;
Maximilian Bandle5c088932025-02-07 10:00:56 +0100179
180 // This list needs 12B
181 TList list(T_I32, 12);
zeshuai00786352b42020-06-15 17:00:33 +0800182 protocol->writeListBegin(list.elemType_, list.size_);
183 protocol->writeListEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100184 BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800185 protocol->readListEnd();
186
Maximilian Bandle5c088932025-02-07 10:00:56 +0100187 // This set needs 12B
188 TSet set(T_I32, 12);
zeshuai00786352b42020-06-15 17:00:33 +0800189 protocol->writeSetBegin(set.elemType_, set.size_);
190 protocol->writeSetEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100191 BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800192 protocol->readSetEnd();
193
Maximilian Bandle5c088932025-02-07 10:00:56 +0100194
195 // This map needs 12B (2x elem)
196 TMap map(T_I32, T_I32, 6);
zeshuai00786352b42020-06-15 17:00:33 +0800197 protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_);
198 protocol->writeMapEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100199 BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800200 protocol->readMapEnd();
Maximilian Bandle5c088932025-02-07 10:00:56 +0100201
202 // This string needs 12B (1 for size + str)
203 string eleven = "1234567890A";
204 protocol->writeString(eleven);
205 BOOST_CHECK_THROW(protocol->readString(eleven), TTransportException);
206}
207
208BOOST_AUTO_TEST_CASE(test_tthriftcompactprotocol_read_check_pass) {
209 // Set Max Message Size to 12 to check the edge case
210 std::shared_ptr<TConfiguration> config (new TConfiguration(12));
211 std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config));
212 std::shared_ptr<TCompactProtocol> protocol(new TCompactProtocol(transport));
213
214 uint32_t val = 0;
215 TType elemType = apache::thrift::protocol::T_STOP;
216 TType elemType1 = apache::thrift::protocol::T_STOP;
217
218 // This list needs 12B
219 TList list(T_I32, 12);
220 protocol->writeListBegin(list.elemType_, list.size_);
221 protocol->writeListEnd();
222 BOOST_CHECK_NO_THROW(protocol->readListBegin(elemType, val));
223 protocol->readListEnd();
224
225 // This set needs 12B
226 TSet set(T_I32, 12);
227 protocol->writeSetBegin(set.elemType_, set.size_);
228 protocol->writeSetEnd();
229 BOOST_CHECK_NO_THROW(protocol->readSetBegin(elemType, val));
230 protocol->readSetEnd();
231
232 // This map needs 12B (2x elem)
233 TMap map(T_I32, T_I32, 6);
234 protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_);
235 protocol->writeMapEnd();
236 BOOST_CHECK_NO_THROW(protocol->readMapBegin(elemType, elemType1, val));
237 protocol->readMapEnd();
238
239 // This string needs 12B (1 for size + str)
240 string eleven = "1234567890A";
241 protocol->writeString(eleven);
242 BOOST_CHECK_NO_THROW(protocol->readString(eleven));
zeshuai00786352b42020-06-15 17:00:33 +0800243}
244
245BOOST_AUTO_TEST_CASE(test_tthriftjsonprotocol_read_check_exception) {
246 std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE));
247 std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config));
248 std::shared_ptr<TJSONProtocol> protocol(new TJSONProtocol(transport));
249
250 uint32_t val = 0;
251 TType elemType = apache::thrift::protocol::T_STOP;
252 TType elemType1 = apache::thrift::protocol::T_STOP;
253 TList list(T_I32, 8);
254 protocol->writeListBegin(list.elemType_, list.size_);
255 protocol->writeListEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100256 BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800257 protocol->readListEnd();
258
259 TSet set(T_I32, 8);
260 protocol->writeSetBegin(set.elemType_, set.size_);
261 protocol->writeSetEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100262 BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800263 protocol->readSetEnd();
264
265 TMap map(T_I32, T_I32, 8);
266 protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_);
267 protocol->writeMapEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100268 BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800269 protocol->readMapEnd();
270}
271
272BOOST_AUTO_TEST_SUITE_END()