aboutsummaryrefslogtreecommitdiff
path: root/tests/test_vli.c
blob: 51487dcf14a21898c2eb4cc89b7e6f84278f30ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// SPDX-License-Identifier: 0BSD

///////////////////////////////////////////////////////////////////////////////
//
/// \file       test_vli.c
/// \brief      Tests liblzma vli functions
//
//  Author:     Jia Tan
//
///////////////////////////////////////////////////////////////////////////////

#include "tests.h"


// Pre-encoded VLI values for testing
// VLI can have between 1 and 9 bytes when encoded
// They are encoded little endian where all but the last
// byte must have the leading 1 bit set
#if defined(HAVE_ENCODERS) || defined(HAVE_DECODERS)
static const uint8_t one_byte[1] = {0x25};
static const lzma_vli one_byte_value = 37;

static const uint8_t two_bytes[2] = {0x80, 0x56};
static const lzma_vli two_byte_value = 11008;

static const uint8_t three_bytes[3] = {0x99, 0x92, 0x20};
static const lzma_vli three_byte_value = 526617;

static const uint8_t four_bytes[4] = {0x97, 0x83, 0x94, 0x47};
static const lzma_vli four_byte_value = 149225879;

static const uint8_t five_bytes[5] = {0xA6, 0x92, 0x88, 0x89, 0x32};
static const lzma_vli five_byte_value = 13440780582;

static const uint8_t six_bytes[6] = {0xA9, 0x84, 0x99, 0x82, 0x94, 0x12};
static const lzma_vli six_byte_value = 623848604201;

static const uint8_t seven_bytes[7] = {0x90, 0x80, 0x90, 0x80, 0x90, 0x80,
				0x79};
static const lzma_vli seven_byte_value = 532167923073040;

static const uint8_t eight_bytes[8] = {0x91, 0x87, 0xF2, 0xB2, 0xC2, 0xD2,
				0x93, 0x63};
static const lzma_vli eight_byte_value = 55818443594433425;

static const uint8_t nine_bytes[9] = {0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1,
				0xE1, 0xF1, 0x1};
static const lzma_vli nine_byte_value = 136100349976529025;
#endif


static void
test_lzma_vli_size(void)
{
	// First test invalid VLI values (should return 0)
	// VLI UNKNOWN is an invalid VLI
	assert_uint_eq(lzma_vli_size(LZMA_VLI_UNKNOWN), 0);
	// Loop over a few VLI values just over the maximum
	for (uint64_t i = LZMA_VLI_MAX + 1; i < LZMA_VLI_MAX + 10; i++)
		assert_uint_eq(lzma_vli_size(i), 0);

	// Number should increment every seven set bits
	lzma_vli vli = 1;
	for (uint32_t i = 1; i < LZMA_VLI_BYTES_MAX; i++, vli <<= 7) {
		// Test the base value and a few others around it
		assert_uint_eq(lzma_vli_size(vli), i);
		assert_uint_eq(lzma_vli_size(vli * 2), i);
		assert_uint_eq(lzma_vli_size(vli + 10), i);
		assert_uint_eq(lzma_vli_size(vli * 3 + 39), i);
	}
}


#ifdef HAVE_ENCODERS
// Helper function for test_lzma_vli_encode
// Encodes an input VLI and compares against a pre-computed value
static void
encode_single_call_mode(lzma_vli input, const uint8_t *expected,
		uint32_t expected_len)
{
	uint8_t out[LZMA_VLI_BYTES_MAX];
	size_t out_pos = 0;
	assert_lzma_ret(lzma_vli_encode(input, NULL, out, &out_pos,
			expected_len), LZMA_OK);
	assert_uint_eq(out_pos, expected_len);
	assert_array_eq(out, expected, expected_len);
}


// Helper function for test_lzma_vli_encode
// Encodes an input VLI one byte at a time with the multi call
// method. Then compares against a pre-computed value
static void
encode_multi_call_mode(lzma_vli input, const uint8_t *expected,
		uint32_t expected_len)
{
	uint8_t out[LZMA_VLI_BYTES_MAX];
	size_t out_pos = 0;
	size_t vli_pos = 0;

	for (uint32_t i = 1; i < expected_len; i++) {
		assert_lzma_ret(lzma_vli_encode(input, &vli_pos, out,
				&out_pos, i), LZMA_OK);
		assert_uint_eq(out_pos, i);
		assert_uint_eq(vli_pos, i);
	}
	assert_lzma_ret(lzma_vli_encode(input, &vli_pos, out, &out_pos,
			expected_len), LZMA_STREAM_END);
	assert_uint_eq(out_pos, expected_len);
	assert_uint_eq(vli_pos, expected_len);
	assert_array_eq(out, expected, expected_len);
}
#endif


