Rizin
unix-like reverse engineering framework and cli tools
test_index.c File Reference

Tests functions handling the lzma_index structure. More...

#include "tests.h"

Go to the source code of this file.

Macros

#define MEMLIMIT   (LZMA_VLI_C(1) << 20)
 
#define SMALL_COUNT   3
 
#define BIG_COUNT   5555
 

Functions

static lzma_indexcreate_empty (void)
 
static lzma_indexcreate_small (void)
 
static lzma_indexcreate_big (void)
 
static bool is_equal (const lzma_index *a, const lzma_index *b)
 
static void test_equal (void)
 
static void test_overflow (void)
 
static void test_copy (const lzma_index *i)
 
static void test_read (lzma_index *i)
 
static void test_code (lzma_index *i)
 
static void test_many (lzma_index *i)
 
static void test_cat (void)
 
static void test_locate (void)
 
static void test_corrupt (void)
 
static void * my_alloc (void *opaque, size_t a, size_t b)
 
int main (void)
 

Variables

static const lzma_allocator my_allocator = { &my_alloc, NULL, NULL }
 

Detailed Description

Tests functions handling the lzma_index structure.

Definition in file test_index.c.

Macro Definition Documentation

◆ BIG_COUNT

#define BIG_COUNT   5555

Definition at line 18 of file test_index.c.

◆ MEMLIMIT

#define MEMLIMIT   (LZMA_VLI_C(1) << 20)

Definition at line 15 of file test_index.c.

◆ SMALL_COUNT

#define SMALL_COUNT   3

Definition at line 17 of file test_index.c.

Function Documentation

◆ create_big()

static lzma_index* create_big ( void  )
static

Definition at line 43 of file test_index.c.

44 {
45  lzma_index *i = lzma_index_init(NULL);
46  expect(i != NULL);
47 
48  lzma_vli total_size = 0;
50 
51  // Add pseudo-random sizes (but always the same size values).
52  uint32_t n = 11;
53  for (size_t j = 0; j < BIG_COUNT; ++j) {
54  n = 7019 * n + 7607;
55  const uint32_t t = n * 3011;
56  expect(lzma_index_append(i, NULL, t, n) == LZMA_OK);
57  total_size += (t + 3) & ~LZMA_VLI_C(3);
59  }
60 
61  expect(lzma_index_block_count(i) == BIG_COUNT);
62  expect(lzma_index_total_size(i) == total_size);
63  expect(lzma_index_uncompressed_size(i) == uncompressed_size);
64  expect(lzma_index_total_size(i) + lzma_index_size(i)
66  == lzma_index_stream_size(i));
67 
68  return i;
69 }
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
#define expect(expr, value)
Definition: lz4.c:170
int n
Definition: mipsasm.c:19
unsigned int uint32_t
Definition: sftypes.h:29
#define LZMA_STREAM_HEADER_SIZE
Size of Stream Header and Stream Footer.
Definition: stream_flags.h:27
uint64_t uncompressed_size
Definition: list.c:106
#define BIG_COUNT
Definition: test_index.c:18
uint64_t lzma_vli
Variable-length integer type.
Definition: vli.h:63
#define LZMA_VLI_C(n)
VLI constant suffix.
Definition: vli.h:49
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58

References BIG_COUNT, expect, i, LZMA_OK, LZMA_STREAM_HEADER_SIZE, LZMA_VLI_C, n, NULL, and uncompressed_size.

Referenced by main(), test_cat(), and test_equal().

◆ create_empty()

static lzma_index* create_empty ( void  )
static

Definition at line 22 of file test_index.c.

23 {
24  lzma_index *i = lzma_index_init(NULL);
25  expect(i != NULL);
26  return i;
27 }

References expect, i, and NULL.

Referenced by main(), test_cat(), test_corrupt(), test_equal(), and test_overflow().

◆ create_small()

static lzma_index* create_small ( void  )
static

Definition at line 31 of file test_index.c.

32 {
33  lzma_index *i = lzma_index_init(NULL);
34  expect(i != NULL);
35  expect(lzma_index_append(i, NULL, 101, 555) == LZMA_OK);
36  expect(lzma_index_append(i, NULL, 602, 777) == LZMA_OK);
37  expect(lzma_index_append(i, NULL, 804, 999) == LZMA_OK);
38  return i;
39 }

