ci: actually fail on FreeBSD
[xz/debian.git] / tests / test_check.c
blob9d51aa0ef054f9cd00198727b42edac4455b57e7
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 #ifdef 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);
92 static void
93 test_lzma_crc64(void)
95 // CRC64 can be disabled.
96 if (!lzma_check_is_supported(LZMA_CHECK_CRC64))
97 assert_skip("CRC64 support is disabled");
99 // If CRC64 is disabled then lzma_crc64() will be missing.
100 // Using an ifdef here avoids a linker error.
101 #ifdef HAVE_CHECK_CRC64
102 const uint64_t test_vector = 0x995DC9BBDF1939FA;
104 // Test 1
105 assert_uint_eq(lzma_crc64(test_string, sizeof(test_string), 0),
106 test_vector);
108 // Test 2
109 assert_uint_eq(lzma_crc64(test_unaligned + 3, sizeof(test_string), 0),
110 test_vector);
112 // Test 3
113 uint64_t crc = 0;
114 for (size_t i = 0; i < sizeof(test_string); ++i)
115 crc = lzma_crc64(test_string + i, 1, crc);
116 assert_uint_eq(crc, test_vector);
118 // Test 4: The CLMUL implementation works on 16-byte chunks.
119 // Test combination of different start and end alignments
120 // and also short buffer lengths where special handling is needed.
121 uint32_t seed = 29;
122 crc = 0x96E30D5184B7FA2C; // Random initial value
123 for (size_t start = 0; start < 32; ++start)
124 for (size_t size = 1; size < 256 - 32; ++size)
125 crc = lzma_crc64(get_random256(&seed), size, crc);
127 assert_uint_eq(crc, 0x23AB787177231C9F);
128 #endif
132 static void
133 test_lzma_supported_checks(void)
135 static const lzma_check expected_check_ids[] = {
136 LZMA_CHECK_NONE,
137 #ifdef HAVE_CHECK_CRC32
138 LZMA_CHECK_CRC32,
139 #endif
140 #ifdef HAVE_CHECK_CRC64
141 LZMA_CHECK_CRC64,
142 #endif
143 #ifdef HAVE_CHECK_SHA256
144 LZMA_CHECK_SHA256,
145 #endif
148 for (lzma_check i = 0; i <= LZMA_CHECK_ID_MAX + 1; i++) {
149 bool matched = false;
150 for (unsigned int j = 0; j < ARRAY_SIZE(expected_check_ids);
151 j++) {
152 if (expected_check_ids[j] == i) {
153 matched = true;
154 break;
158 if (matched)
159 assert_true(lzma_check_is_supported(i));
160 else
161 assert_false(lzma_check_is_supported(i));
166 static void
167 test_lzma_check_size(void)
169 // Expected check sizes taken from src/liblzma/api/lzma/check.h
170 static const uint32_t expected_check_sizes[] = {
171 0, 4, 4, 4, 8, 8, 8, 16, 16, 16,
172 32, 32, 32, 64, 64, 64
175 for (lzma_check i = 0; i < ARRAY_SIZE(expected_check_sizes); i++)
176 assert_uint_eq(expected_check_sizes[i], lzma_check_size(i));
178 assert_uint_eq(lzma_check_size(INVALID_LZMA_CHECK_ID), UINT32_MAX);
182 // Test the single threaded decoder for lzma_get_check
183 static void
184 test_lzma_get_check_st(void)
186 #ifndef HAVE_DECODERS
187 assert_skip("Decoder support disabled");
188 #else
189 const uint32_t flags = LZMA_TELL_ANY_CHECK |
190 LZMA_TELL_UNSUPPORTED_CHECK |
191 LZMA_TELL_NO_CHECK;
193 uint8_t outbuf[128];
194 lzma_stream strm = LZMA_STREAM_INIT;
196 // Test a file with no integrity check:
197 assert_lzma_ret(lzma_stream_decoder(&strm, TEST_CHECK_MEMLIMIT,
198 flags), LZMA_OK);
199 strm.next_in = no_check_xz_data;
200 strm.avail_in = no_check_size;
201 strm.next_out = outbuf;
202 strm.avail_out = sizeof(outbuf);
204 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_NO_CHECK);
205 assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_NONE);
206 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
208 // Test a file with an unsupported integrity check type:
209 assert_lzma_ret(lzma_stream_decoder(&strm, TEST_CHECK_MEMLIMIT,
210 flags), LZMA_OK);
211 strm.next_in = unsupported_check_xz_data;
212 strm.avail_in = unsupported_check_size;
213 strm.next_out = outbuf;
214 strm.avail_out = sizeof(outbuf);
216 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_UNSUPPORTED_CHECK);
217 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
219 // Test a file with CRC32 as the integrity check:
220 #ifdef HAVE_CHECK_CRC32
221 assert_lzma_ret(lzma_stream_decoder(&strm, TEST_CHECK_MEMLIMIT,
222 flags), LZMA_OK);
223 strm.next_in = crc32_xz_data;
224 strm.avail_in = crc32_size;
225 strm.next_out = outbuf;
226 strm.avail_out = sizeof(outbuf);
228 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_GET_CHECK);
229 assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_CRC32);
230 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
231 #endif
233 // Test a file with CRC64 as the integrity check:
234 #ifdef HAVE_CHECK_CRC64
235 assert_lzma_ret(lzma_stream_decoder(&strm, TEST_CHECK_MEMLIMIT,
236 flags), LZMA_OK);
237 strm.next_in = crc64_xz_data;
238 strm.avail_in = crc64_size;
239 strm.next_out = outbuf;
240 strm.avail_out = sizeof(outbuf);
242 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_GET_CHECK);
243 assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_CRC64);
244 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
245 #endif
247 // Test a file with SHA-256 as the integrity check:
248 #ifdef HAVE_CHECK_SHA256
249 assert_lzma_ret(lzma_stream_decoder(&strm, TEST_CHECK_MEMLIMIT,
250 flags), LZMA_OK);
251 strm.next_in = sha256_xz_data;
252 strm.avail_in = sha256_size;
253 strm.next_out = outbuf;
254 strm.avail_out = sizeof(outbuf);
256 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_GET_CHECK);
257 assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_SHA256);
258 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
259 #endif
261 lzma_end(&strm);
262 #endif
266 static void
267 test_lzma_get_check_mt(void)
269 #ifndef MYTHREAD_ENABLED
270 assert_skip("Threading support disabled");
271 #elif !defined(HAVE_DECODERS)
272 assert_skip("Decoder support disabled");
273 #else
274 const uint32_t flags = LZMA_TELL_ANY_CHECK |
275 LZMA_TELL_UNSUPPORTED_CHECK |
276 LZMA_TELL_NO_CHECK;
278 const lzma_mt options = {
279 .flags = flags,
280 .threads = 2,
281 .timeout = 0,
282 .memlimit_threading = TEST_CHECK_MEMLIMIT,
283 .memlimit_stop = TEST_CHECK_MEMLIMIT
286 uint8_t outbuf[128];
287 lzma_stream strm = LZMA_STREAM_INIT;
289 // Test a file with no integrity check:
290 assert_lzma_ret(lzma_stream_decoder_mt(&strm, &options), LZMA_OK);
291 strm.next_in = no_check_xz_data;
292 strm.avail_in = no_check_size;
293 strm.next_out = outbuf;
294 strm.avail_out = sizeof(outbuf);
296 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_NO_CHECK);
297 assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_NONE);
298 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
300 // Test a file with an unsupported integrity check type:
301 assert_lzma_ret(lzma_stream_decoder_mt(&strm, &options), LZMA_OK);
302 strm.next_in = unsupported_check_xz_data;
303 strm.avail_in = unsupported_check_size;
304 strm.next_out = outbuf;
305 strm.avail_out = sizeof(outbuf);
307 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_UNSUPPORTED_CHECK);
308 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
310 // Test a file with CRC32 as the integrity check:
311 #ifdef HAVE_CHECK_CRC32
312 assert_lzma_ret(lzma_stream_decoder_mt(&strm, &options), LZMA_OK);
313 strm.next_in = crc32_xz_data;
314 strm.avail_in = crc32_size;
315 strm.next_out = outbuf;
316 strm.avail_out = sizeof(outbuf);
318 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_GET_CHECK);
319 assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_CRC32);
320 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
321 #endif
323 // Test a file with CRC64 as the integrity check:
324 #ifdef HAVE_CHECK_CRC64
325 assert_lzma_ret(lzma_stream_decoder_mt(&strm, &options), LZMA_OK);
326 strm.next_in = crc64_xz_data;
327 strm.avail_in = crc64_size;
328 strm.next_out = outbuf;
329 strm.avail_out = sizeof(outbuf);
331 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_GET_CHECK);
332 assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_CRC64);
333 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
334 #endif
336 // Test a file with SHA-256 as the integrity check:
337 #ifdef HAVE_CHECK_SHA256
338 assert_lzma_ret(lzma_stream_decoder_mt(&strm,&options), LZMA_OK);
339 strm.next_in = sha256_xz_data;
340 strm.avail_in = sha256_size;
341 strm.next_out = outbuf;
342 strm.avail_out = sizeof(outbuf);
344 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_GET_CHECK);
345 assert_lzma_check(lzma_get_check(&strm), LZMA_CHECK_SHA256);
346 assert_lzma_ret(lzma_code(&strm, LZMA_RUN), LZMA_STREAM_END);
347 #endif
349 lzma_end(&strm);
350 #endif
354 extern int
355 main(int argc, char **argv)
357 tuktest_start(argc, argv);
359 no_check_xz_data = tuktest_file_from_srcdir(
360 "files/good-1-check-none.xz", &no_check_size);
362 unsupported_check_xz_data = tuktest_file_from_srcdir(
363 "files/unsupported-check.xz",
364 &unsupported_check_size);
366 #ifdef HAVE_CHECK_CRC32
367 crc32_xz_data = tuktest_file_from_srcdir(
368 "files/good-1-check-crc32.xz", &crc32_size);
369 #endif
371 #ifdef HAVE_CHECK_CRC64
372 crc64_xz_data = tuktest_file_from_srcdir(
373 "files/good-1-check-crc64.xz", &crc64_size);
374 #endif
376 #ifdef HAVE_CHECK_SHA256
377 sha256_xz_data = tuktest_file_from_srcdir(
378 "files/good-1-check-sha256.xz", &sha256_size);
379 #endif
381 tuktest_run(test_lzma_crc32);
382 tuktest_run(test_lzma_crc64);
383 tuktest_run(test_lzma_supported_checks);
384 tuktest_run(test_lzma_check_size);
385 tuktest_run(test_lzma_get_check_st);
386 tuktest_run(test_lzma_get_check_mt);
388 return tuktest_end();