blob: cf49477d723c4b857e9b5e24ca3f967ea8be3acd [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>
Roger Meier49ff8b12012-04-13 09:12:31 +000024#include <thrift/transport/TBufferTransports.h>
25#include <thrift/protocol/TBinaryProtocol.h>
David Reiss2dc72c32007-08-21 23:59:34 +000026#include "gen-cpp/ThriftTest_types.h"
27
Konrad Grochowski16a23a62014-11-13 15:33:38 +010028BOOST_AUTO_TEST_SUITE(TMemoryBufferTest)
David Reiss2dc72c32007-08-21 23:59:34 +000029
Konrad Grochowski16a23a62014-11-13 15:33:38 +010030BOOST_AUTO_TEST_CASE(test_roundtrip) {
31 using apache::thrift::transport::TMemoryBuffer;
32 using apache::thrift::protocol::TBinaryProtocol;
33 using boost::shared_ptr;
David Reiss2dc72c32007-08-21 23:59:34 +000034
Konrad Grochowski16a23a62014-11-13 15:33:38 +010035 shared_ptr<TMemoryBuffer> strBuffer(new TMemoryBuffer());
36 shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(strBuffer));
David Reiss2dc72c32007-08-21 23:59:34 +000037
Konrad Grochowski16a23a62014-11-13 15:33:38 +010038 thrift::test::Xtruct a;
39 a.i32_thing = 10;
40 a.i64_thing = 30;
41 a.string_thing = "holla back a";
David Reiss2dc72c32007-08-21 23:59:34 +000042
Konrad Grochowski16a23a62014-11-13 15:33:38 +010043 a.write(binaryProtcol.get());
44 std::string serialized = strBuffer->getBufferAsString();
David Reiss2dc72c32007-08-21 23:59:34 +000045
Konrad Grochowski16a23a62014-11-13 15:33:38 +010046 shared_ptr<TMemoryBuffer> strBuffer2(new TMemoryBuffer());
47 shared_ptr<TBinaryProtocol> binaryProtcol2(new TBinaryProtocol(strBuffer2));
David Reiss2dc72c32007-08-21 23:59:34 +000048
Konrad Grochowski16a23a62014-11-13 15:33:38 +010049 strBuffer2->resetBuffer((uint8_t*)serialized.data(), static_cast<uint32_t>(serialized.length()));
50 thrift::test::Xtruct a2;
51 a2.read(binaryProtcol2.get());
David Reiss2dc72c32007-08-21 23:59:34 +000052
Konrad Grochowski16a23a62014-11-13 15:33:38 +010053 assert(a == a2);
54}
55
56BOOST_AUTO_TEST_CASE(test_copy) {
57 using apache::thrift::transport::TMemoryBuffer;
58 using std::string;
59 using std::cout;
60 using std::endl;
61
62 string* str1 = new string("abcd1234");
63 const char* data1 = str1->data();
64 TMemoryBuffer buf((uint8_t*)str1->data(),
65 static_cast<uint32_t>(str1->length()),
66 TMemoryBuffer::COPY);
67 delete str1;
68 string* str2 = new string("plsreuse");
69 bool obj_reuse = (str1 == str2);
70 bool dat_reuse = (data1 == str2->data());
71 cout << "Object reuse: " << obj_reuse << " Data reuse: " << dat_reuse
72 << ((obj_reuse && dat_reuse) ? " YAY!" : "") << endl;
73 delete str2;
74
75 string str3 = "wxyz", str4 = "6789";
76 buf.readAppendToString(str3, 4);
77 buf.readAppendToString(str4, INT_MAX);
78
79 assert(str3 == "wxyzabcd");
80 assert(str4 == "67891234");
81}
82
83BOOST_AUTO_TEST_CASE(test_exceptions) {
84 using apache::thrift::transport::TTransportException;
85 using apache::thrift::transport::TMemoryBuffer;
86 using std::string;
87
88 char data[] = "foo\0bar";
89
90 TMemoryBuffer buf1((uint8_t*)data, 7, TMemoryBuffer::OBSERVE);
91 string str = buf1.getBufferAsString();
92 assert(str.length() == 7);
93 buf1.resetBuffer();
94 try {
95 buf1.write((const uint8_t*)"foo", 3);
96 assert(false);
97 } catch (TTransportException&) {
David Reiss2dc72c32007-08-21 23:59:34 +000098 }
99
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100100 TMemoryBuffer buf2((uint8_t*)data, 7, TMemoryBuffer::COPY);
101 try {
102 buf2.write((const uint8_t*)"bar", 3);
103 } catch (TTransportException&) {
104 assert(false);
David Reiss2dc72c32007-08-21 23:59:34 +0000105 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100106}
David Reissf7baf542008-02-04 21:56:27 +0000107
Roger Meier0069cc42010-10-13 18:10:18 +0000108BOOST_AUTO_TEST_SUITE_END()