CMake: Add empty lines
[xz/debian.git] / tests / test_check.c
blobf45ccdeb0df1ce3902b6ac200052d49eead647d9
1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file test_check.c
6 /// \brief Tests integrity checks
7 //
8 // Authors: Lasse Collin
9 // Jia Tan
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "tests.h"
14 #include "mythread.h"
17 // These must be specified as numbers so that the test works on EBCDIC
18 // systems too.
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;
39 #endif
41 #ifdef HAVE_CHECK_CRC64
42 static size_t crc64_size;
43 static uint8_t *crc64_xz_data;
44 #endif
46 #ifdef HAVE_CHECK_SHA256
47 static size_t sha256_size;
48 static uint8_t *sha256_xz_data;
49 #endif
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);
63 return buf;
65 #endif
68 static void
69 test_lzma_crc32(void)
71 // CRC32 is always enabled.
72 assert_true(lzma_check_is_supported(LZMA_CHECK_CRC32));
74 const uint32_t test_vector = 0xCBF43926;
76 // Test 1
77 assert_uint_eq(lzma_crc32(test_string, sizeof(test_string), 0),
78 test_vector);
80 // Test 2
81 assert_uint_eq(lzma_crc32(test_unaligned + 3, sizeof(test_string), 0),
82 test_vector);
84 // Test 3
85 uint32_t crc = 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.
92 uint32_t seed = 23;
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);
102 static void
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;
114 // Test 1
115 assert_uint_eq(lzma_crc64(test_string, sizeof(test_string), 0),
116 test_vector);
118 // Test 2
119 assert_uint_eq(lzma_crc64(test_unaligned + 3, sizeof(test_string), 0),
120 test_vector);
122 // Test 3
123 uint64_t crc = 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.
130 uint32_t seed = 29;
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);
137 #endif
141 static void
142 test_lzma_supported_checks(void)
144 static const lzma_check expected_check_ids[] = {
145 LZMA_CHECK_NONE,
146 #ifdef HAVE_CHECK_CRC32
147 LZMA_CHECK_CRC32,
148 #endif
149 #ifdef HAVE_CHECK_CRC64
150 LZMA_CHECK_CRC64,
151 #endif
152 #ifdef HAVE_CHECK_SHA256
153 LZMA_CHECK_SHA256,
154 #endif
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);
160 j++) {
161 if (expected_check_ids[j] == i) {
162 matched = true;
163 break;
167 if (matched)
168 assert_true(lzma_check_is_supported(i));
169 else
170 assert_false(lzma_check_is_supported(i));
175 static void
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
192 static void
193 test_lzma_get_check_st(void)
195 #ifndef HAVE_DECODERS
196 assert_skip("Decoder support disabled");
197 #else
198 const uint32_t flags = LZMA_TELL_ANY_CHECK |
199 LZMA_TELL_UNSUPPORTED_CHECK |
200 LZMA_TELL_NO_CHECK;
202 uint8_t outbuf[128];
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,
207 flags), LZMA_OK);
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,
219 flags), LZMA_OK);
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,
231 flags), LZMA_OK);
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);
240 #endif
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,
245 flags), LZMA_OK);
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);
254 #endif
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,
259 flags), LZMA_OK);
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);
268 #endif
270 lzma_end(&strm);
271 #endif
275 static void
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");
282 #else
283 const uint32_t flags = LZMA_TELL_ANY_CHECK |
284 LZMA_TELL_UNSUPPORTED_CHECK |
285 LZMA_TELL_NO_CHECK;
287 const lzma_mt options = {
288 .flags = flags,
289 .threads = 2,
290 .timeout = 0,
291 .memlimit_threading = TEST_CHECK_MEMLIMIT,
292 .memlimit_stop = TEST_CHECK_MEMLIMIT
295 uint8_t outbuf[128];
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);
330 #endif
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);
343 #endif
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);
356 #endif
358 lzma_end(&strm);
359 #endif
363 extern int
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);
378 #endif
380 #ifdef HAVE_CHECK_CRC64
381 crc64_xz_data = tuktest_file_from_srcdir(
382 "files/good-1-check-crc64.xz", &crc64_size);
383 #endif
385 #ifdef HAVE_CHECK_SHA256
386 sha256_xz_data = tuktest_file_from_srcdir(
387 "files/good-1-check-sha256.xz", &sha256_size);
388 #endif
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();