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

Tests Block Header coders. More...

#include "tests.h"

Go to the source code of this file.

Functions

static void code (void)
 
static void test1 (void)
 
static void test2 (void)
 
static void test3 (void)
 
int main (void)
 

Variables

static uint8_t buf [LZMA_BLOCK_HEADER_SIZE_MAX]
 
static lzma_block known_options
 
static lzma_block decoded_options
 
static lzma_options_lzma opt_lzma
 
static lzma_filter filters_none [1]
 
static lzma_filter filters_one [2]
 
static lzma_filter filters_four [5]
 
static lzma_filter filters_five [6]
 

Detailed Description

Tests Block Header coders.

Definition in file test_block_header.c.

Function Documentation

◆ code()

static void code ( void  )
static

Definition at line 81 of file test_block_header.c.

82 {
83  expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK);
84 
86  memcrap(filters, sizeof(filters));
88 
92  expect(lzma_block_header_decode(&decoded_options, NULL, buf)
93  == LZMA_OK);
94 
99 
100  for (size_t i = 0; known_options.filters[i].id
101  != LZMA_VLI_UNKNOWN; ++i)
103 
104  for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i)
106 }
lzma_index ** i
Definition: index.h:629
const lzma_filter * filters
Definition: container.h:315
#define NULL
Definition: cris-opc.c:27
#define LZMA_FILTERS_MAX
Maximum number of filters in a chain.
Definition: filter.h:26
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
#define expect(expr, value)
Definition: lz4.c:170
lzma_vli uncompressed_size
Uncompressed Size in bytes.
Definition: block.h:172
lzma_filter * filters
Array of filters.
Definition: block.h:200
uint32_t header_size
Size of the Block Header field.
Definition: block.h:72
lzma_check check
Type of integrity Check.
Definition: block.h:93
lzma_vli compressed_size
Size of the Compressed Data in bytes.
Definition: block.h:148
Filter options.
Definition: filter.h:43
void * options
Pointer to filter-specific options structure.
Definition: filter.h:63
lzma_vli id
Filter ID.
Definition: filter.h:54
static lzma_block decoded_options
static lzma_block known_options
#define memcrap(buf, size)
Definition: tests.h:22
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
Definition: vli.h:39
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58

References lzma_block::check, lzma_block::compressed_size, decoded_options, expect, lzma_block::filters, filters, free(), lzma_block::header_size, i, lzma_filter::id, known_options, LZMA_FILTERS_MAX, LZMA_OK, LZMA_VLI_UNKNOWN, memcrap, NULL, lzma_filter::options, and lzma_block::uncompressed_size.

Referenced by test2().

◆ main()

int main ( void  )

Definition at line 232 of file test_block_header.c.

233 {
234  succeed(lzma_lzma_preset(&opt_lzma, 1));
235 
236  test1();
237  test2();
238  test3();
239 
240  return 0;
241 }
static void test2(void)
static void test1(void)
static lzma_options_lzma opt_lzma
static void test3(void)
#define succeed(test)
Definition: tests.h:27

References opt_lzma, succeed, test1(), test2(), and test3().

◆ test1()

static void test1 ( void  )
static

Definition at line 110 of file test_block_header.c.

111 {
114  .compressed_size = LZMA_VLI_UNKNOWN,
115  .uncompressed_size = LZMA_VLI_UNKNOWN,
116  .filters = NULL,
117  };
118 
119  expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
120 
122  expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
123 
125  expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
126 
128  expect(lzma_block_header_size(&known_options) == LZMA_OK);
129 
130  // Some invalid value, which gets ignored.
132  expect(lzma_block_header_size(&known_options) == LZMA_OK);
133 
135  expect(lzma_block_header_size(&known_options) == LZMA_OK);
136 
137  known_options.compressed_size = 0; // Cannot be zero.
138  expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
139 
140  // LZMA_VLI_MAX is too big to keep the total size of the Block
141  // a valid VLI, but lzma_block_header_size() is not meant
142  // to validate it. (lzma_block_header_encode() must validate it.)
144  expect(lzma_block_header_size(&known_options) == LZMA_OK);
145 
148  expect(lzma_block_header_size(&known_options) == LZMA_OK);
149 
151  expect(lzma_block_header_size(&known_options) == LZMA_PROG_ERROR);
152 }
lzma_check
Type of the integrity check (Check ID)
Definition: check.h:27
@ LZMA_CHECK_NONE
Definition: check.h:28
Options for the Block and Block Header encoders and decoders.
Definition: block.h:30
static lzma_filter filters_one[2]
static lzma_filter filters_five[6]
static lzma_filter filters_none[1]
#define LZMA_VLI_MAX
Maximum supported value of a variable-length integer.
Definition: vli.h:34
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218

References lzma_block::check, lzma_block::compressed_size, expect, lzma_block::filters, filters_five, filters_none, filters_one, known_options, LZMA_CHECK_NONE, LZMA_OK, LZMA_PROG_ERROR, LZMA_VLI_MAX, LZMA_VLI_UNKNOWN, NULL, and lzma_block::uncompressed_size.

