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