References expect, i, LZMA_OK, and NULL.

Referenced by main(), test_cat(), test_corrupt(), and test_equal().

◆ is_equal()

static bool is_equal ( const lzma_index a,
const lzma_index b 
)
static

Definition at line 73 of file test_index.c.

74 {
75  // Compare only the Stream and Block sizes and offsets.
76  lzma_index_iter ra, rb;
77  lzma_index_iter_init(&ra, a);
78  lzma_index_iter_init(&rb, b);
79 
80  while (true) {
81  bool reta = lzma_index_iter_next(&ra, LZMA_INDEX_ITER_ANY);
82  bool retb = lzma_index_iter_next(&rb, LZMA_INDEX_ITER_ANY);
83  if (reta)
84  return !(reta ^ retb);
85 
86  if (ra.stream.number != rb.stream.number
87  || ra.stream.block_count
88  != rb.stream.block_count
97  || ra.stream.padding
98  != rb.stream.padding)
99  return false;
100 
101  if (ra.stream.block_count == 0)
102  continue;
103 
109  || ra.block.number_in_stream
110  != rb.block.number_in_stream
117  || ra.block.unpadded_size
118  != rb.block.unpadded_size
119  || ra.block.total_size
120  != rb.block.total_size)
121  return false;
122  }
123 }
@ LZMA_INDEX_ITER_ANY
Get the next Block or Stream.
Definition: index.h:227
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41
Iterator to get information about Blocks and Streams.
Definition: index.h:43
struct lzma_index_iter::@635 stream
lzma_vli uncompressed_stream_offset
Uncompressed start offset of this Block.
Definition: index.h:170
lzma_vli number
Stream number in the lzma_index.
Definition: index.h:62
lzma_vli compressed_file_offset
Compressed start offset of this Block.
Definition: index.h:132
lzma_vli uncompressed_file_offset
Uncompressed start offset of this Block.
Definition: index.h:147
lzma_vli number_in_stream
Block number in this Stream.
Definition: index.h:154
lzma_vli compressed_offset
Compressed start offset of this Stream.
Definition: index.h:78
lzma_vli padding
Size of Stream Padding after this Stream.
Definition: index.h:108
lzma_vli compressed_stream_offset
Compressed start offset of this Block.
Definition: index.h:162
lzma_vli compressed_size
Compressed size of this Stream.
Definition: index.h:94
struct lzma_index_iter::@636 block
lzma_vli unpadded_size
Unpadded size of this Block.
Definition: index.h:188
lzma_vli uncompressed_size
Uncompressed size of this Stream.
Definition: index.h:99
lzma_vli block_count
Number of Blocks in the Stream.
Definition: index.h:70
lzma_vli uncompressed_offset
Uncompressed start offset of this Stream.
Definition: index.h:86
lzma_vli number_in_file
Block number in the file.
Definition: index.h:122
lzma_vli total_size
Total compressed size.
Definition: index.h:197

References a, b, lzma_index_iter::block, lzma_index_iter::block_count, lzma_index_iter::compressed_file_offset, lzma_index_iter::compressed_offset, lzma_index_iter::compressed_size, lzma_index_iter::compressed_stream_offset, LZMA_INDEX_ITER_ANY, lzma_index_iter::number, lzma_index_iter::number_in_file, lzma_index_iter::number_in_stream, lzma_index_iter::padding, lzma_index_iter::stream, lzma_index_iter::total_size, lzma_index_iter::uncompressed_file_offset, lzma_index_iter::uncompressed_offset, lzma_index_iter::uncompressed_size, lzma_index_iter::uncompressed_stream_offset, and lzma_index_iter::unpadded_size.

Referenced by test_code(), test_copy(), and test_equal().

◆ main()

int main ( void  )

Definition at line 650 of file test_index.c.

