zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 42 | BOOST_AUTO_TEST_SUITE(ThriftReadCheckExceptionTest) |
| 43 | |
| 44 | using apache::thrift::TConfiguration; |
| 45 | using apache::thrift::protocol::TBinaryProtocol; |
| 46 | using apache::thrift::protocol::TCompactProtocol; |
| 47 | using apache::thrift::protocol::TJSONProtocol; |
| 48 | using apache::thrift::protocol::TType; |
| 49 | using apache::thrift::transport::TPipedTransport; |
| 50 | using apache::thrift::transport::TMemoryBuffer; |
| 51 | using apache::thrift::transport::TSimpleFileTransport; |
| 52 | using apache::thrift::transport::TFileTransport; |
| 53 | using apache::thrift::transport::TFDTransport; |
| 54 | using apache::thrift::transport::TTransportException; |
| 55 | using apache::thrift::transport::TBufferedTransport; |
| 56 | using apache::thrift::transport::TFramedTransport; |
| 57 | using std::shared_ptr; |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 58 | using std::string; |
| 59 | using std::memset; |
| 60 | using namespace apache::thrift; |
| 61 | using namespace apache::thrift::protocol; |
| 62 | |
| 63 | |
| 64 | BOOST_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 Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 73 | BOOST_CHECK_THROW(trans_in.read(buffer, sizeof(buffer)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 74 | trans_in.close(); |
| 75 | } |
| 76 | |
| 77 | BOOST_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 Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 86 | BOOST_CHECK_THROW(trans->read(buffer, sizeof(buffer)), TTransportException); |
| 87 | BOOST_CHECK_THROW(trans->readAll(buffer, sizeof(buffer)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 88 | trans->readEnd(); |
| 89 | pipe->resetBuffer(); |
| 90 | underlying->write((uint8_t*)"ef", 2); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 91 | BOOST_CHECK_THROW(trans->read(buffer, sizeof(buffer)), TTransportException); |
| 92 | BOOST_CHECK_THROW(trans->readAll(buffer, sizeof(buffer)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 93 | trans->readEnd(); |
| 94 | } |
| 95 | |
| 96 | BOOST_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 Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 105 | BOOST_CHECK_THROW(trans_in.read(buffer, sizeof(buffer)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 106 | trans_in.close(); |
| 107 | |
| 108 | remove("./data"); |
| 109 | } |
| 110 | |
| 111 | BOOST_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 Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 119 | BOOST_CHECK_THROW(trans_in.read(buffer, sizeof(buffer)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 120 | |
| 121 | remove("./data"); |
| 122 | } |
| 123 | |
| 124 | BOOST_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 Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 131 | BOOST_CHECK_THROW(trans->read(arr, sizeof(arr)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | BOOST_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 Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 141 | BOOST_CHECK_THROW(trans->read(arr, sizeof(arr)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | BOOST_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 Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 155 | BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 156 | protocol->readListEnd(); |
| 157 | |
| 158 | TSet set(T_I32, 8); |
| 159 | protocol->writeSetBegin(set.elemType_, set.size_); |
| 160 | protocol->writeSetEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 161 | BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 162 | protocol->readSetEnd(); |
| 163 | |
| 164 | TMap map(T_I32, T_I32, 8); |
| 165 | protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_); |
| 166 | protocol->writeMapEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 167 | BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 168 | protocol->readMapEnd(); |
| 169 | } |
| 170 | |
| 171 | BOOST_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 Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 182 | BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 183 | protocol->readListEnd(); |
| 184 | |
| 185 | TSet set(T_I32, 8); |
| 186 | protocol->writeSetBegin(set.elemType_, set.size_); |
| 187 | protocol->writeSetEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 188 | BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 189 | protocol->readSetEnd(); |
| 190 | |
| 191 | TMap map(T_I32, T_I32, 8); |
| 192 | protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_); |
| 193 | protocol->writeMapEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 194 | BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 195 | protocol->readMapEnd(); |
| 196 | } |
| 197 | |
| 198 | BOOST_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 Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 209 | BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 210 | protocol->readListEnd(); |
| 211 | |
| 212 | TSet set(T_I32, 8); |
| 213 | protocol->writeSetBegin(set.elemType_, set.size_); |
| 214 | protocol->writeSetEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 215 | BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 216 | protocol->readSetEnd(); |
| 217 | |
| 218 | TMap map(T_I32, T_I32, 8); |
| 219 | protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_); |
| 220 | protocol->writeMapEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 221 | BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 222 | protocol->readMapEnd(); |
| 223 | } |
| 224 | |
| 225 | BOOST_AUTO_TEST_SUITE_END() |