Debian Codegraph @

crt/aws-checksums/tests/xxhash_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/checksums/checksums.h>
7 #include <aws/checksums/xxhash.h>
8 #include <aws/testing/aws_test_harness.h>
9
10 static const char *TEST_VECTOR = "abcdefghijklmnopqrstuvwxyz";
11
12 static int s_test_xxhash64(struct aws_allocator *allocator, void *ctx) {
13 (void)ctx;
14
15 aws_checksums_library_init(allocator);
16
17 struct aws_byte_buf result;
18 aws_byte_buf_init(&result, allocator, 8);
19
20 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("Hello world");
21
22 ASSERT_SUCCESS(aws_xxhash64_compute(0, input, &result));
23
24 uint8_t expected[] = {0xc5, 0x00, 0xb0, 0xc9, 0x12, 0xb3, 0x76, 0xd8};
25
26 ASSERT_BIN_ARRAYS_EQUALS(result.buffer, result.len, expected, sizeof(expected));
27
28 struct aws_xxhash *hash = aws_xxhash64_new(allocator, 0);
29 aws_xxhash_update(hash, input);
30 aws_byte_buf_reset(&result, false);
31 ASSERT_SUCCESS(aws_xxhash_finalize(hash, &result));
32
33 ASSERT_BIN_ARRAYS_EQUALS(result.buffer, result.len, expected, sizeof(expected));
34
35 aws_byte_buf_clean_up(&result);
36 aws_xxhash_destroy(hash);
37
38 aws_checksums_library_clean_up();
39
40 return AWS_OP_SUCCESS;
41 }
42 AWS_TEST_CASE(test_xxhash64, s_test_xxhash64)
43
44 static int s_test_xxhash64_generic(struct aws_allocator *allocator, void *ctx) {
45 (void)ctx;
46
47 aws_checksums_library_init(allocator);
48
49 struct aws_byte_buf result;
50 aws_byte_buf_init(&result, allocator, 8);
51
52 struct aws_byte_cursor input = aws_byte_cursor_from_c_str(TEST_VECTOR);
53
54 ASSERT_SUCCESS(aws_xxhash64_compute(0, input, &result));
55
56 uint8_t expected[] = {0xcf, 0xe1, 0xf2, 0x78, 0xfa, 0x89, 0x83, 0x5c};
57
58 ASSERT_BIN_ARRAYS_EQUALS(result.buffer, result.len, expected, sizeof(expected));
59
60 struct aws_xxhash *hash = aws_xxhash64_new(allocator, 0);
61 aws_xxhash_update(hash, input);
62 aws_byte_buf_reset(&result, false);
63 ASSERT_SUCCESS(aws_xxhash_finalize(hash, &result));
64
65 ASSERT_BIN_ARRAYS_EQUALS(result.buffer, result.len, expected, sizeof(expected));
66
67 aws_byte_buf_clean_up(&result);
68 aws_xxhash_destroy(hash);
69
70 aws_checksums_library_clean_up();
71
72 return AWS_OP_SUCCESS;
73 }
74 AWS_TEST_CASE(test_xxhash64_generic, s_test_xxhash64_generic)
75
76 static int s_test_xxhash3_64(struct aws_allocator *allocator, void *ctx) {
77 (void)ctx;
78
79 aws_checksums_library_init(allocator);
80
81 struct aws_byte_buf result;
82 aws_byte_buf_init(&result, allocator, 8);
83
84 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("Hello world");
85
86 ASSERT_SUCCESS(aws_xxhash3_64_compute(0, input, &result));
87
88 uint8_t expected[] = {0xb6, 0xac, 0xb9, 0xd8, 0x4a, 0x38, 0xff, 0x74};
89
90 ASSERT_BIN_ARRAYS_EQUALS(result.buffer, result.len, expected, sizeof(expected));
91
92 struct aws_xxhash *hash = aws_xxhash3_64_new(allocator, 0);
93 aws_xxhash_update(hash, input);
94 aws_byte_buf_reset(&result, false);
95 ASSERT_SUCCESS(aws_xxhash_finalize(hash, &result));
96
97 ASSERT_BIN_ARRAYS_EQUALS(result.buffer, result.len, expected, sizeof(expected));
98
99 aws_byte_buf_clean_up(&result);
100 aws_xxhash_destroy(hash);
101
102 aws_checksums_library_clean_up();
103
104 return AWS_OP_SUCCESS;
105 }
106 AWS_TEST_CASE(test_xxhash3_64, s_test_xxhash3_64)
107
108 static int s_test_xxhash3_64_generic(struct aws_allocator *allocator, void *ctx) {
109 (void)ctx;
110
111 aws_checksums_library_init(allocator);
112
113 struct aws_byte_buf result;
114 aws_byte_buf_init(&result, allocator, 8);
115
116 struct aws_byte_cursor input = aws_byte_cursor_from_c_str(TEST_VECTOR);
117
118 ASSERT_SUCCESS(aws_xxhash3_64_compute(0, input, &result));
119
120 uint8_t expected[] = {0x81, 0x0f, 0x9c, 0xa0, 0x67, 0xfb, 0xb9, 0x0c};
121
122 ASSERT_BIN_ARRAYS_EQUALS(result.buffer, result.len, expected, sizeof(expected));
123
124 struct aws_xxhash *hash = aws_xxhash3_64_new(allocator, 0);
125 aws_xxhash_update(hash, input);
126 aws_byte_buf_reset(&result, false);
127 ASSERT_SUCCESS(aws_xxhash_finalize(hash, &result));
128
129 ASSERT_BIN_ARRAYS_EQUALS(result.buffer, result.len, expected, sizeof(expected));
130
131 aws_byte_buf_clean_up(&result);
132 aws_xxhash_destroy(hash);
133
134 aws_checksums_library_clean_up();
135
136 return AWS_OP_SUCCESS;
137 }
138 AWS_TEST_CASE(test_xxhash3_64_generic, s_test_xxhash3_64_generic)
139
140 static int s_test_xxhash3_128(struct aws_allocator *allocator, void *ctx) {
141 (void)ctx;
142
143 aws_checksums_library_init(allocator);
144
145 struct aws_byte_buf result;
146 aws_byte_buf_init(&result, allocator, 16);
147
148 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("Hello world");
149
150 ASSERT_SUCCESS(aws_xxhash3_128_compute(0, input, &result));
151
152 uint8_t expected[] = {
153 0x73, 0x51, 0xf8, 0x98, 0x12, 0xf9, 0x73, 0x82, 0xb9, 0x1d, 0x05, 0xb3, 0x1e, 0x04, 0xdd, 0x7f};
154
155 ASSERT_BIN_ARRAYS_EQUALS(result.buffer, result.len, expected, sizeof(expected));
156
157 struct aws_xxhash *hash = aws_xxhash3_128_new(allocator, 0);
158 aws_xxhash_update(hash, input);
159 aws_byte_buf_reset(&result, false);
160 ASSERT_SUCCESS(aws_xxhash_finalize(hash, &result));
161
162 ASSERT_BIN_ARRAYS_EQUALS(result.buffer, result.len, expected, sizeof(expected));
163
164 aws_byte_buf_clean_up(&result);
165 aws_xxhash_destroy(hash);
166
167 aws_checksums_library_clean_up();
168
169 return AWS_OP_SUCCESS;
170 }
171 AWS_TEST_CASE(test_xxhash3_128, s_test_xxhash3_128)
172
173 static int s_test_xxhash3_128_generic(struct aws_allocator *allocator, void *ctx) {
174 (void)ctx;
175
176 aws_checksums_library_init(allocator);
177
178 struct aws_byte_buf result;
179 aws_byte_buf_init(&result, allocator, 16);
180
181 struct aws_byte_cursor input = aws_byte_cursor_from_c_str(TEST_VECTOR);
182
183 ASSERT_SUCCESS(aws_xxhash3_128_compute(0, input, &result));
184
185 uint8_t expected[] = {
186 0xdb, 0x7c, 0xa4, 0x4e, 0x84, 0x84, 0x3d, 0x67, 0xeb, 0xe1, 0x62, 0x22, 0x01, 0x54, 0xe1, 0xe6};
187
188 ASSERT_BIN_ARRAYS_EQUALS(result.buffer, result.len, expected, sizeof(expected));
189
190 struct aws_xxhash *hash = aws_xxhash3_128_new(allocator, 0);
191 aws_xxhash_update(hash, input);
192 aws_byte_buf_reset(&result, false);
193 ASSERT_SUCCESS(aws_xxhash_finalize(hash, &result));
194
195 ASSERT_BIN_ARRAYS_EQUALS(result.buffer, result.len, expected, sizeof(expected));
196
197 aws_byte_buf_clean_up(&result);
198 aws_xxhash_destroy(hash);
199
200 aws_checksums_library_clean_up();
201
202 return AWS_OP_SUCCESS;
203 }
204 AWS_TEST_CASE(test_xxhash3_128_generic, s_test_xxhash3_128_generic)