mirror of
https://git.tukaani.org/xz.git
synced 2024-04-04 12:36:23 +02:00
7ed9d943b3
Half of developers were already forgetting to use these functions, which could have caused total breakage in some future liblzma version or even now if --enable-small was used. Now liblzma uses pthread_once() to do the initializations unless it has been built with --disable-threads which make these initializations thread-unsafe. When --enable-small isn't used, liblzma currently gets needlessly linked against libpthread (on systems that have it). While it is stupid for now, liblzma will need threads in future anyway, so this stupidity will be temporary only. When --enable-small is used, different code CRC32 and CRC64 is now used than without --enable-small. This made the resulting binary slightly smaller, but the main reason was to clean it up and to handle the lack of lzma_init_check(). The pkg-config file lzma.pc was renamed to liblzma.pc. I'm not sure if it works correctly and portably for static linking (Libs.private includes -pthread or other operating system specific flags). Hopefully someone complains if it is bad. lzma_rc_prices[] is now included as a precomputed array even with --enable-small. It's just 128 bytes now that it uses uint8_t instead of uint32_t. Smaller array seemed to be at least as fast as the more bloated uint32_t array on x86; hopefully it's not bad on other architectures.
187 lines
4.7 KiB
C
187 lines
4.7 KiB
C
///////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
/// \file test_stream_flags.c
|
|
/// \brief Tests Stream Header and Stream Footer 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"
|
|
|
|
|
|
static lzma_stream_flags known_flags;
|
|
static lzma_stream_flags decoded_flags;
|
|
static uint8_t buffer[LZMA_STREAM_HEADER_SIZE];
|
|
|
|
|
|
static bool
|
|
validate(void)
|
|
{
|
|
// TODO: This could require the specific error type as an argument.
|
|
// We could also test that lzma_stream_flags_compare() gives
|
|
// the correct return values in different situations.
|
|
return lzma_stream_flags_compare(&known_flags, &decoded_flags)
|
|
!= LZMA_OK;
|
|
}
|
|
|
|
|
|
static bool
|
|
test_header_decoder(lzma_ret expected_ret)
|
|
{
|
|
memcrap(&decoded_flags, sizeof(decoded_flags));
|
|
|
|
if (lzma_stream_header_decode(&decoded_flags, buffer) != expected_ret)
|
|
return true;
|
|
|
|
if (expected_ret != LZMA_OK)
|
|
return false;
|
|
|
|
// Header doesn't have Backward Size, so make
|
|
// lzma_stream_flags_compare() ignore it.
|
|
decoded_flags.backward_size = LZMA_VLI_UNKNOWN;
|
|
return validate();
|
|
}
|
|
|
|
|
|
static void
|
|
test_header(void)
|
|
{
|
|
memcrap(buffer, sizeof(buffer));
|
|
expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
|
|
succeed(test_header_decoder(LZMA_OK));
|
|
}
|
|
|
|
|
|
static bool
|
|
test_footer_decoder(lzma_ret expected_ret)
|
|
{
|
|
memcrap(&decoded_flags, sizeof(decoded_flags));
|
|
|
|
if (lzma_stream_footer_decode(&decoded_flags, buffer) != expected_ret)
|
|
return true;
|
|
|
|
if (expected_ret != LZMA_OK)
|
|
return false;
|
|
|
|
return validate();
|
|
}
|
|
|
|
|
|
static void
|
|
test_footer(void)
|
|
{
|
|
memcrap(buffer, sizeof(buffer));
|
|
expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
|
|
succeed(test_footer_decoder(LZMA_OK));
|
|
}
|
|
|
|
|
|
static void
|
|
test_encode_invalid(void)
|
|
{
|
|
known_flags.check = LZMA_CHECK_ID_MAX + 1;
|
|
known_flags.backward_size = 1024;
|
|
|
|
expect(lzma_stream_header_encode(&known_flags, buffer)
|
|
== LZMA_PROG_ERROR);
|
|
|
|
expect(lzma_stream_footer_encode(&known_flags, buffer)
|
|
== LZMA_PROG_ERROR);
|
|
|
|
known_flags.check = (lzma_check)(-1);
|
|
|
|
expect(lzma_stream_header_encode(&known_flags, buffer)
|
|
== LZMA_PROG_ERROR);
|
|
|
|
expect(lzma_stream_footer_encode(&known_flags, buffer)
|
|
== LZMA_PROG_ERROR);
|
|
|
|
known_flags.check = LZMA_CHECK_NONE;
|
|
known_flags.backward_size = 0;
|
|
|
|
// Header encoder ignores backward_size.
|
|
expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
|
|
|
|
expect(lzma_stream_footer_encode(&known_flags, buffer)
|
|
== LZMA_PROG_ERROR);
|
|
|
|
known_flags.backward_size = LZMA_VLI_MAX;
|
|
|
|
expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
|
|
|
|
expect(lzma_stream_footer_encode(&known_flags, buffer)
|
|
== LZMA_PROG_ERROR);
|
|
}
|
|
|
|
|
|
static void
|
|
test_decode_invalid(void)
|
|
{
|
|
known_flags.check = LZMA_CHECK_NONE;
|
|
known_flags.backward_size = 1024;
|
|
|
|
expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
|
|
|
|
// Test 1 (invalid Magic Bytes)
|
|
buffer[5] ^= 1;
|
|
succeed(test_header_decoder(LZMA_FORMAT_ERROR));
|
|
buffer[5] ^= 1;
|
|
|
|
// Test 2a (valid CRC32)
|
|
uint32_t crc = lzma_crc32(buffer + 6, 2, 0);
|
|
integer_write_32(buffer + 8, crc);
|
|
succeed(test_header_decoder(LZMA_OK));
|
|
|
|
// Test 2b (invalid Stream Flags with valid CRC32)
|
|
buffer[6] ^= 0x20;
|
|
crc = lzma_crc32(buffer + 6, 2, 0);
|
|
integer_write_32(buffer + 8, crc);
|
|
succeed(test_header_decoder(LZMA_OPTIONS_ERROR));
|
|
|
|
// Test 3 (invalid CRC32)
|
|
expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
|
|
buffer[9] ^= 1;
|
|
succeed(test_header_decoder(LZMA_DATA_ERROR));
|
|
|
|
// Test 4 (invalid Stream Flags with valid CRC32)
|
|
expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
|
|
buffer[9] ^= 0x40;
|
|
crc = lzma_crc32(buffer + 4, 6, 0);
|
|
integer_write_32(buffer, crc);
|
|
succeed(test_footer_decoder(LZMA_OPTIONS_ERROR));
|
|
|
|
// Test 5 (invalid Magic Bytes)
|
|
expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
|
|
buffer[11] ^= 1;
|
|
succeed(test_footer_decoder(LZMA_FORMAT_ERROR));
|
|
}
|
|
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
// Valid headers
|
|
known_flags.backward_size = 1024;
|
|
for (lzma_check check = LZMA_CHECK_NONE;
|
|
check <= LZMA_CHECK_ID_MAX; ++check) {
|
|
test_header();
|
|
test_footer();
|
|
}
|
|
|
|
// Invalid headers
|
|
test_encode_invalid();
|
|
test_decode_invalid();
|
|
|
|
return 0;
|
|
}
|