651 {
652  test_equal();
653 
654  test_overflow();
655 
657  test_many(i);
658  lzma_index_end(i, NULL);
659 
660  i = create_small();
661  test_many(i);
662  lzma_index_end(i, NULL);
663 
664  i = create_big();
665  test_many(i);
666  lzma_index_end(i, NULL);
667 
668  test_cat();
669 
670  test_locate();
671 
672  test_corrupt();
673 
674  // Test for the bug fix 21515d79d778b8730a434f151b07202d52a04611:
675  // liblzma: Fix lzma_index_dup() for empty Streams.
676  i = create_empty();
677  expect(lzma_index_stream_padding(i, 4) == LZMA_OK);
678  test_copy(i);
679  lzma_index_end(i, NULL);
680 
681  // Test for the bug fix 3bf857edfef51374f6f3fffae3d817f57d3264a0:
682  // liblzma: Fix a memory leak in error path of lzma_index_dup().
683  // Use Valgrind to see that there are no leaks.
684  i = create_small();
685  expect(lzma_index_dup(i, &my_allocator) == NULL);
686  lzma_index_end(i, NULL);
687 
688  return 0;
689 }
static void test_cat(void)
Definition: test_index.c:291
static void test_equal(void)
Definition: test_index.c:127
static void test_overflow(void)
Definition: test_index.c:149
static lzma_index * create_empty(void)
Definition: test_index.c:22
static lzma_index * create_small(void)
Definition: test_index.c:31
static lzma_index * create_big(void)
Definition: test_index.c:43
static void test_many(lzma_index *i)
Definition: test_index.c:282
static void test_copy(const lzma_index *i)
Definition: test_index.c:164
static void test_locate(void)
Definition: test_index.c:418
static const lzma_allocator my_allocator
Definition: test_index.c:646
static void test_corrupt(void)
Definition: test_index.c:593

References create_big(), create_empty(), create_small(), expect, i, LZMA_OK, my_allocator, NULL, test_cat(), test_copy(), test_corrupt(), test_equal(), test_locate(), test_many(), and test_overflow().

◆ my_alloc()

static void* my_alloc ( void *  opaque,
size_t  a,
size_t  b 
)
static

Definition at line 635 of file test_index.c.

