David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 1 | /* |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | |
David Reiss | 3cc9dab | 2010-10-06 17:10:21 +0000 | [diff] [blame] | 20 | #define __STDC_FORMAT_MACROS |
| 21 | |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 22 | #ifndef _GNU_SOURCE |
| 23 | #define _GNU_SOURCE // needed for getopt_long |
| 24 | #endif |
| 25 | |
David Reiss | 3cc9dab | 2010-10-06 17:10:21 +0000 | [diff] [blame] | 26 | #include <stdint.h> |
| 27 | #include <inttypes.h> |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 28 | #include <cstddef> |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 29 | #include <fstream> |
| 30 | #include <iostream> |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 31 | #include <thrift/cxxfunctional.h> |
David Reiss | 3cc9dab | 2010-10-06 17:10:21 +0000 | [diff] [blame] | 32 | |
| 33 | #include <boost/random.hpp> |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 34 | #include <boost/shared_array.hpp> |
| 35 | #include <boost/test/unit_test.hpp> |
David Reiss | 3cc9dab | 2010-10-06 17:10:21 +0000 | [diff] [blame] | 36 | |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 37 | #include <thrift/transport/TBufferTransports.h> |
| 38 | #include <thrift/transport/TZlibTransport.h> |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 39 | |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 40 | using namespace std; |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 41 | using namespace apache::thrift::transport; |
| 42 | |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 43 | boost::mt19937 rng; |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 44 | |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 45 | /* |
| 46 | * Utility code |
| 47 | */ |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 48 | |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 49 | class SizeGenerator { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 50 | public: |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 51 | virtual ~SizeGenerator() {} |
| 52 | virtual unsigned int getSize() = 0; |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 55 | class ConstantSizeGenerator : public SizeGenerator { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 56 | public: |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 57 | ConstantSizeGenerator(unsigned int value) : value_(value) {} |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 58 | virtual unsigned int getSize() { return value_; } |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 59 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 60 | private: |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 61 | unsigned int value_; |
| 62 | }; |
| 63 | |
| 64 | class LogNormalSizeGenerator : public SizeGenerator { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 65 | public: |
| 66 | LogNormalSizeGenerator(double mean, double std_dev) |
| 67 | : gen_(rng, boost::lognormal_distribution<double>(mean, std_dev)) {} |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 68 | |
| 69 | virtual unsigned int getSize() { |
| 70 | // Loop until we get a size of 1 or more |
| 71 | while (true) { |
| 72 | unsigned int value = static_cast<unsigned int>(gen_()); |
| 73 | if (value >= 1) { |
| 74 | return value; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 79 | private: |
| 80 | boost::variate_generator<boost::mt19937, boost::lognormal_distribution<double> > gen_; |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 81 | }; |
David Reiss | 3cc9dab | 2010-10-06 17:10:21 +0000 | [diff] [blame] | 82 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 83 | boost::shared_array<uint8_t> gen_uniform_buffer(uint32_t buf_len, uint8_t c) { |
David Reiss | 3cc9dab | 2010-10-06 17:10:21 +0000 | [diff] [blame] | 84 | uint8_t* buf = new uint8_t[buf_len]; |
| 85 | memset(buf, c, buf_len); |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 86 | return boost::shared_array<uint8_t>(buf); |
David Reiss | 3cc9dab | 2010-10-06 17:10:21 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 89 | boost::shared_array<uint8_t> gen_compressible_buffer(uint32_t buf_len) { |
David Reiss | 3cc9dab | 2010-10-06 17:10:21 +0000 | [diff] [blame] | 90 | uint8_t* buf = new uint8_t[buf_len]; |
| 91 | |
| 92 | // Generate small runs of alternately increasing and decreasing bytes |
| 93 | boost::uniform_smallint<uint32_t> run_length_distribution(1, 64); |
| 94 | boost::uniform_smallint<uint8_t> byte_distribution(0, UINT8_MAX); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 95 | boost::variate_generator<boost::mt19937, boost::uniform_smallint<uint8_t> > |
| 96 | byte_generator(rng, byte_distribution); |
| 97 | boost::variate_generator<boost::mt19937, boost::uniform_smallint<uint32_t> > |
| 98 | run_len_generator(rng, run_length_distribution); |
David Reiss | 3cc9dab | 2010-10-06 17:10:21 +0000 | [diff] [blame] | 99 | |
| 100 | uint32_t idx = 0; |
| 101 | int8_t step = 1; |
| 102 | while (idx < buf_len) { |
| 103 | uint32_t run_length = run_len_generator(); |
| 104 | if (idx + run_length > buf_len) { |
| 105 | run_length = buf_len - idx; |
| 106 | } |
| 107 | |
| 108 | uint8_t byte = byte_generator(); |
| 109 | for (uint32_t n = 0; n < run_length; ++n) { |
| 110 | buf[idx] = byte; |
| 111 | ++idx; |
| 112 | byte += step; |
| 113 | } |
| 114 | |
| 115 | step *= -1; |
| 116 | } |
| 117 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 118 | return boost::shared_array<uint8_t>(buf); |
David Reiss | 3cc9dab | 2010-10-06 17:10:21 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 121 | boost::shared_array<uint8_t> gen_random_buffer(uint32_t buf_len) { |
David Reiss | 3cc9dab | 2010-10-06 17:10:21 +0000 | [diff] [blame] | 122 | uint8_t* buf = new uint8_t[buf_len]; |
| 123 | |
| 124 | boost::uniform_smallint<uint8_t> distribution(0, UINT8_MAX); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 125 | boost::variate_generator<boost::mt19937, boost::uniform_smallint<uint8_t> > |
| 126 | generator(rng, distribution); |
David Reiss | 3cc9dab | 2010-10-06 17:10:21 +0000 | [diff] [blame] | 127 | |
| 128 | for (uint32_t n = 0; n < buf_len; ++n) { |
| 129 | buf[n] = generator(); |
| 130 | } |
| 131 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 132 | return boost::shared_array<uint8_t>(buf); |
David Reiss | 3cc9dab | 2010-10-06 17:10:21 +0000 | [diff] [blame] | 133 | } |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 134 | |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 135 | /* |
| 136 | * Test functions |
| 137 | */ |
| 138 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 139 | void test_write_then_read(const boost::shared_array<uint8_t> buf, uint32_t buf_len) { |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 140 | boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer()); |
| 141 | boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf)); |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 142 | zlib_trans->write(buf.get(), buf_len); |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 143 | zlib_trans->finish(); |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 144 | |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 145 | boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]); |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 146 | uint32_t got = zlib_trans->readAll(mirror.get(), buf_len); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 147 | BOOST_REQUIRE_EQUAL(got, buf_len); |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 148 | BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 149 | zlib_trans->verifyChecksum(); |
| 150 | } |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 151 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 152 | void test_separate_checksum(const boost::shared_array<uint8_t> buf, uint32_t buf_len) { |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 153 | // This one is tricky. I separate the last byte of the stream out |
| 154 | // into a separate crbuf_. The last byte is part of the checksum, |
| 155 | // so the entire read goes fine, but when I go to verify the checksum |
| 156 | // it isn't there. The original implementation complained that |
| 157 | // the stream was not complete. I'm about to go fix that. |
| 158 | // It worked. Awesome. |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 159 | boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer()); |
| 160 | boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf)); |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 161 | zlib_trans->write(buf.get(), buf_len); |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 162 | zlib_trans->finish(); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 163 | string tmp_buf; |
| 164 | membuf->appendBufferToString(tmp_buf); |
David Reiss | a0e1159 | 2010-10-06 17:10:27 +0000 | [diff] [blame] | 165 | zlib_trans.reset(new TZlibTransport(membuf, |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 166 | TZlibTransport::DEFAULT_URBUF_SIZE, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 167 | static_cast<uint32_t>(tmp_buf.length() - 1))); |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 168 | |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 169 | boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]); |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 170 | uint32_t got = zlib_trans->readAll(mirror.get(), buf_len); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 171 | BOOST_REQUIRE_EQUAL(got, buf_len); |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 172 | BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 173 | zlib_trans->verifyChecksum(); |
| 174 | } |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 175 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 176 | void test_incomplete_checksum(const boost::shared_array<uint8_t> buf, uint32_t buf_len) { |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 177 | // Make sure we still get that "not complete" error if |
| 178 | // it really isn't complete. |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 179 | boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer()); |
| 180 | boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf)); |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 181 | zlib_trans->write(buf.get(), buf_len); |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 182 | zlib_trans->finish(); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 183 | string tmp_buf; |
| 184 | membuf->appendBufferToString(tmp_buf); |
| 185 | tmp_buf.erase(tmp_buf.length() - 1); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 186 | membuf->resetBuffer(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(tmp_buf.data())), |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 187 | static_cast<uint32_t>(tmp_buf.length())); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 188 | |
| 189 | boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]); |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 190 | uint32_t got = zlib_trans->readAll(mirror.get(), buf_len); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 191 | BOOST_REQUIRE_EQUAL(got, buf_len); |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 192 | BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 193 | try { |
| 194 | zlib_trans->verifyChecksum(); |
| 195 | BOOST_ERROR("verifyChecksum() did not report an error"); |
| 196 | } catch (TTransportException& ex) { |
| 197 | BOOST_CHECK_EQUAL(ex.getType(), TTransportException::CORRUPTED_DATA); |
| 198 | } |
| 199 | } |
| 200 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 201 | void test_read_write_mix(const boost::shared_array<uint8_t> buf, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 202 | uint32_t buf_len, |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 203 | const boost::shared_ptr<SizeGenerator>& write_gen, |
| 204 | const boost::shared_ptr<SizeGenerator>& read_gen) { |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 205 | // Try it with a mix of read/write sizes. |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 206 | boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer()); |
| 207 | boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf)); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 208 | unsigned int tot; |
| 209 | |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 210 | tot = 0; |
| 211 | while (tot < buf_len) { |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 212 | uint32_t write_len = write_gen->getSize(); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 213 | if (tot + write_len > buf_len) { |
| 214 | write_len = buf_len - tot; |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 215 | } |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 216 | zlib_trans->write(buf.get() + tot, write_len); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 217 | tot += write_len; |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 218 | } |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 219 | |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 220 | zlib_trans->finish(); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 221 | |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 222 | tot = 0; |
| 223 | boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]); |
| 224 | while (tot < buf_len) { |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 225 | uint32_t read_len = read_gen->getSize(); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 226 | uint32_t expected_read_len = read_len; |
| 227 | if (tot + read_len > buf_len) { |
| 228 | expected_read_len = buf_len - tot; |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 229 | } |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 230 | uint32_t got = zlib_trans->read(mirror.get() + tot, read_len); |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 231 | BOOST_REQUIRE_LE(got, expected_read_len); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 232 | BOOST_REQUIRE_NE(got, (uint32_t)0); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 233 | tot += got; |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 234 | } |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 235 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 236 | BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 237 | zlib_trans->verifyChecksum(); |
| 238 | } |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 239 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 240 | void test_invalid_checksum(const boost::shared_array<uint8_t> buf, uint32_t buf_len) { |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 241 | // Verify checksum checking. |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 242 | boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer()); |
| 243 | boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf)); |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 244 | zlib_trans->write(buf.get(), buf_len); |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 245 | zlib_trans->finish(); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 246 | string tmp_buf; |
| 247 | membuf->appendBufferToString(tmp_buf); |
| 248 | // Modify a byte at the end of the buffer (part of the checksum). |
| 249 | // On rare occasions, modifying a byte in the middle of the buffer |
| 250 | // isn't caught by the checksum. |
| 251 | // |
| 252 | // (This happens especially often for the uniform buffer. The |
| 253 | // re-inflated data is correct, however. I suspect in this case that |
| 254 | // we're more likely to modify bytes that are part of zlib metadata |
| 255 | // instead of the actual compressed data.) |
| 256 | // |
| 257 | // I've also seen some failure scenarios where a checksum failure isn't |
| 258 | // reported, but zlib keeps trying to decode past the end of the data. |
| 259 | // (When this occurs, verifyChecksum() throws an exception indicating |
| 260 | // that the end of the data hasn't been reached.) I haven't seen this |
| 261 | // error when only modifying checksum bytes. |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 262 | int index = static_cast<int>(tmp_buf.size() - 1); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 263 | tmp_buf[index]++; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 264 | membuf->resetBuffer(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(tmp_buf.data())), |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 265 | static_cast<uint32_t>(tmp_buf.length())); |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 266 | |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 267 | boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]); |
| 268 | try { |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 269 | zlib_trans->readAll(mirror.get(), buf_len); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 270 | zlib_trans->verifyChecksum(); |
| 271 | BOOST_ERROR("verifyChecksum() did not report an error"); |
| 272 | } catch (TZlibTransportException& ex) { |
| 273 | BOOST_CHECK_EQUAL(ex.getType(), TTransportException::INTERNAL_ERROR); |
| 274 | } |
| 275 | } |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 276 | |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 277 | void test_write_after_flush(const boost::shared_array<uint8_t> buf, uint32_t buf_len) { |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 278 | // write some data |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 279 | boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer()); |
| 280 | boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf)); |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 281 | zlib_trans->write(buf.get(), buf_len); |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 282 | |
| 283 | // call finish() |
| 284 | zlib_trans->finish(); |
| 285 | |
| 286 | // make sure write() throws an error |
| 287 | try { |
| 288 | uint8_t write_buf[] = "a"; |
| 289 | zlib_trans->write(write_buf, 1); |
| 290 | BOOST_ERROR("write() after finish() did not raise an exception"); |
| 291 | } catch (TTransportException& ex) { |
| 292 | BOOST_CHECK_EQUAL(ex.getType(), TTransportException::BAD_ARGS); |
| 293 | } |
| 294 | |
| 295 | // make sure flush() throws an error |
| 296 | try { |
| 297 | zlib_trans->flush(); |
| 298 | BOOST_ERROR("flush() after finish() did not raise an exception"); |
| 299 | } catch (TTransportException& ex) { |
| 300 | BOOST_CHECK_EQUAL(ex.getType(), TTransportException::BAD_ARGS); |
| 301 | } |
| 302 | |
| 303 | // make sure finish() throws an error |
| 304 | try { |
| 305 | zlib_trans->finish(); |
| 306 | BOOST_ERROR("finish() after finish() did not raise an exception"); |
| 307 | } catch (TTransportException& ex) { |
| 308 | BOOST_CHECK_EQUAL(ex.getType(), TTransportException::BAD_ARGS); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void test_no_write() { |
| 313 | // Verify that no data is written to the underlying transport if we |
| 314 | // never write data to the TZlibTransport. |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 315 | boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer()); |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 316 | { |
| 317 | // Create a TZlibTransport object, and immediately destroy it |
| 318 | // when it goes out of scope. |
David Reiss | a0e1159 | 2010-10-06 17:10:27 +0000 | [diff] [blame] | 319 | TZlibTransport w_zlib_trans(membuf); |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 322 | BOOST_CHECK_EQUAL(membuf->available_read(), (uint32_t)0); |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 323 | } |
| 324 | |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 325 | /* |
| 326 | * Initialization |
| 327 | */ |
| 328 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 329 | #define ADD_TEST_CASE(suite, name, function, ...) \ |
| 330 | do { \ |
| 331 | ::std::ostringstream name_ss; \ |
| 332 | name_ss << name << "-" << BOOST_STRINGIZE(function); \ |
| 333 | ::boost::unit_test::test_case* tc \ |
| 334 | = ::boost::unit_test::make_test_case(::apache::thrift::stdcxx::bind(function, \ |
| 335 | ##__VA_ARGS__), \ |
| 336 | name_ss.str()); \ |
| 337 | (suite)->add(tc); \ |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 338 | } while (0) |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 339 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 340 | void add_tests(boost::unit_test::test_suite* suite, |
Jim King | 7848d88 | 2015-04-06 21:38:06 -0400 | [diff] [blame] | 341 | const boost::shared_array<uint8_t>& buf, |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 342 | uint32_t buf_len, |
| 343 | const char* name) { |
| 344 | ADD_TEST_CASE(suite, name, test_write_then_read, buf, buf_len); |
| 345 | ADD_TEST_CASE(suite, name, test_separate_checksum, buf, buf_len); |
| 346 | ADD_TEST_CASE(suite, name, test_incomplete_checksum, buf, buf_len); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 347 | ADD_TEST_CASE(suite, name, test_invalid_checksum, buf, buf_len); |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 348 | ADD_TEST_CASE(suite, name, test_write_after_flush, buf, buf_len); |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 349 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 350 | boost::shared_ptr<SizeGenerator> size_32k(new ConstantSizeGenerator(1 << 15)); |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 351 | boost::shared_ptr<SizeGenerator> size_lognormal(new LogNormalSizeGenerator(20, 30)); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 352 | ADD_TEST_CASE(suite, name << "-constant", test_read_write_mix, buf, buf_len, size_32k, size_32k); |
| 353 | ADD_TEST_CASE(suite, |
| 354 | name << "-lognormal-write", |
| 355 | test_read_write_mix, |
| 356 | buf, |
| 357 | buf_len, |
| 358 | size_lognormal, |
| 359 | size_32k); |
| 360 | ADD_TEST_CASE(suite, |
| 361 | name << "-lognormal-read", |
| 362 | test_read_write_mix, |
| 363 | buf, |
| 364 | buf_len, |
| 365 | size_32k, |
| 366 | size_lognormal); |
| 367 | ADD_TEST_CASE(suite, |
| 368 | name << "-lognormal-both", |
| 369 | test_read_write_mix, |
| 370 | buf, |
| 371 | buf_len, |
| 372 | size_lognormal, |
| 373 | size_lognormal); |
David Reiss | f2abcf9 | 2010-10-06 17:10:24 +0000 | [diff] [blame] | 374 | |
| 375 | // Test with a random size distribution, |
| 376 | // but use the exact same distribution for reading as for writing. |
| 377 | // |
| 378 | // Because the SizeGenerator makes a copy of the random number generator, |
| 379 | // both SizeGenerators should return the exact same set of values, since they |
| 380 | // both start with random number generators in the same state. |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 381 | boost::shared_ptr<SizeGenerator> write_size_gen(new LogNormalSizeGenerator(20, 30)); |
| 382 | boost::shared_ptr<SizeGenerator> read_size_gen(new LogNormalSizeGenerator(20, 30)); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 383 | ADD_TEST_CASE(suite, |
| 384 | name << "-lognormal-same-distribution", |
| 385 | test_read_write_mix, |
| 386 | buf, |
| 387 | buf_len, |
| 388 | write_size_gen, |
| 389 | read_size_gen); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | void print_usage(FILE* f, const char* argv0) { |
| 393 | fprintf(f, "Usage: %s [boost_options] [options]\n", argv0); |
| 394 | fprintf(f, "Options:\n"); |
| 395 | fprintf(f, " --seed=<N>, -s <N>\n"); |
| 396 | fprintf(f, " --help\n"); |
| 397 | } |
| 398 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 399 | boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) { |
Konrad Grochowski | b3f5ffc | 2014-11-06 19:32:59 +0100 | [diff] [blame] | 400 | THRIFT_UNUSED_VARIABLE(argc); |
| 401 | THRIFT_UNUSED_VARIABLE(argv); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 402 | uint32_t seed = static_cast<uint32_t>(time(NULL)); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 403 | printf("seed: %" PRIu32 "\n", seed); |
| 404 | rng.seed(seed); |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 405 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 406 | boost::unit_test::test_suite* suite = &boost::unit_test::framework::master_test_suite(); |
David Reiss | 109693c | 2010-10-06 17:10:42 +0000 | [diff] [blame] | 407 | suite->p_name.value = "ZlibTest"; |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 408 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 409 | uint32_t buf_len = 1024 * 32; |
David Reiss | 9a961e7 | 2010-10-06 17:10:23 +0000 | [diff] [blame] | 410 | add_tests(suite, gen_uniform_buffer(buf_len, 'a'), buf_len, "uniform"); |
| 411 | add_tests(suite, gen_compressible_buffer(buf_len), buf_len, "compressible"); |
| 412 | add_tests(suite, gen_random_buffer(buf_len), buf_len, "random"); |
| 413 | |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 414 | suite->add(BOOST_TEST_CASE(test_no_write)); |
| 415 | |
David Reiss | 109693c | 2010-10-06 17:10:42 +0000 | [diff] [blame] | 416 | return NULL; |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 417 | } |