blob: 82b9ed629542d148879a70f83cbc01254a575da7 [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>
23#include <cassert>
Jim King60774812015-05-10 08:08:18 -040024#include <vector>
Roger Meier49ff8b12012-04-13 09:12:31 +000025#include <thrift/transport/TBufferTransports.h>
26#include <thrift/protocol/TBinaryProtocol.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;
34using boost::shared_ptr;
35using std::cout;
36using std::endl;
37using std::string;
David Reiss2dc72c32007-08-21 23:59:34 +000038
Jim King60774812015-05-10 08:08:18 -040039BOOST_AUTO_TEST_CASE(test_read_write_grow)
40{
41 // Added to test the fix for THRIFT-1248
42 TMemoryBuffer uut;
43 const int maxSiz = 65536;
44 std::vector<uint8_t> buf;
45 buf.resize(maxSiz);
46 for (uint32_t i = 0; i < maxSiz; ++i)
47 {
48 buf[i] = static_cast<uint8_t>(i);
49 }
50
51 for (uint32_t i = 1; i < maxSiz; i *= 2)
52 {
53 uut.write(&buf[0], i);
54 }
55
56 for (uint32_t i = 1; i < maxSiz; i *= 2)
57 {
58 uint8_t verify[i];
59 uut.read(verify, i);
60 BOOST_CHECK_EQUAL(0, ::memcmp(verify, &buf[0], i));
61 }
62}
63
64BOOST_AUTO_TEST_CASE(test_roundtrip)
65{
Konrad Grochowski16a23a62014-11-13 15:33:38 +010066 shared_ptr<TMemoryBuffer> strBuffer(new TMemoryBuffer());
67 shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(strBuffer));
David Reiss2dc72c32007-08-21 23:59:34 +000068
Konrad Grochowski16a23a62014-11-13 15:33:38 +010069 thrift::test::Xtruct a;
70 a.i32_thing = 10;
71 a.i64_thing = 30;
72 a.string_thing = "holla back a";
David Reiss2dc72c32007-08-21 23:59:34 +000073
Konrad Grochowski16a23a62014-11-13 15:33:38 +010074 a.write(binaryProtcol.get());
75 std::string serialized = strBuffer->getBufferAsString();
David Reiss2dc72c32007-08-21 23:59:34 +000076
Konrad Grochowski16a23a62014-11-13 15:33:38 +010077 shared_ptr<TMemoryBuffer> strBuffer2(new TMemoryBuffer());
78 shared_ptr<TBinaryProtocol> binaryProtcol2(new TBinaryProtocol(strBuffer2));
David Reiss2dc72c32007-08-21 23:59:34 +000079
Konrad Grochowski16a23a62014-11-13 15:33:38 +010080 strBuffer2->resetBuffer((uint8_t*)serialized.data(), static_cast<uint32_t>(serialized.length()));
81 thrift::test::Xtruct a2;
82 a2.read(binaryProtcol2.get());
David Reiss2dc72c32007-08-21 23:59:34 +000083
Konrad Grochowski16a23a62014-11-13 15:33:38 +010084 assert(a == a2);
85}
86
Jim King60774812015-05-10 08:08:18 -040087BOOST_AUTO_TEST_CASE(test_copy)
88{
Konrad Grochowski16a23a62014-11-13 15:33:38 +010089 string* str1 = new string("abcd1234");
90 const char* data1 = str1->data();
91 TMemoryBuffer buf((uint8_t*)str1->data(),
92 static_cast<uint32_t>(str1->length()),
93 TMemoryBuffer::COPY);
94 delete str1;
95 string* str2 = new string("plsreuse");
96 bool obj_reuse = (str1 == str2);
97 bool dat_reuse = (data1 == str2->data());
98 cout << "Object reuse: " << obj_reuse << " Data reuse: " << dat_reuse
99 << ((obj_reuse && dat_reuse) ? " YAY!" : "") << endl;
100 delete str2;
101
102 string str3 = "wxyz", str4 = "6789";
103 buf.readAppendToString(str3, 4);
104 buf.readAppendToString(str4, INT_MAX);
105
106 assert(str3 == "wxyzabcd");
107 assert(str4 == "67891234");
108}
109
Jim King60774812015-05-10 08:08:18 -0400110BOOST_AUTO_TEST_CASE(test_exceptions)
111{
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100112 char data[] = "foo\0bar";
113
114 TMemoryBuffer buf1((uint8_t*)data, 7, TMemoryBuffer::OBSERVE);
115 string str = buf1.getBufferAsString();
116 assert(str.length() == 7);
117 buf1.resetBuffer();
118 try {
119 buf1.write((const uint8_t*)"foo", 3);
120 assert(false);
121 } catch (TTransportException&) {
David Reiss2dc72c32007-08-21 23:59:34 +0000122 }
123
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100124 TMemoryBuffer buf2((uint8_t*)data, 7, TMemoryBuffer::COPY);
125 try {
126 buf2.write((const uint8_t*)"bar", 3);
127 } catch (TTransportException&) {
128 assert(false);
David Reiss2dc72c32007-08-21 23:59:34 +0000129 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100130}
David Reissf7baf542008-02-04 21:56:27 +0000131
Roger Meier0069cc42010-10-13 18:10:18 +0000132BOOST_AUTO_TEST_SUITE_END()