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