| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
/* ! \file */ |
| 15 |
|
| 16 |
#include <inttypes.h> |
| 17 |
#include <sched.h> /* IWYU pragma: keep */ |
| 18 |
#include <setjmp.h> |
| 19 |
#include <stdarg.h> |
| 20 |
#include <stddef.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#define UNIT_TESTING |
| 25 |
#include <cmocka.h> |
| 26 |
|
| 27 |
#include <isc/crc64.h> |
| 28 |
#include <isc/result.h> |
| 29 |
#include <isc/util.h> |
| 30 |
|
| 31 |
#include <tests/isc.h> |
| 32 |
|
| 33 |
#define TEST_INPUT(x) (x), sizeof(x) - 1 |
| 34 |
|
| 35 |
ISC_RUN_TEST_IMPL(isc_crc64_init) { |
| 36 |
uint64_t crc; |
| 37 |
|
| 38 |
isc_crc64_init(&crc); |
| 39 |
assert_int_equal(crc, 0xffffffffffffffffUL); |
| 40 |
} |
| 41 |
|
| 42 |
static void |
| 43 |
_crc64(const char *buf, size_t buflen, const char *result, const int repeats) { |
| 44 |
uint64_t crc; |
| 45 |
|
| 46 |
isc_crc64_init(&crc); |
| 47 |
assert_int_equal(crc, 0xffffffffffffffffUL); |
| 48 |
|
| 49 |
for (int i = 0; i < repeats; i++) { |
| 50 |
isc_crc64_update(&crc, buf, buflen); |
| 51 |
} |
| 52 |
|
| 53 |
isc_crc64_final(&crc); |
| 54 |
|
| 55 |
char hex[16 + 1]; |
| 56 |
snprintf(hex, sizeof(hex), "%016" PRIX64, crc); |
| 57 |
|
| 58 |
assert_memory_equal(hex, result, (result ? strlen(result) : 0)); |
| 59 |
} |
| 60 |
|
| 61 |
/* 64-bit cyclic redundancy check */ |
| 62 |
ISC_RUN_TEST_IMPL(isc_crc64) { |
| 63 |
_crc64(TEST_INPUT(""), "0000000000000000", 1); |
| 64 |
_crc64(TEST_INPUT("a"), "CE73F427ACC0A99A", 1); |
| 65 |
_crc64(TEST_INPUT("abc"), "048B813AF9F49702", 1); |
| 66 |
_crc64(TEST_INPUT("message digest"), "5273F9EA7A357BF4", 1); |
| 67 |
_crc64(TEST_INPUT("abcdefghijklmnopqrstuvwxyz"), "59F079F9218BAAA1", 1); |
| 68 |
_crc64(TEST_INPUT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm" |
| 69 |
"nopqrstuvwxyz0123456789"), |
| 70 |
"A36DA8F71E78B6FB", 1); |
| 71 |
_crc64(TEST_INPUT("123456789012345678901234567890123456789" |
| 72 |
"01234567890123456789012345678901234567890"), |
| 73 |
"81E5EB73C8E7874A", 1); |
| 74 |
} |
| 75 |
|
| 76 |
ISC_TEST_LIST_START |
| 77 |
|
| 78 |
ISC_TEST_ENTRY(isc_crc64_init) |
| 79 |
ISC_TEST_ENTRY(isc_crc64) |
| 80 |
|
| 81 |
ISC_TEST_LIST_END |
| 82 |
|
| 83 |
ISC_TEST_MAIN |