Build: Removes redundant check for LZMA1 filter support.
[xz/debian.git] / tests / test_vli.c
blobe82e7270e8f8f876dfb3c870f3f5d691ec932957
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file test_vli.c
4 /// \brief Tests liblzma vli functions
5 //
6 // Author: Jia Tan
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "tests.h"
16 // Pre-encoded VLI values for testing
17 // VLI can have between 1 and 9 bytes when encoded
18 // They are encoded little endian where all but the last
19 // byte must have the leading 1 bit set
20 #if defined(HAVE_ENCODERS) || defined(HAVE_DECODERS)
21 static const uint8_t one_byte[1] = {0x25};
22 static const lzma_vli one_byte_value = 37;
24 static const uint8_t two_bytes[2] = {0x80, 0x56};
25 static const lzma_vli two_byte_value = 11008;
27 static const uint8_t three_bytes[3] = {0x99, 0x92, 0x20};
28 static const lzma_vli three_byte_value = 526617;
30 static const uint8_t four_bytes[4] = {0x97, 0x83, 0x94, 0x47};
31 static const lzma_vli four_byte_value = 149225879;
33 static const uint8_t five_bytes[5] = {0xA6, 0x92, 0x88, 0x89, 0x32};
34 static const lzma_vli five_byte_value = 13440780582;
36 static const uint8_t six_bytes[6] = {0xA9, 0x84, 0x99, 0x82, 0x94, 0x12};
37 static const lzma_vli six_byte_value = 623848604201;
39 static const uint8_t seven_bytes[7] = {0x90, 0x80, 0x90, 0x80, 0x90, 0x80,
40 0x79};
41 static const lzma_vli seven_byte_value = 532167923073040;
43 static const uint8_t eight_bytes[8] = {0x91, 0x87, 0xF2, 0xB2, 0xC2, 0xD2,
44 0x93, 0x63};
45 static const lzma_vli eight_byte_value = 55818443594433425;
47 static const uint8_t nine_bytes[9] = {0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1,
48 0xE1, 0xF1, 0x1};
49 static const lzma_vli nine_byte_value = 136100349976529025;
50 #endif
53 static void
54 test_lzma_vli_size(void)
56 // First test invalid VLI values (should return 0)
57 // VLI UNKNOWN is an invalid VLI
58 assert_uint_eq(lzma_vli_size(LZMA_VLI_UNKNOWN), 0);
59 // Loop over a few VLI values just over the maximum
60 for (uint64_t i = LZMA_VLI_MAX + 1; i < LZMA_VLI_MAX + 10; i++)
61 assert_uint_eq(lzma_vli_size(i), 0);
63 // Number should increment every seven set bits
64 lzma_vli vli = 1;
65 for (uint32_t i = 1; i < LZMA_VLI_BYTES_MAX; i++, vli <<= 7) {
66 // Test the base value and a few others around it
67 assert_uint_eq(lzma_vli_size(vli), i);
68 assert_uint_eq(lzma_vli_size(vli * 2), i);
69 assert_uint_eq(lzma_vli_size(vli + 10), i);
70 assert_uint_eq(lzma_vli_size(vli * 3 + 39), i);
75 #ifdef HAVE_ENCODERS
76 // Helper function for test_lzma_vli_encode
77 // Encodes an input VLI and compares against a pre-computed value
78 static void
79 encode_single_call_mode(lzma_vli input, const uint8_t *expected,
80 uint32_t expected_len)
82 uint8_t out[LZMA_VLI_BYTES_MAX];
83 size_t out_pos = 0;
84 assert_lzma_ret(lzma_vli_encode(input, NULL, out, &out_pos,
85 expected_len), LZMA_OK);
86 assert_uint_eq(out_pos, expected_len);
87 assert_array_eq(out, expected, expected_len);
91 // Helper function for test_lzma_vli_encode
92 // Encodes an input VLI one byte at a time with the multi call
93 // method. Then compares agaist a pre-computed value
94 static void
95 encode_multi_call_mode(lzma_vli input, const uint8_t *expected,
96 uint32_t expected_len)
98 uint8_t out[LZMA_VLI_BYTES_MAX];
99 size_t out_pos = 0;
100 size_t vli_pos = 0;
102 for (uint32_t i = 1; i < expected_len; i++) {
103 assert_lzma_ret(lzma_vli_encode(input, &vli_pos, out,
104 &out_pos, i), LZMA_OK);
105 assert_uint_eq(out_pos, i);
106 assert_uint_eq(vli_pos, i);
108 assert_lzma_ret(lzma_vli_encode(input, &vli_pos, out, &out_pos,
109 expected_len), LZMA_STREAM_END);
110 assert_uint_eq(out_pos, expected_len);
111 assert_uint_eq(vli_pos, expected_len);
112 assert_array_eq(out, expected, expected_len);
114 #endif
117 static void
118 test_lzma_vli_encode(void)
120 #ifndef HAVE_ENCODERS
121 assert_skip("Encoder support disabled");
122 #else
123 size_t vli_pos = 0;
124 uint8_t out[LZMA_VLI_BYTES_MAX];
125 uint8_t zeros[LZMA_VLI_BYTES_MAX];
126 memzero(out, LZMA_VLI_BYTES_MAX);
127 memzero(zeros, LZMA_VLI_BYTES_MAX);
128 size_t out_pos = 0;
130 // First test invalid input parameters
131 // VLI invalid
132 assert_lzma_ret(lzma_vli_encode(LZMA_VLI_UNKNOWN, &vli_pos, out,
133 &out_pos, sizeof(out)), LZMA_PROG_ERROR);
134 // Failure should not change params
135 assert_uint_eq(vli_pos, 0);
136 assert_uint_eq(out_pos, 0);
137 assert_array_eq(out, zeros, LZMA_VLI_BYTES_MAX);
139 assert_lzma_ret(lzma_vli_encode(LZMA_VLI_MAX + 1, &vli_pos, out,
140 &out_pos, sizeof(out)), LZMA_PROG_ERROR);
141 assert_uint_eq(vli_pos, 0);
142 assert_uint_eq(out_pos, 0);
143 assert_array_eq(out, zeros, LZMA_VLI_BYTES_MAX);
145 // 0 output size
146 assert_lzma_ret(lzma_vli_encode(one_byte_value, &vli_pos, out,
147 &out_pos, 0), LZMA_BUF_ERROR);
148 assert_uint_eq(vli_pos, 0);
149 assert_uint_eq(out_pos, 0);
150 assert_array_eq(out, zeros, LZMA_VLI_BYTES_MAX);
152 // Size of VLI does not fit in buffer
153 size_t phony_out_pos = 3;
154 assert_lzma_ret(lzma_vli_encode(one_byte_value, NULL, out,
155 &phony_out_pos, 2), LZMA_PROG_ERROR);
157 assert_lzma_ret(lzma_vli_encode(LZMA_VLI_MAX / 2, NULL, out,
158 &out_pos, 2), LZMA_PROG_ERROR);
160 // Test single-call mode (using vli_pos as NULL)
161 encode_single_call_mode(one_byte_value, one_byte,
162 sizeof(one_byte));
163 encode_single_call_mode(two_byte_value, two_bytes,
164 sizeof(two_bytes));
165 encode_single_call_mode(three_byte_value, three_bytes,
166 sizeof(three_bytes));
167 encode_single_call_mode(four_byte_value, four_bytes,
168 sizeof(four_bytes));
169 encode_single_call_mode(five_byte_value, five_bytes,
170 sizeof(five_bytes));
171 encode_single_call_mode(six_byte_value, six_bytes,
172 sizeof(six_bytes));
173 encode_single_call_mode(seven_byte_value, seven_bytes,
174 sizeof(seven_bytes));
175 encode_single_call_mode(eight_byte_value, eight_bytes,
176 sizeof(eight_bytes));
177 encode_single_call_mode(nine_byte_value, nine_bytes,
178 sizeof(nine_bytes));
180 // Test multi-call mode
181 encode_multi_call_mode(one_byte_value, one_byte,
182 sizeof(one_byte));
183 encode_multi_call_mode(two_byte_value, two_bytes,
184 sizeof(two_bytes));
185 encode_multi_call_mode(three_byte_value, three_bytes,
186 sizeof(three_bytes));
187 encode_multi_call_mode(four_byte_value, four_bytes,
188 sizeof(four_bytes));
189 encode_multi_call_mode(five_byte_value, five_bytes,
190 sizeof(five_bytes));
191 encode_multi_call_mode(six_byte_value, six_bytes,
192 sizeof(six_bytes));
193 encode_multi_call_mode(seven_byte_value, seven_bytes,
194 sizeof(seven_bytes));
195 encode_multi_call_mode(eight_byte_value, eight_bytes,
196 sizeof(eight_bytes));
197 encode_multi_call_mode(nine_byte_value, nine_bytes,
198 sizeof(nine_bytes));
199 #endif
203 #ifdef HAVE_DECODERS
204 static void
205 decode_single_call_mode(const uint8_t *input, uint32_t input_len,
206 lzma_vli expected)
208 lzma_vli out = 0;
209 size_t in_pos = 0;
211 assert_lzma_ret(lzma_vli_decode(&out, NULL, input, &in_pos,
212 input_len), LZMA_OK);
213 assert_uint_eq(in_pos, input_len);
214 assert_uint_eq(out, expected);
218 static void
219 decode_multi_call_mode(const uint8_t *input, uint32_t input_len,
220 lzma_vli expected)
222 lzma_vli out = 0;
223 size_t in_pos = 0;
224 size_t vli_pos = 0;
226 for (uint32_t i = 1; i < input_len; i++) {
227 assert_lzma_ret(lzma_vli_decode(&out, &vli_pos, input,
228 &in_pos, i), LZMA_OK);
229 assert_uint_eq(in_pos, i);
230 assert_uint_eq(vli_pos, i);
233 assert_lzma_ret(lzma_vli_decode(&out, &vli_pos, input, &in_pos,
234 input_len), LZMA_STREAM_END);
235 assert_uint_eq(in_pos, input_len);
236 assert_uint_eq(vli_pos, input_len);
237 assert_uint_eq(out, expected);
239 #endif
242 static void
243 test_lzma_vli_decode(void)
245 #ifndef HAVE_DECODERS
246 assert_skip("Decoder support disabled");
247 #else
248 lzma_vli out = 0;
249 size_t in_pos = 0;
251 // First test invalid input params
252 // 0 in_size
253 assert_lzma_ret(lzma_vli_decode(&out, NULL, one_byte, &in_pos, 0),
254 LZMA_DATA_ERROR);
255 assert_uint_eq(out, 0);
256 assert_uint_eq(in_pos, 0);
258 // VLI encoded is invalid (last digit has leading 1 set)
259 uint8_t invalid_vli[3] = {0x80, 0x80, 0x80};
260 assert_lzma_ret(lzma_vli_decode(&out, NULL, invalid_vli, &in_pos,
261 sizeof(invalid_vli)), LZMA_DATA_ERROR);
263 // Bad vli_pos
264 size_t vli_pos = LZMA_VLI_BYTES_MAX;
265 assert_lzma_ret(lzma_vli_decode(&out, &vli_pos, invalid_vli, &in_pos,
266 sizeof(invalid_vli)), LZMA_PROG_ERROR);
268 // Bad in_pos
269 in_pos = sizeof(invalid_vli);
270 assert_lzma_ret(lzma_vli_decode(&out, &in_pos, invalid_vli, &in_pos,
271 sizeof(invalid_vli)), LZMA_BUF_ERROR);
273 // Test single call mode
274 decode_single_call_mode(one_byte, sizeof(one_byte),
275 one_byte_value);
276 decode_single_call_mode(two_bytes, sizeof(two_bytes),
277 two_byte_value);
278 decode_single_call_mode(three_bytes, sizeof(three_bytes),
279 three_byte_value);
280 decode_single_call_mode(four_bytes, sizeof(four_bytes),
281 four_byte_value);
282 decode_single_call_mode(five_bytes, sizeof(five_bytes),
283 five_byte_value);
284 decode_single_call_mode(six_bytes, sizeof(six_bytes),
285 six_byte_value);
286 decode_single_call_mode(seven_bytes, sizeof(seven_bytes),
287 seven_byte_value);
288 decode_single_call_mode(eight_bytes, sizeof(eight_bytes),
289 eight_byte_value);
290 decode_single_call_mode(nine_bytes, sizeof(nine_bytes),
291 nine_byte_value);
293 // Test multi call mode
294 decode_multi_call_mode(one_byte, sizeof(one_byte),
295 one_byte_value);
296 decode_multi_call_mode(two_bytes, sizeof(two_bytes),
297 two_byte_value);
298 decode_multi_call_mode(three_bytes, sizeof(three_bytes),
299 three_byte_value);
300 decode_multi_call_mode(four_bytes, sizeof(four_bytes),
301 four_byte_value);
302 decode_multi_call_mode(five_bytes, sizeof(five_bytes),
303 five_byte_value);
304 decode_multi_call_mode(six_bytes, sizeof(six_bytes),
305 six_byte_value);
306 decode_multi_call_mode(seven_bytes, sizeof(seven_bytes),
307 seven_byte_value);
308 decode_multi_call_mode(eight_bytes, sizeof(eight_bytes),
309 eight_byte_value);
310 decode_multi_call_mode(nine_bytes, sizeof(nine_bytes),
311 nine_byte_value);
312 #endif
316 extern int
317 main(int argc, char **argv)
319 tuktest_start(argc, argv);
320 tuktest_run(test_lzma_vli_size);
321 tuktest_run(test_lzma_vli_encode);
322 tuktest_run(test_lzma_vli_decode);
323 return tuktest_end();