blob: 481185e7c1208aa26dd19d6fc9505affeb3ec429 [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
22#include <boost/test/auto_unit_test.hpp>
23#include <boost/test/unit_test.hpp>
24#include <iostream>
25#include <climits>
26#include <vector>
27#include <thrift/TConfiguration.h>
28#include <thrift/protocol/TBinaryProtocol.h>
29#include <thrift/protocol/TCompactProtocol.h>
30#include <thrift/protocol/TJSONProtocol.h>
31#include <thrift/Thrift.h>
32#include <memory>
33#include <thrift/transport/TTransportUtils.h>
34#include <thrift/transport/TBufferTransports.h>
35#include <thrift/transport/TSimpleFileTransport.h>
36#include <thrift/transport/TFileTransport.h>
37#include <thrift/protocol/TEnum.h>
38#include <thrift/protocol/TList.h>
39#include <thrift/protocol/TSet.h>
40#include <thrift/protocol/TMap.h>
41
42BOOST_AUTO_TEST_SUITE(ThriftReadCheckExceptionTest)
43
44using apache::thrift::TConfiguration;
45using apache::thrift::protocol::TBinaryProtocol;
46using apache::thrift::protocol::TCompactProtocol;
47using apache::thrift::protocol::TJSONProtocol;
48using apache::thrift::protocol::TType;
49using apache::thrift::transport::TPipedTransport;
50using apache::thrift::transport::TMemoryBuffer;
51using apache::thrift::transport::TSimpleFileTransport;
52using apache::thrift::transport::TFileTransport;
53using apache::thrift::transport::TFDTransport;
54using apache::thrift::transport::TTransportException;
55using apache::thrift::transport::TBufferedTransport;
56using apache::thrift::transport::TFramedTransport;
57using std::shared_ptr;
zeshuai00786352b42020-06-15 17:00:33 +080058using std::string;
59using std::memset;
60using namespace apache::thrift;
61using namespace apache::thrift::protocol;
62
63
64BOOST_AUTO_TEST_CASE(test_tmemorybuffer_read_check_exception) {
65 std::shared_ptr<TConfiguration> config(new TConfiguration(MAX_MESSAGE_SIZE));
66 TMemoryBuffer trans_out(config);
67 uint8_t buffer[6] = {1, 2, 3, 4, 5, 6};
68 trans_out.write((const uint8_t*)buffer, sizeof(buffer));
69 trans_out.close();
70
71 TMemoryBuffer trans_in(config);
72 memset(buffer, 0, sizeof(buffer));
Jens Geyer1f734552021-01-28 08:48:24 +010073 BOOST_CHECK_THROW(trans_in.read(buffer, sizeof(buffer)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +080074 trans_in.close();
75}
76
77BOOST_AUTO_TEST_CASE(test_tpipedtransport_read_check_exception) {
78 std::shared_ptr<TConfiguration> config(new TConfiguration(MAX_MESSAGE_SIZE));
79 std::shared_ptr<TMemoryBuffer> pipe(new TMemoryBuffer);
80 std::shared_ptr<TMemoryBuffer> underlying(new TMemoryBuffer);
81 std::shared_ptr<TPipedTransport> trans(new TPipedTransport(underlying, pipe, config));
82
83 uint8_t buffer[4];
84
85 underlying->write((uint8_t*)"abcd", 4);
Jens Geyer1f734552021-01-28 08:48:24 +010086 BOOST_CHECK_THROW(trans->read(buffer, sizeof(buffer)), TTransportException);
87 BOOST_CHECK_THROW(trans->readAll(buffer, sizeof(buffer)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +080088 trans->readEnd();
89 pipe->resetBuffer();
90 underlying->write((uint8_t*)"ef", 2);
Jens Geyer1f734552021-01-28 08:48:24 +010091 BOOST_CHECK_THROW(trans->read(buffer, sizeof(buffer)), TTransportException);
92 BOOST_CHECK_THROW(trans->readAll(buffer, sizeof(buffer)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +080093 trans->readEnd();
94}
95
96BOOST_AUTO_TEST_CASE(test_tsimplefiletransport_read_check_exception) {
97 std::shared_ptr<TConfiguration> config(new TConfiguration(MAX_MESSAGE_SIZE));
98 TSimpleFileTransport trans_out("data", false, true, config);
99 uint8_t buffer[6] = {1, 2, 3, 4, 5, 6};
100 trans_out.write((const uint8_t*)buffer, sizeof(buffer));
101 trans_out.close();
102
103 TSimpleFileTransport trans_in("data",true, false, config);
104 memset(buffer, 0, sizeof(buffer));
Jens Geyer1f734552021-01-28 08:48:24 +0100105 BOOST_CHECK_THROW(trans_in.read(buffer, sizeof(buffer)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800106 trans_in.close();
107
108 remove("./data");
109}
110
111BOOST_AUTO_TEST_CASE(test_tfiletransport_read_check_exception) {
112 std::shared_ptr<TConfiguration> config(new TConfiguration(MAX_MESSAGE_SIZE));
113 TFileTransport trans_out("data", false, config);
114 uint8_t buffer[6] = {1, 2, 3, 4, 5, 6};
115 trans_out.write((const uint8_t*)buffer, sizeof(buffer));
116
117 TFileTransport trans_in("data", false, config);
118 memset(buffer, 0, sizeof(buffer));
Jens Geyer1f734552021-01-28 08:48:24 +0100119 BOOST_CHECK_THROW(trans_in.read(buffer, sizeof(buffer)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800120
121 remove("./data");
122}
123
124BOOST_AUTO_TEST_CASE(test_tbufferedtransport_read_check_exception) {
125 uint8_t arr[4] = {1, 2, 3, 4};
126 std::shared_ptr<TMemoryBuffer> buffer (new TMemoryBuffer(arr, sizeof(arr)));
127 std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE));
128 std::shared_ptr<TBufferedTransport> trans (new TBufferedTransport(buffer, config));
129
130 trans->write((const uint8_t*)arr, sizeof(arr));
Jens Geyer1f734552021-01-28 08:48:24 +0100131 BOOST_CHECK_THROW(trans->read(arr, sizeof(arr)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800132}
133
134BOOST_AUTO_TEST_CASE(test_tframedtransport_read_check_exception) {
135 uint8_t arr[4] = {1, 2, 3, 4};
136 std::shared_ptr<TMemoryBuffer> buffer (new TMemoryBuffer(arr, sizeof(arr)));
137 std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE));
138 std::shared_ptr<TFramedTransport> trans (new TFramedTransport(buffer, config));
139
140 trans->write((const uint8_t*)arr, sizeof(arr));
Jens Geyer1f734552021-01-28 08:48:24 +0100141 BOOST_CHECK_THROW(trans->read(arr, sizeof(arr)), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800142}
143
144BOOST_AUTO_TEST_CASE(test_tthriftbinaryprotocol_read_check_exception) {
145 std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE));
146 std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config));
147 std::shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport));
148
149 uint32_t val = 0;
150 TType elemType = apache::thrift::protocol::T_STOP;
151 TType elemType1 = apache::thrift::protocol::T_STOP;
152 TList list(T_I32, 8);
153 protocol->writeListBegin(list.elemType_, list.size_);
154 protocol->writeListEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100155 BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800156 protocol->readListEnd();
157
158 TSet set(T_I32, 8);
159 protocol->writeSetBegin(set.elemType_, set.size_);
160 protocol->writeSetEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100161 BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800162 protocol->readSetEnd();
163
164 TMap map(T_I32, T_I32, 8);
165 protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_);
166 protocol->writeMapEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100167 BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800168 protocol->readMapEnd();
169}
170
171BOOST_AUTO_TEST_CASE(test_tthriftcompactprotocol_read_check_exception) {
172 std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE));
173 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;
179 TList list(T_I32, 8);
180 protocol->writeListBegin(list.elemType_, list.size_);
181 protocol->writeListEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100182 BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800183 protocol->readListEnd();
184
185 TSet set(T_I32, 8);
186 protocol->writeSetBegin(set.elemType_, set.size_);
187 protocol->writeSetEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100188 BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800189 protocol->readSetEnd();
190
191 TMap map(T_I32, T_I32, 8);
192 protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_);
193 protocol->writeMapEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100194 BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800195 protocol->readMapEnd();
196}
197
198BOOST_AUTO_TEST_CASE(test_tthriftjsonprotocol_read_check_exception) {
199 std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE));
200 std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config));
201 std::shared_ptr<TJSONProtocol> protocol(new TJSONProtocol(transport));
202
203 uint32_t val = 0;
204 TType elemType = apache::thrift::protocol::T_STOP;
205 TType elemType1 = apache::thrift::protocol::T_STOP;
206 TList list(T_I32, 8);
207 protocol->writeListBegin(list.elemType_, list.size_);
208 protocol->writeListEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100209 BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800210 protocol->readListEnd();
211
212 TSet set(T_I32, 8);
213 protocol->writeSetBegin(set.elemType_, set.size_);
214 protocol->writeSetEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100215 BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800216 protocol->readSetEnd();
217
218 TMap map(T_I32, T_I32, 8);
219 protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_);
220 protocol->writeMapEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100221 BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800222 protocol->readMapEnd();
223}
224
225BOOST_AUTO_TEST_SUITE_END()