2009-03-30 21:35:00 +00:00
|
|
|
/*
|
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
|
* distributed with this work for additional information
|
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
|
* under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-05-27 12:08:01 +08:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2007-08-21 23:59:34 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <climits>
|
2015-05-10 08:08:18 -04:00
|
|
|
#include <vector>
|
2012-04-13 09:12:31 +00:00
|
|
|
#include <thrift/protocol/TBinaryProtocol.h>
|
2019-01-05 16:35:14 +08:00
|
|
|
#include <memory>
|
2017-08-05 12:23:54 -04:00
|
|
|
#include <thrift/transport/TBufferTransports.h>
|
2007-08-21 23:59:34 +00:00
|
|
|
#include "gen-cpp/ThriftTest_types.h"
|
|
|
|
|
|
2014-11-13 15:33:38 +01:00
|
|
|
BOOST_AUTO_TEST_SUITE(TMemoryBufferTest)
|
|
|
|
|
|
2015-05-10 08:08:18 -04:00
|
|
|
using apache::thrift::protocol::TBinaryProtocol;
|
|
|
|
|
using apache::thrift::transport::TMemoryBuffer;
|
|
|
|
|
using apache::thrift::transport::TTransportException;
|
2019-01-05 16:35:14 +08:00
|
|
|
using std::shared_ptr;
|
2015-05-10 08:08:18 -04:00
|
|
|
using std::cout;
|
|
|
|
|
using std::endl;
|
|
|
|
|
using std::string;
|
|
|
|
|
|
2015-05-18 18:10:06 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(test_read_write_grow) {
|
2015-05-15 15:21:50 +02:00
|
|
|
// Added to test the fix for THRIFT-1248
|
|
|
|
|
TMemoryBuffer uut;
|
|
|
|
|
const int maxSize = 65536;
|
|
|
|
|
uint8_t verify[maxSize];
|
|
|
|
|
std::vector<uint8_t> buf;
|
|
|
|
|
buf.resize(maxSize);
|
2015-05-10 08:08:18 -04:00
|
|
|
|
2015-05-18 18:10:06 +02:00
|
|
|
for (uint32_t i = 0; i < maxSize; ++i) {
|
2015-05-15 15:21:50 +02:00
|
|
|
buf[i] = static_cast<uint8_t>(i);
|
|
|
|
|
}
|
2015-05-10 08:08:18 -04:00
|
|
|
|
2015-05-18 18:10:06 +02:00
|
|
|
for (uint32_t i = 1; i < maxSize; i *= 2) {
|
2015-05-15 15:21:50 +02:00
|
|
|
uut.write(&buf[0], i);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-18 18:10:06 +02:00
|
|
|
for (uint32_t i = 1; i < maxSize; i *= 2) {
|
2015-05-15 15:21:50 +02:00
|
|
|
uut.read(verify, i);
|
|
|
|
|
BOOST_CHECK_EQUAL(0, ::memcmp(verify, &buf[0], i));
|
|
|
|
|
}
|
2015-05-10 08:08:18 -04:00
|
|
|
}
|
2014-11-13 15:33:38 +01:00
|
|
|
|
2015-05-18 18:10:06 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(test_roundtrip) {
|
2014-11-13 15:33:38 +01:00
|
|
|
shared_ptr<TMemoryBuffer> strBuffer(new TMemoryBuffer());
|
|
|
|
|
shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(strBuffer));
|
|
|
|
|
|
|
|
|
|
thrift::test::Xtruct a;
|
|
|
|
|
a.i32_thing = 10;
|
|
|
|
|
a.i64_thing = 30;
|
|
|
|
|
a.string_thing = "holla back a";
|
|
|
|
|
|
|
|
|
|
a.write(binaryProtcol.get());
|
|
|
|
|
std::string serialized = strBuffer->getBufferAsString();
|
|
|
|
|
|
|
|
|
|
shared_ptr<TMemoryBuffer> strBuffer2(new TMemoryBuffer());
|
|
|
|
|
shared_ptr<TBinaryProtocol> binaryProtcol2(new TBinaryProtocol(strBuffer2));
|
|
|
|
|
|
|
|
|
|
strBuffer2->resetBuffer((uint8_t*)serialized.data(), static_cast<uint32_t>(serialized.length()));
|
|
|
|
|
thrift::test::Xtruct a2;
|
|
|
|
|
a2.read(binaryProtcol2.get());
|
|
|
|
|
|
2015-06-24 10:03:50 +02:00
|
|
|
BOOST_CHECK(a == a2);
|
2014-11-13 15:33:38 +01:00
|
|
|
}
|
|
|
|
|
|
2017-10-28 18:25:45 -04:00
|
|
|
BOOST_AUTO_TEST_CASE(test_readAppendToString) {
|
2019-02-08 13:40:59 +08:00
|
|
|
string str1 = "abcd1234";
|
|
|
|
|
TMemoryBuffer buf((uint8_t*)str1.data(),
|
|
|
|
|
static_cast<uint32_t>(str1.length()),
|
2014-11-13 15:33:38 +01:00
|
|
|
TMemoryBuffer::COPY);
|
|
|
|
|
|
|
|
|
|
string str3 = "wxyz", str4 = "6789";
|
|
|
|
|
buf.readAppendToString(str3, 4);
|
|
|
|
|
buf.readAppendToString(str4, INT_MAX);
|
|
|
|
|
|
2015-06-24 10:03:50 +02:00
|
|
|
BOOST_CHECK(str3 == "wxyzabcd");
|
|
|
|
|
BOOST_CHECK(str4 == "67891234");
|
2014-11-13 15:33:38 +01:00
|
|
|
}
|
|
|
|
|
|
2015-05-18 18:10:06 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(test_exceptions) {
|
2014-11-13 15:33:38 +01:00
|
|
|
char data[] = "foo\0bar";
|
|
|
|
|
|
|
|
|
|
TMemoryBuffer buf1((uint8_t*)data, 7, TMemoryBuffer::OBSERVE);
|
|
|
|
|
string str = buf1.getBufferAsString();
|
2015-06-24 10:03:50 +02:00
|
|
|
BOOST_CHECK(str.length() == 7);
|
|
|
|
|
|
2014-11-13 15:33:38 +01:00
|
|
|
buf1.resetBuffer();
|
2015-06-24 10:03:50 +02:00
|
|
|
|
|
|
|
|
BOOST_CHECK_THROW(buf1.write((const uint8_t*)"foo", 3), TTransportException);
|
2007-08-21 23:59:34 +00:00
|
|
|
|
2014-11-13 15:33:38 +01:00
|
|
|
TMemoryBuffer buf2((uint8_t*)data, 7, TMemoryBuffer::COPY);
|
2015-06-24 10:03:50 +02:00
|
|
|
BOOST_CHECK_NO_THROW(buf2.write((const uint8_t*)"bar", 3));
|
2014-11-13 15:33:38 +01:00
|
|
|
}
|
2008-02-04 21:56:27 +00:00
|
|
|
|
2017-09-22 11:20:15 -07:00
|
|
|
BOOST_AUTO_TEST_CASE(test_default_maximum_buffer_size)
|
|
|
|
|
{
|
2018-12-17 16:21:14 -05:00
|
|
|
BOOST_CHECK_EQUAL((std::numeric_limits<uint32_t>::max)(), TMemoryBuffer().getMaxBufferSize());
|
2017-09-22 11:20:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_default_buffer_size)
|
|
|
|
|
{
|
|
|
|
|
BOOST_CHECK_EQUAL(1024, TMemoryBuffer().getBufferSize());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_error_set_max_buffer_size_too_small)
|
|
|
|
|
{
|
|
|
|
|
TMemoryBuffer buf;
|
|
|
|
|
BOOST_CHECK_THROW(buf.setMaxBufferSize(buf.getBufferSize() - 1), TTransportException);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_maximum_buffer_size)
|
|
|
|
|
{
|
2017-08-03 12:28:17 -07:00
|
|
|
TMemoryBuffer buf;
|
2017-09-22 11:20:15 -07:00
|
|
|
buf.setMaxBufferSize(8192);
|
2017-08-03 12:28:17 -07:00
|
|
|
std::vector<uint8_t> small_buff(1);
|
|
|
|
|
|
2017-09-22 11:20:15 -07:00
|
|
|
for (size_t i = 0; i < 8192; ++i)
|
|
|
|
|
{
|
|
|
|
|
buf.write(&small_buff[0], 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_CHECK_THROW(buf.write(&small_buff[0], 1), TTransportException);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_memory_buffer_to_get_sizeof_objects)
|
|
|
|
|
{
|
|
|
|
|
// This is a demonstration of how to use TMemoryBuffer to determine
|
|
|
|
|
// the serialized size of a thrift object in the Binary protocol.
|
|
|
|
|
// See THRIFT-3480
|
|
|
|
|
|
|
|
|
|
shared_ptr<TMemoryBuffer> memBuffer(new TMemoryBuffer());
|
|
|
|
|
shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(memBuffer));
|
|
|
|
|
|
|
|
|
|
thrift::test::Xtruct object;
|
|
|
|
|
object.i32_thing = 10;
|
|
|
|
|
object.i64_thing = 30;
|
|
|
|
|
object.string_thing = "who's your daddy?";
|
|
|
|
|
|
|
|
|
|
uint32_t size = object.write(binaryProtcol.get());
|
|
|
|
|
BOOST_CHECK_EQUAL(47, size);
|
2017-08-03 12:28:17 -07:00
|
|
|
}
|
|
|
|
|
|
2010-10-13 18:10:18 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|