2007-12-08 17:42:33 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file test_block_header.c
|
|
|
|
/// \brief Tests Block Header coders
|
|
|
|
//
|
2009-04-13 04:27:40 -04:00
|
|
|
// Author: Lasse Collin
|
2007-12-08 17:42:33 -05:00
|
|
|
//
|
2009-04-13 04:27:40 -04:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2007-12-08 17:42:33 -05:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "tests.h"
|
|
|
|
|
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
static uint8_t buf[LZMA_BLOCK_HEADER_SIZE_MAX];
|
2008-08-28 15:53:15 -04:00
|
|
|
static lzma_block known_options;
|
|
|
|
static lzma_block decoded_options;
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-09-27 12:09:21 -04:00
|
|
|
static lzma_options_lzma opt_lzma;
|
|
|
|
|
2008-08-28 15:53:15 -04:00
|
|
|
static lzma_filter filters_none[1] = {
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
2008-09-13 05:10:43 -04:00
|
|
|
.id = LZMA_VLI_UNKNOWN,
|
2007-12-08 17:42:33 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
|
2008-08-28 15:53:15 -04:00
|
|
|
static lzma_filter filters_one[2] = {
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
2008-08-28 15:53:15 -04:00
|
|
|
.id = LZMA_FILTER_LZMA2,
|
2008-09-27 12:09:21 -04:00
|
|
|
.options = &opt_lzma,
|
2007-12-08 17:42:33 -05:00
|
|
|
}, {
|
2008-09-13 05:10:43 -04:00
|
|
|
.id = LZMA_VLI_UNKNOWN,
|
2008-06-18 11:02:10 -04:00
|
|
|
}
|
2007-12-08 17:42:33 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-08-28 15:53:15 -04:00
|
|
|
static lzma_filter filters_four[5] = {
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
2008-06-18 11:02:10 -04:00
|
|
|
.id = LZMA_FILTER_X86,
|
|
|
|
.options = NULL,
|
2007-12-08 17:42:33 -05:00
|
|
|
}, {
|
2008-06-18 11:02:10 -04:00
|
|
|
.id = LZMA_FILTER_X86,
|
2007-12-08 17:42:33 -05:00
|
|
|
.options = NULL,
|
|
|
|
}, {
|
2008-06-18 11:02:10 -04:00
|
|
|
.id = LZMA_FILTER_X86,
|
2007-12-08 17:42:33 -05:00
|
|
|
.options = NULL,
|
2008-06-18 11:02:10 -04:00
|
|
|
}, {
|
2008-08-28 15:53:15 -04:00
|
|
|
.id = LZMA_FILTER_LZMA2,
|
2008-09-27 12:09:21 -04:00
|
|
|
.options = &opt_lzma,
|
2008-06-18 11:02:10 -04:00
|
|
|
}, {
|
2008-09-13 05:10:43 -04:00
|
|
|
.id = LZMA_VLI_UNKNOWN,
|
2008-06-18 11:02:10 -04:00
|
|
|
}
|
2007-12-08 17:42:33 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-08-28 15:53:15 -04:00
|
|
|
static lzma_filter filters_five[6] = {
|
2008-06-18 11:02:10 -04:00
|
|
|
{
|
|
|
|
.id = LZMA_FILTER_X86,
|
|
|
|
.options = NULL,
|
|
|
|
}, {
|
|
|
|
.id = LZMA_FILTER_X86,
|
|
|
|
.options = NULL,
|
|
|
|
}, {
|
|
|
|
.id = LZMA_FILTER_X86,
|
|
|
|
.options = NULL,
|
|
|
|
}, {
|
|
|
|
.id = LZMA_FILTER_X86,
|
|
|
|
.options = NULL,
|
|
|
|
}, {
|
2008-08-28 15:53:15 -04:00
|
|
|
.id = LZMA_FILTER_LZMA2,
|
2008-09-27 12:09:21 -04:00
|
|
|
.options = &opt_lzma,
|
2008-06-18 11:02:10 -04:00
|
|
|
}, {
|
2008-09-13 05:10:43 -04:00
|
|
|
.id = LZMA_VLI_UNKNOWN,
|
2008-01-15 09:25:38 -05:00
|
|
|
}
|
2008-06-18 11:02:10 -04:00
|
|
|
};
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
static void
|
|
|
|
code(void)
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_encode(&known_options, buf),
|
|
|
|
LZMA_OK);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-11-19 13:46:52 -05:00
|
|
|
lzma_filter filters[LZMA_FILTERS_MAX + 1];
|
2008-06-18 11:02:10 -04:00
|
|
|
memcrap(filters, sizeof(filters));
|
2007-12-08 17:42:33 -05:00
|
|
|
memcrap(&decoded_options, sizeof(decoded_options));
|
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
decoded_options.header_size = known_options.header_size;
|
|
|
|
decoded_options.check = known_options.check;
|
|
|
|
decoded_options.filters = filters;
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_decode(&decoded_options, NULL, buf),
|
|
|
|
LZMA_OK);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_uint_eq(decoded_options.compressed_size,
|
|
|
|
known_options.compressed_size);
|
|
|
|
assert_uint_eq(decoded_options.uncompressed_size,
|
|
|
|
known_options.uncompressed_size);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
for (size_t i = 0; known_options.filters[i].id
|
2008-09-13 05:10:43 -04:00
|
|
|
!= LZMA_VLI_UNKNOWN; ++i)
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_uint_eq(filters[i].id, known_options.filters[i].id);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-11-19 13:46:52 -05:00
|
|
|
for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i)
|
2008-06-18 11:02:10 -04:00
|
|
|
free(decoded_options.filters[i].options);
|
2007-12-08 17:42:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
test1(void)
|
|
|
|
{
|
2008-08-28 15:53:15 -04:00
|
|
|
known_options = (lzma_block){
|
2008-06-18 11:02:10 -04:00
|
|
|
.check = LZMA_CHECK_NONE,
|
2008-09-13 05:10:43 -04:00
|
|
|
.compressed_size = LZMA_VLI_UNKNOWN,
|
|
|
|
.uncompressed_size = LZMA_VLI_UNKNOWN,
|
2008-06-18 11:02:10 -04:00
|
|
|
.filters = NULL,
|
2007-12-08 17:42:33 -05:00
|
|
|
};
|
|
|
|
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_size(&known_options),
|
|
|
|
LZMA_PROG_ERROR);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
known_options.filters = filters_none;
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_size(&known_options),
|
|
|
|
LZMA_PROG_ERROR);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
known_options.filters = filters_five;
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_size(&known_options),
|
|
|
|
LZMA_PROG_ERROR);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
known_options.filters = filters_one;
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_size(&known_options), LZMA_OK);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2019-06-24 17:08:13 -04:00
|
|
|
// Some invalid value, which gets ignored.
|
|
|
|
known_options.check = (lzma_check)(99);
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_size(&known_options), LZMA_OK);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-11-19 13:46:52 -05:00
|
|
|
known_options.compressed_size = 5;
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_size(&known_options), LZMA_OK);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
known_options.compressed_size = 0; // Cannot be zero.
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_size(&known_options),
|
|
|
|
LZMA_PROG_ERROR);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-11-19 13:46:52 -05:00
|
|
|
// LZMA_VLI_MAX is too big to keep the total size of the Block
|
|
|
|
// a valid VLI, but lzma_block_header_size() is not meant
|
|
|
|
// to validate it. (lzma_block_header_encode() must validate it.)
|
|
|
|
known_options.compressed_size = LZMA_VLI_MAX;
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_size(&known_options), LZMA_OK);
|
2008-11-19 13:46:52 -05:00
|
|
|
|
2008-09-13 05:10:43 -04:00
|
|
|
known_options.compressed_size = LZMA_VLI_UNKNOWN;
|
2008-06-18 11:02:10 -04:00
|
|
|
known_options.uncompressed_size = 0;
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_size(&known_options), LZMA_OK);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-09-13 05:10:43 -04:00
|
|
|
known_options.uncompressed_size = LZMA_VLI_MAX + 1;
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_size(&known_options),
|
|
|
|
LZMA_PROG_ERROR);
|
2007-12-08 17:42:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-06-18 11:02:10 -04:00
|
|
|
test2(void)
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
2008-08-28 15:53:15 -04:00
|
|
|
known_options = (lzma_block){
|
2008-06-18 11:02:10 -04:00
|
|
|
.check = LZMA_CHECK_CRC32,
|
2008-09-13 05:10:43 -04:00
|
|
|
.compressed_size = LZMA_VLI_UNKNOWN,
|
|
|
|
.uncompressed_size = LZMA_VLI_UNKNOWN,
|
2008-06-18 11:02:10 -04:00
|
|
|
.filters = filters_four,
|
2007-12-08 17:42:33 -05:00
|
|
|
};
|
|
|
|
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_size(&known_options), LZMA_OK);
|
2008-06-18 11:02:10 -04:00
|
|
|
code();
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
known_options.compressed_size = 123456;
|
|
|
|
known_options.uncompressed_size = 234567;
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_size(&known_options), LZMA_OK);
|
2008-06-18 11:02:10 -04:00
|
|
|
code();
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
// We can make the sizes smaller while keeping the header size
|
|
|
|
// the same.
|
|
|
|
known_options.compressed_size = 12;
|
|
|
|
known_options.uncompressed_size = 23;
|
|
|
|
code();
|
2007-12-08 17:42:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-06-18 11:02:10 -04:00
|
|
|
test3(void)
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
2008-08-28 15:53:15 -04:00
|
|
|
known_options = (lzma_block){
|
2008-06-18 11:02:10 -04:00
|
|
|
.check = LZMA_CHECK_CRC32,
|
2008-09-13 05:10:43 -04:00
|
|
|
.compressed_size = LZMA_VLI_UNKNOWN,
|
|
|
|
.uncompressed_size = LZMA_VLI_UNKNOWN,
|
2008-06-18 11:02:10 -04:00
|
|
|
.filters = filters_one,
|
2007-12-08 17:42:33 -05:00
|
|
|
};
|
|
|
|
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_size(&known_options), LZMA_OK);
|
2008-06-18 11:02:10 -04:00
|
|
|
known_options.header_size += 4;
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_encode(&known_options, buf),
|
|
|
|
LZMA_OK);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-11-19 13:46:52 -05:00
|
|
|
lzma_filter filters[LZMA_FILTERS_MAX + 1];
|
2008-06-18 11:02:10 -04:00
|
|
|
decoded_options.header_size = known_options.header_size;
|
|
|
|
decoded_options.check = known_options.check;
|
|
|
|
decoded_options.filters = filters;
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
// Wrong size
|
|
|
|
++buf[0];
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_decode(&decoded_options, NULL, buf),
|
|
|
|
LZMA_PROG_ERROR);
|
2008-06-18 11:02:10 -04:00
|
|
|
--buf[0];
|
|
|
|
|
|
|
|
// Wrong CRC32
|
|
|
|
buf[known_options.header_size - 1] ^= 1;
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_decode(&decoded_options, NULL, buf),
|
|
|
|
LZMA_DATA_ERROR);
|
2008-06-18 11:02:10 -04:00
|
|
|
buf[known_options.header_size - 1] ^= 1;
|
|
|
|
|
|
|
|
// Unsupported filter
|
|
|
|
// NOTE: This may need updating when new IDs become supported.
|
|
|
|
buf[2] ^= 0x1F;
|
2019-12-30 17:41:28 -05:00
|
|
|
write32le(buf + known_options.header_size - 4,
|
2008-06-18 11:02:10 -04:00
|
|
|
lzma_crc32(buf, known_options.header_size - 4, 0));
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_decode(&decoded_options, NULL, buf),
|
|
|
|
LZMA_OPTIONS_ERROR);
|
2008-06-18 11:02:10 -04:00
|
|
|
buf[2] ^= 0x1F;
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
// Non-nul Padding
|
|
|
|
buf[known_options.header_size - 4 - 1] ^= 1;
|
2019-12-30 17:41:28 -05:00
|
|
|
write32le(buf + known_options.header_size - 4,
|
2008-06-18 11:02:10 -04:00
|
|
|
lzma_crc32(buf, known_options.header_size - 4, 0));
|
2022-06-02 13:31:03 -04:00
|
|
|
assert_lzma_ret(lzma_block_header_decode(&decoded_options, NULL, buf),
|
|
|
|
LZMA_OPTIONS_ERROR);
|
2008-06-18 11:02:10 -04:00
|
|
|
buf[known_options.header_size - 4 - 1] ^= 1;
|
2007-12-08 17:42:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-02 13:31:03 -04:00
|
|
|
extern int
|
|
|
|
main(int argc, char **argv)
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
2022-06-02 13:31:03 -04:00
|
|
|
tuktest_start(argc, argv);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2022-06-02 13:31:03 -04:00
|
|
|
if (!lzma_filter_encoder_is_supported(LZMA_FILTER_X86)
|
|
|
|
|| !lzma_filter_decoder_is_supported(LZMA_FILTER_X86))
|
|
|
|
tuktest_early_skip("x86 BCJ encoder and/or decoder "
|
|
|
|
"is disabled");
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2022-06-02 13:31:03 -04:00
|
|
|
if (lzma_lzma_preset(&opt_lzma, 1))
|
|
|
|
tuktest_error("lzma_lzma_preset() failed");
|
|
|
|
|
|
|
|
tuktest_run(test1);
|
|
|
|
tuktest_run(test2);
|
|
|
|
tuktest_run(test3);
|
|
|
|
|
|
|
|
return tuktest_end();
|
2007-12-08 17:42:33 -05:00
|
|
|
}
|