blob: 438418753eecb3aaa9e7ba87c5ccac4089cb9ee2 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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
Bryan Duxburyf7d9f8e2010-05-02 23:04:03 +000020#include <boost/test/auto_unit_test.hpp>
David Reiss2dc72c32007-08-21 23:59:34 +000021#include <iostream>
22#include <climits>
Jim King60774812015-05-10 08:08:18 -040023#include <vector>
Roger Meier49ff8b12012-04-13 09:12:31 +000024#include <thrift/protocol/TBinaryProtocol.h>
James E. King, III82ae9572017-08-05 12:23:54 -040025#include <thrift/stdcxx.h>
26#include <thrift/transport/TBufferTransports.h>
David Reiss2dc72c32007-08-21 23:59:34 +000027#include "gen-cpp/ThriftTest_types.h"
28
Konrad Grochowski16a23a62014-11-13 15:33:38 +010029BOOST_AUTO_TEST_SUITE(TMemoryBufferTest)
David Reiss2dc72c32007-08-21 23:59:34 +000030
Jim King60774812015-05-10 08:08:18 -040031using apache::thrift::protocol::TBinaryProtocol;
32using apache::thrift::transport::TMemoryBuffer;
33using apache::thrift::transport::TTransportException;
James E. King, III82ae9572017-08-05 12:23:54 -040034using apache::thrift::stdcxx::shared_ptr;
Jim King60774812015-05-10 08:08:18 -040035using std::cout;
36using std::endl;
37using std::string;
David Reiss2dc72c32007-08-21 23:59:34 +000038
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020039BOOST_AUTO_TEST_CASE(test_read_write_grow) {
Roger Meiera6b66332015-05-15 15:21:50 +020040 // Added to test the fix for THRIFT-1248
41 TMemoryBuffer uut;
42 const int maxSize = 65536;
43 uint8_t verify[maxSize];
44 std::vector<uint8_t> buf;
45 buf.resize(maxSize);
Jim King60774812015-05-10 08:08:18 -040046
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020047 for (uint32_t i = 0; i < maxSize; ++i) {
Roger Meiera6b66332015-05-15 15:21:50 +020048 buf[i] = static_cast<uint8_t>(i);
49 }
Jim King60774812015-05-10 08:08:18 -040050
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020051 for (uint32_t i = 1; i < maxSize; i *= 2) {
Roger Meiera6b66332015-05-15 15:21:50 +020052 uut.write(&buf[0], i);
53 }
54
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020055 for (uint32_t i = 1; i < maxSize; i *= 2) {
Roger Meiera6b66332015-05-15 15:21:50 +020056 uut.read(verify, i);
57 BOOST_CHECK_EQUAL(0, ::memcmp(verify, &buf[0], i));
58 }
Jim King60774812015-05-10 08:08:18 -040059}
60
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020061BOOST_AUTO_TEST_CASE(test_roundtrip) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010062 shared_ptr<TMemoryBuffer> strBuffer(new TMemoryBuffer());
63 shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(strBuffer));
David Reiss2dc72c32007-08-21 23:59:34 +000064
Konrad Grochowski16a23a62014-11-13 15:33:38 +010065 thrift::test::Xtruct a;
66 a.i32_thing = 10;
67 a.i64_thing = 30;
68 a.string_thing = "holla back a";
David Reiss2dc72c32007-08-21 23:59:34 +000069
Konrad Grochowski16a23a62014-11-13 15:33:38 +010070 a.write(binaryProtcol.get());
71 std::string serialized = strBuffer->getBufferAsString();
David Reiss2dc72c32007-08-21 23:59:34 +000072
Konrad Grochowski16a23a62014-11-13 15:33:38 +010073 shared_ptr<TMemoryBuffer> strBuffer2(new TMemoryBuffer());
74 shared_ptr<TBinaryProtocol> binaryProtcol2(new TBinaryProtocol(strBuffer2));
David Reiss2dc72c32007-08-21 23:59:34 +000075
Konrad Grochowski16a23a62014-11-13 15:33:38 +010076 strBuffer2->resetBuffer((uint8_t*)serialized.data(), static_cast<uint32_t>(serialized.length()));
77 thrift::test::Xtruct a2;
78 a2.read(binaryProtcol2.get());
David Reiss2dc72c32007-08-21 23:59:34 +000079
Claudius Heine5ef662b2015-06-24 10:03:50 +020080 BOOST_CHECK(a == a2);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010081}
82
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020083BOOST_AUTO_TEST_CASE(test_copy) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010084 string* str1 = new string("abcd1234");
James E. King, III0ad20bd2017-09-30 15:44:16 -070085 ptrdiff_t str1addr = reinterpret_cast<ptrdiff_t>(str1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010086 const char* data1 = str1->data();
87 TMemoryBuffer buf((uint8_t*)str1->data(),
88 static_cast<uint32_t>(str1->length()),
89 TMemoryBuffer::COPY);
90 delete str1;
91 string* str2 = new string("plsreuse");
James E. King, III0ad20bd2017-09-30 15:44:16 -070092 bool obj_reuse = (str1addr == reinterpret_cast<ptrdiff_t>(str2));
Konrad Grochowski16a23a62014-11-13 15:33:38 +010093 bool dat_reuse = (data1 == str2->data());
Konrad Grochowskie9bdb412015-09-25 20:17:36 +020094 BOOST_TEST_MESSAGE("Object reuse: " << obj_reuse << " Data reuse: " << dat_reuse
Claudius Heine5ef662b2015-06-24 10:03:50 +020095 << ((obj_reuse && dat_reuse) ? " YAY!" : ""));
Konrad Grochowski16a23a62014-11-13 15:33:38 +010096 delete str2;
97
98 string str3 = "wxyz", str4 = "6789";
99 buf.readAppendToString(str3, 4);
100 buf.readAppendToString(str4, INT_MAX);
101
Claudius Heine5ef662b2015-06-24 10:03:50 +0200102 BOOST_CHECK(str3 == "wxyzabcd");
103 BOOST_CHECK(str4 == "67891234");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100104}
105
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200106BOOST_AUTO_TEST_CASE(test_exceptions) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100107 char data[] = "foo\0bar";
108
109 TMemoryBuffer buf1((uint8_t*)data, 7, TMemoryBuffer::OBSERVE);
110 string str = buf1.getBufferAsString();
Claudius Heine5ef662b2015-06-24 10:03:50 +0200111 BOOST_CHECK(str.length() == 7);
112
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100113 buf1.resetBuffer();
Claudius Heine5ef662b2015-06-24 10:03:50 +0200114
115 BOOST_CHECK_THROW(buf1.write((const uint8_t*)"foo", 3), TTransportException);
David Reiss2dc72c32007-08-21 23:59:34 +0000116
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100117 TMemoryBuffer buf2((uint8_t*)data, 7, TMemoryBuffer::COPY);
Claudius Heine5ef662b2015-06-24 10:03:50 +0200118 BOOST_CHECK_NO_THROW(buf2.write((const uint8_t*)"bar", 3));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100119}
David Reissf7baf542008-02-04 21:56:27 +0000120
James E. King, IIIaded00b2017-09-22 11:20:15 -0700121BOOST_AUTO_TEST_CASE(test_default_maximum_buffer_size)
122{
123 BOOST_CHECK_EQUAL(std::numeric_limits<uint32_t>::max(), TMemoryBuffer().getMaxBufferSize());
Alex Şuhan20116c62017-08-03 12:28:17 -0700124}
James E. King, IIIaded00b2017-09-22 11:20:15 -0700125
126BOOST_AUTO_TEST_CASE(test_default_buffer_size)
127{
128 BOOST_CHECK_EQUAL(1024, TMemoryBuffer().getBufferSize());
129}
130
131BOOST_AUTO_TEST_CASE(test_error_set_max_buffer_size_too_small)
132{
133 TMemoryBuffer buf;
134 BOOST_CHECK_THROW(buf.setMaxBufferSize(buf.getBufferSize() - 1), TTransportException);
135}
136
137BOOST_AUTO_TEST_CASE(test_maximum_buffer_size)
138{
139 TMemoryBuffer buf;
140 buf.setMaxBufferSize(8192);
141 std::vector<uint8_t> small_buff(1);
142
143 for (size_t i = 0; i < 8192; ++i)
144 {
145 buf.write(&small_buff[0], 1);
146 }
147
148 BOOST_CHECK_THROW(buf.write(&small_buff[0], 1), TTransportException);
149}
150
151BOOST_AUTO_TEST_CASE(test_memory_buffer_to_get_sizeof_objects)
152{
153 // This is a demonstration of how to use TMemoryBuffer to determine
154 // the serialized size of a thrift object in the Binary protocol.
155 // See THRIFT-3480
156
157 shared_ptr<TMemoryBuffer> memBuffer(new TMemoryBuffer());
158 shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(memBuffer));
159
160 thrift::test::Xtruct object;
161 object.i32_thing = 10;
162 object.i64_thing = 30;
163 object.string_thing = "who's your daddy?";
164
165 uint32_t size = object.write(binaryProtcol.get());
166 BOOST_CHECK_EQUAL(47, size);
167}
Alex Şuhan20116c62017-08-03 12:28:17 -0700168
Roger Meier0069cc42010-10-13 18:10:18 +0000169BOOST_AUTO_TEST_SUITE_END()