Debian Codegraph @

crt/aws-c-compression/tests/huffman_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/compression/private/huffman_testing.h>
7 #include <aws/testing/aws_test_harness.h>
8
9 #include <aws/compression/huffman.h>
10
11 /* Exported by generated file */
12 struct aws_huffman_symbol_coder *test_get_coder(void);
13
14 static struct huffman_test_code_point s_code_points[] = {
15 #include "test_huffman_static_table.def"
16 };
17 enum { NUM_CODE_POINTS = sizeof(s_code_points) / sizeof(s_code_points[0]) };
18
19 /* Useful data for testing */
20 static const char s_url_string[] = "www.example.com";
21 enum { URL_STRING_LEN = sizeof(s_url_string) - 1 };
22
23 static uint8_t s_encoded_url[] = {0x9e, 0x79, 0xeb, 0x9b, 0x04, 0xb3, 0x5a, 0x94, 0xd5, 0xe0, 0x4c, 0xdf};
24 enum { ENCODED_URL_LEN = sizeof(s_encoded_url) };
25
26 static const char s_all_codes[] = " !\"#$%&'()*+,-./"
27 "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ["
28 "\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
29 enum { ALL_CODES_LEN = sizeof(s_all_codes) - 1 };
30 static uint8_t s_encoded_codes[] = {
31 0x26, 0x9b, 0xa7, 0x69, 0xfa, 0x86, 0xa3, 0xa9, 0x56, 0xd4, 0xf5, 0x4d, 0x57, 0x56, 0xb9, 0xc4, 0x57, 0xd5,
32 0xf5, 0x8d, 0x67, 0x5a, 0xd6, 0xf5, 0xcd, 0x77, 0x5e, 0xd7, 0xf6, 0x0d, 0x87, 0x62, 0xd8, 0xf6, 0x4d, 0x97,
33 0x66, 0xba, 0xd9, 0xf6, 0x8b, 0xbc, 0x4e, 0x2b, 0x17, 0x8c, 0xc6, 0xe3, 0xaf, 0x36, 0x9d, 0xab, 0x1f, 0x90,
34 0xda, 0xf6, 0xcc, 0x8e, 0xdb, 0xb7, 0x6d, 0xf7, 0xbb, 0x86, 0x4a, 0xfb, 0x71, 0xc9, 0xee, 0x5b, 0x9e, 0xe9,
35 0xba, 0xee, 0xdb, 0xbe, 0xf0, 0x5b, 0x10, 0x42, 0x68, 0xac, 0xc6, 0x7b, 0xf9, 0x25, 0x99, 0x09, 0xb5, 0x94,
36 0x52, 0xd8, 0xdc, 0x09, 0xf0, 0x68, 0xde, 0x77, 0xad, 0xef, 0x7c, 0xdf, 0x7f};
37 enum { ENCODED_CODES_LEN = sizeof(s_encoded_codes) };
38
39 static const size_t s_step_sizes[] = {1, 2, 4, 8, 16, 32, 64, 128};
40 enum { NUM_STEP_SIZES = sizeof(s_step_sizes) / sizeof(s_step_sizes[0]) };
41
42 AWS_TEST_CASE(huffman_symbol_encoder, test_huffman_symbol_encoder)
43 static int test_huffman_symbol_encoder(struct aws_allocator *allocator, void *ctx) {
44 (void)allocator;
45 (void)ctx;
46 /* Test encoding each character */
47
48 struct aws_huffman_symbol_coder *coder = test_get_coder();
49
50 for (size_t i = 0; i < NUM_CODE_POINTS; ++i) {
51 struct huffman_test_code_point *value = &s_code_points[i];
52
53 struct aws_huffman_code code = coder->encode(value->symbol, NULL);
54
55 ASSERT_UINT_EQUALS(value->code.pattern, code.pattern);
56 ASSERT_UINT_EQUALS(value->code.num_bits, code.num_bits);
57 }
58
59 return AWS_OP_SUCCESS;
60 }
61
62 AWS_TEST_CASE(huffman_encoder, test_huffman_encoder)
63 static int test_huffman_encoder(struct aws_allocator *allocator, void *ctx) {
64 (void)allocator;
65 (void)ctx;
66 /* Test encoding a short url */
67
68 uint8_t output_buffer[ENCODED_URL_LEN + 1];
69 AWS_ZERO_ARRAY(output_buffer);
70 struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output_buffer, ENCODED_URL_LEN);
71
72 struct aws_huffman_symbol_coder *coder = test_get_coder();
73 struct aws_huffman_encoder encoder;
74 aws_huffman_encoder_init(&encoder, coder);
75
76 struct aws_byte_cursor to_encode = aws_byte_cursor_from_array(s_url_string, URL_STRING_LEN);
77 const size_t encoded_length = aws_huffman_get_encoded_length(&encoder, to_encode);
78 ASSERT_UINT_EQUALS(ENCODED_URL_LEN, encoded_length);
79 int result = aws_huffman_encode(&encoder, &to_encode, &output_buf);
80 ASSERT_SUCCESS(result);
81
82 ASSERT_UINT_EQUALS(ENCODED_URL_LEN, output_buf.len);
83 ASSERT_UINT_EQUALS(0, output_buffer[ENCODED_URL_LEN]);
84 ASSERT_BIN_ARRAYS_EQUALS(s_encoded_url, ENCODED_URL_LEN, output_buf.buffer, output_buf.len);
85
86 return AWS_OP_SUCCESS;
87 }
88
89 AWS_TEST_CASE(huffman_encoder_all_code_points, test_huffman_encoder_all_code_points)
90 static int test_huffman_encoder_all_code_points(struct aws_allocator *allocator, void *ctx) {
91 (void)allocator;
92 (void)ctx;
93 /* Test encoding a sequence of all character values expressable as
94 * characters */
95
96 uint8_t output_buffer[ENCODED_CODES_LEN + 1];
97 AWS_ZERO_ARRAY(output_buffer);
98 struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output_buffer, ENCODED_CODES_LEN);
99
100 struct aws_huffman_symbol_coder *coder = test_get_coder();
101 struct aws_huffman_encoder encoder;
102 aws_huffman_encoder_init(&encoder, coder);
103
104 struct aws_byte_cursor to_encode = aws_byte_cursor_from_array(s_all_codes, ALL_CODES_LEN);
105 const size_t encoded_length = aws_huffman_get_encoded_length(&encoder, to_encode);
106 ASSERT_UINT_EQUALS(ENCODED_CODES_LEN, encoded_length);
107 int result = aws_huffman_encode(&encoder, &to_encode, &output_buf);
108 ASSERT_SUCCESS(result);
109
110 ASSERT_UINT_EQUALS(ENCODED_CODES_LEN, output_buf.len);
111 ASSERT_UINT_EQUALS(0, output_buffer[ENCODED_CODES_LEN]);
112 ASSERT_BIN_ARRAYS_EQUALS(s_encoded_codes, ENCODED_CODES_LEN, output_buf.buffer, output_buf.len);
113
114 return AWS_OP_SUCCESS;
115 }
116
117 AWS_TEST_CASE(huffman_encoder_partial_output, test_huffman_encoder_partial_output)
118 static int test_huffman_encoder_partial_output(struct aws_allocator *allocator, void *ctx) {
119 (void)allocator;
120 (void)ctx;
121 /* Test encoding when the output buffer size is limited */
122
123 struct aws_huffman_encoder encoder;
124 aws_huffman_encoder_init(&encoder, test_get_coder());
125
126 uint8_t output_buffer[ENCODED_CODES_LEN];
127
128 for (size_t i = 0; i < NUM_STEP_SIZES; ++i) {
129 const size_t step_size = s_step_sizes[i];
130
131 aws_huffman_encoder_reset(&encoder);
132
133 struct aws_byte_cursor to_encode = aws_byte_cursor_from_array(s_all_codes, ALL_CODES_LEN);
134 struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output_buffer, (size_t)-1);
135 output_buf.capacity = 0;
136 AWS_ZERO_ARRAY(output_buffer);
137
138 do {
139 output_buf.capacity += step_size;
140 if (output_buf.capacity > ENCODED_CODES_LEN) {
141 output_buf.capacity = ENCODED_CODES_LEN;
142 }
143
144 const size_t previous_output_len = output_buf.len;
145
146 int result = aws_huffman_encode(&encoder, &to_encode, &output_buf);
147
148 ASSERT_TRUE(output_buf.len > previous_output_len);
149 ASSERT_BIN_ARRAYS_EQUALS(s_encoded_codes, output_buf.len, output_buf.buffer, output_buf.len);
150
151 if (output_buf.len == ENCODED_CODES_LEN) {
152 ASSERT_SUCCESS(result);
153 } else {
154 ASSERT_UINT_EQUALS(AWS_ERROR_SHORT_BUFFER, aws_last_error());
155 aws_reset_error();
156 }
157 } while (output_buf.len < ENCODED_CODES_LEN);
158
159 ASSERT_UINT_EQUALS(ENCODED_CODES_LEN, output_buf.len);
160
161 ASSERT_BIN_ARRAYS_EQUALS(s_encoded_codes, ENCODED_CODES_LEN, output_buf.buffer, output_buf.len);
162 }
163
164 return AWS_OP_SUCCESS;
165 }
166
167 AWS_TEST_CASE(huffman_encoder_exact_output, test_huffman_encoder_exact_output)
168 static int test_huffman_encoder_exact_output(struct aws_allocator *allocator, void *ctx) {
169 (void)allocator;
170 (void)ctx;
171 /* Test encoding when the output buffer size is exactly the necessary size */
172 struct aws_huffman_encoder encoder;
173 aws_huffman_encoder_init(&encoder, test_get_coder());
174
175 uint8_t output_buffer[2];
176 struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output_buffer, 2);
177
178 /* Encode a character that uses 8 bits into a 1 byte buffer */
179 struct aws_byte_cursor to_encode = aws_byte_cursor_from_array("?", 1);
180 uint8_t expected_1byte[] = {0xba};
181 output_buf.capacity = 1;
182 ASSERT_SUCCESS(aws_huffman_encode(&encoder, &to_encode, &output_buf));
183 ASSERT_BIN_ARRAYS_EQUALS(expected_1byte, 1, output_buf.buffer, output_buf.len);
184
185 /* Encode 2 characters that sum to 16 bits, into a 2 byte buffer
186 * y: 101000
187 * z: 1101111001
188 * combined: 1010001101111001 == 0xa379 */
189 to_encode = aws_byte_cursor_from_array("yz", 2);
190 uint8_t expected_2byte[] = {0xa3, 0x79};
191 output_buf.capacity = 2;
192 aws_byte_buf_reset(&output_buf, true /*zero*/);
193 ASSERT_SUCCESS(aws_huffman_encode(&encoder, &to_encode, &output_buf));
194 ASSERT_BIN_ARRAYS_EQUALS(expected_2byte, 2, output_buf.buffer, output_buf.len);
195
196 return AWS_OP_SUCCESS;
197 }
198
199 AWS_TEST_CASE(huffman_symbol_decoder, test_huffman_symbol_decoder)
200 static int test_huffman_symbol_decoder(struct aws_allocator *allocator, void *ctx) {
201 (void)allocator;
202 (void)ctx;
203 /* Test decoding each character */
204
205 struct aws_huffman_symbol_coder *coder = test_get_coder();
206
207 for (size_t i = 0; i < NUM_CODE_POINTS; ++i) {
208 struct huffman_test_code_point *value = &s_code_points[i];
209
210 uint32_t bit_pattern = value->code.pattern << (32 - value->code.num_bits);
211
212 uint8_t out;
213 size_t bits_read = coder->decode(bit_pattern, &out, NULL);
214
215 ASSERT_UINT_EQUALS(value->symbol, out);
216 ASSERT_UINT_EQUALS(value->code.num_bits, bits_read);
217 }
218
219 return AWS_OP_SUCCESS;
220 }
221
222 AWS_TEST_CASE(huffman_decoder, test_huffman_decoder)
223 static int test_huffman_decoder(struct aws_allocator *allocator, void *ctx) {
224 (void)allocator;
225 (void)ctx;
226 /* Test decoding a short url */
227
228 char output_buffer[URL_STRING_LEN + 1];
229 AWS_ZERO_ARRAY(output_buffer);
230 struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output_buffer, URL_STRING_LEN);
231
232 struct aws_huffman_symbol_coder *coder = test_get_coder();
233 struct aws_huffman_decoder decoder;
234 aws_huffman_decoder_init(&decoder, coder);
235
236 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(s_encoded_url, ENCODED_URL_LEN);
237 int result = aws_huffman_decode(&decoder, &to_decode, &output_buf);
238
239 ASSERT_SUCCESS(result);
240 ASSERT_UINT_EQUALS(URL_STRING_LEN, output_buf.len);
241 ASSERT_UINT_EQUALS(0, to_decode.len);
242 ASSERT_UINT_EQUALS(output_buffer[URL_STRING_LEN], 0);
243 ASSERT_BIN_ARRAYS_EQUALS(s_url_string, URL_STRING_LEN, output_buf.buffer, output_buf.len);
244
245 return AWS_OP_SUCCESS;
246 }
247
248 AWS_TEST_CASE(huffman_decoder_all_code_points, test_huffman_decoder_all_code_points)
249 static int test_huffman_decoder_all_code_points(struct aws_allocator *allocator, void *ctx) {
250 (void)allocator;
251 (void)ctx;
252 /* Test decoding a sequence of all character values expressable as
253 * characters */
254
255 char output_buffer[ALL_CODES_LEN + 1];
256 AWS_ZERO_ARRAY(output_buffer);
257 struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output_buffer, ALL_CODES_LEN);
258
259 struct aws_huffman_symbol_coder *coder = test_get_coder();
260 struct aws_huffman_decoder decoder;
261 aws_huffman_decoder_init(&decoder, coder);
262
263 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(s_encoded_codes, ENCODED_CODES_LEN);
264 int result = aws_huffman_decode(&decoder, &to_decode, &output_buf);
265
266 ASSERT_SUCCESS(result);
267 ASSERT_UINT_EQUALS(ALL_CODES_LEN, output_buf.len);
268 ASSERT_UINT_EQUALS(0, to_decode.len);
269 ASSERT_UINT_EQUALS(output_buffer[ALL_CODES_LEN], 0);
270 ASSERT_BIN_ARRAYS_EQUALS(s_all_codes, ALL_CODES_LEN, output_buf.buffer, output_buf.len);
271
272 return AWS_OP_SUCCESS;
273 }
274
275 AWS_TEST_CASE(huffman_decoder_partial_input, test_huffman_decoder_partial_input)
276 static int test_huffman_decoder_partial_input(struct aws_allocator *allocator, void *ctx) {
277 (void)allocator;
278 (void)ctx;
279 /* Test decoding a buffer in chunks */
280
281 struct aws_huffman_decoder decoder;
282 aws_huffman_decoder_init(&decoder, test_get_coder());
283
284 char output_buffer[150];
285
286 for (size_t i = 0; i < NUM_STEP_SIZES; ++i) {
287 const size_t step_size = s_step_sizes[i];
288
289 aws_huffman_decoder_reset(&decoder);
290
291 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(s_encoded_codes, ENCODED_CODES_LEN);
292 struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output_buffer, ALL_CODES_LEN);
293 AWS_ZERO_ARRAY(output_buffer);
294
295 do {
296 const size_t chunk_size = step_size < to_decode.len ? step_size : to_decode.len;
297 struct aws_byte_cursor to_decode_chunk = aws_byte_cursor_advance(&to_decode, chunk_size);
298
299 int result = aws_huffman_decode(&decoder, &to_decode_chunk, &output_buf);
300
301 ASSERT_UINT_EQUALS(0, to_decode_chunk.len);
302 ASSERT_BIN_ARRAYS_EQUALS(s_all_codes, output_buf.len, output_buf.buffer, output_buf.len);
303
304 if (output_buf.len == ALL_CODES_LEN) {
305 ASSERT_SUCCESS(result);
306 }
307 } while (output_buf.len < ALL_CODES_LEN);
308
309 ASSERT_UINT_EQUALS(ALL_CODES_LEN, output_buf.len);
310 ASSERT_BIN_ARRAYS_EQUALS(s_all_codes, ALL_CODES_LEN, output_buf.buffer, output_buf.len);
311 }
312
313 return AWS_OP_SUCCESS;
314 }
315
316 AWS_TEST_CASE(huffman_decoder_partial_output, test_huffman_decoder_partial_output)
317 static int test_huffman_decoder_partial_output(struct aws_allocator *allocator, void *ctx) {
318 (void)allocator;
319 (void)ctx;
320 /* Test decoding when the output buffer size is limited */
321
322 struct aws_huffman_decoder decoder;
323 aws_huffman_decoder_init(&decoder, test_get_coder());
324
325 char output_buffer[150];
326
327 for (size_t i = 0; i < NUM_STEP_SIZES; ++i) {
328 const size_t step_size = s_step_sizes[i];
329
330 aws_huffman_decoder_reset(&decoder);
331
332 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(s_encoded_codes, ENCODED_CODES_LEN);
333 struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output_buffer, (size_t)-1);
334 output_buf.capacity = 0; /* Can't set above because it sets buffer to 0 */
335 AWS_ZERO_ARRAY(output_buffer);
336
337 do {
338 output_buf.capacity += step_size;
339 if (output_buf.capacity > ALL_CODES_LEN) {
340 output_buf.capacity = ALL_CODES_LEN;
341 }
342
343 const size_t previous_output_size = output_buf.len;
344
345 int result = aws_huffman_decode(&decoder, &to_decode, &output_buf);
346
347 ASSERT_TRUE(output_buf.len > previous_output_size);
348 ASSERT_BIN_ARRAYS_EQUALS(s_all_codes, output_buf.len, output_buf.buffer, output_buf.len);
349
350 if (output_buf.len == ALL_CODES_LEN) {
351 ASSERT_SUCCESS(result);
352 } else {
353 ASSERT_UINT_EQUALS(AWS_ERROR_SHORT_BUFFER, aws_last_error());
354 aws_reset_error();
355 }
356 } while (output_buf.len < ALL_CODES_LEN);
357
358 ASSERT_UINT_EQUALS(ALL_CODES_LEN, output_buf.len);
359 ASSERT_BIN_ARRAYS_EQUALS(s_all_codes, ALL_CODES_LEN, output_buf.buffer, output_buf.len);
360 }
361
362 return AWS_OP_SUCCESS;
363 }
364
365 AWS_TEST_CASE(huffman_decoder_allow_growth, test_huffman_decoder_allow_growth)
366 static int test_huffman_decoder_allow_growth(struct aws_allocator *allocator, void *ctx) {
367 (void)ctx;
368 /* Test that decoder will grow output buffer if allow-growth is set */
369
370 struct aws_huffman_decoder decoder;
371 aws_huffman_decoder_init(&decoder, test_get_coder());
372 aws_huffman_decoder_allow_growth(&decoder, true);
373
374 struct aws_byte_buf output_buf;
375 ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, allocator, 1 /* way too small */));
376
377 struct aws_byte_cursor to_decode = aws_byte_cursor_from_array(s_encoded_url, ENCODED_URL_LEN);
378 ASSERT_SUCCESS(aws_huffman_decode(&decoder, &to_decode, &output_buf));
379
380 ASSERT_UINT_EQUALS(0, to_decode.len);
381 ASSERT_BIN_ARRAYS_EQUALS(s_url_string, URL_STRING_LEN, output_buf.buffer, output_buf.len);
382
383 aws_byte_buf_clean_up(&output_buf);
384 return AWS_OP_SUCCESS;
385 }
386
387 AWS_TEST_CASE(huffman_transitive, test_huffman_transitive)
388 static int test_huffman_transitive(struct aws_allocator *allocator, void *ctx) {
389 (void)allocator;
390 (void)ctx;
391 /* Test encoding a short url and immediately decoding it */
392
393 const char *error_message = NULL;
394 int result =
395 huffman_test_transitive(test_get_coder(), s_url_string, URL_STRING_LEN, ENCODED_URL_LEN, &error_message);
396 ASSERT_SUCCESS(result, error_message);
397
398 return AWS_OP_SUCCESS;
399 }
400
401 AWS_TEST_CASE(huffman_transitive_even_bytes, test_huffman_transitive_even_bytes)
402 static int test_huffman_transitive_even_bytes(struct aws_allocator *allocator, void *ctx) {
403 (void)allocator;
404 (void)ctx;
405 /* Test encoding a string that encodes to a multiple of 8 bits */
406
407 const char *error_message = NULL;
408 int result = huffman_test_transitive(test_get_coder(), "cdfh", 4, 3, &error_message);
409 ASSERT_SUCCESS(result, error_message);
410
411 return AWS_OP_SUCCESS;
412 }
413
414 AWS_TEST_CASE(huffman_transitive_all_code_points, test_huffman_transitive_all_code_points)
415 static int test_huffman_transitive_all_code_points(struct aws_allocator *allocator, void *ctx) {
416 (void)allocator;
417 (void)ctx;
418 /* Test encoding a sequence of all character values expressable as
419 * characters and immediately decoding it */
420
421 const char *error_message = NULL;
422 int result =
423 huffman_test_transitive(test_get_coder(), s_all_codes, ALL_CODES_LEN, ENCODED_CODES_LEN, &error_message);
424 ASSERT_SUCCESS(result, error_message);
425
426 return AWS_OP_SUCCESS;
427 }
428
429 AWS_TEST_CASE(huffman_transitive_chunked, test_huffman_transitive_chunked)
430 static int test_huffman_transitive_chunked(struct aws_allocator *allocator, void *ctx) {
431 (void)allocator;
432 (void)ctx;
433 /* Test encoding a sequence of all character values expressable as
434 * characters and immediately decoding it */
435
436 for (size_t i = 0; i < NUM_STEP_SIZES; ++i) {
437 const size_t step_size = s_step_sizes[i];
438
439 const char *error_message = NULL;
440 int result = huffman_test_transitive_chunked(
441 test_get_coder(), s_all_codes, ALL_CODES_LEN, ENCODED_CODES_LEN, step_size, &error_message);
442 ASSERT_SUCCESS(result, error_message);
443 }
444
445 return AWS_OP_SUCCESS;
446 }