David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [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 | |
Bryan Duxbury | f7d9f8e | 2010-05-02 23:04:03 +0000 | [diff] [blame] | 20 | #include <boost/test/auto_unit_test.hpp> |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 21 | #include <iostream> |
| 22 | #include <climits> |
| 23 | #include <cassert> |
Jim King | 6077481 | 2015-05-10 08:08:18 -0400 | [diff] [blame] | 24 | #include <vector> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 25 | #include <thrift/transport/TBufferTransports.h> |
| 26 | #include <thrift/protocol/TBinaryProtocol.h> |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 27 | #include "gen-cpp/ThriftTest_types.h" |
| 28 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 29 | BOOST_AUTO_TEST_SUITE(TMemoryBufferTest) |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 30 | |
Jim King | 6077481 | 2015-05-10 08:08:18 -0400 | [diff] [blame] | 31 | using apache::thrift::protocol::TBinaryProtocol; |
| 32 | using apache::thrift::transport::TMemoryBuffer; |
| 33 | using apache::thrift::transport::TTransportException; |
| 34 | using boost::shared_ptr; |
| 35 | using std::cout; |
| 36 | using std::endl; |
| 37 | using std::string; |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 38 | |
Jim King | 6077481 | 2015-05-10 08:08:18 -0400 | [diff] [blame] | 39 | BOOST_AUTO_TEST_CASE(test_read_write_grow) |
| 40 | { |
Roger Meier | a6b6633 | 2015-05-15 15:21:50 +0200 | [diff] [blame^] | 41 | // Added to test the fix for THRIFT-1248 |
| 42 | TMemoryBuffer uut; |
| 43 | const int maxSize = 65536; |
| 44 | uint8_t verify[maxSize]; |
| 45 | std::vector<uint8_t> buf; |
| 46 | buf.resize(maxSize); |
Jim King | 6077481 | 2015-05-10 08:08:18 -0400 | [diff] [blame] | 47 | |
Roger Meier | a6b6633 | 2015-05-15 15:21:50 +0200 | [diff] [blame^] | 48 | for (uint32_t i = 0; i < maxSize; ++i) |
| 49 | { |
| 50 | buf[i] = static_cast<uint8_t>(i); |
| 51 | } |
Jim King | 6077481 | 2015-05-10 08:08:18 -0400 | [diff] [blame] | 52 | |
Roger Meier | a6b6633 | 2015-05-15 15:21:50 +0200 | [diff] [blame^] | 53 | for (uint32_t i = 1; i < maxSize; i *= 2) |
| 54 | { |
| 55 | uut.write(&buf[0], i); |
| 56 | } |
| 57 | |
| 58 | for (uint32_t i = 1; i < maxSize; i *= 2) |
| 59 | { |
| 60 | uut.read(verify, i); |
| 61 | BOOST_CHECK_EQUAL(0, ::memcmp(verify, &buf[0], i)); |
| 62 | } |
Jim King | 6077481 | 2015-05-10 08:08:18 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | BOOST_AUTO_TEST_CASE(test_roundtrip) |
| 66 | { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 67 | shared_ptr<TMemoryBuffer> strBuffer(new TMemoryBuffer()); |
| 68 | shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(strBuffer)); |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 69 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 70 | thrift::test::Xtruct a; |
| 71 | a.i32_thing = 10; |
| 72 | a.i64_thing = 30; |
| 73 | a.string_thing = "holla back a"; |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 74 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 75 | a.write(binaryProtcol.get()); |
| 76 | std::string serialized = strBuffer->getBufferAsString(); |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 77 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 78 | shared_ptr<TMemoryBuffer> strBuffer2(new TMemoryBuffer()); |
| 79 | shared_ptr<TBinaryProtocol> binaryProtcol2(new TBinaryProtocol(strBuffer2)); |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 80 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 81 | strBuffer2->resetBuffer((uint8_t*)serialized.data(), static_cast<uint32_t>(serialized.length())); |
| 82 | thrift::test::Xtruct a2; |
| 83 | a2.read(binaryProtcol2.get()); |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 84 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 85 | assert(a == a2); |
| 86 | } |
| 87 | |
Jim King | 6077481 | 2015-05-10 08:08:18 -0400 | [diff] [blame] | 88 | BOOST_AUTO_TEST_CASE(test_copy) |
| 89 | { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 90 | string* str1 = new string("abcd1234"); |
| 91 | const char* data1 = str1->data(); |
| 92 | TMemoryBuffer buf((uint8_t*)str1->data(), |
| 93 | static_cast<uint32_t>(str1->length()), |
| 94 | TMemoryBuffer::COPY); |
| 95 | delete str1; |
| 96 | string* str2 = new string("plsreuse"); |
| 97 | bool obj_reuse = (str1 == str2); |
| 98 | bool dat_reuse = (data1 == str2->data()); |
| 99 | cout << "Object reuse: " << obj_reuse << " Data reuse: " << dat_reuse |
| 100 | << ((obj_reuse && dat_reuse) ? " YAY!" : "") << endl; |
| 101 | delete str2; |
| 102 | |
| 103 | string str3 = "wxyz", str4 = "6789"; |
| 104 | buf.readAppendToString(str3, 4); |
| 105 | buf.readAppendToString(str4, INT_MAX); |
| 106 | |
| 107 | assert(str3 == "wxyzabcd"); |
| 108 | assert(str4 == "67891234"); |
| 109 | } |
| 110 | |
Jim King | 6077481 | 2015-05-10 08:08:18 -0400 | [diff] [blame] | 111 | BOOST_AUTO_TEST_CASE(test_exceptions) |
| 112 | { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 113 | char data[] = "foo\0bar"; |
| 114 | |
| 115 | TMemoryBuffer buf1((uint8_t*)data, 7, TMemoryBuffer::OBSERVE); |
| 116 | string str = buf1.getBufferAsString(); |
| 117 | assert(str.length() == 7); |
| 118 | buf1.resetBuffer(); |
| 119 | try { |
| 120 | buf1.write((const uint8_t*)"foo", 3); |
| 121 | assert(false); |
| 122 | } catch (TTransportException&) { |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 125 | TMemoryBuffer buf2((uint8_t*)data, 7, TMemoryBuffer::COPY); |
| 126 | try { |
| 127 | buf2.write((const uint8_t*)"bar", 3); |
| 128 | } catch (TTransportException&) { |
| 129 | assert(false); |
David Reiss | 2dc72c3 | 2007-08-21 23:59:34 +0000 | [diff] [blame] | 130 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 131 | } |
David Reiss | f7baf54 | 2008-02-04 21:56:27 +0000 | [diff] [blame] | 132 | |
Roger Meier | 0069cc4 | 2010-10-13 18:10:18 +0000 | [diff] [blame] | 133 | BOOST_AUTO_TEST_SUITE_END() |