Rizin
unix-like reverse engineering framework and cli tools
test_block_header.c
Go to the documentation of this file.
1 //
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
10 //
12 
13 #include "tests.h"
14 
15 
19 
21 
23  {
25  },
26 };
27 
28 
29 static lzma_filter filters_one[2] = {
30  {
32  .options = &opt_lzma,
33  }, {
34  .id = LZMA_VLI_UNKNOWN,
35  }
36 };
37 
38 
40  {
42  .options = NULL,
43  }, {
44  .id = LZMA_FILTER_X86,
45  .options = NULL,
46  }, {
47  .id = LZMA_FILTER_X86,
48  .options = NULL,
49  }, {
50  .id = LZMA_FILTER_LZMA2,
51  .options = &opt_lzma,
52  }, {
53  .id = LZMA_VLI_UNKNOWN,
54  }
55 };
56 
57 
59  {
61  .options = NULL,
62  }, {
63  .id = LZMA_FILTER_X86,
64  .options = NULL,
65  }, {
66  .id = LZMA_FILTER_X86,
67  .options = NULL,
68  }, {
69  .id = LZMA_FILTER_X86,
70  .options = NULL,
71  }, {
72  .id = LZMA_FILTER_LZMA2,
73  .options = &opt_lzma,
74  }, {
75  .id = LZMA_VLI_UNKNOWN,
76  }
77 };
78 
79 
80 static void
81 code(void)
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 }
107 
108 
109 static void
110 test1(void)
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 }
153 
154 
155 static void
156 test2(void)
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 }
179 
180 
181 static void
182 test3(void)
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 }
229 
230 
231 int
232 main(void)
233 {
234  succeed(lzma_lzma_preset(&opt_lzma, 1));
235 
236  test1();
237  test2();
238  test3();
239 
240  return 0;
241 }
lzma_check
Type of the integrity check (Check ID)
Definition: check.h:27
@ LZMA_CHECK_CRC32
Definition: check.h:35
@ LZMA_CHECK_NONE
Definition: check.h:28
lzma_index ** i
Definition: index.h:629
#define LZMA_FILTER_X86
Definition: bcj.h:22
#define LZMA_BLOCK_HEADER_SIZE_MAX
Definition: block.h:74
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
#define LZMA_FILTER_LZMA2
LZMA2 Filter ID.
Definition: lzma12.h:40
unsigned char uint8_t
Definition: sftypes.h:31
Options for the Block and Block Header encoders and decoders.
Definition: block.h:30
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
Options specific to the LZMA1 and LZMA2 filters.
Definition: lzma12.h:185
static void test2(void)
static lzma_filter filters_one[2]
static lzma_block decoded_options
static lzma_filter filters_four[5]
static void test1(void)
static lzma_options_lzma opt_lzma
int main(void)
static lzma_filter filters_five[6]
static lzma_filter filters_none[1]
static void code(void)
static lzma_block known_options
static void test3(void)
Common definitions for test applications.
#define memcrap(buf, size)
Definition: tests.h:22
#define succeed(test)
Definition: tests.h:27
#define write32le(buf, num)
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
Definition: vli.h:39
#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
@ LZMA_DATA_ERROR
Data is corrupt.
Definition: base.h:172
@ LZMA_OPTIONS_ERROR
Invalid or unsupported options.
Definition: base.h:160
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58