static void
test_lzma_vli_encode(void)
{
#ifndef HAVE_ENCODERS
	assert_skip("Encoder support disabled");
#else
	size_t vli_pos = 0;
	uint8_t out[LZMA_VLI_BYTES_MAX];
	uint8_t zeros[LZMA_VLI_BYTES_MAX];
	memzero(out, LZMA_VLI_BYTES_MAX);
	memzero(zeros, LZMA_VLI_BYTES_MAX);
	size_t out_pos = 0;

	// First test invalid input parameters
	// VLI invalid
	assert_lzma_ret(lzma_vli_encode(LZMA_VLI_UNKNOWN, &vli_pos, out,
			&out_pos, sizeof(out)), LZMA_PROG_ERROR);
	// Failure should not change params
	assert_uint_eq(vli_pos, 0);
	assert_uint_eq(out_pos, 0);
	assert_array_eq(out, zeros, LZMA_VLI_BYTES_MAX);

	assert_lzma_ret(lzma_vli_encode(LZMA_VLI_MAX + 1, &vli_pos, out,
		&out_pos, sizeof(out)), LZMA_PROG_ERROR);
	assert_uint_eq(vli_pos, 0);
	assert_uint_eq(out_pos, 0);
	assert_array_eq(out, zeros, LZMA_VLI_BYTES_MAX);

	// 0 output size
	assert_lzma_ret(lzma_vli_encode(one_byte_value, &vli_pos, out,
			&out_pos, 0), LZMA_BUF_ERROR);
	assert_uint_eq(vli_pos, 0);
	assert_uint_eq(out_pos, 0);
	assert_array_eq(out, zeros, LZMA_VLI_BYTES_MAX);

	// Size of VLI does not fit in buffer
	size_t phony_out_pos = 3;
	assert_lzma_ret(lzma_vli_encode(one_byte_value, NULL, out,
			&phony_out_pos, 2), LZMA_PROG_ERROR);

	assert_lzma_ret(lzma_vli_encode(LZMA_VLI_MAX / 2, NULL, out,
			&out_pos, 2), LZMA_PROG_ERROR);

	// Test single-call mode (using vli_pos as NULL)
	encode_single_call_mode(one_byte_value, one_byte,
			sizeof(one_byte));
	encode_single_call_mode(two_byte_value, two_bytes,
			sizeof(two_bytes));
	encode_single_call_mode(three_byte_value, three_bytes,
			sizeof(three_bytes));
	encode_single_call_mode(four_byte_value, four_bytes,
			sizeof(four_bytes));
	encode_single_call_mode(five_byte_value, five_bytes,
			sizeof(five_bytes));
	encode_single_call_mode(six_byte_value, six_bytes,
			sizeof(six_bytes));
	encode_single_call_mode(seven_byte_value, seven_bytes,
			sizeof(seven_bytes));
	encode_single_call_mode(eight_byte_value, eight_bytes,
			sizeof(eight_bytes));
	encode_single_call_mode(nine_byte_value, nine_bytes,
			sizeof(nine_bytes));

	// Test multi-call mode
	encode_multi_call_mode(one_byte_value, one_byte,
			sizeof(one_byte));
	encode_multi_call_mode(two_byte_value, two_bytes,
			sizeof(two_bytes));
	encode_multi_call_mode(three_byte_value, three_bytes,
			sizeof(three_bytes));
	encode_multi_call_mode(four_byte_value, four_bytes,
			sizeof(four_bytes));
	encode_multi_call_mode(five_byte_value, five_bytes,
			sizeof(five_bytes));
	encode_multi_call_mode(six_byte_value, six_bytes,
			sizeof(six_bytes));
	encode_multi_call_mode(seven_byte_value, seven_bytes,
			sizeof(seven_bytes));
	encode_multi_call_mode(eight_byte_value, eight_bytes,
			sizeof(eight_bytes));
	encode_multi_call_mode(nine_byte_value, nine_bytes,
			sizeof(nine_bytes));
#endif
}


#ifdef HAVE_DECODERS
static void
decode_single_call_mode(const uint8_t *input, uint32_t input_len,
		lzma_vli expected)
{
	lzma_vli out = 0;
	size_t in_pos = 0;

	assert_lzma_ret(lzma_vli_decode(&out, NULL, input, &in_pos,
			input_len), LZMA_OK);
	assert_uint_eq(in_pos, input_len);
	assert_uint_eq(out, expected);
}


