diff options
Diffstat (limited to '')
-rw-r--r-- | tests/test_stream_flags.c | 134 |
1 files changed, 64 insertions, 70 deletions
diff --git a/tests/test_stream_flags.c b/tests/test_stream_flags.c index 99e55d5e..0de87cd1 100644 --- a/tests/test_stream_flags.c +++ b/tests/test_stream_flags.c @@ -1,7 +1,7 @@ /////////////////////////////////////////////////////////////////////////////// // /// \file test_stream_flags.c -/// \brief Tests Stream Header and Stream tail coders +/// \brief Tests Stream Header and Stream Footer coders // // Copyright (C) 2007 Lasse Collin // @@ -22,37 +22,30 @@ static lzma_stream_flags known_flags; static lzma_stream_flags decoded_flags; -static uint8_t buffer[LZMA_STREAM_HEADER_SIZE + LZMA_STREAM_TAIL_SIZE]; -static lzma_stream strm = LZMA_STREAM_INIT; +static uint8_t buffer[LZMA_STREAM_HEADER_SIZE]; static bool validate(void) { - if (known_flags.check != decoded_flags.check - || known_flags.has_crc32 != decoded_flags.has_crc32 - || known_flags.is_multi != decoded_flags.is_multi) - return true; - - return false; + return !lzma_stream_flags_equal(&known_flags, &decoded_flags); } static bool -test_header_decoder(size_t expected_size, lzma_ret expected_ret) +test_header_decoder(lzma_ret expected_ret) { memcrap(&decoded_flags, sizeof(decoded_flags)); - if (lzma_stream_header_decoder(&strm, &decoded_flags) != LZMA_OK) + if (lzma_stream_header_decode(&decoded_flags, buffer) != expected_ret) return true; - if (coder_loop(&strm, buffer, expected_size, NULL, 0, - expected_ret, LZMA_RUN)) - return true; - - if (expected_ret != LZMA_STREAM_END) + if (expected_ret != LZMA_OK) return false; + // Header doesn't have Backward Size, so make + // lzma_stream_flags_equal() ignore it. + decoded_flags.backward_size = LZMA_VLI_VALUE_UNKNOWN; return validate(); } @@ -61,36 +54,32 @@ static void test_header(void) { memcrap(buffer, sizeof(buffer)); - expect(lzma_stream_header_encode(buffer, &known_flags) == LZMA_OK); - succeed(test_header_decoder(LZMA_STREAM_HEADER_SIZE, LZMA_STREAM_END)); + expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK); + succeed(test_header_decoder(LZMA_OK)); } static bool -test_tail_decoder(size_t expected_size, lzma_ret expected_ret) +test_footer_decoder(lzma_ret expected_ret) { memcrap(&decoded_flags, sizeof(decoded_flags)); - if (lzma_stream_tail_decoder(&strm, &decoded_flags) != LZMA_OK) - return true; - - if (coder_loop(&strm, buffer, expected_size, NULL, 0, - expected_ret, LZMA_RUN)) + if (lzma_stream_footer_decode(&decoded_flags, buffer) != expected_ret) return true; - if (expected_ret == LZMA_STREAM_END && validate()) - return true; + if (expected_ret != LZMA_OK) + return false; - return false; + return validate(); } static void -test_tail(void) +test_footer(void) { memcrap(buffer, sizeof(buffer)); - expect(lzma_stream_tail_encode(buffer, &known_flags) == LZMA_OK); - succeed(test_tail_decoder(LZMA_STREAM_TAIL_SIZE, LZMA_STREAM_END)); + expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK); + succeed(test_footer_decoder(LZMA_OK)); } @@ -98,21 +87,36 @@ static void test_encode_invalid(void) { known_flags.check = LZMA_CHECK_ID_MAX + 1; - known_flags.has_crc32 = false; - known_flags.is_multi = false; + known_flags.backward_size = 1024; - expect(lzma_stream_header_encode(buffer, &known_flags) + expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_PROG_ERROR); - expect(lzma_stream_tail_encode(buffer, &known_flags) + expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_PROG_ERROR); known_flags.check = (lzma_check_type)(-1); - expect(lzma_stream_header_encode(buffer, &known_flags) + expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_PROG_ERROR); - expect(lzma_stream_tail_encode(buffer, &known_flags) + 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_VALUE_MAX; + + expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK); + + expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_PROG_ERROR); } @@ -121,43 +125,42 @@ static void test_decode_invalid(void) { known_flags.check = LZMA_CHECK_NONE; - known_flags.has_crc32 = false; - known_flags.is_multi = false; + known_flags.backward_size = 1024; - expect(lzma_stream_header_encode(buffer, &known_flags) == LZMA_OK); + expect(lzma_stream_header_encode(&known_flags, buffer) == LZMA_OK); // Test 1 (invalid Magic Bytes) buffer[5] ^= 1; - succeed(test_header_decoder(6, LZMA_DATA_ERROR)); + succeed(test_header_decoder(LZMA_FORMAT_ERROR)); buffer[5] ^= 1; // Test 2a (valid CRC32) - uint32_t crc = lzma_crc32(buffer + 6, 1, 0); - for (size_t i = 0; i < 4; ++i) - buffer[7 + i] = crc >> (i * 8); - succeed(test_header_decoder(LZMA_STREAM_HEADER_SIZE, LZMA_STREAM_END)); + 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, 1, 0); - for (size_t i = 0; i < 4; ++i) - buffer[7 + i] = crc >> (i * 8); - succeed(test_header_decoder(7, LZMA_HEADER_ERROR)); + crc = lzma_crc32(buffer + 6, 2, 0); + integer_write_32(buffer + 8, crc); + succeed(test_header_decoder(LZMA_HEADER_ERROR)); // Test 3 (invalid CRC32) - expect(lzma_stream_header_encode(buffer, &known_flags) == LZMA_OK); - buffer[LZMA_STREAM_HEADER_SIZE - 1] ^= 1; - succeed(test_header_decoder(LZMA_STREAM_HEADER_SIZE, LZMA_DATA_ERROR)); + 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) - expect(lzma_stream_tail_encode(buffer, &known_flags) == LZMA_OK); - buffer[0] ^= 0x40; - succeed(test_tail_decoder(1, LZMA_HEADER_ERROR)); - buffer[0] ^= 0x40; + // 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_HEADER_ERROR)); // Test 5 (invalid Magic Bytes) - buffer[2] ^= 1; - succeed(test_tail_decoder(3, LZMA_DATA_ERROR)); + expect(lzma_stream_footer_encode(&known_flags, buffer) == LZMA_OK); + buffer[11] ^= 1; + succeed(test_footer_decoder(LZMA_FORMAT_ERROR)); } @@ -167,25 +170,16 @@ main(void) lzma_init(); // Valid headers + known_flags.backward_size = 1024; for (lzma_check_type check = LZMA_CHECK_NONE; check <= LZMA_CHECK_ID_MAX; ++check) { - for (int has_crc32 = 0; has_crc32 <= 1; ++has_crc32) { - for (int is_multi = 0; is_multi <= 1; ++is_multi) { - known_flags.check = check; - known_flags.has_crc32 = has_crc32; - known_flags.is_multi = is_multi; - - test_header(); - test_tail(); - } - } + test_header(); + test_footer(); } // Invalid headers test_encode_invalid(); test_decode_invalid(); - lzma_end(&strm); - return 0; } |