Referenced by main().

◆ test2()

static void test2 ( void  )
static

Definition at line 156 of file test_block_header.c.

157 {
160  .compressed_size = LZMA_VLI_UNKNOWN,
161  .uncompressed_size = LZMA_VLI_UNKNOWN,
162  .filters = filters_four,
163  };
164 
165  expect(lzma_block_header_size(&known_options) == LZMA_OK);
166  code();
167 
170  expect(lzma_block_header_size(&known_options) == LZMA_OK);
171  code();
172 
173  // We can make the sizes smaller while keeping the header size
174  // the same.
177  code();
178 }
@ LZMA_CHECK_CRC32
Definition: check.h:35
static lzma_filter filters_four[5]
static void code(void)

References lzma_block::check, code(), lzma_block::compressed_size, expect, filters_four, known_options, LZMA_CHECK_CRC32, LZMA_OK, LZMA_VLI_UNKNOWN, and lzma_block::uncompressed_size.

Referenced by main().

◆ test3()

static void test3 ( void  )
static

Definition at line 182 of file test_block_header.c.

183 {
186  .compressed_size = LZMA_VLI_UNKNOWN,
187  .uncompressed_size = LZMA_VLI_UNKNOWN,
188  .filters = filters_one,
189  };
190 
191  expect(lzma_block_header_size(&known_options) == LZMA_OK);
193  expect(lzma_block_header_encode(&known_options, buf) == LZMA_OK);
194 
199 
200  // Wrong size
201  ++buf[0];
202  expect(lzma_block_header_decode(&decoded_options, NULL, buf)
203  == LZMA_PROG_ERROR);
204  --buf[0];
205 
206  // Wrong CRC32
207  buf[known_options.header_size - 1] ^= 1;
208  expect(lzma_block_header_decode(&decoded_options, NULL, buf)
209  == LZMA_DATA_ERROR);
210  buf[known_options.header_size - 1] ^= 1;
211 
212  // Unsupported filter
213  // NOTE: This may need updating when new IDs become supported.
214  buf[2] ^= 0x1F;
216  lzma_crc32(buf, known_options.header_size - 4, 0));
217  expect(lzma_block_header_decode(&decoded_options, NULL, buf)
218  == LZMA_OPTIONS_ERROR);
219  buf[2] ^= 0x1F;
220 
221  // Non-nul Padding
222  buf[known_options.header_size - 4 - 1] ^= 1;
224  lzma_crc32(buf, known_options.header_size - 4, 0));
225  expect(lzma_block_header_decode(&decoded_options, NULL, buf)
226  == LZMA_OPTIONS_ERROR);
227  buf[known_options.header_size - 4 - 1] ^= 1;
228 }
#define write32le(buf, num)
@ LZMA_DATA_ERROR
Data is corrupt.
Definition: base.h:172
@ LZMA_OPTIONS_ERROR
Invalid or unsupported options.
Definition: base.h:160

References lzma_block::check, decoded_options, expect, lzma_block::filters, filters, filters_one, lzma_block::header_size, known_options, LZMA_CHECK_CRC32, LZMA_DATA_ERROR, LZMA_FILTERS_MAX, LZMA_OK, LZMA_OPTIONS_ERROR, LZMA_PROG_ERROR, LZMA_VLI_UNKNOWN, NULL, and write32le.

Referenced by main().

Variable Documentation

◆ buf

Definition at line 16 of file test_block_header.c.

◆ decoded_options

lzma_block decoded_options
static

Definition at line 18 of file test_block_header.c.

Referenced by code(), and test3().

◆ filters_five

lzma_filter filters_five[6]
static
Initial value:
= {
{
.options = NULL,
}, {
.options = NULL,
}, {
.options = NULL,
}, {
.options = NULL,
}, {
.options = &opt_lzma,
}, {
}
}
#define LZMA_FILTER_X86
Definition: bcj.h:22
#define LZMA_FILTER_LZMA2
LZMA2 Filter ID.
Definition: lzma12.h:40

Definition at line 58 of file test_block_header.c.

Referenced by test1().

◆ filters_four

lzma_filter filters_four[5]
static
Initial value:
= {
{
.options = NULL,
}, {
.options = NULL,
}, {
.options = NULL,
}, {
.options = &opt_lzma,
}, {
}
}

Definition at line 39 of file test_block_header.c.

Referenced by test2().

◆ filters_none

lzma_filter filters_none[1]
static
Initial value:
= {
{
},
}

Definition at line 22 of file test_block_header.c.

Referenced by test1().

◆ filters_one

lzma_filter filters_one[2]
static
Initial value:
= {
{
.options = &opt_lzma,
}, {
}
}

Definition at line 29 of file test_block_header.c.

Referenced by test1(), and test3().

◆ known_options

lzma_block known_options
static

Definition at line 17 of file test_block_header.c.

Referenced by code(), test1(), test2(), and test3().

◆ opt_lzma

lzma_options_lzma opt_lzma
static

Definition at line 20 of file test_block_header.c.

Referenced by coder_init(), main(), and start().