1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
6 /// \brief Tests integrity checks
8 // Authors: Lasse Collin
11 ///////////////////////////////////////////////////////////////////////////////
17 // These must be specified as numbers so that the test works on EBCDIC
19 // static const uint8_t test_string[9] = "123456789";
20 // static const uint8_t test_unaligned[12] = "xxx123456789";
21 static const uint8_t test_string
[9] = { 49, 50, 51, 52, 53, 54, 55, 56, 57 };
22 static const uint8_t test_unaligned
[12]
23 = { 120, 120, 120, 49, 50, 51, 52, 53, 54, 55, 56, 57 };
25 // 2 MB is more than enough for the tests. Actually a tiny value would
26 // work because we don't actually decompress the files, we only test
27 // decoding of the Stream Header fields.
28 #define TEST_CHECK_MEMLIMIT (2U << 20)
30 static size_t no_check_size
;
31 static uint8_t *no_check_xz_data
;
33 static size_t unsupported_check_size
;
34 static uint8_t *unsupported_check_xz_data
;
36 #ifdef HAVE_CHECK_CRC32
37 static size_t crc32_size
;
38 static uint8_t *crc32_xz_data
;
41 #ifdef HAVE_CHECK_CRC64
42 static size_t crc64_size
;
43 static uint8_t *crc64_xz_data
;
46 #ifdef HAVE_CHECK_SHA256
47 static size_t sha256_size
;
48 static uint8_t *sha256_xz_data
;
52 #if defined(HAVE_CHECK_CRC32) || defined(HAVE_CHECK_CRC64)
53 static const uint8_t *
54 get_random256(uint32_t *seed
)
56 static uint8_t buf
[256];
58 for (size_t i
= 0; i
< sizeof(buf
); ++i
) {
59 *seed
= *seed
* 1103515245 + 12345;
60 buf
[i
] = (uint8_t)(*seed
>> 22);
71 // CRC32 is always enabled.
72 assert_true(lzma_check_is_supported(LZMA_CHECK_CRC32
));
74 const uint32_t test_vector
= 0xCBF43926;
77 assert_uint_eq(lzma_crc32(test_string
, sizeof(test_string
), 0),
81 assert_uint_eq(lzma_crc32(test_unaligned
+ 3, sizeof(test_string
), 0),
86 for (size_t i
= 0; i
< sizeof(test_string
); ++i
)
87 crc
= lzma_crc32(test_string
+ i
, 1, crc
);
88 assert_uint_eq(crc
, test_vector
);
90 // Test 4: Test combination of different start and end alignments
91 // and different buffer lengths.
93 crc
= 0x760CD032; // Random initial value
94 for (size_t start
= 0; start
< 32; ++start
)
95 for (size_t size
= 1; size
< 256 - 32; ++size
)
96 crc
= lzma_crc32(get_random256(&seed
), size
, crc
);
98 assert_uint_eq(crc
, 0x924E35FD);
103 test_lzma_crc64(void)
105 // CRC64 can be disabled.
106 if (!lzma_check_is_supported(LZMA_CHECK_CRC64
))
107 assert_skip("CRC64 support is disabled");
109 // If CRC64 is disabled then lzma_crc64() will be missing.
110 // Using an ifdef here avoids a linker error.
111 #ifdef HAVE_CHECK_CRC64
112 const uint64_t test_vector
= 0x995DC9BBDF1939FA;
115 assert_uint_eq(lzma_crc64(test_string
, sizeof(test_string
), 0),
119 assert_uint_eq(lzma_crc64(test_unaligned
+ 3, sizeof(test_string
), 0),
124 for (size_t i
= 0; i
< sizeof(test_string
); ++i
)
125 crc
= lzma_crc64(test_string
+ i
, 1, crc
);
126 assert_uint_eq(crc
, test_vector
);
128 // Test 4: Test combination of different start and end alignments
129 // and different buffer lengths.
131 crc
= 0x96E30D5184B7FA2C; // Random initial value
132 for (size_t start
= 0; start
< 32; ++start
)
133 for (size_t size
= 1; size
< 256 - 32; ++size
)
134 crc
= lzma_crc64(get_random256(&seed
), size
, crc
);
136 assert_uint_eq(crc
, 0x23AB787177231C9F);
142 test_lzma_supported_checks(void)
144 static const lzma_check expected_check_ids
[] = {
146 #ifdef HAVE_CHECK_CRC32
149 #ifdef HAVE_CHECK_CRC64
152 #ifdef HAVE_CHECK_SHA256
157 for (lzma_check i
= 0; i
<= LZMA_CHECK_ID_MAX
+ 1; i
++) {
158 bool matched
= false;
159 for (unsigned int j
= 0; j
< ARRAY_SIZE(expected_check_ids
);
161 if (expected_check_ids
[j
] == i
) {
168 assert_true(lzma_check_is_supported(i
));
170 assert_false(lzma_check_is_supported(i
));
176 test_lzma_check_size(void)
178 // Expected check sizes taken from src/liblzma/api/lzma/check.h
179 static const uint32_t expected_check_sizes
[] = {
180 0, 4, 4, 4, 8, 8, 8, 16, 16, 16,
181 32, 32, 32, 64, 64, 64
184 for (lzma_check i
= 0; i
< ARRAY_SIZE(expected_check_sizes
); i
++)
185 assert_uint_eq(expected_check_sizes
[i
], lzma_check_size(i
));
187 assert_uint_eq(lzma_check_size(INVALID_LZMA_CHECK_ID
), UINT32_MAX
);
191 // Test the single threaded decoder for lzma_get_check
193 test_lzma_get_check_st(void)
195 #ifndef HAVE_DECODERS
196 assert_skip("Decoder support disabled");
198 const uint32_t flags
= LZMA_TELL_ANY_CHECK
|
199 LZMA_TELL_UNSUPPORTED_CHECK
|
203 lzma_stream strm
= LZMA_STREAM_INIT
;
205 // Test a file with no integrity check:
206 assert_lzma_ret(lzma_stream_decoder(&strm
, TEST_CHECK_MEMLIMIT
,
208 strm
.next_in
= no_check_xz_data
;
209 strm
.avail_in
= no_check_size
;
210 strm
.next_out
= outbuf
;
211 strm
.avail_out
= sizeof(outbuf
);
213 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_NO_CHECK
);
214 assert_lzma_check(lzma_get_check(&strm
), LZMA_CHECK_NONE
);
215 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_STREAM_END
);
217 // Test a file with an unsupported integrity check type:
218 assert_lzma_ret(lzma_stream_decoder(&strm
, TEST_CHECK_MEMLIMIT
,
220 strm
.next_in
= unsupported_check_xz_data
;
221 strm
.avail_in
= unsupported_check_size
;
222 strm
.next_out
= outbuf
;
223 strm
.avail_out
= sizeof(outbuf
);
225 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_UNSUPPORTED_CHECK
);
226 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_STREAM_END
);
228 // Test a file with CRC32 as the integrity check:
229 #ifdef HAVE_CHECK_CRC32
230 assert_lzma_ret(lzma_stream_decoder(&strm
, TEST_CHECK_MEMLIMIT
,
232 strm
.next_in
= crc32_xz_data
;
233 strm
.avail_in
= crc32_size
;
234 strm
.next_out
= outbuf
;
235 strm
.avail_out
= sizeof(outbuf
);
237 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_GET_CHECK
);
238 assert_lzma_check(lzma_get_check(&strm
), LZMA_CHECK_CRC32
);
239 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_STREAM_END
);
242 // Test a file with CRC64 as the integrity check:
243 #ifdef HAVE_CHECK_CRC64
244 assert_lzma_ret(lzma_stream_decoder(&strm
, TEST_CHECK_MEMLIMIT
,
246 strm
.next_in
= crc64_xz_data
;
247 strm
.avail_in
= crc64_size
;
248 strm
.next_out
= outbuf
;
249 strm
.avail_out
= sizeof(outbuf
);
251 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_GET_CHECK
);
252 assert_lzma_check(lzma_get_check(&strm
), LZMA_CHECK_CRC64
);
253 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_STREAM_END
);
256 // Test a file with SHA-256 as the integrity check:
257 #ifdef HAVE_CHECK_SHA256
258 assert_lzma_ret(lzma_stream_decoder(&strm
, TEST_CHECK_MEMLIMIT
,
260 strm
.next_in
= sha256_xz_data
;
261 strm
.avail_in
= sha256_size
;
262 strm
.next_out
= outbuf
;
263 strm
.avail_out
= sizeof(outbuf
);
265 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_GET_CHECK
);
266 assert_lzma_check(lzma_get_check(&strm
), LZMA_CHECK_SHA256
);
267 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_STREAM_END
);
276 test_lzma_get_check_mt(void)
278 #ifndef MYTHREAD_ENABLED
279 assert_skip("Threading support disabled");
280 #elif !defined(HAVE_DECODERS)
281 assert_skip("Decoder support disabled");
283 const uint32_t flags
= LZMA_TELL_ANY_CHECK
|
284 LZMA_TELL_UNSUPPORTED_CHECK
|
287 const lzma_mt options
= {
291 .memlimit_threading
= TEST_CHECK_MEMLIMIT
,
292 .memlimit_stop
= TEST_CHECK_MEMLIMIT
296 lzma_stream strm
= LZMA_STREAM_INIT
;
298 // Test a file with no integrity check:
299 assert_lzma_ret(lzma_stream_decoder_mt(&strm
, &options
), LZMA_OK
);
300 strm
.next_in
= no_check_xz_data
;
301 strm
.avail_in
= no_check_size
;
302 strm
.next_out
= outbuf
;
303 strm
.avail_out
= sizeof(outbuf
);
305 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_NO_CHECK
);
306 assert_lzma_check(lzma_get_check(&strm
), LZMA_CHECK_NONE
);
307 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_STREAM_END
);
309 // Test a file with an unsupported integrity check type:
310 assert_lzma_ret(lzma_stream_decoder_mt(&strm
, &options
), LZMA_OK
);
311 strm
.next_in
= unsupported_check_xz_data
;
312 strm
.avail_in
= unsupported_check_size
;
313 strm
.next_out
= outbuf
;
314 strm
.avail_out
= sizeof(outbuf
);
316 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_UNSUPPORTED_CHECK
);
317 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_STREAM_END
);
319 // Test a file with CRC32 as the integrity check:
320 #ifdef HAVE_CHECK_CRC32
321 assert_lzma_ret(lzma_stream_decoder_mt(&strm
, &options
), LZMA_OK
);
322 strm
.next_in
= crc32_xz_data
;
323 strm
.avail_in
= crc32_size
;
324 strm
.next_out
= outbuf
;
325 strm
.avail_out
= sizeof(outbuf
);
327 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_GET_CHECK
);
328 assert_lzma_check(lzma_get_check(&strm
), LZMA_CHECK_CRC32
);
329 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_STREAM_END
);
332 // Test a file with CRC64 as the integrity check:
333 #ifdef HAVE_CHECK_CRC64
334 assert_lzma_ret(lzma_stream_decoder_mt(&strm
, &options
), LZMA_OK
);
335 strm
.next_in
= crc64_xz_data
;
336 strm
.avail_in
= crc64_size
;
337 strm
.next_out
= outbuf
;
338 strm
.avail_out
= sizeof(outbuf
);
340 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_GET_CHECK
);
341 assert_lzma_check(lzma_get_check(&strm
), LZMA_CHECK_CRC64
);
342 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_STREAM_END
);
345 // Test a file with SHA-256 as the integrity check:
346 #ifdef HAVE_CHECK_SHA256
347 assert_lzma_ret(lzma_stream_decoder_mt(&strm
,&options
), LZMA_OK
);
348 strm
.next_in
= sha256_xz_data
;
349 strm
.avail_in
= sha256_size
;
350 strm
.next_out
= outbuf
;
351 strm
.avail_out
= sizeof(outbuf
);
353 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_GET_CHECK
);
354 assert_lzma_check(lzma_get_check(&strm
), LZMA_CHECK_SHA256
);
355 assert_lzma_ret(lzma_code(&strm
, LZMA_RUN
), LZMA_STREAM_END
);
364 main(int argc
, char **argv
)
366 tuktest_start(argc
, argv
);
368 no_check_xz_data
= tuktest_file_from_srcdir(
369 "files/good-1-check-none.xz", &no_check_size
);
371 unsupported_check_xz_data
= tuktest_file_from_srcdir(
372 "files/unsupported-check.xz",
373 &unsupported_check_size
);
375 #ifdef HAVE_CHECK_CRC32
376 crc32_xz_data
= tuktest_file_from_srcdir(
377 "files/good-1-check-crc32.xz", &crc32_size
);
380 #ifdef HAVE_CHECK_CRC64
381 crc64_xz_data
= tuktest_file_from_srcdir(
382 "files/good-1-check-crc64.xz", &crc64_size
);
385 #ifdef HAVE_CHECK_SHA256
386 sha256_xz_data
= tuktest_file_from_srcdir(
387 "files/good-1-check-sha256.xz", &sha256_size
);
390 tuktest_run(test_lzma_crc32
);
391 tuktest_run(test_lzma_crc64
);
392 tuktest_run(test_lzma_supported_checks
);
393 tuktest_run(test_lzma_check_size
);
394 tuktest_run(test_lzma_get_check_st
);
395 tuktest_run(test_lzma_get_check_mt
);
397 return tuktest_end();