blob: 7aab09f41d6497d5d0740610e497df2744eda9dc [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) {
171 std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE));
172 std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config));
173 std::shared_ptr<TCompactProtocol> protocol(new TCompactProtocol(transport));
174
175 uint32_t val = 0;
176 TType elemType = apache::thrift::protocol::T_STOP;
177 TType elemType1 = apache::thrift::protocol::T_STOP;
178 TList list(T_I32, 8);
179 protocol->writeListBegin(list.elemType_, list.size_);
180 protocol->writeListEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100181 BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800182 protocol->readListEnd();
183
184 TSet set(T_I32, 8);
185 protocol->writeSetBegin(set.elemType_, set.size_);
186 protocol->writeSetEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100187 BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800188 protocol->readSetEnd();
189
190 TMap map(T_I32, T_I32, 8);
191 protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_);
192 protocol->writeMapEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100193 BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800194 protocol->readMapEnd();
195}
196
197BOOST_AUTO_TEST_CASE(test_tthriftjsonprotocol_read_check_exception) {
198 std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE));
199 std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config));
200 std::shared_ptr<TJSONProtocol> protocol(new TJSONProtocol(transport));
201
202 uint32_t val = 0;
203 TType elemType = apache::thrift::protocol::T_STOP;
204 TType elemType1 = apache::thrift::protocol::T_STOP;
205 TList list(T_I32, 8);
206 protocol->writeListBegin(list.elemType_, list.size_);
207 protocol->writeListEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100208 BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800209 protocol->readListEnd();
210
211 TSet set(T_I32, 8);
212 protocol->writeSetBegin(set.elemType_, set.size_);
213 protocol->writeSetEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100214 BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800215 protocol->readSetEnd();
216
217 TMap map(T_I32, T_I32, 8);
218 protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_);
219 protocol->writeMapEnd();
Jens Geyer1f734552021-01-28 08:48:24 +0100220 BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException);
zeshuai00786352b42020-06-15 17:00:33 +0800221 protocol->readMapEnd();
222}
223
224BOOST_AUTO_TEST_SUITE_END()