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; |
| 58 | using std::cout; |
| 59 | using std::endl; |
| 60 | using std::string; |
| 61 | using std::memset; |
| 62 | using namespace apache::thrift; |
| 63 | using namespace apache::thrift::protocol; |
| 64 | |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE(test_tmemorybuffer_read_check_exception) { |
| 67 | std::shared_ptr<TConfiguration> config(new TConfiguration(MAX_MESSAGE_SIZE)); |
| 68 | TMemoryBuffer trans_out(config); |
| 69 | uint8_t buffer[6] = {1, 2, 3, 4, 5, 6}; |
| 70 | trans_out.write((const uint8_t*)buffer, sizeof(buffer)); |
| 71 | trans_out.close(); |
| 72 | |
| 73 | TMemoryBuffer trans_in(config); |
| 74 | memset(buffer, 0, sizeof(buffer)); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 75 | BOOST_CHECK_THROW(trans_in.read(buffer, sizeof(buffer)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 76 | trans_in.close(); |
| 77 | } |
| 78 | |
| 79 | BOOST_AUTO_TEST_CASE(test_tpipedtransport_read_check_exception) { |
| 80 | std::shared_ptr<TConfiguration> config(new TConfiguration(MAX_MESSAGE_SIZE)); |
| 81 | std::shared_ptr<TMemoryBuffer> pipe(new TMemoryBuffer); |
| 82 | std::shared_ptr<TMemoryBuffer> underlying(new TMemoryBuffer); |
| 83 | std::shared_ptr<TPipedTransport> trans(new TPipedTransport(underlying, pipe, config)); |
| 84 | |
| 85 | uint8_t buffer[4]; |
| 86 | |
| 87 | underlying->write((uint8_t*)"abcd", 4); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 88 | BOOST_CHECK_THROW(trans->read(buffer, sizeof(buffer)), TTransportException); |
| 89 | BOOST_CHECK_THROW(trans->readAll(buffer, sizeof(buffer)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 90 | trans->readEnd(); |
| 91 | pipe->resetBuffer(); |
| 92 | underlying->write((uint8_t*)"ef", 2); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 93 | BOOST_CHECK_THROW(trans->read(buffer, sizeof(buffer)), TTransportException); |
| 94 | BOOST_CHECK_THROW(trans->readAll(buffer, sizeof(buffer)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 95 | trans->readEnd(); |
| 96 | } |
| 97 | |
| 98 | BOOST_AUTO_TEST_CASE(test_tsimplefiletransport_read_check_exception) { |
| 99 | std::shared_ptr<TConfiguration> config(new TConfiguration(MAX_MESSAGE_SIZE)); |
| 100 | TSimpleFileTransport trans_out("data", false, true, config); |
| 101 | uint8_t buffer[6] = {1, 2, 3, 4, 5, 6}; |
| 102 | trans_out.write((const uint8_t*)buffer, sizeof(buffer)); |
| 103 | trans_out.close(); |
| 104 | |
| 105 | TSimpleFileTransport trans_in("data",true, false, config); |
| 106 | memset(buffer, 0, sizeof(buffer)); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 107 | BOOST_CHECK_THROW(trans_in.read(buffer, sizeof(buffer)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 108 | trans_in.close(); |
| 109 | |
| 110 | remove("./data"); |
| 111 | } |
| 112 | |
| 113 | BOOST_AUTO_TEST_CASE(test_tfiletransport_read_check_exception) { |
| 114 | std::shared_ptr<TConfiguration> config(new TConfiguration(MAX_MESSAGE_SIZE)); |
| 115 | TFileTransport trans_out("data", false, config); |
| 116 | uint8_t buffer[6] = {1, 2, 3, 4, 5, 6}; |
| 117 | trans_out.write((const uint8_t*)buffer, sizeof(buffer)); |
| 118 | |
| 119 | TFileTransport trans_in("data", false, config); |
| 120 | memset(buffer, 0, sizeof(buffer)); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 121 | BOOST_CHECK_THROW(trans_in.read(buffer, sizeof(buffer)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 122 | |
| 123 | remove("./data"); |
| 124 | } |
| 125 | |
| 126 | BOOST_AUTO_TEST_CASE(test_tbufferedtransport_read_check_exception) { |
| 127 | uint8_t arr[4] = {1, 2, 3, 4}; |
| 128 | std::shared_ptr<TMemoryBuffer> buffer (new TMemoryBuffer(arr, sizeof(arr))); |
| 129 | std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE)); |
| 130 | std::shared_ptr<TBufferedTransport> trans (new TBufferedTransport(buffer, config)); |
| 131 | |
| 132 | trans->write((const uint8_t*)arr, sizeof(arr)); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 133 | BOOST_CHECK_THROW(trans->read(arr, sizeof(arr)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | BOOST_AUTO_TEST_CASE(test_tframedtransport_read_check_exception) { |
| 137 | uint8_t arr[4] = {1, 2, 3, 4}; |
| 138 | std::shared_ptr<TMemoryBuffer> buffer (new TMemoryBuffer(arr, sizeof(arr))); |
| 139 | std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE)); |
| 140 | std::shared_ptr<TFramedTransport> trans (new TFramedTransport(buffer, config)); |
| 141 | |
| 142 | trans->write((const uint8_t*)arr, sizeof(arr)); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 143 | BOOST_CHECK_THROW(trans->read(arr, sizeof(arr)), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | BOOST_AUTO_TEST_CASE(test_tthriftbinaryprotocol_read_check_exception) { |
| 147 | std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE)); |
| 148 | std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config)); |
| 149 | std::shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport)); |
| 150 | |
| 151 | uint32_t val = 0; |
| 152 | TType elemType = apache::thrift::protocol::T_STOP; |
| 153 | TType elemType1 = apache::thrift::protocol::T_STOP; |
| 154 | TList list(T_I32, 8); |
| 155 | protocol->writeListBegin(list.elemType_, list.size_); |
| 156 | protocol->writeListEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 157 | BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 158 | protocol->readListEnd(); |
| 159 | |
| 160 | TSet set(T_I32, 8); |
| 161 | protocol->writeSetBegin(set.elemType_, set.size_); |
| 162 | protocol->writeSetEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 163 | BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 164 | protocol->readSetEnd(); |
| 165 | |
| 166 | TMap map(T_I32, T_I32, 8); |
| 167 | protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_); |
| 168 | protocol->writeMapEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 169 | BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 170 | protocol->readMapEnd(); |
| 171 | } |
| 172 | |
| 173 | BOOST_AUTO_TEST_CASE(test_tthriftcompactprotocol_read_check_exception) { |
| 174 | std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE)); |
| 175 | std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config)); |
| 176 | std::shared_ptr<TCompactProtocol> protocol(new TCompactProtocol(transport)); |
| 177 | |
| 178 | uint32_t val = 0; |
| 179 | TType elemType = apache::thrift::protocol::T_STOP; |
| 180 | TType elemType1 = apache::thrift::protocol::T_STOP; |
| 181 | TList list(T_I32, 8); |
| 182 | protocol->writeListBegin(list.elemType_, list.size_); |
| 183 | protocol->writeListEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 184 | BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 185 | protocol->readListEnd(); |
| 186 | |
| 187 | TSet set(T_I32, 8); |
| 188 | protocol->writeSetBegin(set.elemType_, set.size_); |
| 189 | protocol->writeSetEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 190 | BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 191 | protocol->readSetEnd(); |
| 192 | |
| 193 | TMap map(T_I32, T_I32, 8); |
| 194 | protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_); |
| 195 | protocol->writeMapEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 196 | BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 197 | protocol->readMapEnd(); |
| 198 | } |
| 199 | |
| 200 | BOOST_AUTO_TEST_CASE(test_tthriftjsonprotocol_read_check_exception) { |
| 201 | std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE)); |
| 202 | std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config)); |
| 203 | std::shared_ptr<TJSONProtocol> protocol(new TJSONProtocol(transport)); |
| 204 | |
| 205 | uint32_t val = 0; |
| 206 | TType elemType = apache::thrift::protocol::T_STOP; |
| 207 | TType elemType1 = apache::thrift::protocol::T_STOP; |
| 208 | TList list(T_I32, 8); |
| 209 | protocol->writeListBegin(list.elemType_, list.size_); |
| 210 | protocol->writeListEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 211 | BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 212 | protocol->readListEnd(); |
| 213 | |
| 214 | TSet set(T_I32, 8); |
| 215 | protocol->writeSetBegin(set.elemType_, set.size_); |
| 216 | protocol->writeSetEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 217 | BOOST_CHECK_THROW(protocol->readSetBegin(elemType, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 218 | protocol->readSetEnd(); |
| 219 | |
| 220 | TMap map(T_I32, T_I32, 8); |
| 221 | protocol->writeMapBegin(map.keyType_, map.valueType_, map.size_); |
| 222 | protocol->writeMapEnd(); |
Jens Geyer | 1f73455 | 2021-01-28 08:48:24 +0100 | [diff] [blame] | 223 | BOOST_CHECK_THROW(protocol->readMapBegin(elemType, elemType1, val), TTransportException); |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 224 | protocol->readMapEnd(); |
| 225 | } |
| 226 | |
| 227 | BOOST_AUTO_TEST_SUITE_END() |