blob: e155970b0ca9a986a233e8ee4245d5d39b08266e [file] [log] [blame]
David Reissfaebedd2007-09-17 23:20:38 +00001/*
David Reissea2cba82009-03-30 21:35:00 +00002 * 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 Reiss3cc9dab2010-10-06 17:10:21 +000020#define __STDC_FORMAT_MACROS
21
David Reiss9a961e72010-10-06 17:10:23 +000022#ifndef _GNU_SOURCE
23#define _GNU_SOURCE // needed for getopt_long
24#endif
25
David Reiss3cc9dab2010-10-06 17:10:21 +000026#include <stdint.h>
Jim King9de9b1f2015-04-30 16:03:34 -040027#ifdef HAVE_INTTYPES_H
David Reiss3cc9dab2010-10-06 17:10:21 +000028#include <inttypes.h>
Jim King9de9b1f2015-04-30 16:03:34 -040029#endif
David Reissfaebedd2007-09-17 23:20:38 +000030#include <cstddef>
David Reissfaebedd2007-09-17 23:20:38 +000031#include <fstream>
32#include <iostream>
Jake Farrell5d02b802014-01-07 21:42:01 -050033#include <thrift/cxxfunctional.h>
David Reiss3cc9dab2010-10-06 17:10:21 +000034
Konrad Grochowskie9bdb412015-09-25 20:17:36 +020035#include <boost/function.hpp>
David Reiss3cc9dab2010-10-06 17:10:21 +000036#include <boost/random.hpp>
David Reiss9a961e72010-10-06 17:10:23 +000037#include <boost/shared_array.hpp>
38#include <boost/test/unit_test.hpp>
Konrad Grochowskie9bdb412015-09-25 20:17:36 +020039#include <boost/version.hpp>
David Reiss3cc9dab2010-10-06 17:10:21 +000040
Roger Meier49ff8b12012-04-13 09:12:31 +000041#include <thrift/transport/TBufferTransports.h>
42#include <thrift/transport/TZlibTransport.h>
David Reissfaebedd2007-09-17 23:20:38 +000043
David Reiss9a961e72010-10-06 17:10:23 +000044using namespace std;
David Reiss9a961e72010-10-06 17:10:23 +000045using namespace apache::thrift::transport;
46
David Reissf2abcf92010-10-06 17:10:24 +000047boost::mt19937 rng;
David Reissfaebedd2007-09-17 23:20:38 +000048
David Reissf2abcf92010-10-06 17:10:24 +000049/*
50 * Utility code
51 */
David Reissfaebedd2007-09-17 23:20:38 +000052
David Reissf2abcf92010-10-06 17:10:24 +000053class SizeGenerator {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010054public:
David Reissf2abcf92010-10-06 17:10:24 +000055 virtual ~SizeGenerator() {}
56 virtual unsigned int getSize() = 0;
David Reissfaebedd2007-09-17 23:20:38 +000057};
58
David Reissf2abcf92010-10-06 17:10:24 +000059class ConstantSizeGenerator : public SizeGenerator {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010060public:
David Reissf2abcf92010-10-06 17:10:24 +000061 ConstantSizeGenerator(unsigned int value) : value_(value) {}
Konrad Grochowski16a23a62014-11-13 15:33:38 +010062 virtual unsigned int getSize() { return value_; }
David Reissf2abcf92010-10-06 17:10:24 +000063
Konrad Grochowski16a23a62014-11-13 15:33:38 +010064private:
David Reissf2abcf92010-10-06 17:10:24 +000065 unsigned int value_;
66};
67
68class LogNormalSizeGenerator : public SizeGenerator {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010069public:
70 LogNormalSizeGenerator(double mean, double std_dev)
71 : gen_(rng, boost::lognormal_distribution<double>(mean, std_dev)) {}
David Reissf2abcf92010-10-06 17:10:24 +000072
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 Grochowski16a23a62014-11-13 15:33:38 +010083private:
84 boost::variate_generator<boost::mt19937, boost::lognormal_distribution<double> > gen_;
David Reissf2abcf92010-10-06 17:10:24 +000085};
David Reiss3cc9dab2010-10-06 17:10:21 +000086
Jim King7848d882015-04-06 21:38:06 -040087boost::shared_array<uint8_t> gen_uniform_buffer(uint32_t buf_len, uint8_t c) {
David Reiss3cc9dab2010-10-06 17:10:21 +000088 uint8_t* buf = new uint8_t[buf_len];
89 memset(buf, c, buf_len);
Jim King7848d882015-04-06 21:38:06 -040090 return boost::shared_array<uint8_t>(buf);
David Reiss3cc9dab2010-10-06 17:10:21 +000091}
92
Jim King7848d882015-04-06 21:38:06 -040093boost::shared_array<uint8_t> gen_compressible_buffer(uint32_t buf_len) {
David Reiss3cc9dab2010-10-06 17:10:21 +000094 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 Grochowski16a23a62014-11-13 15:33:38 +010099 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 Reiss3cc9dab2010-10-06 17:10:21 +0000103
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 King7848d882015-04-06 21:38:06 -0400122 return boost::shared_array<uint8_t>(buf);
David Reiss3cc9dab2010-10-06 17:10:21 +0000123}
124
Jim King7848d882015-04-06 21:38:06 -0400125boost::shared_array<uint8_t> gen_random_buffer(uint32_t buf_len) {
David Reiss3cc9dab2010-10-06 17:10:21 +0000126 uint8_t* buf = new uint8_t[buf_len];
127
128 boost::uniform_smallint<uint8_t> distribution(0, UINT8_MAX);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100129 boost::variate_generator<boost::mt19937, boost::uniform_smallint<uint8_t> >
130 generator(rng, distribution);
David Reiss3cc9dab2010-10-06 17:10:21 +0000131
132 for (uint32_t n = 0; n < buf_len; ++n) {
133 buf[n] = generator();
134 }
135
Jim King7848d882015-04-06 21:38:06 -0400136 return boost::shared_array<uint8_t>(buf);
David Reiss3cc9dab2010-10-06 17:10:21 +0000137}
David Reissfaebedd2007-09-17 23:20:38 +0000138
David Reissf2abcf92010-10-06 17:10:24 +0000139/*
140 * Test functions
141 */
142
Jim King7848d882015-04-06 21:38:06 -0400143void test_write_then_read(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
Roger Meier611f90c2011-12-11 22:08:51 +0000144 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
145 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400146 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000147 zlib_trans->finish();
David Reissfaebedd2007-09-17 23:20:38 +0000148
David Reiss9a961e72010-10-06 17:10:23 +0000149 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
David Reiss0a2d81e2010-10-06 17:10:40 +0000150 uint32_t got = zlib_trans->readAll(mirror.get(), buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000151 BOOST_REQUIRE_EQUAL(got, buf_len);
Jim King7848d882015-04-06 21:38:06 -0400152 BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0);
David Reiss9a961e72010-10-06 17:10:23 +0000153 zlib_trans->verifyChecksum();
154}
David Reissfaebedd2007-09-17 23:20:38 +0000155
Jim King7848d882015-04-06 21:38:06 -0400156void test_separate_checksum(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
David Reiss9a961e72010-10-06 17:10:23 +0000157 // 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 Meier611f90c2011-12-11 22:08:51 +0000163 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
164 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400165 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000166 zlib_trans->finish();
David Reiss9a961e72010-10-06 17:10:23 +0000167 string tmp_buf;
168 membuf->appendBufferToString(tmp_buf);
David Reissa0e11592010-10-06 17:10:27 +0000169 zlib_trans.reset(new TZlibTransport(membuf,
David Reiss9a961e72010-10-06 17:10:23 +0000170 TZlibTransport::DEFAULT_URBUF_SIZE,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100171 static_cast<uint32_t>(tmp_buf.length() - 1)));
David Reissfaebedd2007-09-17 23:20:38 +0000172
David Reiss9a961e72010-10-06 17:10:23 +0000173 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
David Reiss0a2d81e2010-10-06 17:10:40 +0000174 uint32_t got = zlib_trans->readAll(mirror.get(), buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000175 BOOST_REQUIRE_EQUAL(got, buf_len);
Jim King7848d882015-04-06 21:38:06 -0400176 BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0);
David Reiss9a961e72010-10-06 17:10:23 +0000177 zlib_trans->verifyChecksum();
178}
David Reissfaebedd2007-09-17 23:20:38 +0000179
Jim King7848d882015-04-06 21:38:06 -0400180void test_incomplete_checksum(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
David Reiss9a961e72010-10-06 17:10:23 +0000181 // Make sure we still get that "not complete" error if
182 // it really isn't complete.
Roger Meier611f90c2011-12-11 22:08:51 +0000183 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
184 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400185 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000186 zlib_trans->finish();
David Reiss9a961e72010-10-06 17:10:23 +0000187 string tmp_buf;
188 membuf->appendBufferToString(tmp_buf);
189 tmp_buf.erase(tmp_buf.length() - 1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100190 membuf->resetBuffer(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(tmp_buf.data())),
Jake Farrell5d02b802014-01-07 21:42:01 -0500191 static_cast<uint32_t>(tmp_buf.length()));
David Reiss9a961e72010-10-06 17:10:23 +0000192
193 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
David Reiss0a2d81e2010-10-06 17:10:40 +0000194 uint32_t got = zlib_trans->readAll(mirror.get(), buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000195 BOOST_REQUIRE_EQUAL(got, buf_len);
Jim King7848d882015-04-06 21:38:06 -0400196 BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0);
David Reiss9a961e72010-10-06 17:10:23 +0000197 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 King7848d882015-04-06 21:38:06 -0400205void test_read_write_mix(const boost::shared_array<uint8_t> buf,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100206 uint32_t buf_len,
Roger Meier611f90c2011-12-11 22:08:51 +0000207 const boost::shared_ptr<SizeGenerator>& write_gen,
208 const boost::shared_ptr<SizeGenerator>& read_gen) {
David Reiss9a961e72010-10-06 17:10:23 +0000209 // Try it with a mix of read/write sizes.
Roger Meier611f90c2011-12-11 22:08:51 +0000210 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
211 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
David Reiss9a961e72010-10-06 17:10:23 +0000212 unsigned int tot;
213
David Reiss9a961e72010-10-06 17:10:23 +0000214 tot = 0;
215 while (tot < buf_len) {
David Reissf2abcf92010-10-06 17:10:24 +0000216 uint32_t write_len = write_gen->getSize();
David Reiss9a961e72010-10-06 17:10:23 +0000217 if (tot + write_len > buf_len) {
218 write_len = buf_len - tot;
David Reissfaebedd2007-09-17 23:20:38 +0000219 }
Jim King7848d882015-04-06 21:38:06 -0400220 zlib_trans->write(buf.get() + tot, write_len);
David Reiss9a961e72010-10-06 17:10:23 +0000221 tot += write_len;
David Reiss9a961e72010-10-06 17:10:23 +0000222 }
David Reissfaebedd2007-09-17 23:20:38 +0000223
David Reisse94fa332010-10-06 17:10:26 +0000224 zlib_trans->finish();
David Reiss9a961e72010-10-06 17:10:23 +0000225
David Reiss9a961e72010-10-06 17:10:23 +0000226 tot = 0;
227 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
228 while (tot < buf_len) {
David Reissf2abcf92010-10-06 17:10:24 +0000229 uint32_t read_len = read_gen->getSize();
David Reiss9a961e72010-10-06 17:10:23 +0000230 uint32_t expected_read_len = read_len;
231 if (tot + read_len > buf_len) {
232 expected_read_len = buf_len - tot;
David Reissfaebedd2007-09-17 23:20:38 +0000233 }
David Reiss9a961e72010-10-06 17:10:23 +0000234 uint32_t got = zlib_trans->read(mirror.get() + tot, read_len);
David Reiss0a2d81e2010-10-06 17:10:40 +0000235 BOOST_REQUIRE_LE(got, expected_read_len);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100236 BOOST_REQUIRE_NE(got, (uint32_t)0);
David Reiss9a961e72010-10-06 17:10:23 +0000237 tot += got;
David Reiss9a961e72010-10-06 17:10:23 +0000238 }
David Reissfaebedd2007-09-17 23:20:38 +0000239
Jim King7848d882015-04-06 21:38:06 -0400240 BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0);
David Reiss9a961e72010-10-06 17:10:23 +0000241 zlib_trans->verifyChecksum();
242}
David Reissfaebedd2007-09-17 23:20:38 +0000243
Jim King7848d882015-04-06 21:38:06 -0400244void test_invalid_checksum(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
David Reiss9a961e72010-10-06 17:10:23 +0000245 // Verify checksum checking.
Roger Meier611f90c2011-12-11 22:08:51 +0000246 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
247 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400248 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000249 zlib_trans->finish();
David Reiss9a961e72010-10-06 17:10:23 +0000250 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 Farrell5d02b802014-01-07 21:42:01 -0500266 int index = static_cast<int>(tmp_buf.size() - 1);
David Reiss9a961e72010-10-06 17:10:23 +0000267 tmp_buf[index]++;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100268 membuf->resetBuffer(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(tmp_buf.data())),
Jake Farrell5d02b802014-01-07 21:42:01 -0500269 static_cast<uint32_t>(tmp_buf.length()));
David Reissfaebedd2007-09-17 23:20:38 +0000270
David Reiss9a961e72010-10-06 17:10:23 +0000271 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
272 try {
David Reiss0a2d81e2010-10-06 17:10:40 +0000273 zlib_trans->readAll(mirror.get(), buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000274 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 Reissfaebedd2007-09-17 23:20:38 +0000280
Jim King7848d882015-04-06 21:38:06 -0400281void test_write_after_flush(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
David Reisse94fa332010-10-06 17:10:26 +0000282 // write some data
Roger Meier611f90c2011-12-11 22:08:51 +0000283 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
284 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400285 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000286
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
316void test_no_write() {
317 // Verify that no data is written to the underlying transport if we
318 // never write data to the TZlibTransport.
Roger Meier611f90c2011-12-11 22:08:51 +0000319 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
David Reisse94fa332010-10-06 17:10:26 +0000320 {
321 // Create a TZlibTransport object, and immediately destroy it
322 // when it goes out of scope.
David Reissa0e11592010-10-06 17:10:27 +0000323 TZlibTransport w_zlib_trans(membuf);
David Reisse94fa332010-10-06 17:10:26 +0000324 }
325
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100326 BOOST_CHECK_EQUAL(membuf->available_read(), (uint32_t)0);
David Reisse94fa332010-10-06 17:10:26 +0000327}
328
David Reissf2abcf92010-10-06 17:10:24 +0000329/*
330 * Initialization
331 */
332
Konrad Grochowskie9bdb412015-09-25 20:17:36 +0200333#if (BOOST_VERSION >= 105900)
334#define ADD_TEST_CASE(suite, name, _FUNC, ...) \
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100335 do { \
336 ::std::ostringstream name_ss; \
Konrad Grochowskie9bdb412015-09-25 20:17:36 +0200337 name_ss << name << "-" << BOOST_STRINGIZE(_FUNC); \
338 boost::function<void ()> test_func = ::apache::thrift::stdcxx::bind(_FUNC, ##__VA_ARGS__); \
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100339 ::boost::unit_test::test_case* tc \
Konrad Grochowskie9bdb412015-09-25 20:17:36 +0200340 = ::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 Grochowski16a23a62014-11-13 15:33:38 +0100350 ##__VA_ARGS__), \
351 name_ss.str()); \
352 (suite)->add(tc); \
David Reiss9a961e72010-10-06 17:10:23 +0000353 } while (0)
Konrad Grochowskie9bdb412015-09-25 20:17:36 +0200354#endif
David Reissfaebedd2007-09-17 23:20:38 +0000355
Jake Farrell5d02b802014-01-07 21:42:01 -0500356void add_tests(boost::unit_test::test_suite* suite,
Jim King7848d882015-04-06 21:38:06 -0400357 const boost::shared_array<uint8_t>& buf,
David Reiss9a961e72010-10-06 17:10:23 +0000358 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 Reiss9a961e72010-10-06 17:10:23 +0000363 ADD_TEST_CASE(suite, name, test_invalid_checksum, buf, buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000364 ADD_TEST_CASE(suite, name, test_write_after_flush, buf, buf_len);
David Reissf2abcf92010-10-06 17:10:24 +0000365
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100366 boost::shared_ptr<SizeGenerator> size_32k(new ConstantSizeGenerator(1 << 15));
Roger Meier611f90c2011-12-11 22:08:51 +0000367 boost::shared_ptr<SizeGenerator> size_lognormal(new LogNormalSizeGenerator(20, 30));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100368 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 Reissf2abcf92010-10-06 17:10:24 +0000390
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 Meier611f90c2011-12-11 22:08:51 +0000397 boost::shared_ptr<SizeGenerator> write_size_gen(new LogNormalSizeGenerator(20, 30));
398 boost::shared_ptr<SizeGenerator> read_size_gen(new LogNormalSizeGenerator(20, 30));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100399 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 Reiss9a961e72010-10-06 17:10:23 +0000406}
407
408void 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 Monaco796667b2016-01-04 23:05:19 +0100415#ifdef BOOST_TEST_DYN_LINK
416bool 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
436int main( int argc, char* argv[] ) {
437 return ::boost::unit_test::unit_test_main(&init_unit_test_suite,argc,argv);
438}
439#else
Jake Farrell5d02b802014-01-07 21:42:01 -0500440boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
Konrad Grochowskib3f5ffc2014-11-06 19:32:59 +0100441 THRIFT_UNUSED_VARIABLE(argc);
442 THRIFT_UNUSED_VARIABLE(argv);
Jake Farrell5d02b802014-01-07 21:42:01 -0500443 uint32_t seed = static_cast<uint32_t>(time(NULL));
Jim King9de9b1f2015-04-30 16:03:34 -0400444#ifdef HAVE_INTTYPES_H
David Reiss9a961e72010-10-06 17:10:23 +0000445 printf("seed: %" PRIu32 "\n", seed);
Jim King9de9b1f2015-04-30 16:03:34 -0400446#endif
David Reiss9a961e72010-10-06 17:10:23 +0000447 rng.seed(seed);
David Reiss9a961e72010-10-06 17:10:23 +0000448
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100449 boost::unit_test::test_suite* suite = &boost::unit_test::framework::master_test_suite();
David Reiss109693c2010-10-06 17:10:42 +0000450 suite->p_name.value = "ZlibTest";
David Reiss9a961e72010-10-06 17:10:23 +0000451
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100452 uint32_t buf_len = 1024 * 32;
David Reiss9a961e72010-10-06 17:10:23 +0000453 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 Reisse94fa332010-10-06 17:10:26 +0000457 suite->add(BOOST_TEST_CASE(test_no_write));
458
David Reiss109693c2010-10-06 17:10:42 +0000459 return NULL;
David Reissfaebedd2007-09-17 23:20:38 +0000460}
Antonio Di Monaco796667b2016-01-04 23:05:19 +0100461#endif