From 54f4a4162aae8796580489013583d6148be5a473 Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Sat, 27 Apr 2024 15:13:39 +0300 Subject: [PATCH] Tests: test_index: Test lzma_index_buffer_decode() more (cherry picked from commit 4ad88b2544c2aaf8de8f38af54587098cbe66c1d) --- tests/test_index.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/tests/test_index.c b/tests/test_index.c index 4e10163a..ac3bd98a 100644 --- a/tests/test_index.c +++ b/tests/test_index.c @@ -1701,18 +1701,41 @@ test_lzma_index_buffer_decode(void) decode_buffer, &in_pos, 0), LZMA_DATA_ERROR); assert_true(idx == NULL); + in_pos = 1; + idx = idx_allocated; + assert_lzma_ret(lzma_index_buffer_decode(&idx, &memlimit, NULL, + decode_buffer, &in_pos, 0), LZMA_PROG_ERROR); + assert_true(idx == NULL); + + // Test too short input + in_pos = 0; + idx = idx_allocated; + assert_lzma_ret(lzma_index_buffer_decode(&idx, &memlimit, NULL, + decode_buffer, &in_pos, decode_buffer_size - 1), + LZMA_DATA_ERROR); + assert_true(idx == NULL); + lzma_index_end(idx_allocated, NULL); idx_allocated = NULL; - in_pos = 1; - assert_lzma_ret(lzma_index_buffer_decode(&idx, &memlimit, NULL, - decode_buffer, &in_pos, 0), LZMA_PROG_ERROR); - in_pos = 0; - // Test expected successful decode + in_pos = 0; assert_lzma_ret(lzma_index_buffer_decode(&idx, &memlimit, NULL, decode_buffer, &in_pos, decode_buffer_size), LZMA_OK); + assert_uint_eq(in_pos, decode_buffer_size); + assert_true(index_is_equal(decode_test_index, idx)); + + lzma_index_end(idx, NULL); + + // Test too much input. This won't read past + // the end of the allocated array (decode_buffer_size bytes). + in_pos = 0; + assert_lzma_ret(lzma_index_buffer_decode(&idx, &memlimit, NULL, + decode_buffer, &in_pos, decode_buffer_size + 16), + LZMA_OK); + + assert_uint_eq(in_pos, decode_buffer_size); assert_true(index_is_equal(decode_test_index, idx)); lzma_index_end(idx, NULL);