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