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