static void
decode_multi_call_mode(const uint8_t *input, uint32_t input_len,
		lzma_vli expected)
{
	lzma_vli out = 0;
	size_t in_pos = 0;
	size_t vli_pos = 0;

	for (uint32_t i = 1; i < input_len; i++) {
		assert_lzma_ret(lzma_vli_decode(&out, &vli_pos, input,
				&in_pos, i), LZMA_OK);
		assert_uint_eq(in_pos, i);
		assert_uint_eq(vli_pos, i);
	}

	assert_lzma_ret(lzma_vli_decode(&out, &vli_pos, input, &in_pos,
			input_len), LZMA_STREAM_END);
	assert_uint_eq(in_pos, input_len);
	assert_uint_eq(vli_pos, input_len);
	assert_uint_eq(out, expected);
}
#endif


static void
test_lzma_vli_decode(void)
{
#ifndef HAVE_DECODERS
	assert_skip("Decoder support disabled");
#else
	lzma_vli out = 0;
	size_t in_pos = 0;

	// First test invalid input params
	// 0 in_size
	assert_lzma_ret(lzma_vli_decode(&out, NULL, one_byte, &in_pos, 0),
			LZMA_DATA_ERROR);
	assert_uint_eq(out, 0);
	assert_uint_eq(in_pos, 0);

	// VLI encoded is invalid (last digit has leading 1 set)
	uint8_t invalid_vli[3] = {0x80, 0x80, 0x80};
	assert_lzma_ret(lzma_vli_decode(&out, NULL, invalid_vli, &in_pos,
			sizeof(invalid_vli)), LZMA_DATA_ERROR);

	// Bad vli_pos
	size_t vli_pos = LZMA_VLI_BYTES_MAX;
	assert_lzma_ret(lzma_vli_decode(&out, &vli_pos, invalid_vli, &in_pos,
			sizeof(invalid_vli)), LZMA_PROG_ERROR);

	// Bad in_pos
	in_pos = sizeof(invalid_vli);
	assert_lzma_ret(lzma_vli_decode(&out, &in_pos, invalid_vli, &in_pos,
			sizeof(invalid_vli)), LZMA_BUF_ERROR);

	// Test single call mode
	decode_single_call_mode(one_byte, sizeof(one_byte),
			one_byte_value);
	decode_single_call_mode(two_bytes, sizeof(two_bytes),
			two_byte_value);
	decode_single_call_mode(three_bytes, sizeof(three_bytes),
			three_byte_value);
	decode_single_call_mode(four_bytes, sizeof(four_bytes),
			four_byte_value);
	decode_single_call_mode(five_bytes, sizeof(five_bytes),
			five_byte_value);
	decode_single_call_mode(six_bytes, sizeof(six_bytes),
			six_byte_value);
	decode_single_call_mode(seven_bytes, sizeof(seven_bytes),
			seven_byte_value);
	decode_single_call_mode(eight_bytes, sizeof(eight_bytes),
			eight_byte_value);
	decode_single_call_mode(nine_bytes, sizeof(nine_bytes),
			nine_byte_value);

	// Test multi call mode
	decode_multi_call_mode(one_byte, sizeof(one_byte),
			one_byte_value);
	decode_multi_call_mode(two_bytes, sizeof(two_bytes),
			two_byte_value);
	decode_multi_call_mode(three_bytes, sizeof(three_bytes),
			three_byte_value);
	decode_multi_call_mode(four_bytes, sizeof(four_bytes),
			four_byte_value);
	decode_multi_call_mode(five_bytes, sizeof(five_bytes),
			five_byte_value);
	decode_multi_call_mode(six_bytes, sizeof(six_bytes),
			six_byte_value);
	decode_multi_call_mode(seven_bytes, sizeof(seven_bytes),
			seven_byte_value);
	decode_multi_call_mode(eight_bytes, sizeof(eight_bytes),
			eight_byte_value);
	decode_multi_call_mode(nine_bytes, sizeof(nine_bytes),
			nine_byte_value);
#endif
}


extern int
main(int argc, char **argv)
{
	tuktest_start(argc, argv);
	tuktest_run(test_lzma_vli_size);
	tuktest_run(test_lzma_vli_encode);
	tuktest_run(test_lzma_vli_decode);
	return tuktest_end();
}