aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_check.c279
-rw-r--r--tests/tests.h23
2 files changed, 295 insertions, 7 deletions
diff --git a/tests/test_check.c b/tests/test_check.c
index 9d2d6094..fa1326d1 100644
--- a/tests/test_check.c
+++ b/tests/test_check.c
@@ -2,10 +2,9 @@
//
/// \file test_check.c
/// \brief Tests integrity checks
-///
-/// \todo Add SHA256
//
-// Author: Lasse Collin
+// Authors: Lasse Collin
+// Jia Tan
//
// This file has been put into the public domain.
// You can do whatever you want with this file.
@@ -23,9 +22,35 @@ static const uint8_t test_string[9] = { 49, 50, 51, 52, 53, 54, 55, 56, 57 };
static const uint8_t test_unaligned[12]
= { 120, 120, 120, 49, 50, 51, 52, 53, 54, 55, 56, 57 };
+// 2 MB is more than enough for the tests. Actually a tiny value would
+// work because we don't actually decompress the files, we only test
+// decoding of the Stream Header fields.
+#define TEST_CHECK_MEMLIMIT (2U << 20)
+
+static size_t no_check_size;
+static uint8_t *no_check_xz_data;
+
+static size_t unsupported_check_size;
+static uint8_t *unsupported_check_xz_data;
+
+#ifdef HAVE_CHECK_CRC32
+static size_t crc32_size;
+static uint8_t *crc32_xz_data;
+#endif
+
+#ifdef HAVE_CHECK_CRC64
+static size_t crc64_size;
+static uint8_t *crc64_xz_data;
+#endif
+
+#ifdef HAVE_CHECK_SHA256
+static size_t sha256_size;
+static uint8_t *sha256_xz_data;
+#endif
+
static void
-test_crc32(void)
+test_lzma_crc32(void)
{
// CRC32 is always enabled.
assert_true(lzma_check_is_supported(LZMA_CHECK_CRC32));
@@ -49,7 +74,7 @@ test_crc32(void)
static void
-test_crc64(void)
+test_lzma_crc64(void)
{
// CRC64 can be disabled.
if (!lzma_check_is_supported(LZMA_CHECK_CRC64))
@@ -77,11 +102,251 @@ test_crc64(void)
}
+static void
+test_lzma_supported_checks(void)
+{
+ static const int expected_check_ids[] = {
+ LZMA_CHECK_NONE,
+#ifdef HAVE_CHECK_CRC32
+ LZMA_CHECK_CRC32,
+#endif
+#ifdef HAVE_CHECK_CRC64
+ LZMA_CHECK_CRC64,
+#endif
+#ifdef HAVE_CHECK_SHA256
+ LZMA_CHECK_SHA256,
+#endif
+ };
+
+ for (int i = 0; i <= LZMA_CHECK_ID_MAX + 1; i++) {
+ bool matched = false;
+ for (unsigned int j = 0; j < ARRAY_SIZE(expected_check_ids);
+ j++) {
+ if (expected_check_ids[j] == i) {
+ matched = true;
+ break;
+ }
+ }
+
+ if (matched)
+ assert_true(lzma_check_is_supported(i));
+ else
+ assert_false(lzma_check_is_supported(i));
+ }
+}
+
+
+static void
+test_lzma_check_size(void)
+{
+ // Expected check sizes taken from src/liblzma/api/lzma/check.h
+ static const uint32_t expected_check_sizes[] = {
+ 0, 4, 4, 4, 8, 8, 8, 16, 16, 16,
+ 32, 32, 32, 64, 64, 64
+ };
+
+ for (unsigned int i = 0; i < ARRAY_SIZE(expected_check_sizes); i++)
+ assert_uint_eq(expected_check_sizes[i], lzma_check_size(i));
+
+ assert_uint_eq(lzma_check_size(LZMA_CHECK_ID_MAX + 1), UINT32_MAX);
+}
+
+
+// Test the single threaded decoder for lzma_get_check
+static void
+test_lzma_get_check_st(void)
+{
+ const uint32_t flags = LZMA_TELL_ANY_CHECK |
+ LZMA_TELL_UNSUPPORTED_CHECK |
+ LZMA_TELL_NO_CHECK;
+
+ uint8_t outbuf[128];
+ lzma_stream strm = LZMA_STREAM_INIT;
+
+ // Test a file with no integrity check:
+ assert_lzma_ret(lzma_stream_decoder(&strm, TEST_CHECK_MEMLIMIT,
+ flags), LZMA_OK);
+ strm.next_in = no_check_xz_data;
+ strm.avail_in = no_check_size;
+ strm.next_out = outbuf;
+ strm.avail_out = sizeof(outbuf);
+
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_NO_CHECK);
+ assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_NONE);
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
+
+ // Test a file with an unsupported integrity check type:
+ assert_lzma_ret(lzma_stream_decoder(&strm, TEST_CHECK_MEMLIMIT,
+ flags), LZMA_OK);
+ strm.next_in = unsupported_check_xz_data;
+ strm.avail_in = unsupported_check_size;
+ strm.next_out = outbuf;
+ strm.avail_out = sizeof(outbuf);
+
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_UNSUPPORTED_CHECK);
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
+
+ // Test a file with CRC32 as the integrity check:
+#ifdef HAVE_CHECK_CRC32
+ assert_lzma_ret(lzma_stream_decoder(&strm, TEST_CHECK_MEMLIMIT,
+ flags), LZMA_OK);
+ strm.next_in = crc32_xz_data;
+ strm.avail_in = crc32_size;
+ strm.next_out = outbuf;
+ strm.avail_out = sizeof(outbuf);
+
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_GET_CHECK);
+ assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_CRC32);
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
+#endif
+
+ // Test a file with CRC64 as the integrity check:
+#ifdef HAVE_CHECK_CRC64
+ assert_lzma_ret(lzma_stream_decoder(&strm, TEST_CHECK_MEMLIMIT,
+ flags), LZMA_OK);
+ strm.next_in = crc64_xz_data;
+ strm.avail_in = crc64_size;
+ strm.next_out = outbuf;
+ strm.avail_out = sizeof(outbuf);
+
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_GET_CHECK);
+ assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_CRC64);
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
+#endif
+
+ // Test a file with SHA-256 as the integrity check:
+#ifdef HAVE_CHECK_SHA256
+ assert_lzma_ret(lzma_stream_decoder(&strm, TEST_CHECK_MEMLIMIT,
+ flags), LZMA_OK);
+ strm.next_in = sha256_xz_data;
+ strm.avail_in = sha256_size;
+ strm.next_out = outbuf;
+ strm.avail_out = sizeof(outbuf);
+
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_GET_CHECK);
+ assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_SHA256);
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
+#endif
+
+ lzma_end(&strm);
+}
+
+
+static void
+test_lzma_get_check_mt(void)
+{
+ const uint32_t flags = LZMA_TELL_ANY_CHECK |
+ LZMA_TELL_UNSUPPORTED_CHECK |
+ LZMA_TELL_NO_CHECK;
+
+ const lzma_mt options = {
+ .flags = flags,
+ .threads = 2,
+ .timeout = 0,
+ .memlimit_threading = TEST_CHECK_MEMLIMIT,
+ .memlimit_stop = TEST_CHECK_MEMLIMIT
+ };
+
+ uint8_t outbuf[128];
+ lzma_stream strm = LZMA_STREAM_INIT;
+
+ // Test a file with no integrity check:
+ assert_lzma_ret(lzma_stream_decoder_mt(&strm, &options), LZMA_OK);
+ strm.next_in = no_check_xz_data;
+ strm.avail_in = no_check_size;
+ strm.next_out = outbuf;
+ strm.avail_out = sizeof(outbuf);
+
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_NO_CHECK);
+ assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_NONE);
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
+
+ // Test a file with an unsupported integrity check type:
+ assert_lzma_ret(lzma_stream_decoder_mt(&strm, &options), LZMA_OK);
+ strm.next_in = unsupported_check_xz_data;
+ strm.avail_in = unsupported_check_size;
+ strm.next_out = outbuf;
+ strm.avail_out = sizeof(outbuf);
+
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_UNSUPPORTED_CHECK);
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
+
+ // Test a file with CRC32 as the integrity check:
+#ifdef HAVE_CHECK_CRC32
+ assert_lzma_ret(lzma_stream_decoder_mt(&strm, &options), LZMA_OK);
+ strm.next_in = crc32_xz_data;
+ strm.avail_in = crc32_size;
+ strm.next_out = outbuf;
+ strm.avail_out = sizeof(outbuf);
+
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_GET_CHECK);
+ assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_CRC32);
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
+#endif
+
+ // Test a file with CRC64 as the integrity check:
+#ifdef HAVE_CHECK_CRC64
+ assert_lzma_ret(lzma_stream_decoder_mt(&strm, &options), LZMA_OK);
+ strm.next_in = crc64_xz_data;
+ strm.avail_in = crc64_size;
+ strm.next_out = outbuf;
+ strm.avail_out = sizeof(outbuf);
+
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_GET_CHECK);
+ assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_CRC64);
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
+#endif
+
+ // Test a file with SHA-256 as the integrity check:
+#ifdef HAVE_CHECK_SHA256
+ assert_lzma_ret(lzma_stream_decoder_mt(&strm,&options), LZMA_OK);
+ strm.next_in = sha256_xz_data;
+ strm.avail_in = sha256_size;
+ strm.next_out = outbuf;
+ strm.avail_out = sizeof(outbuf);
+
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_GET_CHECK);
+ assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_SHA256);
+ assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
+#endif
+
+ lzma_end(&strm);
+}
+
+
extern int
main(int argc, char **argv)
{
tuktest_start(argc, argv);
- tuktest_run(test_crc32);
- tuktest_run(test_crc64);
+
+ no_check_xz_data = tuktest_file_from_srcdir(
+ "files/good-1-check-none.xz", &no_check_size);
+
+ unsupported_check_xz_data = tuktest_file_from_srcdir(
+ "files/unsupported-check.xz",
+ &unsupported_check_size);
+
+#ifdef HAVE_CHECK_CRC32
+ crc32_xz_data = tuktest_file_from_srcdir(
+ "files/good-1-check-crc32.xz", &crc32_size);
+#endif
+
+#ifdef HAVE_CHECK_CRC64
+ crc64_xz_data = tuktest_file_from_srcdir(
+ "files/good-1-check-crc64.xz", &crc64_size);
+#endif
+
+#ifdef HAVE_CHECK_SHA256
+ sha256_xz_data = tuktest_file_from_srcdir(
+ "files/good-1-check-sha256.xz", &sha256_size);
+#endif
+
+ tuktest_run(test_lzma_crc32);
+ tuktest_run(test_lzma_crc64);
+ tuktest_run(test_lzma_supported_checks);
+ tuktest_run(test_lzma_check_size);
+ tuktest_run(test_lzma_get_check_st);
+ tuktest_run(test_lzma_get_check_mt);
+
return tuktest_end();
}
diff --git a/tests/tests.h b/tests/tests.h
index b76a448e..4d6169b0 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -53,6 +53,29 @@ static const char enum_strings_lzma_ret[][24] = {
assert_enum_eq(test_expr, ref_val, enum_strings_lzma_ret)
+static const char enum_strings_lzma_check[][24] = {
+ "LZMA_CHECK_NONE",
+ "LZMA_CHECK_CRC32",
+ "LZMA_CHECK_UNKNOWN_2",
+ "LZMA_CHECK_UNKNOWN_3",
+ "LZMA_CHECK_CRC64",
+ "LZMA_CHECK_UNKNOWN_5",
+ "LZMA_CHECK_UNKNOWN_6",
+ "LZMA_CHECK_UNKNOWN_7",
+ "LZMA_CHECK_UNKNOWN_8",
+ "LZMA_CHECK_UNKNOWN_9",
+ "LZMA_CHECK_SHA256",
+ "LZMA_CHECK_UNKNOWN_11",
+ "LZMA_CHECK_UNKNOWN_12",
+ "LZMA_CHECK_UNKNOWN_13",
+ "LZMA_CHECK_UNKNOWN_14",
+ "LZMA_CHECK_UNKNOWN_15",
+};
+
+#define assert_lzma_check(test_expr, ref_val) \
+ assert_enum_eq(test_expr, ref_val, enum_strings_lzma_check)
+
+
static inline bool
coder_loop(lzma_stream *strm, uint8_t *in, size_t in_size,
uint8_t *out, size_t out_size,