1
0
Fork 0
forked from lthn/blockchain

zlib_helper::pack() fixed for small amount of data + unit test added (resolves #62)

This commit is contained in:
sowle 2019-08-05 17:07:10 +03:00
parent 40711646ed
commit a6ab2adf86
No known key found for this signature in database
GPG key ID: C07A24B2D89D49FC
2 changed files with 30 additions and 8 deletions

View file

@ -42,9 +42,8 @@ namespace zlib_helper
int ret = deflateInit(&zstream, Z_DEFAULT_COMPRESSION);
if(target.size())
{
result_packed_buff.resize(target.size()*2, 'X');
size_t estimated_output_size_max = deflateBound(&zstream, target.size());
result_packed_buff.resize(estimated_output_size_max, 'X');
zstream.next_in = (Bytef*)target.data();
zstream.avail_in = (uInt)target.size();
@ -52,12 +51,10 @@ namespace zlib_helper
zstream.avail_out = (uInt)result_packed_buff.size();
ret = deflate(&zstream, Z_FINISH);
CHECK_AND_ASSERT_MES(ret>=0, false, "Failed to deflate. err = " << ret);
// as we allocated enough room for a signel pass avail_out should not be zero
CHECK_AND_ASSERT_MES(ret == Z_STREAM_END && zstream.avail_out != 0, false, "Failed to deflate. err = " << ret);
if(result_packed_buff.size() != zstream.avail_out)
result_packed_buff.resize(result_packed_buff.size()-zstream.avail_out);
result_packed_buff.resize(result_packed_buff.size() - zstream.avail_out);
result_packed_buff.erase(0, 2);
}

View file

@ -0,0 +1,25 @@
// Copyright (c) 2019 Zano Project
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "gtest/gtest.h"
#include "epee/include/misc_log_ex.h"
#include "epee/include/zlib_helper.h"
#include "crypto/crypto.h"
TEST(zlib_helper, test_0)
{
for (size_t len = 0; len <= 1024; ++len)
{
for(size_t iteration = 0; iteration < 4; ++iteration)
{
std::string original(len, 'X');
if (len > 0)
crypto::generate_random_bytes(len, &original.front());
std::string result, decoded;
ASSERT_TRUE(epee::zlib_helper::pack(original, result));
ASSERT_TRUE(epee::zlib_helper::unpack(result, decoded));
ASSERT_EQ(original, decoded);
}
}
}