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