| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/checksums/checksums.h> |
| 7 |
#include <aws/checksums/crc.h> |
| 8 |
#include <aws/checksums/private/crc32_priv.h> |
| 9 |
#include <aws/checksums/private/crc_util.h> |
| 10 |
|
| 11 |
#include <aws/common/device_random.h> |
| 12 |
|
| 13 |
#include <aws/testing/aws_test_harness.h> |
| 14 |
|
| 15 |
static const uint8_t DATA_32_ZEROS[32] = {0}; |
| 16 |
static const uint8_t DATA_32_VALUES[32] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 17 |
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; |
| 18 |
|
| 19 |
static const uint8_t TEST_VECTOR[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; |
| 20 |
|
| 21 |
|
| 22 |
static const uint32_t POLY_CRC32 = 0xedb88320; |
| 23 |
|
| 24 |
static const uint32_t RESIDUE_CRC32 = ~0xdebb20e3; |
| 25 |
static const uint32_t KNOWN_CRC32_32_ZEROES = 0x190A55AD; |
| 26 |
static const uint32_t KNOWN_CRC32_32_VALUES = 0x91267E8A; |
| 27 |
static const uint32_t KNOWN_CRC32_TEST_VECTOR = 0xCBF43926; |
| 28 |
|
| 29 |
|
| 30 |
static const uint32_t POLY_CRC32C = 0x82f63b78; |
| 31 |
|
| 32 |
static const uint32_t RESIDUE_CRC32C = ~0xb798b438; |
| 33 |
static const uint32_t KNOWN_CRC32C_32_ZEROES = 0x8A9136AA; |
| 34 |
static const uint32_t KNOWN_CRC32C_32_VALUES = 0x46DD794E; |
| 35 |
static const uint32_t KNOWN_CRC32C_TEST_VECTOR = 0xE3069283; |
| 36 |
|
| 37 |
typedef uint32_t(crc_fn)(const uint8_t *input, int length, uint32_t previousCrc32); |
| 38 |
#define CRC_FUNC_NAME(crc_func) #crc_func, crc_func |
| 39 |
#define DATA_NAME(dataset) #dataset, dataset, sizeof(dataset) |
| 40 |
#define TEST_BUFFER_SIZE 2048 + 64 |
| 41 |
|
| 42 |
|
| 43 |
static uint32_t s_crc_32_reference(const uint8_t *input, int length, const uint32_t previousCrc, uint32_t polynomial) { |
| 44 |
uint32_t crc = ~previousCrc; |
| 45 |
while (length-- > 0) { |
| 46 |
crc ^= *input++; |
| 47 |
for (int j = 8; j > 0; --j) { |
| 48 |
crc = (crc >> 1) ^ ((((crc & 1) ^ 1) - 1) & polynomial); |
| 49 |
} |
| 50 |
} |
| 51 |
return ~crc; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
static uint32_t s_crc32_reference(const uint8_t *input, int length, const uint32_t previousCrc) { |
| 56 |
return s_crc_32_reference(input, length, previousCrc, POLY_CRC32); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
static uint32_t s_crc32c_reference(const uint8_t *input, int length, const uint32_t previousCrc) { |
| 61 |
return s_crc_32_reference(input, length, previousCrc, POLY_CRC32C); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
static int s_test_known_crc_32( |
| 66 |
const char *func_name, |
| 67 |
crc_fn *func, |
| 68 |
const char *data_name, |
| 69 |
const uint8_t *input, |
| 70 |
const size_t length, |
| 71 |
const uint32_t expected_crc, |
| 72 |
const uint32_t expected_residue) { |
| 73 |
|
| 74 |
uint32_t result = func(input, (int)length, 0); |
| 75 |
ASSERT_HEX_EQUALS(expected_crc, result, "%s(%s)", func_name, data_name); |
| 76 |
|
| 77 |
uint32_t result_le = aws_bswap32_if_be(result); |
| 78 |
|
| 79 |
uint32_t residue = (uint32_t)func((const uint8_t *)&result_le, 4, result); |
| 80 |
ASSERT_HEX_EQUALS(expected_residue, residue, "len %d residue %s(%s)", length, func_name, data_name); |
| 81 |
|
| 82 |
|
| 83 |
uint32_t crc1 = func(input, (int)(length / 2), 0); |
| 84 |
result = func(input + (length / 2), (int)(length - length / 2), crc1); |
| 85 |
ASSERT_HEX_EQUALS(expected_crc, result, "chaining %s(%s)", func_name, data_name); |
| 86 |
|
| 87 |
crc1 = 0; |
| 88 |
for (size_t i = 0; i < length; ++i) { |
| 89 |
crc1 = func(input + i, 1, crc1); |
| 90 |
} |
| 91 |
ASSERT_HEX_EQUALS(expected_crc, crc1, "one byte at a time %s(%s)", func_name, data_name); |
| 92 |
|
| 93 |
return AWS_OP_SUCCESS; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
static int s_test_vs_reference_crc_32( |
| 98 |
struct aws_allocator *allocator, |
| 99 |
uint32_t polynomial, |
| 100 |
uint32_t residue, |
| 101 |
const char *func_name, |
| 102 |
crc_fn *func) { |
| 103 |
|
| 104 |
int res = 0; |
| 105 |
|
| 106 |
struct aws_byte_buf test_buf; |
| 107 |
ASSERT_SUCCESS(aws_byte_buf_init(&test_buf, allocator, TEST_BUFFER_SIZE)); |
| 108 |
|
| 109 |
|
| 110 |
for (int off = 0; off < 16; off++) { |
| 111 |
|
| 112 |
aws_byte_buf_write_u8_n(&test_buf, (uint8_t)off + 129, test_buf.capacity - test_buf.len); |
| 113 |
uint32_t expected = 0; |
| 114 |
int len = 1; |
| 115 |
|
| 116 |
for (int i = 0; i < (TEST_BUFFER_SIZE - off) && !res; i++, len++) { |
| 117 |
test_buf.buffer[off + i] = (uint8_t)((i + 1) * 131); |
| 118 |
|
| 119 |
expected = s_crc_32_reference(&test_buf.buffer[off + i], 1, expected, polynomial); |
| 120 |
|
| 121 |
res |= s_test_known_crc_32(func_name, func, "test_buffer", &test_buf.buffer[off], len, expected, residue); |
| 122 |
if (res != 0) { |
| 123 |
continue; |
| 124 |
} |
| 125 |
} |
| 126 |
aws_byte_buf_reset(&test_buf, false); |
| 127 |
} |
| 128 |
aws_byte_buf_clean_up(&test_buf); |
| 129 |
|
| 130 |
return res; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
static int s_test_known_crc32(struct aws_allocator *allocator, const char *func_name, crc_fn *func) { |
| 135 |
int res = 0; |
| 136 |
res |= s_test_known_crc_32(func_name, func, DATA_NAME(DATA_32_ZEROS), KNOWN_CRC32_32_ZEROES, RESIDUE_CRC32); |
| 137 |
res |= s_test_known_crc_32(func_name, func, DATA_NAME(DATA_32_VALUES), KNOWN_CRC32_32_VALUES, RESIDUE_CRC32); |
| 138 |
res |= s_test_known_crc_32(func_name, func, DATA_NAME(TEST_VECTOR), KNOWN_CRC32_TEST_VECTOR, RESIDUE_CRC32); |
| 139 |
if (func != s_crc32_reference) { |
| 140 |
res |= s_test_vs_reference_crc_32(allocator, POLY_CRC32, RESIDUE_CRC32, func_name, func); |
| 141 |
} |
| 142 |
return res; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
static int s_test_known_crc32c(struct aws_allocator *allocator, const char *func_name, crc_fn *func) { |
| 147 |
int res = 0; |
| 148 |
|
| 149 |
res |= s_test_known_crc_32(func_name, func, DATA_NAME(DATA_32_ZEROS), KNOWN_CRC32C_32_ZEROES, RESIDUE_CRC32C); |
| 150 |
res |= s_test_known_crc_32(func_name, func, DATA_NAME(DATA_32_VALUES), KNOWN_CRC32C_32_VALUES, RESIDUE_CRC32C); |
| 151 |
res |= s_test_known_crc_32(func_name, func, DATA_NAME(TEST_VECTOR), KNOWN_CRC32C_TEST_VECTOR, RESIDUE_CRC32C); |
| 152 |
if (func != s_crc32c_reference) { |
| 153 |
res |= s_test_vs_reference_crc_32(allocator, POLY_CRC32C, RESIDUE_CRC32C, func_name, func); |
| 154 |
} |
| 155 |
|
| 156 |
return res; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
static int s_test_crc32c(struct aws_allocator *allocator, void *ctx) { |
| 164 |
(void)ctx; |
| 165 |
|
| 166 |
int res = 0; |
| 167 |
|
| 168 |
res |= s_test_known_crc32c(allocator, CRC_FUNC_NAME(s_crc32c_reference)); |
| 169 |
res |= s_test_known_crc32c(allocator, CRC_FUNC_NAME(aws_checksums_crc32c_sw)); |
| 170 |
res |= s_test_known_crc32c(allocator, CRC_FUNC_NAME(aws_checksums_crc32c)); |
| 171 |
|
| 172 |
return res; |
| 173 |
} |
| 174 |
AWS_TEST_CASE(test_crc32c, s_test_crc32c) |
| 175 |
|
| 176 |
static int s_test_crc32c_init(struct aws_allocator *allocator, void *ctx) { |
| 177 |
(void)ctx; |
| 178 |
|
| 179 |
aws_checksums_library_init(allocator); |
| 180 |
|
| 181 |
int res = 0; |
| 182 |
|
| 183 |
res |= s_test_known_crc32c(allocator, CRC_FUNC_NAME(s_crc32c_reference)); |
| 184 |
res |= s_test_known_crc32c(allocator, CRC_FUNC_NAME(aws_checksums_crc32c_sw)); |
| 185 |
res |= s_test_known_crc32c(allocator, CRC_FUNC_NAME(aws_checksums_crc32c)); |
| 186 |
|
| 187 |
aws_checksums_library_clean_up(); |
| 188 |
|
| 189 |
return res; |
| 190 |
} |
| 191 |
AWS_TEST_CASE(test_crc32c_init, s_test_crc32c_init) |
| 192 |
|
| 193 |
static int s_test_crc32(struct aws_allocator *allocator, void *ctx) { |
| 194 |
(void)ctx; |
| 195 |
|
| 196 |
int res = 0; |
| 197 |
|
| 198 |
res |= s_test_known_crc32(allocator, CRC_FUNC_NAME(s_crc32_reference)); |
| 199 |
res |= s_test_known_crc32(allocator, CRC_FUNC_NAME(aws_checksums_crc32_sw)); |
| 200 |
res |= s_test_known_crc32(allocator, CRC_FUNC_NAME(aws_checksums_crc32)); |
| 201 |
|
| 202 |
return res; |
| 203 |
} |
| 204 |
AWS_TEST_CASE(test_crc32, s_test_crc32) |
| 205 |
|
| 206 |
static int s_test_crc32_init(struct aws_allocator *allocator, void *ctx) { |
| 207 |
(void)ctx; |
| 208 |
|
| 209 |
aws_checksums_library_init(allocator); |
| 210 |
|
| 211 |
int res = 0; |
| 212 |
|
| 213 |
res |= s_test_known_crc32(allocator, CRC_FUNC_NAME(s_crc32_reference)); |
| 214 |
res |= s_test_known_crc32(allocator, CRC_FUNC_NAME(aws_checksums_crc32_sw)); |
| 215 |
res |= s_test_known_crc32(allocator, CRC_FUNC_NAME(aws_checksums_crc32)); |
| 216 |
|
| 217 |
aws_checksums_library_clean_up(); |
| 218 |
|
| 219 |
return res; |
| 220 |
} |
| 221 |
AWS_TEST_CASE(test_crc32_init, s_test_crc32_init) |
| 222 |
|
| 223 |
static int s_test_large_buffer_crc32(struct aws_allocator *allocator, void *ctx) { |
| 224 |
(void)ctx; |
| 225 |
#if SIZE_BITS == 32 || defined(__OpenBSD__) |
| 226 |
(void)allocator; |
| 227 |
return AWS_OP_SKIP; |
| 228 |
#else |
| 229 |
const size_t len = 3 * 1024 * 1024 * 1024ULL; |
| 230 |
const uint8_t *many_zeroes = aws_mem_calloc(allocator, len, sizeof(uint8_t)); |
| 231 |
uint32_t result = aws_checksums_crc32_ex(many_zeroes, len, 0); |
| 232 |
aws_mem_release(allocator, (void *)many_zeroes); |
| 233 |
ASSERT_HEX_EQUALS(0x480BBE37, result); |
| 234 |
return AWS_OP_SUCCESS; |
| 235 |
#endif |
| 236 |
} |
| 237 |
AWS_TEST_CASE(test_large_buffer_crc32, s_test_large_buffer_crc32) |
| 238 |
|
| 239 |
static int s_test_crc32_combine(struct aws_allocator *allocator, void *ctx) { |
| 240 |
(void)ctx; |
| 241 |
|
| 242 |
aws_checksums_library_init(allocator); |
| 243 |
|
| 244 |
uint8_t *a = (uint8_t *)"aaaa"; |
| 245 |
uint8_t *b = (uint8_t *)"bbbb"; |
| 246 |
uint8_t *ab = (uint8_t *)"aaaabbbb"; |
| 247 |
|
| 248 |
uint32_t crc_a = aws_checksums_crc32(a, 4, 0); |
| 249 |
uint32_t crc_b = aws_checksums_crc32(b, 4, 0); |
| 250 |
uint32_t crc_ab = aws_checksums_crc32(ab, 8, 0); |
| 251 |
|
| 252 |
ASSERT_INT_EQUALS(crc_ab, aws_checksums_crc32_combine(crc_a, crc_b, 4)); |
| 253 |
|
| 254 |
uint8_t *c = (uint8_t *)"cccccccccccccccccccc"; |
| 255 |
uint8_t *d = (uint8_t *)"dd"; |
| 256 |
uint8_t *cd = (uint8_t *)"ccccccccccccccccccccdd"; |
| 257 |
|
| 258 |
uint32_t crc_c = aws_checksums_crc32(c, 20, 0); |
| 259 |
uint32_t crc_d = aws_checksums_crc32(d, 2, 0); |
| 260 |
uint32_t crc_cd = aws_checksums_crc32(cd, 22, 0); |
| 261 |
|
| 262 |
ASSERT_INT_EQUALS(crc_cd, aws_checksums_crc32_combine(crc_c, crc_d, 2)); |
| 263 |
|
| 264 |
uint8_t *e = (uint8_t *)""; |
| 265 |
uint8_t *f = (uint8_t *)"ffff"; |
| 266 |
uint8_t *ef = (uint8_t *)"ffff"; |
| 267 |
|
| 268 |
uint32_t crc_e = aws_checksums_crc32(e, 0, 0); |
| 269 |
uint32_t crc_f = aws_checksums_crc32(f, 4, 0); |
| 270 |
uint32_t crc_ef = aws_checksums_crc32(ef, 4, 0); |
| 271 |
|
| 272 |
ASSERT_INT_EQUALS(crc_ef, aws_checksums_crc32_combine(crc_e, crc_f, 4)); |
| 273 |
|
| 274 |
aws_checksums_library_clean_up(); |
| 275 |
|
| 276 |
return AWS_OP_SUCCESS; |
| 277 |
} |
| 278 |
AWS_TEST_CASE(test_crc32_combine, s_test_crc32_combine) |
| 279 |
|
| 280 |
static int s_test_crc32c_combine(struct aws_allocator *allocator, void *ctx) { |
| 281 |
(void)ctx; |
| 282 |
|
| 283 |
aws_checksums_library_init(allocator); |
| 284 |
|
| 285 |
uint8_t *a = (uint8_t *)"aaaa"; |
| 286 |
uint8_t *b = (uint8_t *)"bbbb"; |
| 287 |
uint8_t *ab = (uint8_t *)"aaaabbbb"; |
| 288 |
|
| 289 |
uint32_t crc_a = aws_checksums_crc32c(a, 4, 0); |
| 290 |
uint32_t crc_b = aws_checksums_crc32c(b, 4, 0); |
| 291 |
uint32_t crc_ab = aws_checksums_crc32c(ab, 8, 0); |
| 292 |
|
| 293 |
ASSERT_INT_EQUALS(crc_ab, aws_checksums_crc32c_combine(crc_a, crc_b, 4)); |
| 294 |
|
| 295 |
uint8_t *c = (uint8_t *)"cccccccccccccccccccc"; |
| 296 |
uint8_t *d = (uint8_t *)"dd"; |
| 297 |
uint8_t *cd = (uint8_t *)"ccccccccccccccccccccdd"; |
| 298 |
|
| 299 |
uint32_t crc_c = aws_checksums_crc32c(c, 20, 0); |
| 300 |
uint32_t crc_d = aws_checksums_crc32c(d, 2, 0); |
| 301 |
uint32_t crc_cd = aws_checksums_crc32c(cd, 22, 0); |
| 302 |
|
| 303 |
ASSERT_INT_EQUALS(crc_cd, aws_checksums_crc32c_combine(crc_c, crc_d, 2)); |
| 304 |
|
| 305 |
uint8_t *e = (uint8_t *)""; |
| 306 |
uint8_t *f = (uint8_t *)"ffff"; |
| 307 |
uint8_t *ef = (uint8_t *)"ffff"; |
| 308 |
|
| 309 |
uint32_t crc_e = aws_checksums_crc32c(e, 0, 0); |
| 310 |
uint32_t crc_f = aws_checksums_crc32c(f, 4, 0); |
| 311 |
uint32_t crc_ef = aws_checksums_crc32c(ef, 4, 0); |
| 312 |
|
| 313 |
ASSERT_INT_EQUALS(crc_ef, aws_checksums_crc32c_combine(crc_e, crc_f, 4)); |
| 314 |
|
| 315 |
aws_checksums_library_clean_up(); |
| 316 |
|
| 317 |
return AWS_OP_SUCCESS; |
| 318 |
} |
| 319 |
AWS_TEST_CASE(test_crc32c_combine, s_test_crc32c_combine) |