Tests: Fix memory leaks in test_index.
[xz/debian.git] / tests / test_index.c
blobe01919ee017095652af964748c678eef5d15a3f2
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file test_index.c
4 /// \brief Tests functions handling the lzma_index structure
5 ///
6 /// \todo Implement tests for lzma_file_info_decoder
7 //
8 // Authors: Jia Tan
9 // Lasse Collin
12 // This file has been put into the public domain.
13 // You can do whatever you want with this file.
15 ///////////////////////////////////////////////////////////////////////////////
17 #include "tests.h"
19 // liblzma internal header file needed for:
20 // UNPADDED_SIZE_MIN
21 // UNPADDED_SIZE_MAX
22 // vli_ceil4
23 #include "common/index.h"
26 #define MEMLIMIT (UINT64_C(1) << 20)
28 static uint8_t *decode_buffer;
29 static size_t decode_buffer_size = 0;
30 static lzma_index *decode_test_index;
33 static void
34 test_lzma_index_memusage(void)
36 // The return value from lzma_index_memusage is an approximation
37 // of the amount of memory needed for lzma_index for a given
38 // amount of Streams and Blocks. It will be an upperbound,
39 // so this test will mostly sanity check and error check the
40 // function.
42 // The maximum number of Streams should be UINT32_MAX in the
43 // current implementation even though the parameter is lzma_vli.
44 assert_uint_eq(lzma_index_memusage((lzma_vli)UINT32_MAX + 1, 1),
45 UINT64_MAX);
47 // The maximum number of Blocks should be LZMA_VLI_MAX
48 assert_uint_eq(lzma_index_memusage(1, LZMA_VLI_MAX), UINT64_MAX);
50 // Number of Streams must be non-zero
51 assert_uint_eq(lzma_index_memusage(0, 1), UINT64_MAX);
53 // Number of Blocks CAN be zero
54 assert_uint(lzma_index_memusage(1, 0), !=, UINT64_MAX);
56 // Arbitrary values for Stream and Block should work without error
57 // and should always increase
58 uint64_t previous = 1;
59 lzma_vli streams = 1;
60 lzma_vli blocks = 1;
62 // Test 100 different increasing values for Streams and Block
63 for (int i = 0; i < 100; i++) {
64 uint64_t current = lzma_index_memusage(streams, blocks);
65 assert_uint(current, >, previous);
66 previous = current;
67 streams += 29;
68 blocks += 107;
71 // Force integer overflow in calculation (should result in an error)
72 assert_uint_eq(lzma_index_memusage(UINT32_MAX, LZMA_VLI_MAX),
73 UINT64_MAX);
77 static void
78 test_lzma_index_memused(void)
80 // Very similar to test_lzma_index_memusage above since
81 // lzma_index_memused is essentially a wrapper for
82 // lzma_index_memusage
83 lzma_index *idx = lzma_index_init(NULL);
84 assert_true(idx != NULL);
86 // Test with empty Index
87 assert_uint(lzma_index_memused(idx), <, UINT64_MAX);
89 // Append small Blocks and then test again (should pass).
90 for (lzma_vli i = 0; i < 10; i++)
91 assert_lzma_ret(lzma_index_append(idx, NULL,
92 UNPADDED_SIZE_MIN, 1), LZMA_OK);
94 assert_uint(lzma_index_memused(idx), <, UINT64_MAX);
96 lzma_index_end(idx, NULL);
100 static void
101 test_lzma_index_append(void)
103 // Basic input-ouput test done here.
104 // Less trivial tests for this function are done throughout
105 // other tests.
107 // First test with NULL lzma_index
108 assert_lzma_ret(lzma_index_append(NULL, NULL, UNPADDED_SIZE_MIN,
109 1), LZMA_PROG_ERROR);
111 lzma_index *idx = lzma_index_init(NULL);
112 assert_true(idx != NULL);
114 // Test with invalid Unpadded Size
115 assert_lzma_ret(lzma_index_append(idx, NULL,
116 UNPADDED_SIZE_MIN - 1, 1), LZMA_PROG_ERROR);
117 assert_lzma_ret(lzma_index_append(idx, NULL,
118 UNPADDED_SIZE_MAX + 1, 1), LZMA_PROG_ERROR);
120 // Test with invalid Uncompressed Size
121 assert_lzma_ret(lzma_index_append(idx, NULL,
122 UNPADDED_SIZE_MAX, LZMA_VLI_MAX + 1),
123 LZMA_PROG_ERROR);
125 // Test expected successful Block appends
126 assert_lzma_ret(lzma_index_append(idx, NULL, UNPADDED_SIZE_MIN,
127 1), LZMA_OK);
128 assert_lzma_ret(lzma_index_append(idx, NULL,
129 UNPADDED_SIZE_MIN * 2,
130 2), LZMA_OK);
131 assert_lzma_ret(lzma_index_append(idx, NULL,
132 UNPADDED_SIZE_MIN * 3,
133 3), LZMA_OK);
135 lzma_index_end(idx, NULL);
137 // Test uncompressed .xz file size growing too large.
138 // Should result in LZMA_DATA_ERROR.
139 idx = lzma_index_init(NULL);
141 assert_lzma_ret(lzma_index_append(idx, NULL, UNPADDED_SIZE_MAX,
142 1), LZMA_DATA_ERROR);
144 // Test compressed size growing too large.
145 // Should result in LZMA_DATA_ERROR.
146 assert_lzma_ret(lzma_index_append(idx, NULL,
147 UNPADDED_SIZE_MIN, LZMA_VLI_MAX), LZMA_OK);
148 assert_lzma_ret(lzma_index_append(idx, NULL,
149 UNPADDED_SIZE_MIN, 1), LZMA_DATA_ERROR);
151 // Currently not testing for error case when the size of the Index
152 // grows too large to be stored. This was not practical to test for
153 // since too many Blocks needed to be created to cause this.
155 lzma_index_end(idx, NULL);
159 static void
160 test_lzma_index_stream_flags(void)
162 // Only trivial tests done here testing for basic functionality.
163 // More in-depth testing for this function will be done in
164 // test_lzma_index_checks.
166 // Testing for NULL inputs
167 assert_lzma_ret(lzma_index_stream_flags(NULL, NULL),
168 LZMA_PROG_ERROR);
170 lzma_index *idx = lzma_index_init(NULL);
171 assert_true(idx != NULL);
173 assert_lzma_ret(lzma_index_stream_flags(idx, NULL),
174 LZMA_PROG_ERROR);
176 lzma_stream_flags stream_flags = {
177 .version = 0,
178 .backward_size = LZMA_BACKWARD_SIZE_MIN,
179 .check = LZMA_CHECK_CRC32
182 assert_lzma_ret(lzma_index_stream_flags(idx, &stream_flags),
183 LZMA_OK);
185 lzma_index_end(idx, NULL);
189 static void
190 test_lzma_index_checks(void)
192 // Tests should still pass, even if some of the check types
193 // are disabled.
194 lzma_index *idx = lzma_index_init(NULL);
195 assert_true(idx != NULL);
197 lzma_stream_flags stream_flags = {
198 .version = 0,
199 .backward_size = LZMA_BACKWARD_SIZE_MIN,
200 .check = LZMA_CHECK_NONE
203 // First set the check type to None
204 assert_lzma_ret(lzma_index_stream_flags(idx, &stream_flags),
205 LZMA_OK);
206 assert_uint_eq(lzma_index_checks(idx),
207 UINT32_C(1) << LZMA_CHECK_NONE);
209 // Set the check type to CRC32 and repeat
210 stream_flags.check = LZMA_CHECK_CRC32;
211 assert_lzma_ret(lzma_index_stream_flags(idx, &stream_flags),
212 LZMA_OK);
213 assert_uint_eq(lzma_index_checks(idx),
214 UINT32_C(1) << LZMA_CHECK_CRC32);
216 // Set the check type to CRC64 and repeat
217 stream_flags.check = LZMA_CHECK_CRC64;
218 assert_lzma_ret(lzma_index_stream_flags(idx, &stream_flags),
219 LZMA_OK);
220 assert_uint_eq(lzma_index_checks(idx),
221 UINT32_C(1) << LZMA_CHECK_CRC64);
223 // Set the check type to SHA256 and repeat
224 stream_flags.check = LZMA_CHECK_SHA256;
225 assert_lzma_ret(lzma_index_stream_flags(idx, &stream_flags),
226 LZMA_OK);
227 assert_uint_eq(lzma_index_checks(idx),
228 UINT32_C(1) << LZMA_CHECK_SHA256);
230 // Create second lzma_index and cat to first
231 lzma_index *second = lzma_index_init(NULL);
232 assert_true(second != NULL);
234 // Set the check type to CRC32 for the second lzma_index
235 stream_flags.check = LZMA_CHECK_CRC32;
236 assert_lzma_ret(lzma_index_stream_flags(second, &stream_flags),
237 LZMA_OK);
239 assert_uint_eq(lzma_index_checks(second),
240 UINT32_C(1) << LZMA_CHECK_CRC32);
242 assert_lzma_ret(lzma_index_cat(idx, second, NULL), LZMA_OK);
244 // Index should now have both CRC32 and SHA256
245 assert_uint_eq(lzma_index_checks(idx),
246 (UINT32_C(1) << LZMA_CHECK_CRC32) |
247 (UINT32_C(1) << LZMA_CHECK_SHA256));
249 // Change the check type of the second Stream to SHA256
250 stream_flags.check = LZMA_CHECK_SHA256;
251 assert_lzma_ret(lzma_index_stream_flags(idx, &stream_flags),
252 LZMA_OK);
254 // Index should now have only SHA256
255 assert_uint_eq(lzma_index_checks(idx),
256 UINT32_C(1) << LZMA_CHECK_SHA256);
258 // Test with a third Stream
259 lzma_index *third = lzma_index_init(NULL);
260 assert_true(third != NULL);
262 stream_flags.check = LZMA_CHECK_CRC64;
263 assert_lzma_ret(lzma_index_stream_flags(third, &stream_flags),
264 LZMA_OK);
266 assert_uint_eq(lzma_index_checks(third),
267 UINT32_C(1) << LZMA_CHECK_CRC64);
269 assert_lzma_ret(lzma_index_cat(idx, third, NULL), LZMA_OK);
271 // Index should now have CRC64 and SHA256
272 assert_uint_eq(lzma_index_checks(idx),
273 (UINT32_C(1) << LZMA_CHECK_CRC64) |
274 (UINT32_C(1) << LZMA_CHECK_SHA256));
276 lzma_index_end(idx, NULL);
280 static void
281 test_lzma_index_stream_padding(void)
283 // Test NULL lzma_index
284 assert_lzma_ret(lzma_index_stream_padding(NULL, 0),
285 LZMA_PROG_ERROR);
287 lzma_index *idx = lzma_index_init(NULL);
288 assert_true(idx != NULL);
290 // Test Stream Padding not a multiple of 4
291 assert_lzma_ret(lzma_index_stream_padding(idx, 3),
292 LZMA_PROG_ERROR);
294 // Test Stream Padding too large
295 assert_lzma_ret(lzma_index_stream_padding(idx, LZMA_VLI_MAX - 3),
296 LZMA_DATA_ERROR);
298 // Test Stream Padding valid
299 assert_lzma_ret(lzma_index_stream_padding(idx, 0x1000),
300 LZMA_OK);
301 assert_lzma_ret(lzma_index_stream_padding(idx, 4),
302 LZMA_OK);
303 assert_lzma_ret(lzma_index_stream_padding(idx, 0),
304 LZMA_OK);
306 // Test Stream Padding causing the file size to grow too large
307 assert_lzma_ret(lzma_index_append(idx, NULL,
308 LZMA_VLI_MAX - 0x1000, 1), LZMA_OK);
309 assert_lzma_ret(lzma_index_stream_padding(idx, 0x1000),
310 LZMA_DATA_ERROR);
312 lzma_index_end(idx, NULL);
316 static void
317 test_lzma_index_stream_count(void)
319 lzma_index *idx = lzma_index_init(NULL);
320 assert_true(idx != NULL);
322 assert_uint_eq(lzma_index_stream_count(idx), 1);
324 // Appending Blocks should not change the Stream count value
325 assert_lzma_ret(lzma_index_append(idx, NULL, UNPADDED_SIZE_MIN,
326 1), LZMA_OK);
328 assert_uint_eq(lzma_index_stream_count(idx), 1);
330 // Test with multiple Streams
331 for (uint32_t i = 0; i < 100; i++) {
332 lzma_index *idx_cat = lzma_index_init(NULL);
333 assert_true(idx != NULL);
334 assert_lzma_ret(lzma_index_cat(idx, idx_cat, NULL), LZMA_OK);
335 assert_uint_eq(lzma_index_stream_count(idx), i + 2);
338 lzma_index_end(idx, NULL);
342 static void
343 test_lzma_index_block_count(void)
345 lzma_index *idx = lzma_index_init(NULL);
346 assert_true(idx != NULL);
348 assert_uint_eq(lzma_index_block_count(idx), 0);
350 const uint32_t iterations = 0x1000;
351 for (uint32_t i = 0; i < iterations; i++) {
352 assert_lzma_ret(lzma_index_append(idx, NULL,
353 UNPADDED_SIZE_MIN, 1), LZMA_OK);
354 assert_uint_eq(lzma_index_block_count(idx), i + 1);
357 // Create new lzma_index with a few Blocks
358 lzma_index *second = lzma_index_init(NULL);
359 assert_true(second != NULL);
361 assert_lzma_ret(lzma_index_append(second, NULL,
362 UNPADDED_SIZE_MIN, 1), LZMA_OK);
363 assert_lzma_ret(lzma_index_append(second, NULL,
364 UNPADDED_SIZE_MIN, 1), LZMA_OK);
365 assert_lzma_ret(lzma_index_append(second, NULL,
366 UNPADDED_SIZE_MIN, 1), LZMA_OK);
368 assert_uint_eq(lzma_index_block_count(second), 3);
370 // Concatenate the lzma_indexes together and the result should have
371 // the sum of the two individual counts.
372 assert_lzma_ret(lzma_index_cat(idx, second, NULL), LZMA_OK);
373 assert_uint_eq(lzma_index_block_count(idx), iterations + 3);
375 assert_lzma_ret(lzma_index_append(idx, NULL,
376 UNPADDED_SIZE_MIN, 1), LZMA_OK);
378 assert_uint_eq(lzma_index_block_count(idx), iterations + 4);
380 lzma_index_end(idx, NULL);
384 static void
385 test_lzma_index_size(void)
387 lzma_index *idx = lzma_index_init(NULL);
388 assert_true(idx != NULL);
390 // Base size should be:
391 // 1 byte Index Indicator
392 // 1 byte Number of Records
393 // 0 bytes Records
394 // 2 bytes Index Padding
395 // 4 bytes CRC32
396 // Total: 8 bytes
397 assert_uint_eq(lzma_index_size(idx), 8);
399 assert_lzma_ret(lzma_index_append(idx, NULL,
400 UNPADDED_SIZE_MIN, 1), LZMA_OK);
402 // New size should be:
403 // 1 byte Index Indicator
404 // 1 byte Number of Records
405 // 2 bytes Records
406 // 0 bytes Index Padding
407 // 4 bytes CRC32
408 // Total: 8 bytes
409 assert_uint_eq(lzma_index_size(idx), 8);
411 assert_lzma_ret(lzma_index_append(idx, NULL,
412 LZMA_VLI_MAX / 4, LZMA_VLI_MAX / 4), LZMA_OK);
414 // New size should be:
415 // 1 byte Index Indicator
416 // 1 byte Number of Records
417 // 20 bytes Records
418 // 2 bytes Index Padding
419 // 4 bytes CRC32
420 // Total: 28 bytes
421 assert_uint_eq(lzma_index_size(idx), 28);
423 lzma_index_end(idx, NULL);
427 static void
428 test_lzma_index_stream_size(void)
430 lzma_index *idx = lzma_index_init(NULL);
431 assert_true(idx != NULL);
433 // Stream size calculated by:
434 // Size of Stream Header (12 bytes)
435 // Size of all Blocks
436 // Size of the Index
437 // Size of the Stream Footer (12 bytes)
439 // First test with empty Index
440 // Stream size should be:
441 // Size of Stream Header - 12 bytes
442 // Size of all Blocks - 0 bytes
443 // Size of Index - 8 bytes
444 // Size of Stream Footer - 12 bytes
445 // Total: 32 bytes
446 assert_uint_eq(lzma_index_stream_size(idx), 32);
448 // Next, append a few Blocks and retest
449 assert_lzma_ret(lzma_index_append(idx, NULL, 1000, 1), LZMA_OK);
450 assert_lzma_ret(lzma_index_append(idx, NULL, 1000, 1), LZMA_OK);
451 assert_lzma_ret(lzma_index_append(idx, NULL, 1000, 1), LZMA_OK);
453 // Stream size should be:
454 // Size of Stream Header - 12 bytes
455 // Size of all Blocks - 3000 bytes
456 // Size of Index - 16 bytes
457 // Size of Stream Footer - 12 bytes
458 // Total: 3040 bytes
459 assert_uint_eq(lzma_index_stream_size(idx), 3040);
461 lzma_index *second = lzma_index_init(NULL);
462 assert_true(second != NULL);
464 assert_uint_eq(lzma_index_stream_size(second), 32);
465 assert_lzma_ret(lzma_index_append(second, NULL, 1000, 1), LZMA_OK);
467 // Stream size should be:
468 // Size of Stream Header - 12 bytes
469 // Size of all Blocks - 1000 bytes
470 // Size of Index - 12 bytes
471 // Size of Stream Footer - 12 bytes
472 // Total: 1036 bytes
473 assert_uint_eq(lzma_index_stream_size(second), 1036);
475 assert_lzma_ret(lzma_index_cat(idx, second, NULL), LZMA_OK);
477 // Stream size should be:
478 // Size of Stream Header - 12 bytes
479 // Size of all Blocks - 4000 bytes
480 // Size of Index - 20 bytes
481 // Size of Stream Footer - 12 bytes
482 // Total: 4044 bytes
483 assert_uint_eq(lzma_index_stream_size(idx), 4044);
485 lzma_index_end(idx, NULL);
489 static void
490 test_lzma_index_total_size(void)
492 lzma_index *idx = lzma_index_init(NULL);
493 assert_true(idx != NULL);
495 // First test empty lzma_index.
496 // Result should be 0 since no Blocks have been added.
497 assert_uint_eq(lzma_index_total_size(idx), 0);
499 // Add a few Blocks and retest after each append
500 assert_lzma_ret(lzma_index_append(idx, NULL, 1000, 1), LZMA_OK);
501 assert_uint_eq(lzma_index_total_size(idx), 1000);
503 assert_lzma_ret(lzma_index_append(idx, NULL, 1000, 1), LZMA_OK);
504 assert_uint_eq(lzma_index_total_size(idx), 2000);
506 assert_lzma_ret(lzma_index_append(idx, NULL, 1000, 1), LZMA_OK);
507 assert_uint_eq(lzma_index_total_size(idx), 3000);
509 // Create second lzma_index and append Blocks to it.
510 lzma_index *second = lzma_index_init(NULL);
511 assert_true(second != NULL);
513 assert_uint_eq(lzma_index_total_size(second), 0);
515 assert_lzma_ret(lzma_index_append(second, NULL, 100, 1), LZMA_OK);
516 assert_uint_eq(lzma_index_total_size(second), 100);
518 assert_lzma_ret(lzma_index_append(second, NULL, 100, 1), LZMA_OK);
519 assert_uint_eq(lzma_index_total_size(second), 200);
521 // Concatenate the Streams together
522 assert_lzma_ret(lzma_index_cat(idx, second, NULL), LZMA_OK);
524 // The resulting total size should be the size of all Blocks
525 // from both Streams
526 assert_uint_eq(lzma_index_total_size(idx), 3200);
528 lzma_index_end(idx, NULL);
532 static void
533 test_lzma_index_file_size(void)
535 lzma_index *idx = lzma_index_init(NULL);
536 assert_true(idx != NULL);
538 // Should be the same as test_lzma_index_stream_size with
539 // only one Stream and no Stream Padding.
540 assert_uint_eq(lzma_index_file_size(idx), 32);
542 assert_lzma_ret(lzma_index_append(idx, NULL, 1000, 1), LZMA_OK);
543 assert_lzma_ret(lzma_index_append(idx, NULL, 1000, 1), LZMA_OK);
544 assert_lzma_ret(lzma_index_append(idx, NULL, 1000, 1), LZMA_OK);
546 assert_uint_eq(lzma_index_file_size(idx), 3040);
548 // Next add Stream Padding
549 assert_lzma_ret(lzma_index_stream_padding(idx, 1000),
550 LZMA_OK);
552 assert_uint_eq(lzma_index_file_size(idx), 4040);
554 // Create second lzma_index.
555 // Very similar to test_lzma_index_stream_size, but
556 // the values should include the headers of the second Stream.
557 lzma_index *second = lzma_index_init(NULL);
558 assert_true(second != NULL);
560 assert_lzma_ret(lzma_index_append(second, NULL, 1000, 1), LZMA_OK);
561 assert_uint_eq(lzma_index_stream_size(second), 1036);
563 assert_lzma_ret(lzma_index_cat(idx, second, NULL), LZMA_OK);
565 // .xz file size should be:
566 // Size of 2 Stream Headers - 12 * 2 bytes
567 // Size of all Blocks - 3000 + 1000 bytes
568 // Size of 2 Indexes - 16 + 12 bytes
569 // Size of Stream Padding - 1000 bytes
570 // Size of 2 Stream Footers - 12 * 2 bytes
571 // Total: 5076 bytes
572 assert_uint_eq(lzma_index_file_size(idx), 5076);
574 lzma_index_end(idx, NULL);
578 static void
579 test_lzma_index_uncompressed_size(void)
581 lzma_index *idx = lzma_index_init(NULL);
582 assert_true(idx != NULL);
584 // Empty lzma_index should have 0 uncompressed .xz file size.
585 assert_uint_eq(lzma_index_uncompressed_size(idx), 0);
587 // Append a few small Blocks
588 assert_lzma_ret(lzma_index_append(idx, NULL, 1000, 1), LZMA_OK);
589 assert_lzma_ret(lzma_index_append(idx, NULL, 1000, 10), LZMA_OK);
590 assert_lzma_ret(lzma_index_append(idx, NULL, 1000, 100), LZMA_OK);
592 assert_uint_eq(lzma_index_uncompressed_size(idx), 111);
594 // Create another lzma_index
595 lzma_index *second = lzma_index_init(NULL);
596 assert_true(second != NULL);
598 // Append a few small Blocks
599 assert_lzma_ret(lzma_index_append(second, NULL, 1000, 2), LZMA_OK);
600 assert_lzma_ret(lzma_index_append(second, NULL, 1000, 20), LZMA_OK);
601 assert_lzma_ret(lzma_index_append(second, NULL, 1000, 200), LZMA_OK);
603 assert_uint_eq(lzma_index_uncompressed_size(second), 222);
605 // Concatenate second lzma_index to first
606 assert_lzma_ret(lzma_index_cat(idx, second, NULL), LZMA_OK);
608 // New uncompressed .xz file size should be the sum of the two Streams
609 assert_uint_eq(lzma_index_uncompressed_size(idx), 333);
611 // Append one more Block to the lzma_index and ensure that
612 // it is properly updated
613 assert_lzma_ret(lzma_index_append(idx, NULL, 1000, 111), LZMA_OK);
614 assert_uint_eq(lzma_index_uncompressed_size(idx), 444);
616 lzma_index_end(idx, NULL);
620 static void
621 test_lzma_index_iter_init(void)
623 // Testing basic init functionality.
624 // The init function should call rewind on the iterator.
625 lzma_index *first = lzma_index_init(NULL);
626 assert_true(first != NULL);
628 lzma_index *second = lzma_index_init(NULL);
629 assert_true(second != NULL);
631 lzma_index *third = lzma_index_init(NULL);
632 assert_true(third != NULL);
634 assert_lzma_ret(lzma_index_cat(first, second, NULL), LZMA_OK);
635 assert_lzma_ret(lzma_index_cat(first, third, NULL), LZMA_OK);
637 lzma_index_iter iter;
638 lzma_index_iter_init(&iter, first);
640 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_STREAM));
641 assert_uint_eq(iter.stream.number, 1);
642 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_STREAM));
643 assert_uint_eq(iter.stream.number, 2);
645 lzma_index_iter_init(&iter, first);
647 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_STREAM));
648 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_STREAM));
649 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_STREAM));
650 assert_uint_eq(iter.stream.number, 3);
652 lzma_index_end(first, NULL);
656 static void
657 test_lzma_index_iter_rewind(void)
659 lzma_index *first = lzma_index_init(NULL);
660 assert_true(first != NULL);
662 lzma_index_iter iter;
663 lzma_index_iter_init(&iter, first);
665 // Append 3 Blocks and iterate over each. This is to test
666 // the LZMA_INDEX_ITER_BLOCK mode.
667 for (uint32_t i = 0; i < 3; i++) {
668 assert_lzma_ret(lzma_index_append(first, NULL,
669 UNPADDED_SIZE_MIN, 1), LZMA_OK);
670 assert_false(lzma_index_iter_next(&iter,
671 LZMA_INDEX_ITER_BLOCK));
672 assert_uint_eq(iter.block.number_in_file, i + 1);
675 // Rewind back to the begining and iterate over the Blocks again
676 lzma_index_iter_rewind(&iter);
678 // Should be able to re-iterate over the Blocks again.
679 for (uint32_t i = 0; i < 3; i++) {
680 assert_false(lzma_index_iter_next(&iter,
681 LZMA_INDEX_ITER_BLOCK));
682 assert_uint_eq(iter.block.number_in_file, i + 1);
685 // Next concatenate two more lzma_indexes, iterate over them,
686 // rewind, and iterate over them again. This is to test
687 // the LZMA_INDEX_ITER_STREAM mode.
688 lzma_index *second = lzma_index_init(NULL);
689 assert_true(second != NULL);
691 lzma_index *third = lzma_index_init(NULL);
692 assert_true(third != NULL);
694 assert_lzma_ret(lzma_index_cat(first, second, NULL), LZMA_OK);
695 assert_lzma_ret(lzma_index_cat(first, third, NULL), LZMA_OK);
697 assert_false(lzma_index_iter_next(&iter,
698 LZMA_INDEX_ITER_STREAM));
699 assert_false(lzma_index_iter_next(&iter,
700 LZMA_INDEX_ITER_STREAM));
702 assert_uint_eq(iter.stream.number, 3);
704 lzma_index_iter_rewind(&iter);
706 for (uint32_t i = 0; i < 3; i++) {
707 assert_false(lzma_index_iter_next(&iter,
708 LZMA_INDEX_ITER_STREAM));
709 assert_uint_eq(iter.stream.number, i + 1);
712 lzma_index_end(first, NULL);
716 static void
717 test_lzma_index_iter_next(void)
719 lzma_index *first = lzma_index_init(NULL);
720 assert_true(first != NULL);
722 lzma_index_iter iter;
723 lzma_index_iter_init(&iter, first);
725 // First test bad mode values
726 for (uint32_t i = LZMA_INDEX_ITER_NONEMPTY_BLOCK + 1; i < 100; i++)
727 assert_true(lzma_index_iter_next(&iter, i));
729 // Test iterating over Blocks
730 assert_lzma_ret(lzma_index_append(first, NULL,
731 UNPADDED_SIZE_MIN, 1), LZMA_OK);
732 assert_lzma_ret(lzma_index_append(first, NULL,
733 UNPADDED_SIZE_MIN * 2, 10), LZMA_OK);
734 assert_lzma_ret(lzma_index_append(first, NULL,
735 UNPADDED_SIZE_MIN * 3, 100), LZMA_OK);
737 // For Blocks, need to verify:
738 // - number_in_file (overall Block number)
739 // - compressed_file_offset
740 // - uncompressed_file_offset
741 // - number_in_stream (Block number relative to current Stream)
742 // - compressed_stream_offset
743 // - uncompressed_stream_offset
744 // - uncompressed_size
745 // - unpadded_size
746 // - total_size
748 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK));
750 // Verify Block data stored correctly
751 assert_uint_eq(iter.block.number_in_file, 1);
753 // Should start right after the Stream Header
754 assert_uint_eq(iter.block.compressed_file_offset,
755 LZMA_STREAM_HEADER_SIZE);
756 assert_uint_eq(iter.block.uncompressed_file_offset, 0);
757 assert_uint_eq(iter.block.number_in_stream, 1);
758 assert_uint_eq(iter.block.compressed_stream_offset,
759 LZMA_STREAM_HEADER_SIZE);
760 assert_uint_eq(iter.block.uncompressed_stream_offset, 0);
761 assert_uint_eq(iter.block.unpadded_size, UNPADDED_SIZE_MIN);
762 assert_uint_eq(iter.block.total_size, vli_ceil4(UNPADDED_SIZE_MIN));
764 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK));
766 // Verify Block data stored correctly
767 assert_uint_eq(iter.block.number_in_file, 2);
768 assert_uint_eq(iter.block.compressed_file_offset,
769 LZMA_STREAM_HEADER_SIZE +
770 vli_ceil4(UNPADDED_SIZE_MIN));
771 assert_uint_eq(iter.block.uncompressed_file_offset, 1);
772 assert_uint_eq(iter.block.number_in_stream, 2);
773 assert_uint_eq(iter.block.compressed_stream_offset,
774 LZMA_STREAM_HEADER_SIZE +
775 vli_ceil4(UNPADDED_SIZE_MIN));
776 assert_uint_eq(iter.block.uncompressed_stream_offset, 1);
777 assert_uint_eq(iter.block.unpadded_size, UNPADDED_SIZE_MIN * 2);
778 assert_uint_eq(iter.block.total_size, vli_ceil4(UNPADDED_SIZE_MIN * 2));
780 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK));
782 // Verify Block data stored correctly
783 assert_uint_eq(iter.block.number_in_file, 3);
784 assert_uint_eq(iter.block.compressed_file_offset,
785 LZMA_STREAM_HEADER_SIZE +
786 vli_ceil4(UNPADDED_SIZE_MIN) +
787 vli_ceil4(UNPADDED_SIZE_MIN * 2));
788 assert_uint_eq(iter.block.uncompressed_file_offset, 11);
789 assert_uint_eq(iter.block.number_in_stream, 3);
790 assert_uint_eq(iter.block.compressed_stream_offset,
791 LZMA_STREAM_HEADER_SIZE +
792 vli_ceil4(UNPADDED_SIZE_MIN) +
793 vli_ceil4(UNPADDED_SIZE_MIN * 2));
794 assert_uint_eq(iter.block.uncompressed_stream_offset, 11);
795 assert_uint_eq(iter.block.unpadded_size, UNPADDED_SIZE_MIN * 3);
796 assert_uint_eq(iter.block.total_size,
797 vli_ceil4(UNPADDED_SIZE_MIN * 3));
799 // Only three Blocks were added, so this should return true
800 assert_true(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK));
802 const lzma_vli second_stream_compressed_start =
803 LZMA_STREAM_HEADER_SIZE * 2 +
804 vli_ceil4(UNPADDED_SIZE_MIN) +
805 vli_ceil4(UNPADDED_SIZE_MIN * 2) +
806 vli_ceil4(UNPADDED_SIZE_MIN * 3) +
807 lzma_index_size(first);
808 const lzma_vli second_stream_uncompressed_start = 1 + 10 + 100;
810 // Test iterating over Streams.
811 // The second Stream will have 0 Blocks
812 lzma_index *second = lzma_index_init(NULL);
813 assert_true(second != NULL);
815 // Set Stream Flags for Stream 2
816 lzma_stream_flags flags = {
817 .version = 0,
818 .backward_size = LZMA_BACKWARD_SIZE_MIN,
819 .check = LZMA_CHECK_CRC32
822 assert_lzma_ret(lzma_index_stream_flags(second, &flags), LZMA_OK);
824 // The Second stream will have 8 bytes of Stream Padding
825 assert_lzma_ret(lzma_index_stream_padding(second, 8), LZMA_OK);
827 const lzma_vli second_stream_index_size = lzma_index_size(second);
829 // The third Stream will have 2 Blocks
830 lzma_index *third = lzma_index_init(NULL);
831 assert_true(third != NULL);
833 assert_lzma_ret(lzma_index_append(third, NULL, 32, 20), LZMA_OK);
834 assert_lzma_ret(lzma_index_append(third, NULL, 64, 40), LZMA_OK);
836 const lzma_vli third_stream_index_size = lzma_index_size(third);
838 assert_lzma_ret(lzma_index_cat(first, second, NULL), LZMA_OK);
839 assert_lzma_ret(lzma_index_cat(first, third, NULL), LZMA_OK);
841 // For Streams, need to verify:
842 // - flags (Stream Flags)
843 // - number (Stream count)
844 // - block_count
845 // - compressed_offset
846 // - uncompressed_offset
847 // - compressed_size
848 // - uncompressed_size
849 // - padding (Stream Padding)
850 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_STREAM));
852 // Verify Stream
853 assert_uint_eq(iter.stream.flags->backward_size,
854 LZMA_BACKWARD_SIZE_MIN);
855 assert_uint_eq(iter.stream.flags->check, LZMA_CHECK_CRC32);
856 assert_uint_eq(iter.stream.number, 2);
857 assert_uint_eq(iter.stream.block_count, 0);
858 assert_uint_eq(iter.stream.compressed_offset,
859 second_stream_compressed_start);
860 assert_uint_eq(iter.stream.uncompressed_offset,
861 second_stream_uncompressed_start);
862 assert_uint_eq(iter.stream.compressed_size,
863 LZMA_STREAM_HEADER_SIZE * 2 +
864 second_stream_index_size);
865 assert_uint_eq(iter.stream.uncompressed_size, 0);
866 assert_uint_eq(iter.stream.padding, 8);
868 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_STREAM));
870 // Verify Stream
871 const lzma_vli third_stream_compressed_start =
872 second_stream_compressed_start +
873 LZMA_STREAM_HEADER_SIZE * 2 +
874 8 + // Stream padding
875 second_stream_index_size;
876 const lzma_vli third_stream_uncompressed_start =
877 second_stream_uncompressed_start;
879 assert_uint_eq(iter.stream.number, 3);
880 assert_uint_eq(iter.stream.block_count, 2);
881 assert_uint_eq(iter.stream.compressed_offset,
882 third_stream_compressed_start);
883 assert_uint_eq(iter.stream.uncompressed_offset,
884 third_stream_uncompressed_start);
885 assert_uint_eq(iter.stream.compressed_size,
886 LZMA_STREAM_HEADER_SIZE * 2 +
887 96 + // Total compressed size
888 third_stream_index_size);
889 assert_uint_eq(iter.stream.uncompressed_size, 60);
890 assert_uint_eq(iter.stream.padding, 0);
892 assert_true(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_STREAM));
894 // Even after a failing call to next with ITER_STREAM mode,
895 // should still be able to iterate over the 2 Blocks in
896 // Stream 3.
897 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK));
899 // Verify both Blocks
901 // Next call to iterate Block should return true because the
902 // first Block can already be read from the LZMA_INDEX_ITER_STREAM
903 // call.
904 assert_true(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK));
906 // Rewind to test LZMA_INDEX_ITER_ANY
907 lzma_index_iter_rewind(&iter);
909 // Iterate past the first three Blocks
910 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_ANY));
911 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_ANY));
912 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_ANY));
914 // Iterate past the next Stream
915 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_ANY));
917 // Iterate past the next Stream
918 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_ANY));
919 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_ANY));
921 // Last call should fail
922 assert_true(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_ANY));
924 // Rewind to test LZMA_INDEX_ITER_NONEMPTY_BLOCK
925 lzma_index_iter_rewind(&iter);
927 // Iterate past the first three Blocks
928 assert_false(lzma_index_iter_next(&iter,
929 LZMA_INDEX_ITER_NONEMPTY_BLOCK));
930 assert_false(lzma_index_iter_next(&iter,
931 LZMA_INDEX_ITER_NONEMPTY_BLOCK));
932 assert_false(lzma_index_iter_next(&iter,
933 LZMA_INDEX_ITER_NONEMPTY_BLOCK));
935 // Skip past the next Stream which has no Blocks.
936 // We will get to the first Block of the third Stream.
937 assert_false(lzma_index_iter_next(&iter,
938 LZMA_INDEX_ITER_NONEMPTY_BLOCK));
940 // Iterate past the second (the last) Block in the third Stream
941 assert_false(lzma_index_iter_next(&iter,
942 LZMA_INDEX_ITER_NONEMPTY_BLOCK));
944 // Last call should fail since there is nothing left to iterate over.
945 assert_true(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_ANY));
947 lzma_index_end(first, NULL);
951 static void
952 test_lzma_index_iter_locate(void)
954 lzma_index *idx = lzma_index_init(NULL);
955 assert_true(idx != NULL);
957 lzma_index_iter iter;
958 lzma_index_iter_init(&iter, idx);
960 // Cannot locate anything from an empty Index.
961 assert_true(lzma_index_iter_locate(&iter, 0));
962 assert_true(lzma_index_iter_locate(&iter, 555));
964 // One empty Record: nothing is found since there's no uncompressed
965 // data.
966 assert_lzma_ret(lzma_index_append(idx, NULL, 16, 0), LZMA_OK);
967 assert_true(lzma_index_iter_locate(&iter, 0));
969 // Non-empty Record and we can find something.
970 assert_lzma_ret(lzma_index_append(idx, NULL, 32, 5), LZMA_OK);
971 assert_false(lzma_index_iter_locate(&iter, 0));
972 assert_uint_eq(iter.block.total_size, 32);
973 assert_uint_eq(iter.block.uncompressed_size, 5);
974 assert_uint_eq(iter.block.compressed_file_offset,
975 LZMA_STREAM_HEADER_SIZE + 16);
976 assert_uint_eq(iter.block.uncompressed_file_offset, 0);
978 // Still cannot find anything past the end.
979 assert_true(lzma_index_iter_locate(&iter, 5));
981 // Add the third Record.
982 assert_lzma_ret(lzma_index_append(idx, NULL, 40, 11), LZMA_OK);
984 assert_false(lzma_index_iter_locate(&iter, 0));
985 assert_uint_eq(iter.block.total_size, 32);
986 assert_uint_eq(iter.block.uncompressed_size, 5);
987 assert_uint_eq(iter.block.compressed_file_offset,
988 LZMA_STREAM_HEADER_SIZE + 16);
989 assert_uint_eq(iter.block.uncompressed_file_offset, 0);
991 assert_false(lzma_index_iter_next(&iter, LZMA_INDEX_ITER_BLOCK));
992 assert_uint_eq(iter.block.total_size, 40);
993 assert_uint_eq(iter.block.uncompressed_size, 11);
994 assert_uint_eq(iter.block.compressed_file_offset,
995 LZMA_STREAM_HEADER_SIZE + 16 + 32);
996 assert_uint_eq(iter.block.uncompressed_file_offset, 5);
998 assert_false(lzma_index_iter_locate(&iter, 2));
999 assert_uint_eq(iter.block.total_size, 32);
1000 assert_uint_eq(iter.block.uncompressed_size, 5);
1001 assert_uint_eq(iter.block.compressed_file_offset,
1002 LZMA_STREAM_HEADER_SIZE + 16);
1003 assert_uint_eq(iter.block.uncompressed_file_offset, 0);
1005 assert_false(lzma_index_iter_locate(&iter, 5));
1006 assert_uint_eq(iter.block.total_size, 40);
1007 assert_uint_eq(iter.block.uncompressed_size, 11);
1008 assert_uint_eq(iter.block.compressed_file_offset,
1009 LZMA_STREAM_HEADER_SIZE + 16 + 32);
1010 assert_uint_eq(iter.block.uncompressed_file_offset, 5);
1012 assert_false(lzma_index_iter_locate(&iter, 5 + 11 - 1));
1013 assert_uint_eq(iter.block.total_size, 40);
1014 assert_uint_eq(iter.block.uncompressed_size, 11);
1015 assert_uint_eq(iter.block.compressed_file_offset,
1016 LZMA_STREAM_HEADER_SIZE + 16 + 32);
1017 assert_uint_eq(iter.block.uncompressed_file_offset, 5);
1019 assert_true(lzma_index_iter_locate(&iter, 5 + 11));
1020 assert_true(lzma_index_iter_locate(&iter, 5 + 15));
1022 // Large Index
1023 lzma_index_end(idx, NULL);
1024 idx = lzma_index_init(NULL);
1025 assert_true(idx != NULL);
1026 lzma_index_iter_init(&iter, idx);
1028 for (uint32_t n = 4; n <= 4 * 5555; n += 4)
1029 assert_lzma_ret(lzma_index_append(idx, NULL, n + 8, n),
1030 LZMA_OK);
1032 assert_uint_eq(lzma_index_block_count(idx), 5555);
1034 // First Record
1035 assert_false(lzma_index_iter_locate(&iter, 0));
1036 assert_uint_eq(iter.block.total_size, 4 + 8);
1037 assert_uint_eq(iter.block.uncompressed_size, 4);
1038 assert_uint_eq(iter.block.compressed_file_offset,
1039 LZMA_STREAM_HEADER_SIZE);
1040 assert_uint_eq(iter.block.uncompressed_file_offset, 0);
1042 assert_false(lzma_index_iter_locate(&iter, 3));
1043 assert_uint_eq(iter.block.total_size, 4 + 8);
1044 assert_uint_eq(iter.block.uncompressed_size, 4);
1045 assert_uint_eq(iter.block.compressed_file_offset,
1046 LZMA_STREAM_HEADER_SIZE);
1047 assert_uint_eq(iter.block.uncompressed_file_offset, 0);
1049 // Second Record
1050 assert_false(lzma_index_iter_locate(&iter, 4));
1051 assert_uint_eq(iter.block.total_size, 2 * 4 + 8);
1052 assert_uint_eq(iter.block.uncompressed_size, 2 * 4);
1053 assert_uint_eq(iter.block.compressed_file_offset,
1054 LZMA_STREAM_HEADER_SIZE + 4 + 8);
1055 assert_uint_eq(iter.block.uncompressed_file_offset, 4);
1057 // Last Record
1058 assert_false(lzma_index_iter_locate(
1059 &iter, lzma_index_uncompressed_size(idx) - 1));
1060 assert_uint_eq(iter.block.total_size, 4 * 5555 + 8);
1061 assert_uint_eq(iter.block.uncompressed_size, 4 * 5555);
1062 assert_uint_eq(iter.block.compressed_file_offset,
1063 lzma_index_total_size(idx)
1064 + LZMA_STREAM_HEADER_SIZE - 4 * 5555 - 8);
1065 assert_uint_eq(iter.block.uncompressed_file_offset,
1066 lzma_index_uncompressed_size(idx) - 4 * 5555);
1068 // Allocation chunk boundaries. See INDEX_GROUP_SIZE in
1069 // liblzma/common/index.c.
1070 const uint32_t group_multiple = 256 * 4;
1071 const uint32_t radius = 8;
1072 const uint32_t start = group_multiple - radius;
1073 lzma_vli ubase = 0;
1074 lzma_vli tbase = 0;
1075 uint32_t n;
1076 for (n = 1; n < start; ++n) {
1077 ubase += n * 4;
1078 tbase += n * 4 + 8;
1081 while (n < start + 2 * radius) {
1082 assert_false(lzma_index_iter_locate(&iter, ubase + n * 4));
1084 assert_uint_eq(iter.block.compressed_file_offset,
1085 tbase + n * 4 + 8
1086 + LZMA_STREAM_HEADER_SIZE);
1087 assert_uint_eq(iter.block.uncompressed_file_offset,
1088 ubase + n * 4);
1090 tbase += n * 4 + 8;
1091 ubase += n * 4;
1092 ++n;
1094 assert_uint_eq(iter.block.total_size, n * 4 + 8);
1095 assert_uint_eq(iter.block.uncompressed_size, n * 4);
1098 // Do it also backwards.
1099 while (n > start) {
1100 assert_false(lzma_index_iter_locate(
1101 &iter, ubase + (n - 1) * 4));
1103 assert_uint_eq(iter.block.total_size, n * 4 + 8);
1104 assert_uint_eq(iter.block.uncompressed_size, n * 4);
1106 --n;
1107 tbase -= n * 4 + 8;
1108 ubase -= n * 4;
1110 assert_uint_eq(iter.block.compressed_file_offset,
1111 tbase + n * 4 + 8
1112 + LZMA_STREAM_HEADER_SIZE);
1113 assert_uint_eq(iter.block.uncompressed_file_offset,
1114 ubase + n * 4);
1117 // Test locating in concatenated Index.
1118 lzma_index_end(idx, NULL);
1119 idx = lzma_index_init(NULL);
1120 assert_true(idx != NULL);
1121 lzma_index_iter_init(&iter, idx);
1122 for (n = 0; n < group_multiple; ++n)
1123 assert_lzma_ret(lzma_index_append(idx, NULL, 8, 0),
1124 LZMA_OK);
1125 assert_lzma_ret(lzma_index_append(idx, NULL, 16, 1), LZMA_OK);
1126 assert_false(lzma_index_iter_locate(&iter, 0));
1127 assert_uint_eq(iter.block.total_size, 16);
1128 assert_uint_eq(iter.block.uncompressed_size, 1);
1129 assert_uint_eq(iter.block.compressed_file_offset,
1130 LZMA_STREAM_HEADER_SIZE + group_multiple * 8);
1131 assert_uint_eq(iter.block.uncompressed_file_offset, 0);
1133 lzma_index_end(idx, NULL);
1137 static void
1138 test_lzma_index_cat(void)
1140 // Most complex tests for this function are done in other tests.
1141 // This will mostly test basic functionality.
1143 lzma_index *dest = lzma_index_init(NULL);
1144 assert_true(dest != NULL);
1146 lzma_index *src = lzma_index_init(NULL);
1147 assert_true(src != NULL);
1149 // First test NULL dest or src
1150 assert_lzma_ret(lzma_index_cat(NULL, NULL, NULL), LZMA_PROG_ERROR);
1151 assert_lzma_ret(lzma_index_cat(dest, NULL, NULL), LZMA_PROG_ERROR);
1152 assert_lzma_ret(lzma_index_cat(NULL, src, NULL), LZMA_PROG_ERROR);
1154 // Check for uncompressed size overflow
1155 assert_lzma_ret(lzma_index_append(dest, NULL,
1156 (UNPADDED_SIZE_MAX / 2) + 1, 1), LZMA_OK);
1157 assert_lzma_ret(lzma_index_append(src, NULL,
1158 (UNPADDED_SIZE_MAX / 2) + 1, 1), LZMA_OK);
1159 assert_lzma_ret(lzma_index_cat(dest, src, NULL), LZMA_DATA_ERROR);
1161 // Check for compressed size overflow
1162 lzma_index_end(src, NULL);
1163 lzma_index_end(dest, NULL);
1165 dest = lzma_index_init(NULL);
1166 assert_true(dest != NULL);
1168 src = lzma_index_init(NULL);
1169 assert_true(src != NULL);
1171 assert_lzma_ret(lzma_index_append(dest, NULL,
1172 UNPADDED_SIZE_MIN, LZMA_VLI_MAX - 1), LZMA_OK);
1173 assert_lzma_ret(lzma_index_append(src, NULL,
1174 UNPADDED_SIZE_MIN, LZMA_VLI_MAX - 1), LZMA_OK);
1175 assert_lzma_ret(lzma_index_cat(dest, src, NULL), LZMA_DATA_ERROR);
1177 lzma_index_end(dest, NULL);
1178 lzma_index_end(src, NULL);
1182 // Helper function for test_lzma_index_dup().
1183 static bool
1184 index_is_equal(const lzma_index *a, const lzma_index *b)
1186 // Compare only the Stream and Block sizes and offsets.
1187 lzma_index_iter ra, rb;
1188 lzma_index_iter_init(&ra, a);
1189 lzma_index_iter_init(&rb, b);
1191 while (true) {
1192 bool reta = lzma_index_iter_next(&ra, LZMA_INDEX_ITER_ANY);
1193 bool retb = lzma_index_iter_next(&rb, LZMA_INDEX_ITER_ANY);
1195 // If both iterators finish at the same time, then the Indexes
1196 // are identical.
1197 if (reta)
1198 return retb;
1200 if (ra.stream.number != rb.stream.number
1201 || ra.stream.block_count
1202 != rb.stream.block_count
1203 || ra.stream.compressed_offset
1204 != rb.stream.compressed_offset
1205 || ra.stream.uncompressed_offset
1206 != rb.stream.uncompressed_offset
1207 || ra.stream.compressed_size
1208 != rb.stream.compressed_size
1209 || ra.stream.uncompressed_size
1210 != rb.stream.uncompressed_size
1211 || ra.stream.padding
1212 != rb.stream.padding)
1213 return false;
1215 if (ra.stream.block_count == 0)
1216 continue;
1218 if (ra.block.number_in_file != rb.block.number_in_file
1219 || ra.block.compressed_file_offset
1220 != rb.block.compressed_file_offset
1221 || ra.block.uncompressed_file_offset
1222 != rb.block.uncompressed_file_offset
1223 || ra.block.number_in_stream
1224 != rb.block.number_in_stream
1225 || ra.block.compressed_stream_offset
1226 != rb.block.compressed_stream_offset
1227 || ra.block.uncompressed_stream_offset
1228 != rb.block.uncompressed_stream_offset
1229 || ra.block.uncompressed_size
1230 != rb.block.uncompressed_size
1231 || ra.block.unpadded_size
1232 != rb.block.unpadded_size
1233 || ra.block.total_size
1234 != rb.block.total_size)
1235 return false;
1240 // Allocator that succeeds for the first two allocation but fails the rest.
1241 static void *
1242 my_alloc(void *opaque, size_t a, size_t b)
1244 (void)opaque;
1246 static unsigned count = 0;
1247 if (++count > 2)
1248 return NULL;
1250 return malloc(a * b);
1253 static const lzma_allocator test_index_dup_alloc = { &my_alloc, NULL, NULL };
1256 static void
1257 test_lzma_index_dup(void)
1259 lzma_index *idx = lzma_index_init(NULL);
1260 assert_true(idx != NULL);
1262 // Test for the bug fix 21515d79d778b8730a434f151b07202d52a04611:
1263 // liblzma: Fix lzma_index_dup() for empty Streams.
1264 assert_lzma_ret(lzma_index_stream_padding(idx, 4), LZMA_OK);
1265 lzma_index *copy = lzma_index_dup(idx, NULL);
1266 assert_true(copy != NULL);
1267 assert_true(index_is_equal(idx, copy));
1268 lzma_index_end(copy, NULL);
1270 // Test for the bug fix 3bf857edfef51374f6f3fffae3d817f57d3264a0:
1271 // liblzma: Fix a memory leak in error path of lzma_index_dup().
1272 // Use Valgrind to see that there are no leaks.
1273 assert_lzma_ret(lzma_index_append(idx, NULL,
1274 UNPADDED_SIZE_MIN, 10), LZMA_OK);
1275 assert_lzma_ret(lzma_index_append(idx, NULL,
1276 UNPADDED_SIZE_MIN * 2, 100), LZMA_OK);
1277 assert_lzma_ret(lzma_index_append(idx, NULL,
1278 UNPADDED_SIZE_MIN * 3, 1000), LZMA_OK);
1280 assert_true(lzma_index_dup(idx, &test_index_dup_alloc) == NULL);
1282 // Test a few streams and blocks
1283 lzma_index *second = lzma_index_init(NULL);
1284 assert_true(second != NULL);
1286 assert_lzma_ret(lzma_index_stream_padding(second, 16), LZMA_OK);
1288 lzma_index *third = lzma_index_init(NULL);
1289 assert_true(third != NULL);
1291 assert_lzma_ret(lzma_index_append(third, NULL,
1292 UNPADDED_SIZE_MIN * 10, 40), LZMA_OK);
1293 assert_lzma_ret(lzma_index_append(third, NULL,
1294 UNPADDED_SIZE_MIN * 20, 400), LZMA_OK);
1295 assert_lzma_ret(lzma_index_append(third, NULL,
1296 UNPADDED_SIZE_MIN * 30, 4000), LZMA_OK);
1298 assert_lzma_ret(lzma_index_cat(idx, second, NULL), LZMA_OK);
1299 assert_lzma_ret(lzma_index_cat(idx, third, NULL), LZMA_OK);
1301 copy = lzma_index_dup(idx, NULL);
1302 assert_true(copy != NULL);
1303 assert_true(index_is_equal(idx, copy));
1305 lzma_index_end(copy, NULL);
1306 lzma_index_end(idx, NULL);
1309 #if defined(HAVE_ENCODERS) && defined(HAVE_DECODERS)
1310 static void
1311 verify_index_buffer(const lzma_index *idx, const uint8_t *buffer,
1312 const size_t buffer_size)
1314 lzma_index_iter iter;
1315 lzma_index_iter_init(&iter, idx);
1317 size_t buffer_pos = 0;
1319 // Verify Index Indicator
1320 assert_uint_eq(buffer[buffer_pos++], 0);
1322 // Get Number of Records
1323 lzma_vli number_of_records = 0;
1324 lzma_vli block_count = 0;
1325 assert_lzma_ret(lzma_vli_decode(&number_of_records, NULL, buffer,
1326 &buffer_pos, buffer_size), LZMA_OK);
1328 while (!lzma_index_iter_next(&iter, LZMA_INDEX_ITER_ANY)) {
1329 // Verify each Record (Unpadded Size, then Uncompressed Size).
1330 // Verify Unpadded Size.
1331 lzma_vli unpadded_size, uncompressed_size;
1332 assert_lzma_ret(lzma_vli_decode(&unpadded_size,
1333 NULL, buffer, &buffer_pos,
1334 buffer_size), LZMA_OK);
1335 assert_uint_eq(unpadded_size,
1336 iter.block.unpadded_size);
1338 // Verify Uncompressed Size
1339 assert_lzma_ret(lzma_vli_decode(&uncompressed_size,
1340 NULL, buffer, &buffer_pos,
1341 buffer_size), LZMA_OK);
1342 assert_uint_eq(uncompressed_size,
1343 iter.block.uncompressed_size);
1345 block_count++;
1348 // Verify Number of Records
1349 assert_uint_eq(number_of_records, block_count);
1351 // Verify Index Padding
1352 for (; buffer_pos % 4 != 0; buffer_pos++)
1353 assert_uint_eq(buffer[buffer_pos], 0);
1355 // Verify CRC32
1356 uint32_t crc32 = lzma_crc32(buffer, buffer_pos, 0);
1357 assert_uint_eq(read32le(buffer + buffer_pos), crc32);
1361 // In a few places the Index size is needed as a size_t but lzma_index_size()
1362 // returns lzma_vli.
1363 static size_t
1364 get_index_size(const lzma_index *idx)
1366 const lzma_vli size = lzma_index_size(idx);
1367 assert_uint(size, <, SIZE_MAX);
1368 return (size_t)size;
1370 #endif
1373 static void
1374 test_lzma_index_encoder(void)
1376 #if !defined(HAVE_ENCODERS) || !defined(HAVE_DECODERS)
1377 assert_skip("Encoder or decoder support disabled");
1378 #else
1379 lzma_index *idx = lzma_index_init(NULL);
1380 assert_true(idx != NULL);
1382 lzma_stream strm = LZMA_STREAM_INIT;
1384 // First do basic NULL checks
1385 assert_lzma_ret(lzma_index_encoder(NULL, NULL), LZMA_PROG_ERROR);
1386 assert_lzma_ret(lzma_index_encoder(&strm, NULL), LZMA_PROG_ERROR);
1387 assert_lzma_ret(lzma_index_encoder(NULL, idx), LZMA_PROG_ERROR);
1389 // Append three small Blocks
1390 assert_lzma_ret(lzma_index_append(idx, NULL,
1391 UNPADDED_SIZE_MIN, 10), LZMA_OK);
1392 assert_lzma_ret(lzma_index_append(idx, NULL,
1393 UNPADDED_SIZE_MIN * 2, 100), LZMA_OK);
1394 assert_lzma_ret(lzma_index_append(idx, NULL,
1395 UNPADDED_SIZE_MIN * 3, 1000), LZMA_OK);
1397 // Encode this lzma_index into a buffer
1398 size_t buffer_size = get_index_size(idx);
1399 uint8_t *buffer = tuktest_malloc(buffer_size);
1401 assert_lzma_ret(lzma_index_encoder(&strm, idx), LZMA_OK);
1403 strm.avail_out = buffer_size;
1404 strm.next_out = buffer;
1406 assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
1407 assert_uint_eq(strm.avail_out, 0);
1409 lzma_end(&strm);
1411 verify_index_buffer(idx, buffer, buffer_size);
1413 // Test with multiple Streams concatenated into 1 Index
1414 lzma_index *second = lzma_index_init(NULL);
1415 assert_true(second != NULL);
1417 // Include 1 Block
1418 assert_lzma_ret(lzma_index_append(second, NULL,
1419 UNPADDED_SIZE_MIN * 4, 20), LZMA_OK);
1421 // Include Stream Padding
1422 assert_lzma_ret(lzma_index_stream_padding(second, 16), LZMA_OK);
1424 assert_lzma_ret(lzma_index_cat(idx, second, NULL), LZMA_OK);
1425 buffer_size = get_index_size(idx);
1426 buffer = tuktest_malloc(buffer_size);
1427 assert_lzma_ret(lzma_index_encoder(&strm, idx), LZMA_OK);
1429 strm.avail_out = buffer_size;
1430 strm.next_out = buffer;
1432 assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
1433 assert_uint_eq(strm.avail_out, 0);
1435 verify_index_buffer(idx, buffer, buffer_size);
1437 lzma_index_end(idx, NULL);
1438 lzma_end(&strm);
1439 #endif
1442 static void
1443 generate_index_decode_buffer(void)
1445 #ifdef HAVE_ENCODERS
1446 decode_test_index = lzma_index_init(NULL);
1447 if (decode_test_index == NULL)
1448 return;
1450 // Add 4 Blocks
1451 for (uint32_t i = 1; i < 5; i++)
1452 if (lzma_index_append(decode_test_index, NULL,
1453 0x1000 * i, 0x100 * i) != LZMA_OK)
1454 return;
1456 size_t size = lzma_index_size(decode_test_index);
1457 decode_buffer = tuktest_malloc(size);
1459 if (lzma_index_buffer_encode(decode_test_index,
1460 decode_buffer, &decode_buffer_size, size) != LZMA_OK)
1461 decode_buffer_size = 0;
1462 #endif
1466 #ifdef HAVE_DECODERS
1467 static void
1468 decode_index(const uint8_t *buffer, const size_t size, lzma_stream *strm,
1469 lzma_ret expected_error)
1471 strm->avail_in = size;
1472 strm->next_in = buffer;
1473 assert_lzma_ret(lzma_code(strm, LZMA_FINISH), expected_error);
1475 #endif
1478 static void
1479 test_lzma_index_decoder(void)
1481 #ifndef HAVE_DECODERS
1482 assert_skip("Decoder support disabled");
1483 #else
1484 if (decode_buffer_size == 0)
1485 assert_skip("Could not initialize decode test buffer");
1487 lzma_stream strm = LZMA_STREAM_INIT;
1489 assert_lzma_ret(lzma_index_decoder(NULL, NULL, MEMLIMIT),
1490 LZMA_PROG_ERROR);
1491 assert_lzma_ret(lzma_index_decoder(&strm, NULL, MEMLIMIT),
1492 LZMA_PROG_ERROR);
1493 assert_lzma_ret(lzma_index_decoder(NULL, &decode_test_index,
1494 MEMLIMIT), LZMA_PROG_ERROR);
1496 // Do actual decode
1497 lzma_index *idx;
1498 assert_lzma_ret(lzma_index_decoder(&strm, &idx, MEMLIMIT),
1499 LZMA_OK);
1501 decode_index(decode_buffer, decode_buffer_size, &strm,
1502 LZMA_STREAM_END);
1504 // Compare results with expected
1505 assert_true(index_is_equal(decode_test_index, idx));
1507 lzma_index_end(idx, NULL);
1509 // Test again with too low memory limit
1510 assert_lzma_ret(lzma_index_decoder(&strm, &idx, 0), LZMA_OK);
1512 decode_index(decode_buffer, decode_buffer_size, &strm,
1513 LZMA_MEMLIMIT_ERROR);
1515 uint8_t *corrupt_buffer = tuktest_malloc(decode_buffer_size);
1516 memcpy(corrupt_buffer, decode_buffer, decode_buffer_size);
1518 assert_lzma_ret(lzma_index_decoder(&strm, &idx, MEMLIMIT),
1519 LZMA_OK);
1521 // First corrupt the Index Indicator
1522 corrupt_buffer[0] ^= 1;
1523 decode_index(corrupt_buffer, decode_buffer_size, &strm,
1524 LZMA_DATA_ERROR);
1525 corrupt_buffer[0] ^= 1;
1527 // Corrupt something in the middle of Index
1528 corrupt_buffer[decode_buffer_size / 2] ^= 1;
1529 assert_lzma_ret(lzma_index_decoder(&strm, &idx, MEMLIMIT),
1530 LZMA_OK);
1531 decode_index(corrupt_buffer, decode_buffer_size, &strm,
1532 LZMA_DATA_ERROR);
1533 corrupt_buffer[decode_buffer_size / 2] ^= 1;
1535 // Corrupt CRC32
1536 corrupt_buffer[decode_buffer_size - 1] ^= 1;
1537 assert_lzma_ret(lzma_index_decoder(&strm, &idx, MEMLIMIT),
1538 LZMA_OK);
1539 decode_index(corrupt_buffer, decode_buffer_size, &strm,
1540 LZMA_DATA_ERROR);
1541 corrupt_buffer[decode_buffer_size - 1] ^= 1;
1543 // Corrupt Index Padding by setting it to non-zero
1544 corrupt_buffer[decode_buffer_size - 5] ^= 1;
1545 assert_lzma_ret(lzma_index_decoder(&strm, &idx, MEMLIMIT),
1546 LZMA_OK);
1547 decode_index(corrupt_buffer, decode_buffer_size, &strm,
1548 LZMA_DATA_ERROR);
1549 corrupt_buffer[decode_buffer_size - 1] ^= 1;
1551 lzma_end(&strm);
1552 #endif
1556 static void
1557 test_lzma_index_buffer_encode(void)
1559 #if !defined(HAVE_ENCODERS) || !defined(HAVE_DECODERS)
1560 assert_skip("Encoder or decoder support disabled");
1561 #else
1562 // More simple test than test_lzma_index_encoder() because
1563 // currently lzma_index_buffer_encode() is mostly a wrapper
1564 // around lzma_index_encoder() anyway.
1565 lzma_index *idx = lzma_index_init(NULL);
1566 assert_true(idx != NULL);
1568 assert_lzma_ret(lzma_index_append(idx, NULL,
1569 UNPADDED_SIZE_MIN, 10), LZMA_OK);
1570 assert_lzma_ret(lzma_index_append(idx, NULL,
1571 UNPADDED_SIZE_MIN * 2, 100), LZMA_OK);
1572 assert_lzma_ret(lzma_index_append(idx, NULL,
1573 UNPADDED_SIZE_MIN * 3, 1000), LZMA_OK);
1575 size_t buffer_size = get_index_size(idx);
1576 uint8_t *buffer = tuktest_malloc(buffer_size);
1577 size_t out_pos = 1;
1579 // First test bad arguments
1580 assert_lzma_ret(lzma_index_buffer_encode(NULL, NULL, NULL, 0),
1581 LZMA_PROG_ERROR);
1582 assert_lzma_ret(lzma_index_buffer_encode(idx, NULL, NULL, 0),
1583 LZMA_PROG_ERROR);
1584 assert_lzma_ret(lzma_index_buffer_encode(idx, buffer, NULL, 0),
1585 LZMA_PROG_ERROR);
1586 assert_lzma_ret(lzma_index_buffer_encode(idx, buffer, &out_pos,
1587 0), LZMA_PROG_ERROR);
1588 out_pos = 0;
1589 assert_lzma_ret(lzma_index_buffer_encode(idx, buffer, &out_pos,
1590 1), LZMA_BUF_ERROR);
1592 // Do encoding
1593 assert_lzma_ret(lzma_index_buffer_encode(idx, buffer, &out_pos,
1594 buffer_size), LZMA_OK);
1595 assert_uint_eq(out_pos, buffer_size);
1597 // Validate results
1598 verify_index_buffer(idx, buffer, buffer_size);
1600 lzma_index_end(idx, NULL);
1601 #endif
1605 static void
1606 test_lzma_index_buffer_decode(void)
1608 #ifndef HAVE_DECODERS
1609 assert_skip("Decoder support disabled");
1610 #else
1611 if (decode_buffer_size == 0)
1612 assert_skip("Could not initialize decode test buffer");
1614 // Simple test since test_lzma_index_decoder() covers most of the
1615 // lzma_index_buffer_decode() code anyway.
1617 // First test NULL checks
1618 assert_lzma_ret(lzma_index_buffer_decode(NULL, NULL, NULL, NULL,
1619 NULL, 0), LZMA_PROG_ERROR);
1621 lzma_index *idx;
1622 uint64_t memlimit = MEMLIMIT;
1623 size_t in_pos = 0;
1625 assert_lzma_ret(lzma_index_buffer_decode(&idx, NULL, NULL, NULL,
1626 NULL, 0), LZMA_PROG_ERROR);
1628 assert_lzma_ret(lzma_index_buffer_decode(&idx, &memlimit, NULL,
1629 NULL, NULL, 0), LZMA_PROG_ERROR);
1631 assert_lzma_ret(lzma_index_buffer_decode(&idx, &memlimit, NULL,
1632 decode_buffer, NULL, 0), LZMA_PROG_ERROR);
1634 assert_lzma_ret(lzma_index_buffer_decode(&idx, &memlimit, NULL,
1635 decode_buffer, NULL, 0), LZMA_PROG_ERROR);
1637 assert_lzma_ret(lzma_index_buffer_decode(&idx, &memlimit, NULL,
1638 decode_buffer, &in_pos, 0), LZMA_DATA_ERROR);
1640 in_pos = 1;
1641 assert_lzma_ret(lzma_index_buffer_decode(&idx, &memlimit, NULL,
1642 decode_buffer, &in_pos, 0), LZMA_PROG_ERROR);
1643 in_pos = 0;
1645 // Test expected successful decode
1646 assert_lzma_ret(lzma_index_buffer_decode(&idx, &memlimit, NULL,
1647 decode_buffer, &in_pos, decode_buffer_size), LZMA_OK);
1649 assert_true(index_is_equal(decode_test_index, idx));
1651 lzma_index_end(idx, NULL);
1653 // Test too small memlimit
1654 in_pos = 0;
1655 memlimit = 1;
1656 assert_lzma_ret(lzma_index_buffer_decode(&idx, &memlimit, NULL,
1657 decode_buffer, &in_pos, decode_buffer_size),
1658 LZMA_MEMLIMIT_ERROR);
1659 assert_uint(memlimit, >, 1);
1660 assert_uint(memlimit, <, MEMLIMIT);
1661 #endif
1665 extern int
1666 main(int argc, char **argv)
1668 tuktest_start(argc, argv);
1669 generate_index_decode_buffer();
1670 tuktest_run(test_lzma_index_memusage);
1671 tuktest_run(test_lzma_index_memused);
1672 tuktest_run(test_lzma_index_append);
1673 tuktest_run(test_lzma_index_stream_flags);
1674 tuktest_run(test_lzma_index_checks);
1675 tuktest_run(test_lzma_index_stream_padding);
1676 tuktest_run(test_lzma_index_stream_count);
1677 tuktest_run(test_lzma_index_block_count);
1678 tuktest_run(test_lzma_index_size);
1679 tuktest_run(test_lzma_index_stream_size);
1680 tuktest_run(test_lzma_index_total_size);
1681 tuktest_run(test_lzma_index_file_size);
1682 tuktest_run(test_lzma_index_uncompressed_size);
1683 tuktest_run(test_lzma_index_iter_init);
1684 tuktest_run(test_lzma_index_iter_rewind);
1685 tuktest_run(test_lzma_index_iter_next);
1686 tuktest_run(test_lzma_index_iter_locate);
1687 tuktest_run(test_lzma_index_cat);
1688 tuktest_run(test_lzma_index_dup);
1689 tuktest_run(test_lzma_index_encoder);
1690 tuktest_run(test_lzma_index_decoder);
1691 tuktest_run(test_lzma_index_buffer_encode);
1692 tuktest_run(test_lzma_index_buffer_decode);
1693 lzma_index_end(decode_test_index, NULL);
1694 return tuktest_end();