2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file test_block_header.c
|
|
|
|
/// \brief Tests Block Header coders
|
|
|
|
//
|
|
|
|
// Copyright (C) 2007 Lasse Collin
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "tests.h"
|
|
|
|
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
static void
|
|
|
|
test4(void)
|
|
|
|
{
|
|
|
|
known_options = (lzma_options_block){
|
|
|
|
.has_crc32 = false,
|
|
|
|
.has_eopm = true,
|
|
|
|
.is_metadata = false,
|
|
|
|
.compressed_size = 0,
|
|
|
|
.uncompressed_size = 0,
|
|
|
|
.compressed_reserve = LZMA_VLI_BYTES_MAX,
|
|
|
|
.uncompressed_reserve = LZMA_VLI_BYTES_MAX,
|
|
|
|
.padding = 0,
|
|
|
|
};
|
|
|
|
memcpy(known_options.filters, filters_powerpc,
|
|
|
|
sizeof(filters_powerpc));
|
|
|
|
expect(!code(21));
|
|
|
|
|
|
|
|
// Reserved bits
|
|
|
|
buffer[0] ^= 0x40;
|
|
|
|
expect(!decode_ret(1, LZMA_HEADER_ERROR));
|
|
|
|
buffer[0] ^= 0x40;
|
|
|
|
|
|
|
|
buffer[1] ^= 0x40;
|
|
|
|
expect(decode_ret(21, LZMA_HEADER_ERROR));
|
|
|
|
buffer[1] ^= 0x40;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
static uint8_t buf[LZMA_BLOCK_HEADER_SIZE_MAX];
|
2007-12-08 23:42:33 +01:00
|
|
|
static lzma_options_block known_options;
|
|
|
|
static lzma_options_block decoded_options;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
static lzma_options_filter filters_none[1] = {
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
|
|
|
.id = LZMA_VLI_VALUE_UNKNOWN,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
|
|
|
|
static lzma_options_filter filters_one[2] = {
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2008-06-18 17:02:10 +02:00
|
|
|
.id = LZMA_FILTER_LZMA,
|
|
|
|
.options = (void *)(&lzma_preset_lzma[0]),
|
2007-12-08 23:42:33 +01:00
|
|
|
}, {
|
|
|
|
.id = LZMA_VLI_VALUE_UNKNOWN,
|
2008-06-18 17:02:10 +02:00
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
static lzma_options_filter filters_four[5] = {
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2008-06-18 17:02:10 +02:00
|
|
|
.id = LZMA_FILTER_X86,
|
|
|
|
.options = NULL,
|
2007-12-08 23:42:33 +01:00
|
|
|
}, {
|
2008-06-18 17:02:10 +02:00
|
|
|
.id = LZMA_FILTER_X86,
|
2007-12-08 23:42:33 +01:00
|
|
|
.options = NULL,
|
|
|
|
}, {
|
2008-06-18 17:02:10 +02:00
|
|
|
.id = LZMA_FILTER_X86,
|
2007-12-08 23:42:33 +01:00
|
|
|
.options = NULL,
|
2008-06-18 17:02:10 +02:00
|
|
|
}, {
|
|
|
|
.id = LZMA_FILTER_LZMA,
|
|
|
|
.options = (void *)(&lzma_preset_lzma[0]),
|
|
|
|
}, {
|
|
|
|
.id = LZMA_VLI_VALUE_UNKNOWN,
|
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
static lzma_options_filter filters_five[6] = {
|
|
|
|
{
|
|
|
|
.id = LZMA_FILTER_X86,
|
|
|
|
.options = NULL,
|
|
|
|
}, {
|
|
|
|
.id = LZMA_FILTER_X86,
|
|
|
|
.options = NULL,
|
|
|
|
}, {
|
|
|
|
.id = LZMA_FILTER_X86,
|
|
|
|
.options = NULL,
|
|
|
|
}, {
|
|
|
|
.id = LZMA_FILTER_X86,
|
|
|
|
.options = NULL,
|
|
|
|
}, {
|
|
|
|
.id = LZMA_FILTER_LZMA,
|
|
|
|
.options = (void *)(&lzma_preset_lzma[0]),
|
|
|
|
}, {
|
|
|
|
.id = LZMA_VLI_VALUE_UNKNOWN,
|
2008-01-15 15:25:38 +01:00
|
|
|
}
|
2008-06-18 17:02:10 +02:00
|
|
|
};
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
static void
|
|
|
|
code(void)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2008-06-18 17:02:10 +02:00
|
|
|
expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
lzma_options_filter filters[LZMA_BLOCK_FILTERS_MAX + 1];
|
|
|
|
memcrap(filters, sizeof(filters));
|
2007-12-08 23:42:33 +01:00
|
|
|
memcrap(&decoded_options, sizeof(decoded_options));
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
decoded_options.header_size = known_options.header_size;
|
|
|
|
decoded_options.check = known_options.check;
|
|
|
|
decoded_options.filters = filters;
|
|
|
|
expect(lzma_block_header_decode(&decoded_options, NULL, buf)
|
|
|
|
== LZMA_OK);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
expect(known_options.compressed_size
|
|
|
|
== decoded_options.compressed_size);
|
|
|
|
expect(known_options.uncompressed_size
|
|
|
|
== decoded_options.uncompressed_size);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
for (size_t i = 0; known_options.filters[i].id
|
|
|
|
!= LZMA_VLI_VALUE_UNKNOWN; ++i)
|
|
|
|
expect(known_options.filters[i].id == filters[i].id);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
for (size_t i = 0; i < LZMA_BLOCK_FILTERS_MAX; ++i)
|
|
|
|
free(decoded_options.filters[i].options);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
test1(void)
|
|
|
|
{
|
|
|
|
known_options = (lzma_options_block){
|
2008-06-18 17:02:10 +02:00
|
|
|
.check = LZMA_CHECK_NONE,
|
2007-12-08 23:42:33 +01:00
|
|
|
.compressed_size = LZMA_VLI_VALUE_UNKNOWN,
|
|
|
|
.uncompressed_size = LZMA_VLI_VALUE_UNKNOWN,
|
2008-06-18 17:02:10 +02:00
|
|
|
.filters = NULL,
|
2007-12-08 23:42:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
known_options.filters = filters_none;
|
|
|
|
expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
known_options.filters = filters_five;
|
|
|
|
expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
known_options.filters = filters_one;
|
|
|
|
expect(lzma_block_header_size(&known_options) == LZMA_OK);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
known_options.check = 999; // Some invalid value, which gets ignored.
|
|
|
|
expect(lzma_block_header_size(&known_options) == LZMA_OK);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
known_options.compressed_size = 5; // Not a multiple of four.
|
2007-12-08 23:42:33 +01:00
|
|
|
expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
known_options.compressed_size = 0; // Cannot be zero.
|
|
|
|
expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
known_options.compressed_size = LZMA_VLI_VALUE_UNKNOWN;
|
|
|
|
known_options.uncompressed_size = 0;
|
|
|
|
expect(lzma_block_header_size(&known_options) == LZMA_OK);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
known_options.uncompressed_size = LZMA_VLI_VALUE_MAX + 1;
|
|
|
|
expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-06-18 17:02:10 +02:00
|
|
|
test2(void)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
|
|
|
known_options = (lzma_options_block){
|
2008-06-18 17:02:10 +02:00
|
|
|
.check = LZMA_CHECK_CRC32,
|
2007-12-08 23:42:33 +01:00
|
|
|
.compressed_size = LZMA_VLI_VALUE_UNKNOWN,
|
|
|
|
.uncompressed_size = LZMA_VLI_VALUE_UNKNOWN,
|
2008-06-18 17:02:10 +02:00
|
|
|
.filters = filters_four,
|
2007-12-08 23:42:33 +01:00
|
|
|
};
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
expect(lzma_block_header_size(&known_options) == LZMA_OK);
|
|
|
|
code();
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
known_options.compressed_size = 123456;
|
|
|
|
known_options.uncompressed_size = 234567;
|
|
|
|
expect(lzma_block_header_size(&known_options) == LZMA_OK);
|
|
|
|
code();
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02: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 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-06-18 17:02:10 +02:00
|
|
|
test3(void)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
|
|
|
known_options = (lzma_options_block){
|
2008-06-18 17:02:10 +02:00
|
|
|
.check = LZMA_CHECK_CRC32,
|
|
|
|
.compressed_size = LZMA_VLI_VALUE_UNKNOWN,
|
|
|
|
.uncompressed_size = LZMA_VLI_VALUE_UNKNOWN,
|
|
|
|
.filters = filters_one,
|
2007-12-08 23:42:33 +01:00
|
|
|
};
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
expect(lzma_block_header_size(&known_options) == LZMA_OK);
|
|
|
|
known_options.header_size += 4;
|
|
|
|
expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
lzma_options_filter filters[LZMA_BLOCK_FILTERS_MAX + 1];
|
|
|
|
decoded_options.header_size = known_options.header_size;
|
|
|
|
decoded_options.check = known_options.check;
|
|
|
|
decoded_options.filters = filters;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// Wrong size
|
|
|
|
++buf[0];
|
|
|
|
expect(lzma_block_header_decode(&decoded_options, NULL, buf)
|
|
|
|
== LZMA_PROG_ERROR);
|
|
|
|
--buf[0];
|
|
|
|
|
|
|
|
// Wrong CRC32
|
|
|
|
buf[known_options.header_size - 1] ^= 1;
|
|
|
|
expect(lzma_block_header_decode(&decoded_options, NULL, buf)
|
|
|
|
== LZMA_DATA_ERROR);
|
|
|
|
buf[known_options.header_size - 1] ^= 1;
|
|
|
|
|
|
|
|
// Unsupported filter
|
|
|
|
// NOTE: This may need updating when new IDs become supported.
|
|
|
|
buf[2] ^= 0x1F;
|
|
|
|
integer_write_32(buf + known_options.header_size - 4,
|
|
|
|
lzma_crc32(buf, known_options.header_size - 4, 0));
|
|
|
|
expect(lzma_block_header_decode(&decoded_options, NULL, buf)
|
|
|
|
== LZMA_HEADER_ERROR);
|
|
|
|
buf[2] ^= 0x1F;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// Non-nul Padding
|
|
|
|
buf[known_options.header_size - 4 - 1] ^= 1;
|
|
|
|
integer_write_32(buf + known_options.header_size - 4,
|
|
|
|
lzma_crc32(buf, known_options.header_size - 4, 0));
|
|
|
|
expect(lzma_block_header_decode(&decoded_options, NULL, buf)
|
|
|
|
== LZMA_HEADER_ERROR);
|
|
|
|
buf[known_options.header_size - 4 - 1] ^= 1;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2007-12-09 10:03:28 +01:00
|
|
|
main(void)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
|
|
|
lzma_init();
|
|
|
|
|
|
|
|
test1();
|
|
|
|
test2();
|
|
|
|
test3();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|