2007-12-08 17:42:33 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file test_stream_flags.c
|
2008-06-18 11:02:10 -04:00
|
|
|
/// \brief Tests Stream Header and Stream Footer coders
|
2007-12-08 17:42:33 -05:00
|
|
|
//
|
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"
|
|
|
|
|
|
|
|
|
|
|
|
static lzma_stream_flags known_flags;
|
|
|
|
static lzma_stream_flags decoded_flags;
|
2008-06-18 11:02:10 -04:00
|
|
|
static uint8_t buffer[LZMA_STREAM_HEADER_SIZE];
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
validate(void)
|
|
|
|
{
|
2008-09-12 15:41:40 -04:00
|
|
|
// 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;
|
2007-12-08 17:42:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
2008-06-18 11:02:10 -04:00
|
|
|
test_header_decoder(lzma_ret expected_ret)
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
|
|
|
memcrap(&decoded_flags, sizeof(decoded_flags));
|
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
if (lzma_stream_header_decode(&decoded_flags, buffer) != expected_ret)
|
2007-12-08 17:42:33 -05:00
|
|
|
return true;
|
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
if (expected_ret != LZMA_OK)
|
2007-12-08 17:42:33 -05:00
|
|
|
return false;
|
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
// Header doesn't have Backward Size, so make
|
2008-09-12 15:41:40 -04:00
|
|
|
// lzma_stream_flags_compare() ignore it.
|
2008-09-13 05:10:43 -04:00
|
|
|
decoded_flags.backward_size = LZMA_VLI_UNKNOWN;
|
2007-12-08 17:42:33 -05:00
|
|
|
return validate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_header(void)
|
|
|
|
{
|
|
|
|
memcrap(buffer, sizeof(buffer));
|
2008-06-18 11:02:10 -04:00
|
|
|
expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
|
|
|
|
succeed(test_header_decoder(LZMA_OK));
|
2007-12-08 17:42:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
2008-06-18 11:02:10 -04:00
|
|
|
test_footer_decoder(lzma_ret expected_ret)
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
|
|
|
memcrap(&decoded_flags, sizeof(decoded_flags));
|
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
if (lzma_stream_footer_decode(&decoded_flags, buffer) != expected_ret)
|
2007-12-08 17:42:33 -05:00
|
|
|
return true;
|
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
if (expected_ret != LZMA_OK)
|
|
|
|
return false;
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
return validate();
|
2007-12-08 17:42:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-06-18 11:02:10 -04:00
|
|
|
test_footer(void)
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
|
|
|
memcrap(buffer, sizeof(buffer));
|
2008-06-18 11:02:10 -04:00
|
|
|
expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
|
|
|
|
succeed(test_footer_decoder(LZMA_OK));
|
2007-12-08 17:42:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_encode_invalid(void)
|
|
|
|
{
|
2019-06-24 17:08:13 -04:00
|
|
|
known_flags.check = (lzma_check)(LZMA_CHECK_ID_MAX + 1);
|
2008-06-18 11:02:10 -04:00
|
|
|
known_flags.backward_size = 1024;
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
expect(lzma_stream_header_encode(&known_flags, buffer)
|
2007-12-08 17:42:33 -05:00
|
|
|
== LZMA_PROG_ERROR);
|
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
expect(lzma_stream_footer_encode(&known_flags, buffer)
|
2007-12-08 17:42:33 -05:00
|
|
|
== LZMA_PROG_ERROR);
|
|
|
|
|
2008-08-28 15:53:15 -04:00
|
|
|
known_flags.check = (lzma_check)(-1);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
expect(lzma_stream_header_encode(&known_flags, buffer)
|
2007-12-08 17:42:33 -05:00
|
|
|
== LZMA_PROG_ERROR);
|
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
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);
|
|
|
|
|
2008-09-13 05:10:43 -04:00
|
|
|
known_flags.backward_size = LZMA_VLI_MAX;
|
2008-06-18 11:02:10 -04:00
|
|
|
|
|
|
|
expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
|
|
|
|
|
|
|
|
expect(lzma_stream_footer_encode(&known_flags, buffer)
|
2007-12-08 17:42:33 -05:00
|
|
|
== LZMA_PROG_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_decode_invalid(void)
|
|
|
|
{
|
|
|
|
known_flags.check = LZMA_CHECK_NONE;
|
2008-06-18 11:02:10 -04:00
|
|
|
known_flags.backward_size = 1024;
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
// Test 1 (invalid Magic Bytes)
|
|
|
|
buffer[5] ^= 1;
|
2008-06-18 11:02:10 -04:00
|
|
|
succeed(test_header_decoder(LZMA_FORMAT_ERROR));
|
2007-12-08 17:42:33 -05:00
|
|
|
buffer[5] ^= 1;
|
|
|
|
|
|
|
|
// Test 2a (valid CRC32)
|
2008-06-18 11:02:10 -04:00
|
|
|
uint32_t crc = lzma_crc32(buffer + 6, 2, 0);
|
2019-12-31 15:41:45 -05:00
|
|
|
write32le(buffer + 8, crc);
|
2008-06-18 11:02:10 -04:00
|
|
|
succeed(test_header_decoder(LZMA_OK));
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
// Test 2b (invalid Stream Flags with valid CRC32)
|
|
|
|
buffer[6] ^= 0x20;
|
2008-06-18 11:02:10 -04:00
|
|
|
crc = lzma_crc32(buffer + 6, 2, 0);
|
2019-12-31 15:41:45 -05:00
|
|
|
write32le(buffer + 8, crc);
|
2008-09-13 05:10:43 -04:00
|
|
|
succeed(test_header_decoder(LZMA_OPTIONS_ERROR));
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
// Test 3 (invalid CRC32)
|
2008-06-18 11:02:10 -04:00
|
|
|
expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK);
|
|
|
|
buffer[9] ^= 1;
|
|
|
|
succeed(test_header_decoder(LZMA_DATA_ERROR));
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-06-18 11:02:10 -04:00
|
|
|
// 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);
|
2019-12-31 15:41:45 -05:00
|
|
|
write32le(buffer, crc);
|
2008-09-13 05:10:43 -04:00
|
|
|
succeed(test_footer_decoder(LZMA_OPTIONS_ERROR));
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
// Test 5 (invalid Magic Bytes)
|
2008-06-18 11:02:10 -04:00
|
|
|
expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK);
|
|
|
|
buffer[11] ^= 1;
|
|
|
|
succeed(test_footer_decoder(LZMA_FORMAT_ERROR));
|
2007-12-08 17:42:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2007-12-09 04:03:28 -05:00
|
|
|
main(void)
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
|
|
|
// Valid headers
|
2008-06-18 11:02:10 -04:00
|
|
|
known_flags.backward_size = 1024;
|
2008-08-28 15:53:15 -04:00
|
|
|
for (lzma_check check = LZMA_CHECK_NONE;
|
2007-12-08 17:42:33 -05:00
|
|
|
check <= LZMA_CHECK_ID_MAX; ++check) {
|
2008-06-18 11:02:10 -04:00
|
|
|
test_header();
|
|
|
|
test_footer();
|
2007-12-08 17:42:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid headers
|
|
|
|
test_encode_invalid();
|
|
|
|
test_decode_invalid();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|