636 {
637  (void)opaque;
638 
639  static unsigned count = 0;
640  if (++count > 2)
641  return NULL;
642 
643  return malloc(a * b);
644 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
void * malloc(size_t size)
Definition: malloc.c:123

References a, b, count, malloc(), and NULL.

◆ test_cat()

static void test_cat ( void  )
static

Definition at line 291 of file test_index.c.

292 {
293  lzma_index *a, *b, *c;
295 
296  // Empty Indexes
297  a = create_empty();
298  b = create_empty();
299  expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
300  expect(lzma_index_block_count(a) == 0);
301  expect(lzma_index_stream_size(a) == 2 * LZMA_STREAM_HEADER_SIZE + 8);
302  expect(lzma_index_file_size(a)
303  == 2 * (2 * LZMA_STREAM_HEADER_SIZE + 8));
304  lzma_index_iter_init(&r, a);
305  expect(lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK));
306 
307  b = create_empty();
308  expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
309  expect(lzma_index_block_count(a) == 0);
310  expect(lzma_index_stream_size(a) == 2 * LZMA_STREAM_HEADER_SIZE + 8);
311  expect(lzma_index_file_size(a)
312  == 3 * (2 * LZMA_STREAM_HEADER_SIZE + 8));
313 
314  b = create_empty();
315  c = create_empty();
316  expect(lzma_index_stream_padding(b, 4) == LZMA_OK);
317  expect(lzma_index_cat(b, c, NULL) == LZMA_OK);
318  expect(lzma_index_block_count(b) == 0);
319  expect(lzma_index_stream_size(b) == 2 * LZMA_STREAM_HEADER_SIZE + 8);
320  expect(lzma_index_file_size(b)
321  == 2 * (2 * LZMA_STREAM_HEADER_SIZE + 8) + 4);
322 
323  expect(lzma_index_stream_padding(a, 8) == LZMA_OK);
324  expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
325  expect(lzma_index_block_count(a) == 0);
326  expect(lzma_index_stream_size(a) == 2 * LZMA_STREAM_HEADER_SIZE + 8);
327  expect(lzma_index_file_size(a)
328  == 5 * (2 * LZMA_STREAM_HEADER_SIZE + 8) + 4 + 8);
329 
330  expect(lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK));
331  lzma_index_iter_rewind(&r);
332  expect(lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK));
333  lzma_index_end(a, NULL);
334 
335  // Small Indexes
336  a = create_small();
337  lzma_vli stream_size = lzma_index_stream_size(a);
338  lzma_index_iter_init(&r, a);
339  for (int i = SMALL_COUNT; i >= 0; --i)
340  expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
341  ^ (i == 0));
342 
343  b = create_small();
344  expect(lzma_index_stream_padding(a, 4) == LZMA_OK);
345  expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
346  expect(lzma_index_file_size(a) == stream_size * 2 + 4);
347  expect(lzma_index_stream_size(a) > stream_size);
348  expect(lzma_index_stream_size(a) < stream_size * 2);
349  for (int i = SMALL_COUNT; i >= 0; --i)
350  expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
351  ^ (i == 0));
352 
353  lzma_index_iter_rewind(&r);
354  for (int i = SMALL_COUNT * 2; i >= 0; --i)
355  expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
356  ^ (i == 0));
357 
358  b = create_small();
359  c = create_small();
360  expect(lzma_index_stream_padding(b, 8) == LZMA_OK);
361  expect(lzma_index_cat(b, c, NULL) == LZMA_OK);
362  expect(lzma_index_stream_padding(a, 12) == LZMA_OK);
363  expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
364  expect(lzma_index_file_size(a) == stream_size * 4 + 4 + 8 + 12);
365 
366  expect(lzma_index_block_count(a) == SMALL_COUNT * 4);
367  for (int i = SMALL_COUNT * 2; i >= 0; --i)
368  expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
369  ^ (i == 0));
370 
371  lzma_index_iter_rewind(&r);
372  for (int i = SMALL_COUNT * 4; i >= 0; --i)
373  expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
374  ^ (i == 0));
375 
376  lzma_index_end(a, NULL);
377 
378  // Mix of empty and small
379  a = create_empty();
380  b = create_small();
381  expect(lzma_index_stream_padding(a, 4) == LZMA_OK);
382  expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
383  lzma_index_iter_init(&r, a);
384  for (int i = SMALL_COUNT; i >= 0; --i)
385  expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
386  ^ (i == 0));
387 
388  lzma_index_end(a, NULL);
389 
390  // Big Indexes
391  a = create_big();
392  stream_size = lzma_index_stream_size(a);
393  b = create_big();
394  expect(lzma_index_stream_padding(a, 4) == LZMA_OK);
395  expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
396  expect(lzma_index_file_size(a) == stream_size * 2 + 4);
397  expect(lzma_index_stream_size(a) > stream_size);
398  expect(lzma_index_stream_size(a) < stream_size * 2);
399 
400  b = create_big();
401  c = create_big();
402  expect(lzma_index_stream_padding(b, 8) == LZMA_OK);
403  expect(lzma_index_cat(b, c, NULL) == LZMA_OK);
404  expect(lzma_index_stream_padding(a, 12) == LZMA_OK);
405  expect(lzma_index_cat(a, b, NULL) == LZMA_OK);
406  expect(lzma_index_file_size(a) == stream_size * 4 + 4 + 8 + 12);
407 
408  lzma_index_iter_init(&r, a);
409  for (int i = BIG_COUNT * 4; i >= 0; --i)
410  expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)
411  ^ (i == 0));
412 
413  lzma_index_end(a, NULL);
414 }
@ LZMA_INDEX_ITER_BLOCK
Get the next Block.
Definition: index.h:249
#define r
Definition: crypto_rc6.c:12
#define c(i)
Definition: sha256.c:43
#define SMALL_COUNT
Definition: test_index.c:17

References a, b, BIG_COUNT, c, create_big(), create_empty(), create_small(), expect, i, LZMA_INDEX_ITER_BLOCK, LZMA_OK, LZMA_STREAM_HEADER_SIZE, NULL, r, and SMALL_COUNT.

Referenced by main().

◆ test_code()

static void test_code ( lzma_index i)
static

Definition at line 212 of file test_index.c.

