2018-08-14 14:56:59 -04:00
|
|
|
/***********************************************************************************************************************************
|
2020-02-27 12:09:05 -05:00
|
|
|
Test Compression
|
2018-08-14 14:56:59 -04:00
|
|
|
***********************************************************************************************************************************/
|
|
|
|
#include "common/io/filter/group.h"
|
|
|
|
#include "common/io/bufferRead.h"
|
|
|
|
#include "common/io/bufferWrite.h"
|
|
|
|
#include "common/io/io.h"
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Compress data
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
static Buffer *
|
2019-05-02 17:52:24 -04:00
|
|
|
testCompress(IoFilter *compress, Buffer *decompressed, size_t inputSize, size_t outputSize)
|
2018-08-14 14:56:59 -04:00
|
|
|
{
|
|
|
|
Buffer *compressed = bufNew(1024 * 1024);
|
|
|
|
size_t inputTotal = 0;
|
|
|
|
ioBufferSizeSet(outputSize);
|
|
|
|
|
2019-05-02 17:52:24 -04:00
|
|
|
IoWrite *write = ioBufferWriteNew(compressed);
|
2019-06-24 10:20:47 -04:00
|
|
|
ioFilterGroupAdd(ioWriteFilterGroup(write), compress);
|
2018-08-14 14:56:59 -04:00
|
|
|
ioWriteOpen(write);
|
|
|
|
|
|
|
|
// Compress input data
|
|
|
|
while (inputTotal < bufSize(decompressed))
|
|
|
|
{
|
|
|
|
// Generate the input buffer based on input size. This breaks the data up into chunks as it would be in a real scenario.
|
|
|
|
Buffer *input = bufNewC(
|
2019-05-02 12:43:09 -04:00
|
|
|
bufPtr(decompressed) + inputTotal,
|
|
|
|
inputSize > bufSize(decompressed) - inputTotal ? bufSize(decompressed) - inputTotal : inputSize);
|
2018-08-14 14:56:59 -04:00
|
|
|
|
|
|
|
ioWrite(write, input);
|
|
|
|
|
|
|
|
inputTotal += bufUsed(input);
|
|
|
|
bufFree(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
ioWriteClose(write);
|
2020-02-27 12:09:05 -05:00
|
|
|
memContextFree(((GzCompress *)ioFilterDriver(compress))->memContext);
|
2018-08-14 14:56:59 -04:00
|
|
|
|
|
|
|
return compressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Decompress data
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
static Buffer *
|
2019-05-02 17:52:24 -04:00
|
|
|
testDecompress(IoFilter *decompress, Buffer *compressed, size_t inputSize, size_t outputSize)
|
2018-08-14 14:56:59 -04:00
|
|
|
{
|
|
|
|
Buffer *decompressed = bufNew(1024 * 1024);
|
|
|
|
Buffer *output = bufNew(outputSize);
|
|
|
|
ioBufferSizeSet(inputSize);
|
|
|
|
|
2019-05-02 17:52:24 -04:00
|
|
|
IoRead *read = ioBufferReadNew(compressed);
|
2019-06-24 10:20:47 -04:00
|
|
|
ioFilterGroupAdd(ioReadFilterGroup(read), decompress);
|
2018-08-14 14:56:59 -04:00
|
|
|
ioReadOpen(read);
|
|
|
|
|
|
|
|
while (!ioReadEof(read))
|
|
|
|
{
|
|
|
|
ioRead(read, output);
|
|
|
|
bufCat(decompressed, output);
|
|
|
|
bufUsedZero(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
ioReadClose(read);
|
|
|
|
bufFree(output);
|
2020-02-27 12:09:05 -05:00
|
|
|
memContextFree(((GzDecompress *)ioFilterDriver(decompress))->memContext);
|
2018-08-14 14:56:59 -04:00
|
|
|
|
|
|
|
return decompressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Test Run
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
void
|
|
|
|
testRun(void)
|
|
|
|
{
|
|
|
|
FUNCTION_HARNESS_VOID();
|
|
|
|
|
|
|
|
// *****************************************************************************************************************************
|
2020-02-27 12:09:05 -05:00
|
|
|
if (testBegin("gzError"))
|
2018-08-14 14:56:59 -04:00
|
|
|
{
|
2020-02-27 12:09:05 -05:00
|
|
|
TEST_RESULT_INT(gzError(Z_OK), Z_OK, "check ok");
|
|
|
|
TEST_RESULT_INT(gzError(Z_STREAM_END), Z_STREAM_END, "check stream end");
|
|
|
|
TEST_ERROR(gzError(Z_NEED_DICT), AssertError, "zlib threw error: [2] need dictionary");
|
|
|
|
TEST_ERROR(gzError(Z_ERRNO), AssertError, "zlib threw error: [-1] file error");
|
|
|
|
TEST_ERROR(gzError(Z_STREAM_ERROR), FormatError, "zlib threw error: [-2] stream error");
|
|
|
|
TEST_ERROR(gzError(Z_DATA_ERROR), FormatError, "zlib threw error: [-3] data error");
|
|
|
|
TEST_ERROR(gzError(Z_MEM_ERROR), MemoryError, "zlib threw error: [-4] insufficient memory");
|
|
|
|
TEST_ERROR(gzError(Z_BUF_ERROR), AssertError, "zlib threw error: [-5] no space in buffer");
|
|
|
|
TEST_ERROR(gzError(Z_VERSION_ERROR), FormatError, "zlib threw error: [-6] incompatible version");
|
|
|
|
TEST_ERROR(gzError(999), AssertError, "zlib threw error: [999] unknown error");
|
2018-08-14 14:56:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// *****************************************************************************************************************************
|
2020-02-27 12:09:05 -05:00
|
|
|
if (testBegin("GzCompress and GzDecompress"))
|
2018-08-14 14:56:59 -04:00
|
|
|
{
|
|
|
|
const char *simpleData = "A simple string";
|
|
|
|
Buffer *compressed = NULL;
|
2019-05-02 12:43:09 -04:00
|
|
|
Buffer *decompressed = bufNewC(simpleData, strlen(simpleData));
|
2018-08-14 14:56:59 -04:00
|
|
|
|
2019-06-24 10:20:47 -04:00
|
|
|
VariantList *compressParamList = varLstNew();
|
|
|
|
varLstAdd(compressParamList, varNewUInt(3));
|
|
|
|
varLstAdd(compressParamList, varNewBool(false));
|
|
|
|
|
2018-08-14 14:56:59 -04:00
|
|
|
TEST_ASSIGN(
|
2020-02-27 12:09:05 -05:00
|
|
|
compressed, testCompress(gzCompressNewVar(compressParamList), decompressed, 1024, 1024),
|
2018-08-14 14:56:59 -04:00
|
|
|
"simple data - compress large in/large out buffer");
|
|
|
|
|
|
|
|
TEST_RESULT_BOOL(
|
2020-02-27 12:19:40 -05:00
|
|
|
bufEq(compressed, testCompress(gzCompressNew(3), decompressed, 1024, 1)), true,
|
2018-08-14 14:56:59 -04:00
|
|
|
"simple data - compress large in/small out buffer");
|
|
|
|
|
|
|
|
TEST_RESULT_BOOL(
|
2020-02-27 12:19:40 -05:00
|
|
|
bufEq(compressed, testCompress(gzCompressNew(3), decompressed, 1, 1024)), true,
|
2018-08-14 14:56:59 -04:00
|
|
|
"simple data - compress small in/large out buffer");
|
|
|
|
|
|
|
|
TEST_RESULT_BOOL(
|
2020-02-27 12:19:40 -05:00
|
|
|
bufEq(compressed, testCompress(gzCompressNew(3), decompressed, 1, 1)), true,
|
2018-08-14 14:56:59 -04:00
|
|
|
"simple data - compress small in/small out buffer");
|
|
|
|
|
|
|
|
TEST_RESULT_BOOL(
|
2020-02-27 12:19:40 -05:00
|
|
|
bufEq(decompressed, testDecompress(gzDecompressNew(), compressed, 1024, 1024)), true,
|
2018-08-14 14:56:59 -04:00
|
|
|
"simple data - decompress large in/large out buffer");
|
|
|
|
|
|
|
|
TEST_RESULT_BOOL(
|
2020-02-27 12:19:40 -05:00
|
|
|
bufEq(decompressed, testDecompress(gzDecompressNew(), compressed, 1024, 1)), true,
|
2018-08-14 14:56:59 -04:00
|
|
|
"simple data - decompress large in/small out buffer");
|
|
|
|
|
|
|
|
TEST_RESULT_BOOL(
|
2020-02-27 12:19:40 -05:00
|
|
|
bufEq(decompressed, testDecompress(gzDecompressNew(), compressed, 1, 1024)), true,
|
2018-08-14 14:56:59 -04:00
|
|
|
"simple data - decompress small in/large out buffer");
|
|
|
|
|
|
|
|
TEST_RESULT_BOOL(
|
2020-02-27 12:19:40 -05:00
|
|
|
bufEq(decompressed, testDecompress(gzDecompressNew(), compressed, 1, 1)), true,
|
2018-08-14 14:56:59 -04:00
|
|
|
"simple data - decompress small in/small out buffer");
|
|
|
|
|
2019-12-11 08:48:46 -05:00
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TEST_TITLE("error on no compression data");
|
|
|
|
|
2020-02-27 12:19:40 -05:00
|
|
|
TEST_ERROR(testDecompress(gzDecompressNew(), bufNew(0), 1, 1), FormatError, "unexpected eof in compressed data");
|
2019-12-11 08:48:46 -05:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TEST_TITLE("error on truncated compression data");
|
|
|
|
|
|
|
|
Buffer *truncated = bufNew(0);
|
|
|
|
bufCatSub(truncated, compressed, 0, bufUsed(compressed) - 1);
|
|
|
|
|
|
|
|
TEST_RESULT_UINT(bufUsed(truncated), bufUsed(compressed) - 1, "check truncated buffer size");
|
2020-02-27 12:19:40 -05:00
|
|
|
TEST_ERROR(testDecompress(gzDecompressNew(), truncated, 512, 512), FormatError, "unexpected eof in compressed data");
|
2019-12-11 08:48:46 -05:00
|
|
|
|
2018-08-14 14:56:59 -04:00
|
|
|
// Compress a large zero input buffer into small output buffer
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
decompressed = bufNew(1024 * 1024 - 1);
|
|
|
|
memset(bufPtr(decompressed), 0, bufSize(decompressed));
|
|
|
|
bufUsedSet(decompressed, bufSize(decompressed));
|
|
|
|
|
|
|
|
TEST_ASSIGN(
|
2020-02-27 12:19:40 -05:00
|
|
|
compressed, testCompress(gzCompressNew(3), decompressed, bufSize(decompressed), 1024),
|
2018-08-14 14:56:59 -04:00
|
|
|
"zero data - compress large in/small out buffer");
|
|
|
|
|
|
|
|
TEST_RESULT_BOOL(
|
2020-02-27 12:19:40 -05:00
|
|
|
bufEq(decompressed, testDecompress(gzDecompressNew(), compressed, bufSize(compressed), 1024 * 256)), true,
|
2018-08-14 14:56:59 -04:00
|
|
|
"zero data - decompress large in/small out buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
// *****************************************************************************************************************************
|
2020-02-27 12:09:05 -05:00
|
|
|
if (testBegin("gzDecompressToLog() and gzCompressToLog()"))
|
2018-08-14 14:56:59 -04:00
|
|
|
{
|
2020-02-27 12:19:40 -05:00
|
|
|
GzDecompress *decompress = (GzDecompress *)ioFilterDriver(gzDecompressNew());
|
2018-08-14 14:56:59 -04:00
|
|
|
|
2020-02-27 12:09:05 -05:00
|
|
|
TEST_RESULT_STR_Z(gzDecompressToLog(decompress), "{inputSame: false, done: false, availIn: 0}", "format object");
|
2018-08-14 14:56:59 -04:00
|
|
|
|
|
|
|
decompress->inputSame = true;
|
|
|
|
decompress->done = true;
|
2020-02-27 12:09:05 -05:00
|
|
|
TEST_RESULT_STR_Z(gzDecompressToLog(decompress), "{inputSame: true, done: true, availIn: 0}", "format object");
|
2018-08-14 14:56:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
FUNCTION_HARNESS_RESULT_VOID();
|
|
|
|
}
|