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