| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/checksums/checksums.h> |
| 7 |
#include <aws/checksums/crc.h> |
| 8 |
#include <aws/checksums/private/crc64_priv.h> |
| 9 |
#include <aws/checksums/private/crc_util.h> |
| 10 |
#include <aws/common/encoding.h> |
| 11 |
#include <aws/testing/aws_test_harness.h> |
| 12 |
|
| 13 |
|
| 14 |
static const uint64_t POLY_CRC64NVME = 0x9a6c9329ac4bc9b5; |
| 15 |
|
| 16 |
|
| 17 |
static const uint64_t RESIDUE_CRC64NVME = (uint64_t)~0xf310303b2b6f6e42; |
| 18 |
|
| 19 |
static const uint8_t DATA_32_ZEROS[32] = {0}; |
| 20 |
static const uint64_t KNOWN_CRC64NVME_32_ZEROES = 0xCF3473434D4ECF3B; |
| 21 |
|
| 22 |
static const uint8_t DATA_32_VALUES[32] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 23 |
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; |
| 24 |
static const uint64_t KNOWN_CRC64NVME_32_VALUES = 0xB9D9D4A8492CBD7F; |
| 25 |
|
| 26 |
static const uint8_t TEST_VECTOR[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; |
| 27 |
static const uint64_t KNOWN_CRC64NVME_TEST_VECTOR = 0xAE8B14860A799888; |
| 28 |
|
| 29 |
typedef uint64_t(crc_fn)(const uint8_t *input, int length, uint64_t previousCrc64); |
| 30 |
#define CRC_FUNC_NAME(crc_func) #crc_func, crc_func |
| 31 |
#define DATA_NAME(dataset) #dataset, dataset, sizeof(dataset) |
| 32 |
#define TEST_BUFFER_SIZE 2048 + 64 |
| 33 |
|
| 34 |
|
| 35 |
static uint64_t crc64nvme_reference(const uint8_t *input, int length, const uint64_t previousCrc64) { |
| 36 |
uint64_t crc = ~previousCrc64; |
| 37 |
while (length-- > 0) { |
| 38 |
crc ^= *input++; |
| 39 |
for (int j = 8; j > 0; --j) { |
| 40 |
crc = (crc >> 1) ^ ((((crc & 1) ^ 1) - 1) & POLY_CRC64NVME); |
| 41 |
} |
| 42 |
} |
| 43 |
return ~crc; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
static int s_test_known_crc( |
| 48 |
const char *func_name, |
| 49 |
crc_fn *func, |
| 50 |
const char *data_name, |
| 51 |
const uint8_t *input, |
| 52 |
const size_t length, |
| 53 |
const uint64_t expected_crc, |
| 54 |
const uint64_t expected_residue) { |
| 55 |
|
| 56 |
uint64_t result = func(input, (int)length, 0); |
| 57 |
ASSERT_HEX_EQUALS(expected_crc, result, "%s(%s)", func_name, data_name); |
| 58 |
|
| 59 |
|
| 60 |
uint64_t result_le = aws_bswap64_if_be(result); |
| 61 |
uint64_t residue = func((const uint8_t *)&result_le, 8, result); |
| 62 |
ASSERT_HEX_EQUALS(expected_residue, residue, "len %d residue %s(%s)", length, func_name, data_name); |
| 63 |
|
| 64 |
|
| 65 |
uint64_t crc1 = func(input, (int)(length / 2), 0); |
| 66 |
result = func(input + (length / 2), (int)(length - length / 2), crc1); |
| 67 |
ASSERT_HEX_EQUALS(expected_crc, result, "chaining %s(%s)", func_name, data_name); |
| 68 |
|
| 69 |
crc1 = 0; |
| 70 |
for (size_t i = 0; i < length; ++i) { |
| 71 |
crc1 = func(input + i, 1, crc1); |
| 72 |
} |
| 73 |
ASSERT_HEX_EQUALS(expected_crc, crc1, "one byte at a time %s(%s)", func_name, data_name); |
| 74 |
|
| 75 |
return AWS_OP_SUCCESS; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
static int s_test_known_crc64nvme(struct aws_allocator *allocator, const char *func_name, crc_fn *func) { |
| 80 |
int res = 0; |
| 81 |
|
| 82 |
|
| 83 |
res |= s_test_known_crc(func_name, func, DATA_NAME(DATA_32_ZEROS), KNOWN_CRC64NVME_32_ZEROES, RESIDUE_CRC64NVME); |
| 84 |
res |= s_test_known_crc(func_name, func, DATA_NAME(DATA_32_VALUES), KNOWN_CRC64NVME_32_VALUES, RESIDUE_CRC64NVME); |
| 85 |
res |= s_test_known_crc(func_name, func, DATA_NAME(TEST_VECTOR), KNOWN_CRC64NVME_TEST_VECTOR, RESIDUE_CRC64NVME); |
| 86 |
|
| 87 |
if (func == crc64nvme_reference) { |
| 88 |
|
| 89 |
return res; |
| 90 |
} |
| 91 |
|
| 92 |
struct aws_byte_buf test_buf; |
| 93 |
ASSERT_SUCCESS(aws_byte_buf_init(&test_buf, allocator, TEST_BUFFER_SIZE)); |
| 94 |
|
| 95 |
|
| 96 |
for (int off = 0; off < 16; off++) { |
| 97 |
|
| 98 |
aws_byte_buf_write_u8_n(&test_buf, (uint8_t)off + 129, test_buf.capacity - test_buf.len); |
| 99 |
uint64_t expected = 0; |
| 100 |
int len = 1; |
| 101 |
|
| 102 |
for (int i = 0; i < (TEST_BUFFER_SIZE - off) && !res; i++, len++) { |
| 103 |
test_buf.buffer[off + i] = (uint8_t)((i + 1) * 131); |
| 104 |
|
| 105 |
expected = crc64nvme_reference(&test_buf.buffer[off + i], 1, expected); |
| 106 |
|
| 107 |
res |= s_test_known_crc( |
| 108 |
func_name, func, "test_buffer", &test_buf.buffer[off], len, expected, RESIDUE_CRC64NVME); |
| 109 |
} |
| 110 |
aws_byte_buf_reset(&test_buf, false); |
| 111 |
} |
| 112 |
aws_byte_buf_clean_up(&test_buf); |
| 113 |
|
| 114 |
return res; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
static int s_test_crc64nvme(struct aws_allocator *allocator, void *ctx) { |
| 121 |
(void)ctx; |
| 122 |
|
| 123 |
int res = 0; |
| 124 |
|
| 125 |
res |= s_test_known_crc64nvme(allocator, CRC_FUNC_NAME(crc64nvme_reference)); |
| 126 |
res |= s_test_known_crc64nvme(allocator, CRC_FUNC_NAME(aws_checksums_crc64nvme_sw)); |
| 127 |
res |= s_test_known_crc64nvme(allocator, CRC_FUNC_NAME(aws_checksums_crc64nvme)); |
| 128 |
|
| 129 |
return res; |
| 130 |
} |
| 131 |
|
| 132 |
AWS_TEST_CASE(test_crc64nvme, s_test_crc64nvme) |
| 133 |
|
| 134 |
static int s_test_crc64nvme_init(struct aws_allocator *allocator, void *ctx) { |
| 135 |
(void)ctx; |
| 136 |
|
| 137 |
aws_checksums_library_init(allocator); |
| 138 |
|
| 139 |
int res = 0; |
| 140 |
|
| 141 |
res |= s_test_known_crc64nvme(allocator, CRC_FUNC_NAME(crc64nvme_reference)); |
| 142 |
res |= s_test_known_crc64nvme(allocator, CRC_FUNC_NAME(aws_checksums_crc64nvme_sw)); |
| 143 |
res |= s_test_known_crc64nvme(allocator, CRC_FUNC_NAME(aws_checksums_crc64nvme)); |
| 144 |
|
| 145 |
aws_checksums_library_clean_up(); |
| 146 |
|
| 147 |
return res; |
| 148 |
} |
| 149 |
AWS_TEST_CASE(test_crc64nvme_init, s_test_crc64nvme_init) |
| 150 |
|
| 151 |
static int s_test_large_buffer_crc64(struct aws_allocator *allocator, void *ctx) { |
| 152 |
(void)ctx; |
| 153 |
#if SIZE_BITS == 32 || defined(__OpenBSD__) |
| 154 |
(void)allocator; |
| 155 |
return AWS_OP_SKIP; |
| 156 |
#else |
| 157 |
const size_t len = 3 * 1024 * 1024 * 1024ULL; |
| 158 |
const uint8_t *many_zeroes = aws_mem_calloc(allocator, len, sizeof(uint8_t)); |
| 159 |
uint64_t result = aws_checksums_crc64nvme_ex(many_zeroes, len, 0); |
| 160 |
aws_mem_release(allocator, (void *)many_zeroes); |
| 161 |
ASSERT_HEX_EQUALS(0xa1dddd7c6fd17075, result); |
| 162 |
return AWS_OP_SUCCESS; |
| 163 |
#endif |
| 164 |
} |
| 165 |
AWS_TEST_CASE(test_large_buffer_crc64, s_test_large_buffer_crc64) |
| 166 |
|
| 167 |
static int s_test_crc64nvme_combine(struct aws_allocator *allocator, void *ctx) { |
| 168 |
(void)ctx; |
| 169 |
|
| 170 |
aws_checksums_library_init(allocator); |
| 171 |
|
| 172 |
uint8_t *a = (uint8_t *)"aaaa"; |
| 173 |
uint8_t *b = (uint8_t *)"bbbb"; |
| 174 |
uint8_t *ab = (uint8_t *)"aaaabbbb"; |
| 175 |
|
| 176 |
uint64_t crc_a = aws_checksums_crc64nvme(a, 4, 0); |
| 177 |
uint64_t crc_b = aws_checksums_crc64nvme(b, 4, 0); |
| 178 |
uint64_t crc_ab = aws_checksums_crc64nvme(ab, 8, 0); |
| 179 |
|
| 180 |
ASSERT_INT_EQUALS(crc_ab, aws_checksums_crc64nvme_combine(crc_a, crc_b, 4)); |
| 181 |
|
| 182 |
uint8_t *c = (uint8_t *)"cccccccccccccccccccc"; |
| 183 |
uint8_t *d = (uint8_t *)"dd"; |
| 184 |
uint8_t *cd = (uint8_t *)"ccccccccccccccccccccdd"; |
| 185 |
|
| 186 |
uint64_t crc_c = aws_checksums_crc64nvme(c, 20, 0); |
| 187 |
uint64_t crc_d = aws_checksums_crc64nvme(d, 2, 0); |
| 188 |
uint64_t crc_cd = aws_checksums_crc64nvme(cd, 22, 0); |
| 189 |
|
| 190 |
ASSERT_INT_EQUALS(crc_cd, aws_checksums_crc64nvme_combine(crc_c, crc_d, 2)); |
| 191 |
|
| 192 |
uint8_t *e = (uint8_t *)""; |
| 193 |
uint8_t *f = (uint8_t *)"ffff"; |
| 194 |
uint8_t *ef = (uint8_t *)"ffff"; |
| 195 |
|
| 196 |
uint64_t crc_e = aws_checksums_crc64nvme(e, 0, 0); |
| 197 |
uint64_t crc_f = aws_checksums_crc64nvme(f, 4, 0); |
| 198 |
uint64_t crc_ef = aws_checksums_crc64nvme(ef, 4, 0); |
| 199 |
|
| 200 |
ASSERT_INT_EQUALS(crc_ef, aws_checksums_crc64nvme_combine(crc_e, crc_f, 4)); |
| 201 |
|
| 202 |
aws_checksums_library_clean_up(); |
| 203 |
|
| 204 |
return AWS_OP_SUCCESS; |
| 205 |
} |
| 206 |
AWS_TEST_CASE(test_crc64nvme_combine, s_test_crc64nvme_combine) |