diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 5 | ||||
-rw-r--r-- | tests/test_block_header.c | 411 | ||||
-rwxr-xr-x | tests/test_compress.sh | 65 | ||||
-rw-r--r-- | tests/test_filter_flags.c | 116 | ||||
-rw-r--r-- | tests/test_index.c | 504 | ||||
-rw-r--r-- | tests/test_info.c | 717 | ||||
-rw-r--r-- | tests/test_stream_flags.c | 134 | ||||
-rw-r--r-- | tests/tests.h | 14 |
8 files changed, 776 insertions, 1190 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 3ecddf59..f9f15c54 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -39,8 +39,7 @@ check_PROGRAMS = \ test_stream_flags \ test_filter_flags \ test_block_header \ - test_index \ - test_info + test_index TESTS = \ test_memlimit \ @@ -49,8 +48,6 @@ TESTS = \ test_filter_flags \ test_block_header \ test_index \ - test_info \ - test_files.sh \ test_compress.sh clean-local: diff --git a/tests/test_block_header.c b/tests/test_block_header.c index b38f4b24..658f7fe2 100644 --- a/tests/test_block_header.c +++ b/tests/test_block_header.c @@ -20,204 +20,124 @@ #include "tests.h" -static uint8_t buffer[4096]; -static lzma_stream strm = LZMA_STREAM_INIT; +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]; static lzma_options_block known_options; static lzma_options_block decoded_options; -// We want to test zero, one, and two filters in the chain. - -static const lzma_options_filter filters_none[1] = { +static lzma_options_filter filters_none[1] = { { .id = LZMA_VLI_VALUE_UNKNOWN, - .options = NULL, }, }; -static const lzma_options_filter filters_powerpc[2] = { + +static lzma_options_filter filters_one[2] = { { - .id = LZMA_FILTER_POWERPC, - .options = NULL, + .id = LZMA_FILTER_LZMA, + .options = (void *)(&lzma_preset_lzma[0]), }, { .id = LZMA_VLI_VALUE_UNKNOWN, - .options = NULL, - }, + } }; -static const lzma_options_delta options_delta = { - .distance = 4, -}; -static const lzma_options_filter filters_delta[3] = { +static lzma_options_filter filters_four[5] = { { - .id = LZMA_FILTER_DELTA, - .options = (void *)(&options_delta), + .id = LZMA_FILTER_X86, + .options = NULL, }, { - .id = LZMA_FILTER_COPY, + .id = LZMA_FILTER_X86, .options = NULL, }, { - .id = LZMA_VLI_VALUE_UNKNOWN, + .id = LZMA_FILTER_X86, .options = NULL, - }, + }, { + .id = LZMA_FILTER_LZMA, + .options = (void *)(&lzma_preset_lzma[0]), + }, { + .id = LZMA_VLI_VALUE_UNKNOWN, + } }; -static void -free_decoded_options(void) -{ - for (size_t i = 0; i < sizeof(decoded_options.filters) - / sizeof(decoded_options.filters[0]); ++i) { - free(decoded_options.filters[i].options); - decoded_options.filters[i].options = NULL; +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, } -} - - -static bool -encode(uint32_t header_size) -{ - memcrap(buffer, sizeof(buffer)); - - if (lzma_block_header_size(&known_options) != LZMA_OK) - return true; - - if (known_options.header_size != header_size) - return true; - - if (lzma_block_header_encode(buffer, &known_options) != LZMA_OK) - return true; - - return false; -} +}; -static bool -decode_ret(uint32_t header_size, lzma_ret ret_ok) +static void +code(void) { - memcrap(&decoded_options, sizeof(decoded_options)); - decoded_options.has_crc32 = known_options.has_crc32; + expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK); - expect(lzma_block_header_decoder(&strm, &decoded_options) == LZMA_OK); - - const bool ret = decoder_loop_ret(&strm, buffer, header_size, ret_ok); - free_decoded_options(); - return ret; -} - - -static bool -decode(uint32_t header_size) -{ + lzma_options_filter filters[LZMA_BLOCK_FILTERS_MAX + 1]; + memcrap(filters, sizeof(filters)); memcrap(&decoded_options, sizeof(decoded_options)); - decoded_options.has_crc32 = known_options.has_crc32; - - expect(lzma_block_header_decoder(&strm, &decoded_options) == LZMA_OK); - - const bool ret = decoder_loop(&strm, buffer, header_size); - free_decoded_options(); - if (ret) - return true; - - if (known_options.has_eopm != decoded_options.has_eopm) - return true; - - if (known_options.is_metadata != decoded_options.is_metadata) - return true; - if (known_options.compressed_size == LZMA_VLI_VALUE_UNKNOWN - && known_options.compressed_reserve != 0) { - if (decoded_options.compressed_size != 0) - return true; - } else if (known_options.compressed_size - != decoded_options.compressed_size) { - return true; - } - - if (known_options.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN - && known_options.uncompressed_reserve != 0) { - if (decoded_options.uncompressed_size != 0) - return true; - } else if (known_options.uncompressed_size - != decoded_options.uncompressed_size) { - return true; - } - - if (known_options.compressed_reserve != 0 - && known_options.compressed_reserve - != decoded_options.compressed_reserve) - return true; - - if (known_options.uncompressed_reserve != 0 - && known_options.uncompressed_reserve - != decoded_options.uncompressed_reserve) - return true; - - if (known_options.padding != decoded_options.padding) - return true; - - return false; -} - - -static bool -code(uint32_t header_size) -{ - return encode(header_size) || decode(header_size); -} - - -static bool -helper_loop(uint32_t unpadded_size, uint32_t multiple) -{ - for (int i = 0; i <= LZMA_BLOCK_HEADER_PADDING_MAX; ++i) { - known_options.padding = i; - if (code(unpadded_size + i)) - return true; - } - - for (int i = 0 - LZMA_BLOCK_HEADER_PADDING_MAX - 1; - i <= LZMA_BLOCK_HEADER_PADDING_MAX + 1; ++i) { - known_options.alignment = i; - - uint32_t size = unpadded_size; - while ((size + known_options.alignment) % multiple) - ++size; - - known_options.padding = LZMA_BLOCK_HEADER_PADDING_AUTO; - if (code(size)) - return true; - - } while (++known_options.alignment - <= LZMA_BLOCK_HEADER_PADDING_MAX + 1); + 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); - return false; -} + expect(known_options.compressed_size + == decoded_options.compressed_size); + expect(known_options.uncompressed_size + == decoded_options.uncompressed_size); + for (size_t i = 0; known_options.filters[i].id + != LZMA_VLI_VALUE_UNKNOWN; ++i) + expect(known_options.filters[i].id == filters[i].id); -static bool -helper(uint32_t unpadded_size, uint32_t multiple) -{ - known_options.has_crc32 = false; - known_options.is_metadata = false; - if (helper_loop(unpadded_size, multiple)) - return true; - - known_options.has_crc32 = false; - known_options.is_metadata = true; - if (helper_loop(unpadded_size, multiple)) - return true; - - known_options.has_crc32 = true; - known_options.is_metadata = false; - if (helper_loop(unpadded_size + 4, multiple)) - return true; - - known_options.has_crc32 = true; - known_options.is_metadata = true; - if (helper_loop(unpadded_size + 4, multiple)) - return true; - - return false; + for (size_t i = 0; i < LZMA_BLOCK_FILTERS_MAX; ++i) + free(decoded_options.filters[i].options); } @@ -225,128 +145,114 @@ static void test1(void) { known_options = (lzma_options_block){ - .has_eopm = true, + .check = LZMA_CHECK_NONE, .compressed_size = LZMA_VLI_VALUE_UNKNOWN, .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN, - .compressed_reserve = 0, - .uncompressed_reserve = 0, + .filters = NULL, }; - memcpy(known_options.filters, filters_none, sizeof(filters_none)); - expect(!helper(2, 1)); - - memcpy(known_options.filters, filters_powerpc, - sizeof(filters_powerpc)); - expect(!helper(3, 4)); - - memcpy(known_options.filters, filters_delta, sizeof(filters_delta)); - expect(!helper(5, 1)); - known_options.padding = LZMA_BLOCK_HEADER_PADDING_MAX + 1; expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR); -} + known_options.filters = filters_none; + expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR); -static void -test2_helper(uint32_t unpadded_size, uint32_t multiple) -{ - known_options.has_eopm = true; - known_options.compressed_size = LZMA_VLI_VALUE_UNKNOWN; - known_options.uncompressed_size = LZMA_VLI_VALUE_UNKNOWN; - known_options.compressed_reserve = 1; - known_options.uncompressed_reserve = 1; - expect(!helper(unpadded_size + 2, multiple)); + known_options.filters = filters_five; + expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR); - known_options.compressed_reserve = LZMA_VLI_BYTES_MAX; - known_options.uncompressed_reserve = LZMA_VLI_BYTES_MAX; - expect(!helper(unpadded_size + 18, multiple)); + known_options.filters = filters_one; + expect(lzma_block_header_size(&known_options) == LZMA_OK); - known_options.compressed_size = 1234; - known_options.uncompressed_size = 2345; - expect(!helper(unpadded_size + 18, multiple)); + known_options.check = 999; // Some invalid value, which gets ignored. + expect(lzma_block_header_size(&known_options) == LZMA_OK); - known_options.compressed_reserve = 1; - known_options.uncompressed_reserve = 1; + known_options.compressed_size = 5; // Not a multiple of four. expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR); -} + known_options.compressed_size = 0; // Cannot be zero. + expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR); -static void -test2(void) -{ - memcpy(known_options.filters, filters_none, sizeof(filters_none)); - test2_helper(2, 1); - - memcpy(known_options.filters, filters_powerpc, - sizeof(filters_powerpc)); - test2_helper(3, 4); + known_options.compressed_size = LZMA_VLI_VALUE_UNKNOWN; + known_options.uncompressed_size = 0; + expect(lzma_block_header_size(&known_options) == LZMA_OK); - memcpy(known_options.filters, filters_delta, - sizeof(filters_delta)); - test2_helper(5, 1); + known_options.uncompressed_size = LZMA_VLI_VALUE_MAX + 1; + expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR); } static void -test3(void) +test2(void) { known_options = (lzma_options_block){ - .has_crc32 = false, - .has_eopm = true, - .is_metadata = false, + .check = LZMA_CHECK_CRC32, .compressed_size = LZMA_VLI_VALUE_UNKNOWN, .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN, - .compressed_reserve = 1, - .uncompressed_reserve = 1, + .filters = filters_four, }; - memcpy(known_options.filters, filters_none, sizeof(filters_none)); - - known_options.header_size = 3; - expect(lzma_block_header_encode(buffer, &known_options) - == LZMA_PROG_ERROR); - known_options.header_size = 4; - expect(lzma_block_header_encode(buffer, &known_options) == LZMA_OK); + expect(lzma_block_header_size(&known_options) == LZMA_OK); + code(); - known_options.header_size = 5; - expect(lzma_block_header_encode(buffer, &known_options) - == LZMA_PROG_ERROR); + known_options.compressed_size = 123456; + known_options.uncompressed_size = 234567; + expect(lzma_block_header_size(&known_options) == LZMA_OK); + code(); - // NOTE: This assumes that Filter ID 0x1F is not supported. Update - // this test to use some other ID if 0x1F becomes supported. - known_options.filters[0].id = 0x1F; - known_options.header_size = 5; - expect(lzma_block_header_encode(buffer, &known_options) - == LZMA_HEADER_ERROR); + // We can make the sizes smaller while keeping the header size + // the same. + known_options.compressed_size = 12; + known_options.uncompressed_size = 23; + code(); } static void -test4(void) +test3(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, + .check = LZMA_CHECK_CRC32, + .compressed_size = LZMA_VLI_VALUE_UNKNOWN, + .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN, + .filters = filters_one, }; - 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; + expect(lzma_block_header_size(&known_options) == LZMA_OK); + known_options.header_size += 4; + expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK); - buffer[1] ^= 0x40; - expect(decode_ret(21, LZMA_HEADER_ERROR)); - buffer[1] ^= 0x40; + 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; + // 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; + // 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; } @@ -358,9 +264,6 @@ main(void) test1(); test2(); test3(); - test4(); - - lzma_end(&strm); return 0; } diff --git a/tests/test_compress.sh b/tests/test_compress.sh index 5cf21cf4..e322d385 100755 --- a/tests/test_compress.sh +++ b/tests/test_compress.sh @@ -25,14 +25,10 @@ if test $? != 42 ; then fi test_lzma() { - ################ - # Non-streamed # - ################ - if $LZMA -c "$@" "$FILE" > tmp_compressed; then : else - echo "Non-streamed compressing failed: $* $FILE" + echo "Compressing failed: $* $FILE" (exit 1) exit 1 fi @@ -40,7 +36,7 @@ test_lzma() { if $LZMA -cd tmp_compressed > tmp_uncompressed ; then : else - echo "Decoding of non-streamed file failed: $* $FILE" + echo "Decoding failed: $* $FILE" (exit 1) exit 1 fi @@ -48,7 +44,7 @@ test_lzma() { if cmp tmp_uncompressed "$FILE" ; then : else - echo "Decoded non-streamed file does not match the original: $* $FILE" + echo "Decoded file does not match the original: $* $FILE" (exit 1) exit 1 fi @@ -56,51 +52,7 @@ test_lzma() { if $LZMADEC tmp_compressed > tmp_uncompressed ; then : else - echo "Decoding of non-streamed file failed: $* $FILE" - (exit 1) - exit 1 - fi - - if cmp tmp_uncompressed "$FILE" ; then - : - else - echo "Decoded non-streamed file does not match the original: $* $FILE" - (exit 1) - exit 1 - fi - - ############ - # Streamed # - ############ - - if $LZMA -c "$@" < "$FILE" > tmp_compressed; then - : - else - echo "Streamed compressing failed: $* $FILE" - (exit 1) - exit 1 - fi - - if $LZMA -cd < tmp_compressed > tmp_uncompressed ; then - : - else - echo "Decoding of streamed file failed: $* $FILE" - (exit 1) - exit 1 - fi - - if cmp tmp_uncompressed "$FILE" ; then - : - else - echo "Decoded streamed file does not match the original: $* $FILE" - (exit 1) - exit 1 - fi - - if $LZMADEC < tmp_compressed > tmp_uncompressed ; then - : - else - echo "Decoding of streamed file failed: $* $FILE" + echo "Decoding failed: $* $FILE" (exit 1) exit 1 fi @@ -108,7 +60,7 @@ test_lzma() { if cmp tmp_uncompressed "$FILE" ; then : else - echo "Decoded streamed file does not match the original: $* $FILE" + echo "Decoded file does not match the original: $* $FILE" (exit 1) exit 1 fi @@ -151,7 +103,6 @@ do test_lzma -4 for ARGS in \ - --copy \ --subblock \ --subblock=size=1 \ --subblock=size=1,rle=1 \ @@ -170,10 +121,8 @@ do --armthumb \ --sparc do - test_lzma $ARGS - test_lzma --subblock $ARGS - test_lzma $ARGS --subblock - test_lzma --subblock $ARGS --subblock + test_lzma $ARGS --lzma=dict=64KiB,fb=32,mode=fast + test_lzma --subblock $ARGS --lzma=dict=64KiB,fb=32,mode=fast done echo diff --git a/tests/test_filter_flags.c b/tests/test_filter_flags.c index bab344ae..8df6da6c 100644 --- a/tests/test_filter_flags.c +++ b/tests/test_filter_flags.c @@ -51,14 +51,14 @@ encode(uint32_t known_size) static bool -decode_ret(uint32_t known_size, lzma_ret ret_ok) +decode_ret(uint32_t known_size, lzma_ret expected_ret) { memcrap(&decoded_flags, sizeof(decoded_flags)); - if (lzma_filter_flags_decoder(&strm, &decoded_flags) != LZMA_OK) - return true; - - if (decoder_loop_ret(&strm, buffer, known_size, ret_ok)) + size_t pos = 0; + if (lzma_filter_flags_decode(&decoded_flags, NULL, + buffer, &pos, known_size) != expected_ret + || pos != known_size) return true; return false; @@ -68,7 +68,7 @@ decode_ret(uint32_t known_size, lzma_ret ret_ok) static bool decode(uint32_t known_size) { - if (decode_ret(known_size, LZMA_STREAM_END)) + if (decode_ret(known_size, LZMA_OK)) return true; if (known_flags.id != decoded_flags.id) @@ -78,49 +78,7 @@ decode(uint32_t known_size) } -static void -test_copy(void) -{ - // Test 1 (good) - known_flags.id = LZMA_FILTER_COPY; - known_flags.options = NULL; - - expect(!encode(1)); - expect(!decode(1)); - expect(decoded_flags.options == NULL); - - // Test 2 (invalid encoder options) - known_flags.options = &known_flags; - expect(encode(99)); - - // Test 3 (good but unusual Filter Flags field) - buffer[0] = 0xE0; - buffer[1] = LZMA_FILTER_COPY; - expect(!decode(2)); - expect(decoded_flags.options == NULL); - - // Test 4 (invalid Filter Flags field) - buffer[0] = 0xE1; - buffer[1] = LZMA_FILTER_COPY; - buffer[2] = 0; - expect(!decode_ret(3, LZMA_HEADER_ERROR)); - - // Test 5 (good but weird Filter Flags field) - buffer[0] = 0xFF; - buffer[1] = LZMA_FILTER_COPY; - buffer[2] = 0; - expect(!decode(3)); - expect(decoded_flags.options == NULL); - - // Test 6 (invalid Filter Flags field) - buffer[0] = 0xFF; - buffer[1] = LZMA_FILTER_COPY; - buffer[2] = 1; - buffer[3] = 0; - expect(!decode_ret(4, LZMA_HEADER_ERROR)); -} - - +#ifdef HAVE_FILTER_SUBBLOCK static void test_subblock(void) { @@ -128,16 +86,16 @@ test_subblock(void) known_flags.id = LZMA_FILTER_SUBBLOCK; known_flags.options = NULL; - expect(!encode(1)); - expect(!decode(1)); + expect(!encode(2)); + expect(!decode(2)); expect(decoded_flags.options != NULL); expect(((lzma_options_subblock *)(decoded_flags.options)) ->allow_subfilters); // Test 2 known_flags.options = decoded_flags.options; - expect(!encode(1)); - expect(!decode(1)); + expect(!encode(2)); + expect(!decode(2)); expect(decoded_flags.options != NULL); expect(((lzma_options_subblock *)(decoded_flags.options)) ->allow_subfilters); @@ -146,14 +104,15 @@ test_subblock(void) free(known_flags.options); // Test 3 - buffer[0] = 0xFF; - buffer[1] = LZMA_FILTER_SUBBLOCK; - buffer[2] = 1; - buffer[3] = 0; - expect(!decode_ret(4, LZMA_HEADER_ERROR)); + buffer[0] = LZMA_FILTER_SUBBLOCK; + buffer[1] = 1; + buffer[2] = 0; + expect(!decode_ret(3, LZMA_HEADER_ERROR)); } +#endif +#ifdef HAVE_FILTER_SIMPLE static void test_simple(void) { @@ -161,16 +120,16 @@ test_simple(void) known_flags.id = LZMA_FILTER_X86; known_flags.options = NULL; - expect(!encode(1)); - expect(!decode(1)); + expect(!encode(2)); + expect(!decode(2)); expect(decoded_flags.options == NULL); // Test 2 lzma_options_simple options; options.start_offset = 0; known_flags.options = &options; - expect(!encode(1)); - expect(!decode(1)); + expect(!encode(2)); + expect(!decode(2)); expect(decoded_flags.options == NULL); // Test 3 @@ -185,8 +144,10 @@ test_simple(void) free(decoded); } +#endif +#ifdef HAVE_FILTER_DELTA static void test_delta(void) { @@ -202,8 +163,8 @@ test_delta(void) // Test 3 options.distance = LZMA_DELTA_DISTANCE_MIN; - expect(!encode(2)); - expect(!decode(2)); + expect(!encode(3)); + expect(!decode(3)); expect(((lzma_options_delta *)(decoded_flags.options)) ->distance == options.distance); @@ -211,8 +172,8 @@ test_delta(void) // Test 4 options.distance = LZMA_DELTA_DISTANCE_MAX; - expect(!encode(2)); - expect(!decode(2)); + expect(!encode(3)); + expect(!decode(3)); expect(((lzma_options_delta *)(decoded_flags.options)) ->distance == options.distance); @@ -222,8 +183,10 @@ test_delta(void) options.distance = LZMA_DELTA_DISTANCE_MAX + 1; expect(encode(99)); } +#endif +#ifdef HAVE_FILTER_LZMA static void validate_lzma(void) { @@ -271,12 +234,13 @@ test_lzma(void) expect(encode(99)); // Test 4 (brute-force test some valid dictionary sizes) + options.dictionary_size = LZMA_DICTIONARY_SIZE_MIN; while (options.dictionary_size != LZMA_DICTIONARY_SIZE_MAX) { if (++options.dictionary_size == 5000) options.dictionary_size = LZMA_DICTIONARY_SIZE_MAX - 5; - expect(!encode(3)); - expect(!decode(3)); + expect(!encode(4)); + expect(!decode(4)); validate_lzma(); free(decoded_flags.options); @@ -287,7 +251,7 @@ test_lzma(void) expect(encode(99)); // Test 6 (brute-force test lc/lp/pb) - options.dictionary_size = 1; + options.dictionary_size = LZMA_DICTIONARY_SIZE_MIN; for (uint32_t lc = LZMA_LITERAL_CONTEXT_BITS_MIN; lc <= LZMA_LITERAL_CONTEXT_BITS_MAX; ++lc) { for (uint32_t lp = LZMA_LITERAL_POS_BITS_MIN; @@ -298,8 +262,8 @@ test_lzma(void) options.literal_pos_bits = lp; options.pos_bits = pb; - expect(!encode(3)); - expect(!decode(3)); + expect(!encode(4)); + expect(!decode(4)); validate_lzma(); free(decoded_flags.options); @@ -307,6 +271,7 @@ test_lzma(void) } } } +#endif int @@ -314,11 +279,18 @@ main(void) { lzma_init(); - test_copy(); +#ifdef HAVE_FILTER_SUBBLOCK test_subblock(); +#endif +#ifdef HAVE_FILTER_SIMPLE test_simple(); +#endif +#ifdef HAVE_FILTER_DELTA test_delta(); +#endif +#ifdef HAVE_FILTER_LZMA test_lzma(); +#endif lzma_end(&strm); diff --git a/tests/test_index.c b/tests/test_index.c index 105abb16..8623d9ab 100644 --- a/tests/test_index.c +++ b/tests/test_index.c @@ -3,7 +3,7 @@ /// \file test_index.c /// \brief Tests functions handling the lzma_index structure // -// Copyright (C) 2007 Lasse Collin +// Copyright (C) 2007-2008 Lasse Collin // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -20,24 +20,504 @@ #include "tests.h" +static lzma_index * +create_empty(void) +{ + lzma_index *i = lzma_index_init(NULL, NULL); + expect(i != NULL); + return i; +} + + +static lzma_index * +create_small(void) +{ + lzma_index *i = lzma_index_init(NULL, NULL); + expect(i != NULL); + expect(lzma_index_append(i, NULL, 400, 555) == LZMA_OK); + expect(lzma_index_append(i, NULL, 600, 777) == LZMA_OK); + expect(lzma_index_append(i, NULL, 800, 999) == LZMA_OK); + return i; +} + + +static lzma_index * +create_big(void) +{ + lzma_index *i = lzma_index_init(NULL, NULL); + expect(i != NULL); + + lzma_vli total_size = 0; + lzma_vli uncompressed_size = 0; + + // Add pseudo-random sizes (but always the same size values). + const size_t count = 5555; + uint32_t n = 11; + for (size_t j = 0; j < count; ++j) { + n = 7019 * n + 7607; + const uint32_t t = (n * 3011) & ~UINT32_C(3); + expect(lzma_index_append(i, NULL, t, n) == LZMA_OK); + total_size += t; + uncompressed_size += n; + } + + expect(lzma_index_count(i) == count); + expect(lzma_index_total_size(i) == total_size); + expect(lzma_index_uncompressed_size(i) == uncompressed_size); + expect(lzma_index_total_size(i) + lzma_index_size(i) + + 2 * LZMA_STREAM_HEADER_SIZE + == lzma_index_stream_size(i)); + + return i; +} + + +static void +test_equal(void) +{ + lzma_index *a = create_empty(); + lzma_index *b = create_small(); + lzma_index *c = create_big(); + expect(a && b && c); + + expect(lzma_index_equal(a, a)); + expect(lzma_index_equal(b, b)); + expect(lzma_index_equal(c, c)); + + expect(!lzma_index_equal(a, b)); + expect(!lzma_index_equal(a, c)); + expect(!lzma_index_equal(b, c)); + + lzma_index_end(a, NULL); + lzma_index_end(b, NULL); + lzma_index_end(c, NULL); +} + + +static void +test_overflow(void) +{ + // Integer overflow tests + lzma_index *i = create_empty(); + + expect(lzma_index_append(i, NULL, LZMA_VLI_VALUE_MAX - 5, 1234) + == LZMA_DATA_ERROR); + + // TODO + + lzma_index_end(i, NULL); +} + + +static void +test_copy(const lzma_index *i) +{ + lzma_index *d = lzma_index_dup(i, NULL); + expect(d != NULL); + lzma_index_end(d, NULL); +} + + +static void +test_read(lzma_index *i) +{ + lzma_index_record record; + + // Try twice so we see that rewinding works. + for (size_t j = 0; j < 2; ++j) { + lzma_vli total_size = 0; + lzma_vli uncompressed_size = 0; + lzma_vli stream_offset = LZMA_STREAM_HEADER_SIZE; + lzma_vli uncompressed_offset = 0; + uint32_t count = 0; + + while (!lzma_index_read(i, &record)) { + ++count; + + total_size += record.total_size; + uncompressed_size += record.uncompressed_size; + + expect(record.stream_offset == stream_offset); + expect(record.uncompressed_offset + == uncompressed_offset); + + stream_offset += record.total_size; + uncompressed_offset += record.uncompressed_size; + } + + expect(lzma_index_total_size(i) == total_size); + expect(lzma_index_uncompressed_size(i) == uncompressed_size); + expect(lzma_index_count(i) == count); + + lzma_index_rewind(i); + } +} + + +static void +test_code(lzma_index *i) +{ + const size_t alloc_size = 128 * 1024; + uint8_t *buf = malloc(alloc_size); + expect(buf != NULL); + + // Encode + lzma_stream strm = LZMA_STREAM_INIT; + expect(lzma_index_encoder(&strm, i) == LZMA_OK); + const lzma_vli index_size = lzma_index_size(i); + succeed(coder_loop(&strm, NULL, 0, buf, index_size, + LZMA_STREAM_END, LZMA_RUN)); + + // Decode + lzma_index *d; + expect(lzma_index_decoder(&strm, &d) == LZMA_OK); + succeed(decoder_loop(&strm, buf, index_size)); + + expect(lzma_index_equal(i, d)); + + lzma_index_end(d, NULL); + lzma_end(&strm); + + // Decode with hashing + lzma_index_hash *h = lzma_index_hash_init(NULL, NULL); + expect(h != NULL); + lzma_index_rewind(i); + lzma_index_record r; + while (!lzma_index_read(i, &r)) + expect(lzma_index_hash_append(h, r.total_size, + r.uncompressed_size) == LZMA_OK); + size_t pos = 0; + while (pos < index_size - 1) + expect(lzma_index_hash_decode(h, buf, &pos, pos + 1) + == LZMA_OK); + expect(lzma_index_hash_decode(h, buf, &pos, pos + 1) + == LZMA_STREAM_END); + + lzma_index_hash_end(h, NULL); + + free(buf); +} + + +static void +test_many(lzma_index *i) +{ + test_copy(i); + test_read(i); + test_code(i); +} + + +static void +test_cat(void) +{ + lzma_index *a, *b, *c; + + // Empty Indexes + a = create_empty(); + b = create_empty(); + expect(lzma_index_cat(a, b, NULL, 0) == LZMA_OK); + expect(lzma_index_count(a) == 0); + expect(lzma_index_stream_size(a) == 2 * LZMA_STREAM_HEADER_SIZE + 8); + expect(lzma_index_file_size(a) + == 2 * (2 * LZMA_STREAM_HEADER_SIZE + 8)); + + b = create_empty(); + expect(lzma_index_cat(a, b, NULL, 0) == LZMA_OK); + expect(lzma_index_count(a) == 0); + expect(lzma_index_stream_size(a) == 2 * LZMA_STREAM_HEADER_SIZE + 8); + expect(lzma_index_file_size(a) + == 3 * (2 * LZMA_STREAM_HEADER_SIZE + 8)); + + b = create_empty(); + c = create_empty(); + expect(lzma_index_cat(b, c, NULL, 4) == LZMA_OK); + expect(lzma_index_count(b) == 0); + expect(lzma_index_stream_size(b) == 2 * LZMA_STREAM_HEADER_SIZE + 8); + expect(lzma_index_file_size(b) + == 2 * (2 * LZMA_STREAM_HEADER_SIZE + 8) + 4); + + expect(lzma_index_cat(a, b, NULL, 8) == LZMA_OK); + expect(lzma_index_count(a) == 0); + expect(lzma_index_stream_size(a) == 2 * LZMA_STREAM_HEADER_SIZE + 8); + expect(lzma_index_file_size(a) + == 5 * (2 * LZMA_STREAM_HEADER_SIZE + 8) + 4 + 8); + + lzma_index_end(a, NULL); + + // Small Indexes + a = create_small(); + lzma_vli stream_size = lzma_index_stream_size(a); + b = create_small(); + expect(lzma_index_cat(a, b, NULL, 4) == LZMA_OK); + expect(lzma_index_file_size(a) == stream_size * 2 + 4); + expect(lzma_index_stream_size(a) > stream_size); + expect(lzma_index_stream_size(a) < stream_size * 2); + + b = create_small(); + c = create_small(); + expect(lzma_index_cat(b, c, NULL, 8) == LZMA_OK); + expect(lzma_index_cat(a, b, NULL, 12) == LZMA_OK); + expect(lzma_index_file_size(a) == stream_size * 4 + 4 + 8 + 12); + + lzma_index_end(a, NULL); + + // Big Indexes + a = create_big(); + stream_size = lzma_index_stream_size(a); + b = create_big(); + expect(lzma_index_cat(a, b, NULL, 4) == LZMA_OK); + expect(lzma_index_file_size(a) == stream_size * 2 + 4); + expect(lzma_index_stream_size(a) > stream_size); + expect(lzma_index_stream_size(a) < stream_size * 2); + + b = create_big(); + c = create_big(); + expect(lzma_index_cat(b, c, NULL, 8) == LZMA_OK); + expect(lzma_index_cat(a, b, NULL, 12) == LZMA_OK); + expect(lzma_index_file_size(a) == stream_size * 4 + 4 + 8 + 12); + + lzma_index_end(a, NULL); +} + + +static void +test_locate(void) +{ + lzma_index_record r; + lzma_index *i = lzma_index_init(NULL, NULL); + expect(i != NULL); + + // Cannot locate anything from an empty Index. + expect(lzma_index_locate(i, &r, 0)); + expect(lzma_index_locate(i, &r, 555)); + + // One empty Record: nothing is found since there's no uncompressed + // data. + expect(lzma_index_append(i, NULL, 16, 0) == LZMA_OK); + expect(lzma_index_locate(i, &r, 0)); + + // Non-empty Record and we can find something. + expect(lzma_index_append(i, NULL, 32, 5) == LZMA_OK); + expect(!lzma_index_locate(i, &r, 0)); + expect(r.total_size == 32); + expect(r.uncompressed_size == 5); + expect(r.stream_offset = LZMA_STREAM_HEADER_SIZE + 16); + expect(r.uncompressed_offset == 0); + + // Still cannot find anything past the end. + expect(lzma_index_locate(i, &r, 5)); + + // Add the third Record. + expect(lzma_index_append(i, NULL, 40, 11) == LZMA_OK); + + expect(!lzma_index_locate(i, &r, 0)); + expect(r.total_size == 32); + expect(r.uncompressed_size == 5); + expect(r.stream_offset = LZMA_STREAM_HEADER_SIZE + 16); + expect(r.uncompressed_offset == 0); + + expect(!lzma_index_read(i, &r)); + expect(r.total_size == 40); + expect(r.uncompressed_size == 11); + expect(r.stream_offset = LZMA_STREAM_HEADER_SIZE + 16 + 32); + expect(r.uncompressed_offset == 5); + + expect(!lzma_index_locate(i, &r, 2)); + expect(r.total_size == 32); + expect(r.uncompressed_size == 5); + expect(r.stream_offset = LZMA_STREAM_HEADER_SIZE + 16); + expect(r.uncompressed_offset == 0); + + expect(!lzma_index_locate(i, &r, 5)); + expect(r.total_size == 40); + expect(r.uncompressed_size == 11); + expect(r.stream_offset = LZMA_STREAM_HEADER_SIZE + 16 + 32); + expect(r.uncompressed_offset == 5); + + expect(!lzma_index_locate(i, &r, 5 + 11 - 1)); + expect(r.total_size == 40); + expect(r.uncompressed_size == 11); + expect(r.stream_offset = LZMA_STREAM_HEADER_SIZE + 16 + 32); + expect(r.uncompressed_offset == 5); + + expect(lzma_index_locate(i, &r, 5 + 11)); + expect(lzma_index_locate(i, &r, 5 + 15)); + + // Large Index + i = lzma_index_init(i, NULL); + expect(i != NULL); + + for (size_t n = 4; n <= 4 * 5555; n += 4) + expect(lzma_index_append(i, NULL, n + 8, n) == LZMA_OK); + + expect(lzma_index_count(i) == 5555); + + // First Record + expect(!lzma_index_locate(i, &r, 0)); + expect(r.total_size == 4 + 8); + expect(r.uncompressed_size == 4); + expect(r.stream_offset = LZMA_STREAM_HEADER_SIZE); + expect(r.uncompressed_offset == 0); + + expect(!lzma_index_locate(i, &r, 3)); + expect(r.total_size == 4 + 8); + expect(r.uncompressed_size == 4); + expect(r.stream_offset = LZMA_STREAM_HEADER_SIZE); + expect(r.uncompressed_offset == 0); + + // Second Record + expect(!lzma_index_locate(i, &r, 4)); + expect(r.total_size == 2 * 4 + 8); + expect(r.uncompressed_size == 2 * 4); + expect(r.stream_offset = LZMA_STREAM_HEADER_SIZE + 4 + 8); + expect(r.uncompressed_offset == 4); + + // Last Record + expect(!lzma_index_locate(i, &r, lzma_index_uncompressed_size(i) - 1)); + expect(r.total_size == 4 * 5555 + 8); + expect(r.uncompressed_size == 4 * 5555); + expect(r.stream_offset = lzma_index_total_size(i) + + LZMA_STREAM_HEADER_SIZE - 4 * 5555 - 8); + expect(r.uncompressed_offset + == lzma_index_uncompressed_size(i) - 4 * 5555); + + // Allocation chunk boundaries. See INDEX_GROUP_SIZE in + // liblzma/common/index.c. + const size_t group_multiple = 256 * 4; + const size_t radius = 8; + const size_t start = group_multiple - radius; + lzma_vli ubase = 0; + lzma_vli tbase = 0; + size_t n; + for (n = 1; n < start; ++n) { + ubase += n * 4; + tbase += n * 4 + 8; + } + + while (n < start + 2 * radius) { + expect(!lzma_index_locate(i, &r, ubase + n * 4)); + + expect(r.stream_offset == tbase + n * 4 + 8 + + LZMA_STREAM_HEADER_SIZE); + expect(r.uncompressed_offset == ubase + n * 4); + + tbase += n * 4 + 8; + ubase += n * 4; + ++n; + + expect(r.total_size == n * 4 + 8); + expect(r.uncompressed_size == n * 4); + } + + // Do it also backwards since lzma_index_locate() uses relative search. + while (n > start) { + expect(!lzma_index_locate(i, &r, ubase + (n - 1) * 4)); + + expect(r.total_size == n * 4 + 8); + expect(r.uncompressed_size == n * 4); + + --n; + tbase -= n * 4 + 8; + ubase -= n * 4; + + expect(r.stream_offset == tbase + n * 4 + 8 + + LZMA_STREAM_HEADER_SIZE); + expect(r.uncompressed_offset == ubase + n * 4); + } + + // Test locating in concatend Index. + i = lzma_index_init(i, NULL); + expect(i != NULL); + for (n = 0; n < group_multiple; ++n) + expect(lzma_index_append(i, NULL, 8, 0) == LZMA_OK); + expect(lzma_index_append(i, NULL, 16, 1) == LZMA_OK); + expect(!lzma_index_locate(i, &r, 0)); + expect(r.total_size == 16); + expect(r.uncompressed_size == 1); + expect(r.stream_offset + == LZMA_STREAM_HEADER_SIZE + group_multiple * 8); + expect(r.uncompressed_offset == 0); + + lzma_index_end(i, NULL); +} + + +static void +test_corrupt(void) +{ + const size_t alloc_size = 128 * 1024; + uint8_t *buf = malloc(alloc_size); + expect(buf != NULL); + lzma_stream strm = LZMA_STREAM_INIT; + + lzma_index *i = create_empty(); + expect(lzma_index_append(i, NULL, 7, 1) == LZMA_OK); + expect(lzma_index_encoder(&strm, i) == LZMA_OK); + succeed(coder_loop(&strm, NULL, 0, buf, 2, LZMA_PROG_ERROR, LZMA_RUN)); + lzma_index_end(i, NULL); + + i = create_empty(); + expect(lzma_index_append(i, NULL, 0, 1) == LZMA_OK); + expect(lzma_index_encoder(&strm, i) == LZMA_OK); + succeed(coder_loop(&strm, NULL, 0, buf, 2, LZMA_PROG_ERROR, LZMA_RUN)); + lzma_index_end(i, NULL); + + // Create a valid Index and corrupt it in different ways. + i = create_small(); + expect(lzma_index_encoder(&strm, i) == LZMA_OK); + succeed(coder_loop(&strm, NULL, 0, buf, 20, + LZMA_STREAM_END, LZMA_RUN)); + lzma_index_end(i, NULL); + + // Wrong Index Indicator + buf[0] ^= 1; + expect(lzma_index_decoder(&strm, &i) == LZMA_OK); + succeed(decoder_loop_ret(&strm, buf, 1, LZMA_DATA_ERROR)); + buf[0] ^= 1; + + // Wrong Number of Records and thus CRC32 fails. + --buf[1]; + expect(lzma_index_decoder(&strm, &i) == LZMA_OK); + succeed(decoder_loop_ret(&strm, buf, 10, LZMA_DATA_ERROR)); + ++buf[1]; + + // Padding not NULs + buf[15] ^= 1; + expect(lzma_index_decoder(&strm, &i) == LZMA_OK); + succeed(decoder_loop_ret(&strm, buf, 16, LZMA_DATA_ERROR)); + + lzma_end(&strm); + free(buf); +} + + int main(void) { - lzma_index my_index[3] = { - { 22, 33, my_index + 1 }, - { 44, 55, my_index + 2 }, - { 66, 77, NULL }, - }; + lzma_init(); - lzma_index *i = lzma_index_dup(my_index, NULL); - expect(i != NULL); + test_equal(); + + test_overflow(); + + lzma_index *i = create_empty(); + test_many(i); + lzma_index_end(i, NULL); + + i = create_small(); + test_many(i); + lzma_index_end(i, NULL); + + i = create_big(); + test_many(i); + lzma_index_end(i, NULL); - expect(lzma_index_is_equal(my_index, i)); + test_cat(); - i->next->next->uncompressed_size = 99; - expect(!lzma_index_is_equal(my_index, i)); + test_locate(); - lzma_index_free(i, NULL); + test_corrupt(); return 0; } diff --git a/tests/test_info.c b/tests/test_info.c deleted file mode 100644 index 0de95431..00000000 --- a/tests/test_info.c +++ /dev/null @@ -1,717 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// -/// \file test_info.c -/// \brief Tests functions handling the lzma_info structure -// -// 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_info *info = NULL; -static lzma_info_iter iter; - -static lzma_vli stream_start = 0; -static lzma_vli header_metadata_size = LZMA_VLI_VALUE_UNKNOWN; -static lzma_vli total_size = LZMA_VLI_VALUE_UNKNOWN; -static lzma_vli uncompressed_size = LZMA_VLI_VALUE_UNKNOWN; -static lzma_vli footer_metadata_size = LZMA_VLI_VALUE_UNKNOWN; - -static lzma_index my_index[3] = { - { 22, 33, my_index + 1 }, - { 44, 55, my_index + 2 }, - { 66, 77, NULL }, -}; - -static lzma_metadata my_metadata = { - .header_metadata_size = 11, - .total_size = 22 + 44 + 66, - .uncompressed_size = 33 + 55 + 77, - .index = my_index, - .extra = NULL, -}; - - -static void -reset(void) -{ - expect(lzma_info_init(info, NULL) == info); - stream_start = 0; - header_metadata_size = LZMA_VLI_VALUE_UNKNOWN; - total_size = LZMA_VLI_VALUE_UNKNOWN; - uncompressed_size = LZMA_VLI_VALUE_UNKNOWN; - footer_metadata_size = LZMA_VLI_VALUE_UNKNOWN; -} - - -static void -validate(void) -{ - expect(lzma_info_size_get(info, LZMA_INFO_STREAM_START) - == stream_start); - expect(lzma_info_size_get(info, LZMA_INFO_HEADER_METADATA) - == header_metadata_size); - expect(lzma_info_size_get(info, LZMA_INFO_TOTAL) == total_size); - expect(lzma_info_size_get(info, LZMA_INFO_UNCOMPRESSED) - == uncompressed_size); - expect(lzma_info_size_get(info, LZMA_INFO_FOOTER_METADATA) - == footer_metadata_size); -} - - -static void -test1(void) -{ - // Basics - expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START, - stream_start = 1234) == LZMA_OK); - validate(); - expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, - header_metadata_size = 2345) == LZMA_OK); - validate(); - expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, total_size = 3456) - == LZMA_OK); - validate(); - expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, - uncompressed_size = 4567) == LZMA_OK); - validate(); - expect(lzma_info_size_set(info, LZMA_INFO_FOOTER_METADATA, - footer_metadata_size = 5432) == LZMA_OK); - validate(); - - // Not everything allow zero size - reset(); - expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START, - stream_start = 0) == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, - header_metadata_size = 0) == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, - uncompressed_size = 0) == LZMA_OK); - validate(); - - reset(); - expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, 0) - == LZMA_PROG_ERROR); - - reset(); - expect(lzma_info_size_set(info, LZMA_INFO_FOOTER_METADATA, 0) - == LZMA_PROG_ERROR); - - // Invalid sizes - reset(); - expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START, - LZMA_VLI_VALUE_MAX + 1) == LZMA_PROG_ERROR); - reset(); - expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, - LZMA_VLI_VALUE_MAX + 1) == LZMA_PROG_ERROR); - reset(); - expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, - LZMA_VLI_VALUE_MAX + 1) == LZMA_PROG_ERROR); - reset(); - expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, - LZMA_VLI_VALUE_MAX + 1) == LZMA_PROG_ERROR); - reset(); - expect(lzma_info_size_set(info, LZMA_INFO_FOOTER_METADATA, - LZMA_VLI_VALUE_MAX + 1) == LZMA_PROG_ERROR); - - reset(); -} - - -static bool -test2_helper(lzma_vli *num, lzma_info_size type) -{ - expect(lzma_info_size_set(info, type, *num = 1234) == LZMA_OK); - validate(); - const bool ret = lzma_info_size_set(info, type, 4321) != LZMA_OK; - reset(); - return ret; -} - - -static void -test2(void) -{ - // Excluding start offset of Stream, once a size has been set, - // trying to set some other known value fails. - expect(!test2_helper(&stream_start, LZMA_INFO_STREAM_START)); - expect(test2_helper(&header_metadata_size, LZMA_INFO_HEADER_METADATA)); - expect(test2_helper(&total_size, LZMA_INFO_TOTAL)); - expect(test2_helper(&uncompressed_size, LZMA_INFO_UNCOMPRESSED)); - expect(test2_helper(&footer_metadata_size, LZMA_INFO_FOOTER_METADATA)); -} - - -static void -test3_init(void) -{ - reset(); - lzma_info_iter_begin(info, &iter); - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); -} - - -static void -test3(void) -{ - // Setting the same sizes multiple times for the same Index Record - // is OK, but the values must always be the same. - test3_init(); - expect(lzma_info_index_count_get(info) == 1); - expect(lzma_info_iter_set(&iter, 1234, 2345) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 1); - expect(lzma_info_iter_set(&iter, 1234, 2345) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 1); - expect(lzma_info_iter_set(&iter, 1111, 2345) == LZMA_DATA_ERROR); - - // Cannot finish an empty Index. - test3_init(); - expect(lzma_info_index_finish(info) == LZMA_DATA_ERROR); - - test3_init(); - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 2); - expect(lzma_info_iter_set(&iter, 1234, 2345) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 2); - expect(lzma_info_index_finish(info) == LZMA_DATA_ERROR); - - test3_init(); - expect(lzma_info_iter_set(&iter, 1234, 2345) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 1); - expect(lzma_info_index_finish(info) == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, 1234) == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, 2345) - == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, 1111) - == LZMA_DATA_ERROR); - - test3_init(); - expect(lzma_info_iter_set(&iter, 1234, 2345) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 1); - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 2); - expect(lzma_info_iter_set(&iter, 4321, 5432) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 2); - expect(lzma_info_index_finish(info) == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, 1234 + 4321) - == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, 2345 + 5432) - == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, 1111) - == LZMA_DATA_ERROR); - - test3_init(); - expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, 1234 + 4321) - == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, 2345 + 5432) - == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, 1111) - == LZMA_DATA_ERROR); - expect(lzma_info_iter_set(&iter, 1234, 2345) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 1); - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 2); - expect(lzma_info_iter_set(&iter, 4321, 5432) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 2); - expect(lzma_info_index_finish(info) == LZMA_OK); - - test3_init(); - expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, 1000) == LZMA_OK); - expect(lzma_info_iter_set(&iter, 1001, 2001) == LZMA_DATA_ERROR); - - test3_init(); - expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, 2000) - == LZMA_OK); - expect(lzma_info_iter_set(&iter, 1001, 2001) == LZMA_DATA_ERROR); - - reset(); -} - - -static void -test4(void) -{ - // 4a - lzma_info_iter_begin(info, &iter); - expect(lzma_info_index_count_get(info) == 0); - - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.stream_offset == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.uncompressed_offset == 0); - expect(lzma_info_index_count_get(info) == 1); - - expect(lzma_info_iter_set(&iter, 22, 33) == LZMA_OK); - expect(iter.total_size == 22); - expect(iter.uncompressed_size == 33); - expect(iter.stream_offset == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.uncompressed_offset == 0); - expect(lzma_info_index_count_get(info) == 1); - - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.stream_offset == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.uncompressed_offset == 33); - - // 4b - reset(); - lzma_info_iter_begin(info, &iter); - expect(lzma_info_index_count_get(info) == 0); - expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START, 5) == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, 11) - == LZMA_OK); - - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11); - expect(iter.uncompressed_offset == 0); - expect(lzma_info_index_count_get(info) == 1); - - expect(lzma_info_iter_set(&iter, 22, 33) == LZMA_OK); - expect(iter.total_size == 22); - expect(iter.uncompressed_size == 33); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11); - expect(iter.uncompressed_offset == 0); - expect(lzma_info_index_count_get(info) == 1); - - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11 + 22); - expect(iter.uncompressed_offset == 33); - expect(lzma_info_index_count_get(info) == 2); - - expect(lzma_info_iter_set(&iter, 44, 55) == LZMA_OK); - expect(iter.total_size == 44); - expect(iter.uncompressed_size == 55); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11 + 22); - expect(iter.uncompressed_offset == 33); - expect(lzma_info_index_count_get(info) == 2); - - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE - + 11 + 22 + 44); - expect(iter.uncompressed_offset == 33 + 55); - expect(lzma_info_index_count_get(info) == 3); - - expect(lzma_info_iter_set(&iter, 66, 77) == LZMA_OK); - expect(iter.total_size == 66); - expect(iter.uncompressed_size == 77); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE - + 11 + 22 + 44); - expect(iter.uncompressed_offset == 33 + 55); - expect(lzma_info_index_count_get(info) == 3); - - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE - + 11 + 22 + 44 + 66); - expect(iter.uncompressed_offset == 33 + 55 + 77); - expect(lzma_info_index_count_get(info) == 4); - - expect(lzma_info_iter_set(&iter, 88, 99) == LZMA_OK); - expect(iter.total_size == 88); - expect(iter.uncompressed_size == 99); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE - + 11 + 22 + 44 + 66); - expect(iter.uncompressed_offset == 33 + 55 + 77); - expect(lzma_info_index_count_get(info) == 4); - - // 4c (continues from 4b) - lzma_info_iter_begin(info, &iter); - expect(lzma_info_index_count_get(info) == 4); - - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(iter.total_size == 22); - expect(iter.uncompressed_size == 33); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11); - expect(iter.uncompressed_offset == 0); - expect(lzma_info_index_count_get(info) == 4); - - expect(lzma_info_iter_set(&iter, 22, LZMA_VLI_VALUE_UNKNOWN) - == LZMA_OK); - expect(iter.total_size == 22); - expect(iter.uncompressed_size == 33); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11); - expect(iter.uncompressed_offset == 0); - expect(lzma_info_index_count_get(info) == 4); - - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(iter.total_size == 44); - expect(iter.uncompressed_size == 55); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11 + 22); - expect(iter.uncompressed_offset == 33); - expect(lzma_info_index_count_get(info) == 4); - - expect(lzma_info_iter_set(&iter, LZMA_VLI_VALUE_UNKNOWN, 55) - == LZMA_OK); - expect(iter.total_size == 44); - expect(iter.uncompressed_size == 55); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11 + 22); - expect(iter.uncompressed_offset == 33); - expect(lzma_info_index_count_get(info) == 4); - - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(iter.total_size == 66); - expect(iter.uncompressed_size == 77); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE - + 11 + 22 + 44); - expect(iter.uncompressed_offset == 33 + 55); - expect(lzma_info_index_count_get(info) == 4); - - expect(lzma_info_iter_set(&iter, LZMA_VLI_VALUE_UNKNOWN, - LZMA_VLI_VALUE_UNKNOWN) == LZMA_OK); - expect(iter.total_size == 66); - expect(iter.uncompressed_size == 77); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE - + 11 + 22 + 44); - expect(iter.uncompressed_offset == 33 + 55); - expect(lzma_info_index_count_get(info) == 4); - - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(iter.total_size == 88); - expect(iter.uncompressed_size == 99); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE - + 11 + 22 + 44 + 66); - expect(iter.uncompressed_offset == 33 + 55 + 77); - expect(lzma_info_index_count_get(info) == 4); - - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE - + 11 + 22 + 44 + 66 + 88); - expect(iter.uncompressed_offset == 33 + 55 + 77 + 99); - expect(lzma_info_index_count_get(info) == 5); - - expect(lzma_info_iter_set(&iter, 1234, LZMA_VLI_VALUE_UNKNOWN) - == LZMA_OK); - expect(iter.total_size == 1234); - expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE - + 11 + 22 + 44 + 66 + 88); - expect(iter.uncompressed_offset == 33 + 55 + 77 + 99); - expect(lzma_info_index_count_get(info) == 5); - - // Test 4d (continues from 4c) - lzma_info_iter_begin(info, &iter); - for (size_t i = 0; i < 4; ++i) - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(lzma_info_iter_set(&iter, 88, 99) == LZMA_OK); - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(iter.total_size == 1234); - expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE - + 11 + 22 + 44 + 66 + 88); - expect(iter.uncompressed_offset == 33 + 55 + 77 + 99); - expect(lzma_info_index_count_get(info) == 5); - - expect(lzma_info_iter_set(&iter, LZMA_VLI_VALUE_UNKNOWN, 4321) - == LZMA_OK); - expect(iter.total_size == 1234); - expect(iter.uncompressed_size == 4321); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE - + 11 + 22 + 44 + 66 + 88); - expect(iter.uncompressed_offset == 33 + 55 + 77 + 99); - expect(lzma_info_index_count_get(info) == 5); - - expect(lzma_info_index_finish(info) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 5); - - // Test 4e (continues from 4d) - lzma_info_iter_begin(info, &iter); - for (size_t i = 0; i < 5; ++i) - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(lzma_info_iter_set(&iter, 1234, 4321) == LZMA_OK); - expect(lzma_info_iter_next(&iter, NULL) == LZMA_DATA_ERROR); - - reset(); -} - - -static void -test5(void) -{ - lzma_index *i; - - expect(lzma_info_index_set(info, NULL, NULL, true) - == LZMA_PROG_ERROR); - - reset(); - expect(lzma_info_index_set(info, NULL, my_index, false) == LZMA_OK); - i = lzma_index_dup(my_index, NULL); - expect(i != NULL); - i->next->uncompressed_size = 99; - expect(lzma_info_index_set(info, NULL, i, true) == LZMA_DATA_ERROR); - - reset(); - expect(lzma_info_index_set(info, NULL, my_index, false) == LZMA_OK); - i = lzma_index_dup(my_index, NULL); - expect(i != NULL); - lzma_index_free(i->next->next, NULL); - i->next->next = NULL; - expect(lzma_info_index_set(info, NULL, i, true) == LZMA_DATA_ERROR); - - reset(); - expect(lzma_info_index_set(info, NULL, my_index, false) == LZMA_OK); - i = lzma_index_dup(my_index, NULL); - expect(i != NULL); - lzma_index_free(i->next->next, NULL); - i->next->next = lzma_index_dup(my_index, NULL); - expect(i->next->next != NULL); - expect(lzma_info_index_set(info, NULL, i, true) == LZMA_DATA_ERROR); - - reset(); - expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, - total_size = 22 + 44 + 66) == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, - uncompressed_size = 33 + 55 + 77) == LZMA_OK); - validate(); - expect(lzma_info_index_set(info, NULL, my_index, false) == LZMA_OK); - validate(); - - reset(); - expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, total_size = 77) - == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, - uncompressed_size = 33 + 55 + 77) == LZMA_OK); - validate(); - expect(lzma_info_index_set(info, NULL, my_index, false) - == LZMA_DATA_ERROR); - - reset(); - expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, - total_size = 22 + 44 + 66) == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, - uncompressed_size = 777777) == LZMA_OK); - validate(); - expect(lzma_info_index_set(info, NULL, my_index, false) - == LZMA_DATA_ERROR); - - reset(); -} - - -static void -test6(void) -{ - lzma_metadata metadata; - - // Same complete Metadata in both Header and Footer - expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, - my_metadata.header_metadata_size) == LZMA_OK); - expect(lzma_info_metadata_set(info, NULL, &my_metadata, true, false) - == LZMA_OK); - expect(lzma_info_metadata_set(info, NULL, &my_metadata, false, false) - == LZMA_OK); - - // Header Metadata is not present but Size of Header Metadata is - // still present in Footer. - reset(); - metadata = my_metadata; - metadata.header_metadata_size = 0; - expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, 0) - == LZMA_OK); - expect(lzma_info_metadata_set(info, NULL, &metadata, true, false) - == LZMA_OK); - expect(lzma_info_metadata_set(info, NULL, &my_metadata, false, false) - == LZMA_DATA_ERROR); - - // Header Metadata is present but Size of Header Metadata is missing - // from Footer. - reset(); - metadata = my_metadata; - metadata.header_metadata_size = 0; - expect(lzma_info_metadata_set(info, NULL, &my_metadata, true, false) - == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, - my_metadata.header_metadata_size) == LZMA_OK); - expect(lzma_info_metadata_set(info, NULL, &metadata, false, false) - == LZMA_DATA_ERROR); - - // Index missing - reset(); - metadata = my_metadata; - metadata.index = NULL; - expect(lzma_info_metadata_set(info, NULL, &metadata, true, false) - == LZMA_OK); - expect(lzma_info_metadata_set(info, NULL, &metadata, false, false) - == LZMA_DATA_ERROR); - - // Index in Header Metadata but not in Footer Metadata - reset(); - expect(lzma_info_metadata_set(info, NULL, &my_metadata, true, false) - == LZMA_OK); - expect(lzma_info_metadata_set(info, NULL, &metadata, false, false) - == LZMA_OK); - - // Index in Header Metadata but not in Footer Metadata but - // Total Size is missing from Footer. - reset(); - metadata.total_size = LZMA_VLI_VALUE_UNKNOWN; - expect(lzma_info_metadata_set(info, NULL, &my_metadata, true, false) - == LZMA_OK); - expect(lzma_info_metadata_set(info, NULL, &metadata, false, false) - == LZMA_DATA_ERROR); - - // Total Size doesn't match the Index - reset(); - metadata = my_metadata; - metadata.total_size = 9999; - expect(lzma_info_metadata_set(info, NULL, &metadata, true, false) - == LZMA_DATA_ERROR); - - // Uncompressed Size doesn't match the Index - reset(); - metadata = my_metadata; - metadata.uncompressed_size = 9999; - expect(lzma_info_metadata_set(info, NULL, &metadata, true, false) - == LZMA_DATA_ERROR); - - reset(); -} - - -static void -test7(void) -{ - // No info yet, so we cannot locate anything. - expect(lzma_info_metadata_locate(info, true) - == LZMA_VLI_VALUE_UNKNOWN); - expect(lzma_info_metadata_locate(info, false) - == LZMA_VLI_VALUE_UNKNOWN); - - // Setting the Stream start offset doesn't change this situation. - expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START, 5) == LZMA_OK); - expect(lzma_info_metadata_locate(info, true) - == LZMA_VLI_VALUE_UNKNOWN); - expect(lzma_info_metadata_locate(info, false) - == LZMA_VLI_VALUE_UNKNOWN); - - // Setting the Size of Header Metadata known allows us to locate - // the Header Metadata Block. - expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, 11) - == LZMA_OK); - expect(lzma_info_metadata_locate(info, true) - == 5 + LZMA_STREAM_HEADER_SIZE); - expect(lzma_info_metadata_locate(info, false) - == LZMA_VLI_VALUE_UNKNOWN); - - // Adding a Data Block. As long as Index is not Finished, we cannot - // locate Footer Metadata Block. - lzma_info_iter_begin(info, &iter); - expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK); - expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11); - expect(iter.uncompressed_offset == 0); - expect(lzma_info_iter_set(&iter, 22, 33) == LZMA_OK); - expect(lzma_info_metadata_locate(info, true) - == 5 + LZMA_STREAM_HEADER_SIZE); - expect(lzma_info_metadata_locate(info, false) - == LZMA_VLI_VALUE_UNKNOWN); - - // Once the Index is finished, we can locate Footer Metadata Block too. - expect(lzma_info_index_finish(info) == LZMA_OK); - expect(lzma_info_metadata_locate(info, true) - == 5 + LZMA_STREAM_HEADER_SIZE); - expect(lzma_info_metadata_locate(info, false) - == 5 + LZMA_STREAM_HEADER_SIZE + 11 + 22); - - // A retry of most of the above but now with unknown Size of Header - // Metadata Block, which makes locating Footer Metadata Block - // impossible. - reset(); - expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START, 5) == LZMA_OK); - expect(lzma_info_metadata_locate(info, true) - == LZMA_VLI_VALUE_UNKNOWN); - expect(lzma_info_metadata_locate(info, false) - == LZMA_VLI_VALUE_UNKNOWN); - - expect(lzma_info_index_set(info, NULL, my_index, false) == LZMA_OK); - expect(lzma_info_metadata_locate(info, true) - == LZMA_VLI_VALUE_UNKNOWN); - expect(lzma_info_metadata_locate(info, false) - == LZMA_VLI_VALUE_UNKNOWN); - - expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, 11) - == LZMA_OK); - expect(lzma_info_metadata_locate(info, true) - == 5 + LZMA_STREAM_HEADER_SIZE); - expect(lzma_info_metadata_locate(info, false) - == LZMA_STREAM_HEADER_SIZE + 5 + 11 + 22 + 44 + 66); - - reset(); -} - - -static void -test8(void) -{ - expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START, 5) == LZMA_OK); - expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, 11) - == LZMA_OK); - - lzma_info_iter_begin(info, &iter); - expect(lzma_info_iter_locate(&iter, NULL, 0, false) - == LZMA_DATA_ERROR); - expect(lzma_info_index_count_get(info) == 0); - - lzma_info_iter_begin(info, &iter); - expect(lzma_info_iter_locate(&iter, NULL, 0, true) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 1); - expect(lzma_info_iter_locate(&iter, NULL, 0, false) == LZMA_OK); - expect(lzma_info_index_count_get(info) == 1); - - // TODO -} - - -/* -static void -test9(void) -{ - // TODO Various integer overflow checks -} -*/ - - -int -main(void) -{ - lzma_init(); - - info = lzma_info_init(NULL, NULL); - if (info == NULL) - return 1; - - validate(); - - test1(); - test2(); - test3(); - test4(); - test5(); - test6(); - test7(); - test8(); - - lzma_info_free(info, NULL); - return 0; -} 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; } diff --git a/tests/tests.h b/tests/tests.h index c7b43fd0..89880552 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -21,13 +21,14 @@ #define LZMA_TESTS_H #include "sysdefs.h" +#include "integer.h" #include <stdio.h> #define memcrap(buf, size) memset(buf, 0xFD, size) #define expect(test) ((test) ? 0 : (fprintf(stderr, "%s:%u: %s\n", \ - __FILE__, __LINE__, #test), exit(1), 0)) + __FILE__, __LINE__, #test), abort(), 0)) #define succeed(test) expect(!(test)) @@ -71,6 +72,14 @@ lzma_ret_sym(lzma_ret ret) case LZMA_UNSUPPORTED_CHECK: str = "LZMA_UNSUPPORTED_CHECK"; break; + + case LZMA_FORMAT_ERROR: + str = "LZMA_FORMAT_ERROR"; + break; + + case LZMA_MEMLIMIT_ERROR: + str = "LZMA_MEMLIMIT_ERROR"; + break; } return str; @@ -121,8 +130,7 @@ coder_loop(lzma_stream *strm, uint8_t *in, size_t in_size, if (strm->total_in != in_size || strm->total_out != out_size) error = true; } else { - if (strm->total_in + 1 != in_size - || strm->total_out != out_size) + if (strm->total_in != in_size || strm->total_out != out_size) error = true; } |