1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Tests liblzma vli functions
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
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,
41 static const lzma_vli seven_byte_value
= 532167923073040;
43 static const uint8_t eight_bytes
[8] = {0x91, 0x87, 0xF2, 0xB2, 0xC2, 0xD2,
45 static const lzma_vli eight_byte_value
= 55818443594433425;
47 static const uint8_t nine_bytes
[9] = {0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1,
49 static const lzma_vli nine_byte_value
= 136100349976529025;
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
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
);
76 // Helper function for test_lzma_vli_encode
77 // Encodes an input VLI and compares against a pre-computed value
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
];
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
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
];
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
);
118 test_lzma_vli_encode(void)
120 #ifndef HAVE_ENCODERS
121 assert_skip("Encoder support disabled");
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
);
130 // First test invalid input parameters
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
);
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
,
163 encode_single_call_mode(two_byte_value
, 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
,
169 encode_single_call_mode(five_byte_value
, five_bytes
,
171 encode_single_call_mode(six_byte_value
, 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
,
180 // Test multi-call mode
181 encode_multi_call_mode(one_byte_value
, one_byte
,
183 encode_multi_call_mode(two_byte_value
, 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
,
189 encode_multi_call_mode(five_byte_value
, five_bytes
,
191 encode_multi_call_mode(six_byte_value
, 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
,
205 decode_single_call_mode(const uint8_t *input
, uint32_t input_len
,
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
);
219 decode_multi_call_mode(const uint8_t *input
, uint32_t input_len
,
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
);
243 test_lzma_vli_decode(void)
245 #ifndef HAVE_DECODERS
246 assert_skip("Decoder support disabled");
251 // First test invalid input params
253 assert_lzma_ret(lzma_vli_decode(&out
, NULL
, one_byte
, &in_pos
, 0),
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
);
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
);
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
),
276 decode_single_call_mode(two_bytes
, sizeof(two_bytes
),
278 decode_single_call_mode(three_bytes
, sizeof(three_bytes
),
280 decode_single_call_mode(four_bytes
, sizeof(four_bytes
),
282 decode_single_call_mode(five_bytes
, sizeof(five_bytes
),
284 decode_single_call_mode(six_bytes
, sizeof(six_bytes
),
286 decode_single_call_mode(seven_bytes
, sizeof(seven_bytes
),
288 decode_single_call_mode(eight_bytes
, sizeof(eight_bytes
),
290 decode_single_call_mode(nine_bytes
, sizeof(nine_bytes
),
293 // Test multi call mode
294 decode_multi_call_mode(one_byte
, sizeof(one_byte
),
296 decode_multi_call_mode(two_bytes
, sizeof(two_bytes
),
298 decode_multi_call_mode(three_bytes
, sizeof(three_bytes
),
300 decode_multi_call_mode(four_bytes
, sizeof(four_bytes
),
302 decode_multi_call_mode(five_bytes
, sizeof(five_bytes
),
304 decode_multi_call_mode(six_bytes
, sizeof(six_bytes
),
306 decode_multi_call_mode(seven_bytes
, sizeof(seven_bytes
),
308 decode_multi_call_mode(eight_bytes
, sizeof(eight_bytes
),
310 decode_multi_call_mode(nine_bytes
, sizeof(nine_bytes
),
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();