213 {
214  const size_t alloc_size = 128 * 1024;
215  uint8_t *buf = malloc(alloc_size);
216  expect(buf != NULL);
217 
218  // Encode
220  expect(lzma_index_encoder(&strm, i) == LZMA_OK);
221  const lzma_vli index_size = lzma_index_size(i);
224 
225  // Decode
226  lzma_index *d;
227  expect(lzma_index_decoder(&strm, &d, MEMLIMIT) == LZMA_OK);
228  expect(d == NULL);
230 
231  expect(is_equal(i, d));
232 
233  lzma_index_end(d, NULL);
234  lzma_end(&strm);
235 
236  // Decode with hashing
237  lzma_index_hash *h = lzma_index_hash_init(NULL, NULL);
238  expect(h != NULL);
240  lzma_index_iter_init(&r, i);
241  while (!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK))
242  expect(lzma_index_hash_append(h, r.block.unpadded_size,
243  r.block.uncompressed_size) == LZMA_OK);
244  size_t pos = 0;
245  while (pos < index_size - 1)
246  expect(lzma_index_hash_decode(h, buf, &pos, pos + 1)
247  == LZMA_OK);
248  expect(lzma_index_hash_decode(h, buf, &pos, pos + 1)
249  == LZMA_STREAM_END);
250 
251  lzma_index_hash_end(h, NULL);
252 
253  // Encode buffer
254  size_t buf_pos = 1;
255  expect(lzma_index_buffer_encode(i, buf, &buf_pos, index_size)
256  == LZMA_BUF_ERROR);
257  expect(buf_pos == 1);
258 
259  succeed(lzma_index_buffer_encode(i, buf, &buf_pos, index_size + 1));
260  expect(buf_pos == index_size + 1);
261 
262  // Decode buffer
263  buf_pos = 1;
265  expect(lzma_index_buffer_decode(&d, &memlimit, NULL, buf, &buf_pos,
267  expect(buf_pos == 1);
268  expect(d == NULL);
269 
270  succeed(lzma_index_buffer_decode(&d, &memlimit, NULL, buf, &buf_pos,
271  index_size + 1));
272  expect(buf_pos == index_size + 1);
273  expect(is_equal(i, d));
274 
275  lzma_index_end(d, NULL);
276 
277  free(buf);
278 }
static lzma_vli index_size(lzma_vli count, lzma_vli index_list_size)
Calculate the size of the Index field including Index Padding.
Definition: index.h:57
uint64_t memlimit
Definition: container.h:537
static lzma_stream strm
Definition: full_flush.c:20
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
unsigned long uint64_t
Definition: sftypes.h:28
unsigned char uint8_t
Definition: sftypes.h:31
#define d(i)
Definition: sha256.c:44
#define h(i)
Definition: sha256.c:48
Passing data to and from liblzma.
Definition: base.h:485
int pos
Definition: main.c:11
static bool is_equal(const lzma_index *a, const lzma_index *b)
Definition: test_index.c:73
#define MEMLIMIT
Definition: test_index.c:15
static bool coder_loop(lzma_stream *strm, uint8_t *in, size_t in_size, uint8_t *out, size_t out_size, lzma_ret expected_ret, lzma_action finishing_action)
Definition: tests.h:58
static bool decoder_loop(lzma_stream *strm, uint8_t *in, size_t in_size)
Definition: tests.h:113
#define succeed(test)
Definition: tests.h:27
@ LZMA_DATA_ERROR
Data is corrupt.
Definition: base.h:172
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_BUF_ERROR
No progress is possible.
Definition: base.h:191
@ LZMA_RUN
Continue coding.
Definition: base.h:251
#define LZMA_STREAM_INIT
Initialization for lzma_stream.
Definition: base.h:545

References coder_loop(), d, decoder_loop(), expect, free(), h, i, index_size(), is_equal(), LZMA_BUF_ERROR, LZMA_DATA_ERROR, LZMA_INDEX_ITER_BLOCK, LZMA_OK, LZMA_RUN, LZMA_STREAM_END, LZMA_STREAM_INIT, malloc(), memlimit, MEMLIMIT, NULL, pos, r, strm, and succeed.

Referenced by test_many().

◆ test_copy()

static void test_copy ( const lzma_index i)
static

Definition at line 164 of file test_index.c.

165 {
166  lzma_index *d = lzma_index_dup(i, NULL);
167  expect(d != NULL);
168  expect(is_equal(i, d));
169  lzma_index_end(d, NULL);
170 }

References d, expect, i, is_equal(), and NULL.

Referenced by main(), and test_many().

◆ test_corrupt()

static void test_corrupt ( void  )
static

Definition at line 593 of file test_index.c.

594 {
595  const size_t alloc_size = 128 * 1024;
596  uint8_t *buf = malloc(alloc_size);
597  expect(buf != NULL);
599 
601  expect(lzma_index_append(i, NULL, 0, 1) == LZMA_PROG_ERROR);
602  lzma_index_end(i, NULL);
603 
604  // Create a valid Index and corrupt it in different ways.
605  i = create_small();
606  expect(lzma_index_encoder(&strm, i) == LZMA_OK);
607  succeed(coder_loop(&strm, NULL, 0, buf, 20,
609  lzma_index_end(i, NULL);
610 
611  // Wrong Index Indicator
612  buf[0] ^= 1;
613  expect(lzma_index_decoder(&strm, &i, MEMLIMIT) == LZMA_OK);
615  buf[0] ^= 1;
616 
617  // Wrong Number of Records and thus CRC32 fails.
618  --buf[1];
619  expect(lzma_index_decoder(&strm, &i, MEMLIMIT) == LZMA_OK);
621  ++buf[1];
622 
623  // Padding not NULs
624  buf[15] ^= 1;
625  expect(lzma_index_decoder(&strm, &i, MEMLIMIT) == LZMA_OK);
627 
628  lzma_end(&strm);
629  free(buf);
630 }
static bool decoder_loop_ret(lzma_stream *strm, uint8_t *in, size_t in_size, lzma_ret expected_ret)
Definition: tests.h:105
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218

References coder_loop(), create_empty(), create_small(), decoder_loop_ret(), expect, free(), i, LZMA_DATA_ERROR, LZMA_OK, LZMA_PROG_ERROR, LZMA_RUN, LZMA_STREAM_END, LZMA_STREAM_INIT, malloc(), MEMLIMIT, NULL, strm, and succeed.

Referenced by main().

◆ test_equal()

static void test_equal ( void  )
static

Definition at line 127 of file test_index.c.

128 {
131  lzma_index *c = create_big();
132  expect(a && b && c);
133 
134  expect(is_equal(a, a));
135  expect(is_equal(b, b));
136  expect(is_equal(c, c));
137 
138  expect(!is_equal(a, b));
139  expect(!is_equal(a, c));
140  expect(!is_equal(b, c));
141 
142  lzma_index_end(a, NULL);
143  lzma_index_end(b, NULL);
144  lzma_index_end(c, NULL);
145 }

References a, b, c, create_big(), create_empty(), create_small(), expect, is_equal(), and NULL.

Referenced by main().

◆ test_locate()

static void test_locate ( void  )
static

Definition at line 418 of file test_index.c.

419 {
420  lzma_index *i = lzma_index_init(NULL);
421  expect(i != NULL);
423  lzma_index_iter_init(&r, i);
424 
425  // Cannot locate anything from an empty Index.
426  expect(lzma_index_iter_locate(&r, 0));
427  expect(lzma_index_iter_locate(&r, 555));
428 
429  // One empty Record: nothing is found since there's no uncompressed
430  // data.
431  expect(lzma_index_append(i, NULL, 16, 0) == LZMA_OK);
432  expect(lzma_index_iter_locate(&r, 0));
433 
434  // Non-empty Record and we can find something.
435  expect(lzma_index_append(i, NULL, 32, 5) == LZMA_OK);
436  expect(!lzma_index_iter_locate(&r, 0));
437  expect(r.block.total_size == 32);
438  expect(r.block.uncompressed_size == 5);
439  expect(r.block.compressed_file_offset
440  == LZMA_STREAM_HEADER_SIZE + 16);
441  expect(r.block.uncompressed_file_offset == 0);
442 
443  // Still cannot find anything past the end.
444  expect(lzma_index_iter_locate(&r, 5));
445 
446  // Add the third Record.
447  expect(lzma_index_append(i, NULL, 40, 11) == LZMA_OK);
448 
449  expect(!lzma_index_iter_locate(&r, 0));
450  expect(r.block.total_size == 32);
451  expect(r.block.uncompressed_size == 5);
452  expect(r.block.compressed_file_offset
453  == LZMA_STREAM_HEADER_SIZE + 16);
454  expect(r.block.uncompressed_file_offset == 0);
455 
456  expect(!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK));
457  expect(r.block.total_size == 40);
458  expect(r.block.uncompressed_size == 11);
459  expect(r.block.compressed_file_offset
460  == LZMA_STREAM_HEADER_SIZE + 16 + 32);
461  expect(r.block.uncompressed_file_offset == 5);
462 
463  expect(!lzma_index_iter_locate(&r, 2));
464  expect(r.block.total_size == 32);
465  expect(r.block.uncompressed_size == 5);
466  expect(r.block.compressed_file_offset
467  == LZMA_STREAM_HEADER_SIZE + 16);
468  expect(r.block.uncompressed_file_offset == 0);
469 
470  expect(!lzma_index_iter_locate(&r, 5));
471  expect(r.block.total_size == 40);
472  expect(r.block.uncompressed_size == 11);
473  expect(r.block.compressed_file_offset
474  == LZMA_STREAM_HEADER_SIZE + 16 + 32);
475  expect(r.block.uncompressed_file_offset == 5);
476 
477  expect(!lzma_index_iter_locate(&r, 5 + 11 - 1));
478  expect(r.block.total_size == 40);
479  expect(r.block.uncompressed_size == 11);
480  expect(r.block.compressed_file_offset
481  == LZMA_STREAM_HEADER_SIZE + 16 + 32);
482  expect(r.block.uncompressed_file_offset == 5);
483 
484  expect(lzma_index_iter_locate(&r, 5 + 11));
485  expect(lzma_index_iter_locate(&r, 5 + 15));
486 
487  // Large Index
488  lzma_index_end(i, NULL);
489  i = lzma_index_init(NULL);
490  expect(i != NULL);
491  lzma_index_iter_init(&r, i);
492 
493  for (size_t n = 4; n <= 4 * 5555; n += 4)
494  expect(lzma_index_append(i, NULL, n + 8, n) == LZMA_OK);
495 
496  expect(lzma_index_block_count(i) == 5555);
497 
498  // First Record
499  expect(!lzma_index_iter_locate(&r, 0));
500  expect(r.block.total_size == 4 + 8);
501  expect(r.block.uncompressed_size == 4);
502  expect(r.block.compressed_file_offset == LZMA_STREAM_HEADER_SIZE);
503  expect(r.block.uncompressed_file_offset == 0);
504 
505  expect(!lzma_index_iter_locate(&r, 3));
506  expect(r.block.total_size == 4 + 8);
507  expect(r.block.uncompressed_size == 4);
508  expect(r.block.compressed_file_offset == LZMA_STREAM_HEADER_SIZE);
509  expect(r.block.uncompressed_file_offset == 0);
510 
511  // Second Record
512  expect(!lzma_index_iter_locate(&r, 4));
513  expect(r.block.total_size == 2 * 4 + 8);
514  expect(r.block.uncompressed_size == 2 * 4);
515  expect(r.block.compressed_file_offset
516  == LZMA_STREAM_HEADER_SIZE + 4 + 8);
517  expect(r.block.uncompressed_file_offset == 4);
518 
519  // Last Record
520  expect(!lzma_index_iter_locate(
521  &r, lzma_index_uncompressed_size(i) - 1));
522  expect(r.block.total_size == 4 * 5555 + 8);
523  expect(r.block.uncompressed_size == 4 * 5555);
524  expect(r.block.compressed_file_offset == lzma_index_total_size(i)
525  + LZMA_STREAM_HEADER_SIZE - 4 * 5555 - 8);
526  expect(r.block.uncompressed_file_offset
527  == lzma_index_uncompressed_size(i) - 4 * 5555);
528 
529  // Allocation chunk boundaries. See INDEX_GROUP_SIZE in
530  // liblzma/common/index.c.
531  const size_t group_multiple = 256 * 4;
532  const size_t radius = 8;
533  const size_t start = group_multiple - radius;
534  lzma_vli ubase = 0;
535  lzma_vli tbase = 0;
536  size_t n;
537  for (n = 1; n < start; ++n) {
538  ubase += n * 4;
539  tbase += n * 4 + 8;
540  }
541 
542  while (n < start + 2 * radius) {
543  expect(!lzma_index_iter_locate(&r, ubase + n * 4));
544 
545  expect(r.block.compressed_file_offset == tbase + n * 4 + 8
547  expect(r.block.uncompressed_file_offset == ubase + n * 4);
548 
549  tbase += n * 4 + 8;
550  ubase += n * 4;
551  ++n;
552 
553  expect(r.block.total_size == n * 4 + 8);
554  expect(r.block.uncompressed_size == n * 4);
555  }
556 
557  // Do it also backwards.
558  while (n > start) {
559  expect(!lzma_index_iter_locate(&r, ubase + (n - 1) * 4));
560 
561  expect(r.block.total_size == n * 4 + 8);
562  expect(r.block.uncompressed_size == n * 4);
563 
564  --n;
565  tbase -= n * 4 + 8;
566  ubase -= n * 4;
567 
568  expect(r.block.compressed_file_offset == tbase + n * 4 + 8
570  expect(r.block.uncompressed_file_offset == ubase + n * 4);
571  }
572 
573  // Test locating in concatenated Index.
574  lzma_index_end(i, NULL);
575  i = lzma_index_init(NULL);
576  expect(i != NULL);
577  lzma_index_iter_init(&r, i);
578  for (n = 0; n < group_multiple; ++n)
579  expect(lzma_index_append(i, NULL, 8, 0) == LZMA_OK);
580  expect(lzma_index_append(i, NULL, 16, 1) == LZMA_OK);
581  expect(!lzma_index_iter_locate(&r, 0));
582  expect(r.block.total_size == 16);
583  expect(r.block.uncompressed_size == 1);
584  expect(r.block.compressed_file_offset
585  == LZMA_STREAM_HEADER_SIZE + group_multiple * 8);
586  expect(r.block.uncompressed_file_offset == 0);
587 
588  lzma_index_end(i, NULL);
589 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133

References expect, i, LZMA_INDEX_ITER_BLOCK, LZMA_OK, LZMA_STREAM_HEADER_SIZE, n, NULL, r, and start.

Referenced by main().

◆ test_many()

static void test_many ( lzma_index i)
static

Definition at line 282 of file test_index.c.

283 {
284  test_copy(i);
285  test_read(i);
286  test_code(i);
287 }
static void test_read(lzma_index *i)
Definition: test_index.c:174
static void test_code(lzma_index *i)
Definition: test_index.c:212

References i, test_code(), test_copy(), and test_read().

Referenced by main().

◆ test_overflow()

static void test_overflow ( void  )
static

Definition at line 149 of file test_index.c.

150 {
151  // Integer overflow tests
153 
154  expect(lzma_index_append(i, NULL, LZMA_VLI_MAX - 5, 1234)
155  == LZMA_DATA_ERROR);
156 
157  // TODO
158 
159  lzma_index_end(i, NULL);
160 }
#define LZMA_VLI_MAX
Maximum supported value of a variable-length integer.
Definition: vli.h:34

References create_empty(), expect, i, LZMA_DATA_ERROR, LZMA_VLI_MAX, and NULL.

Referenced by main().

◆ test_read()

static void test_read ( lzma_index i)
static

Definition at line 174 of file test_index.c.

175 {
177  lzma_index_iter_init(&r, i);
178 
179  // Try twice so we see that rewinding works.
180  for (size_t j = 0; j < 2; ++j) {
181  lzma_vli total_size = 0;
184  lzma_vli uncompressed_offset = 0;
185  uint32_t count = 0;
186 
187  while (!lzma_index_iter_next(&r, LZMA_INDEX_ITER_BLOCK)) {
188  ++count;
189 
190  total_size += r.block.total_size;
191  uncompressed_size += r.block.uncompressed_size;
192 
193  expect(r.block.compressed_file_offset
194  == stream_offset);
195  expect(r.block.uncompressed_file_offset
196  == uncompressed_offset);
197 
198  stream_offset += r.block.total_size;
199  uncompressed_offset += r.block.uncompressed_size;
200  }
201 
202  expect(lzma_index_total_size(i) == total_size);
203  expect(lzma_index_uncompressed_size(i) == uncompressed_size);
204  expect(lzma_index_block_count(i) == count);
205 
206  lzma_index_iter_rewind(&r);
207  }
208 }
static uint32_t stream_offset(Stream *self)
Definition: query.c:386

References count, expect, i, LZMA_INDEX_ITER_BLOCK, LZMA_STREAM_HEADER_SIZE, r, stream_offset(), and uncompressed_size.

Referenced by test_many().

Variable Documentation

◆ my_allocator

const lzma_allocator my_allocator = { &my_alloc, NULL, NULL }
static

Definition at line 646 of file test_index.c.

Referenced by main().