From nobody Tue Oct 15 23:15:16 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XSqh51N8pz5Z89K; Tue, 15 Oct 2024 23:15:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XSqh50YVbz42Th; Tue, 15 Oct 2024 23:15:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729034117; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yZoLaSOib5ye1oi1P4syDxNr3yTxAWavk+rvd7XbPQs=; b=VEOjLrw0ZTF6eXxQXz6/OuVTunl8XdIPXbqjXdIT5+DrUxGomqmILt5JWc3uldYyflAtVD N4ZspE3uv+7EqZS0UmWzJO409NcILnprqFw7Anm+8YJ1sJbRnK2kvZZXmaaKBpwtcD+8Wo 5jz3f7lRvXLFiGSBMrzpFEf8JGjOiCeOSdjqdDq5Gyhku28lz4WYdbn4YOMkvTnioW7sZV F8lUSYCTEHzxjUaN/oYKtDGGN3JrMSNMotLAOJoNZWfTXaOXEh+BgPlQ5X1tT2MsMx0kvS xzAJns1KpnZGk9GrK4NiqbQGBKPR/l5rXpRNcAiCD65K+Tb9M1xowyqOtqjdkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729034117; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yZoLaSOib5ye1oi1P4syDxNr3yTxAWavk+rvd7XbPQs=; b=ONdixphr5fze06L5y7P1LRKyU7OQMAXjSIlQESyuv++pCmBcBFe0uKOCARiq9iKKpHuUB/ KPeeoC8qYFYJvIU5m2TDGCOfz/mWysNa05DqSr95d8wJ9aIvQ1uASuO4LUg2zz0iPxDeRu fqCf/E+T42U8FRVaYJiqpZlzcuYy4vC8ql+IIol7itEcsb/RZQBYjWJCegiE/RGU0vm7dC /g6OF0QZieF8JtJNVc0uCquYhgF/jVV6yFOhjhQM1tk+ImB1vXyq6uSOkVlHshyyEGsg0W JFfyv752FDnmQ09p9HIp+R67w4qUT9G2jjOdxvkWT3K0HfoVWsh4/ghtPDuqmg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1729034117; a=rsa-sha256; cv=none; b=VvNBIYUixMtpglqYHXWiNbvaNt1mnqjS6EUY/UNh+nfm6O5BHYam5NJaxiEkmiz6klev/C CHUhBT2JWAfLdDWqrgYy+d55XjJFTxKHj3TuwX40rkZg39RxNET14gx+j+aOAU8EnSNty5 c50MLRBxhIiqQv/Wg6D8cV1eWSzJ6jh8syh0/TaDYdgf/6Yqd5OQfym4PEBbDJ8BUpT+tW 1YhgQt6mRRaM6brpWYOYOtPGYD/yBNLEtD16J6r+2URxdMqb+MbU3DxIwLSeUn4Ux3YxGK B3uy8/+TWOWoOtimmt9XYZXXYdxAhzzCg+k2raaomm4qvE5m/LuyccGhIABcaQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XSqh46yH3zhrD; Tue, 15 Oct 2024 23:15:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 49FNFG9T024703; Tue, 15 Oct 2024 23:15:16 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49FNFGER024700; Tue, 15 Oct 2024 23:15:16 GMT (envelope-from git) Date: Tue, 15 Oct 2024 23:15:16 GMT Message-Id: <202410152315.49FNFGER024700@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: ac77b2621508 - main - tests: Test endian.h, byteswap.h, sys/endian.h and both endian.h and byteswap.h together List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: ac77b2621508c6a50ab01d07fe8d43795d908f05 Auto-Submitted: auto-generated The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=ac77b2621508c6a50ab01d07fe8d43795d908f05 commit ac77b2621508c6a50ab01d07fe8d43795d908f05 Author: Warner Losh AuthorDate: 2024-10-15 23:14:42 +0000 Commit: Warner Losh CommitDate: 2024-10-15 23:14:42 +0000 tests: Test endian.h, byteswap.h, sys/endian.h and both endian.h and byteswap.h together What's required and not required to be defined is complicated. Write tests to enshrine it: endian.h and sys/endian.h: [bl]e{16,32,64}toh hto[bl]e{16,32,64} byteswap.h: {__,}bswap_{16,32,64} sys/endian.h: {__,}bswap{16,32,64} _BYTE_ORDER _BIG_ENDIAN _LITTLE_ENDIAN _PDP_ENDIAN endian.h: __BYTE_ORDER __BIG_ENDIAN __LITTLE_ENDIAN __PDP_ENDIAN __FLOAT_WORD_ORDER We also ensure that the sys/endian.h conditions hold true when we include both endian.h and sys/endian.h in either order. NOT TESTED: deprecated symbols, internal to glibc symbols Sponsored by: Netflix Discussed with: markj (made the changes he requested) Differential Revision: https://reviews.freebsd.org/D32052 --- tests/include/Makefile | 6 ++ tests/include/byteswap_endian_test.c | 9 +++ tests/include/byteswap_test.c | 76 ++++++++++++++++++ tests/include/endian_sys_endian_test.c | 13 +++ tests/include/endian_test.c | 139 +++++++++++++++++++++++++++++++++ tests/include/sys_endian_endian_test.c | 12 +++ tests/include/sys_endian_test.c | 136 ++++++++++++++++++++++++++++++++ 7 files changed, 391 insertions(+) diff --git a/tests/include/Makefile b/tests/include/Makefile index e98d08da1f2b..0f6ef88f9e35 100644 --- a/tests/include/Makefile +++ b/tests/include/Makefile @@ -2,6 +2,12 @@ TESTSDIR= ${TESTSBASE}/include +ATF_TESTS_C+= byteswap_test +ATF_TESTS_C+= byteswap_endian_test +ATF_TESTS_C+= endian_test +ATF_TESTS_C+= endian_sys_endian_test ATF_TESTS_C+= stdckdint_test +ATF_TESTS_C+= sys_endian_test +ATF_TESTS_C+= sys_endian_endian_test .include diff --git a/tests/include/byteswap_endian_test.c b/tests/include/byteswap_endian_test.c new file mode 100644 index 000000000000..012c1b5c447e --- /dev/null +++ b/tests/include/byteswap_endian_test.c @@ -0,0 +1,9 @@ +/*- + * Copyright (c) 2021 M. Warner Losh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +/* Make sure this still passes if both endian.h and byteswap.h included */ +#include +#include "byteswap_test.c" diff --git a/tests/include/byteswap_test.c b/tests/include/byteswap_test.c new file mode 100644 index 000000000000..ed23351e689b --- /dev/null +++ b/tests/include/byteswap_test.c @@ -0,0 +1,76 @@ +/*- + * Copyright (c) 2021 M. Warner Losh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include + +ATF_TC(byteswap); +ATF_TC_HEAD(byteswap, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test swapping macros in "); +} + +ATF_TC_BODY(byteswap, tc) +{ + uint16_t ui16; + uint32_t ui32; + uint64_t ui64; + + /* glibc defines the {__,}bswap_{16,32,64} */ +#ifndef __bswap_16 + atf_tc_fail_nonfatal("__bswap_16 not defined"); +#endif +#ifndef bswap_16 + atf_tc_fail_nonfatal("bswap_16 not defined"); +#endif +#ifndef __bswap_32 + atf_tc_fail_nonfatal("__bswap_32 not defined"); +#endif +#ifndef bswap_32 + atf_tc_fail_nonfatal("bswap_32 not defined"); +#endif +#ifndef __bswap_64 + atf_tc_fail_nonfatal("__bswap_64 not defined"); +#endif +#ifndef bswap_64 + atf_tc_fail_nonfatal("bswap_64 not defined"); +#endif + + /* glibc does not define bswap{16,32,64} */ +#ifdef bswap16 + atf_tc_fail_nonfatal("bswap16 improperly defined"); +#endif +#ifdef bswap32 + atf_tc_fail_nonfatal("bswap32 improperly defined"); +#endif +#ifdef bswap64 + atf_tc_fail_nonfatal("bswap64 improperly defined"); +#endif + + ui16 = 0x1234; + ATF_REQUIRE_MSG(0x3412 == bswap_16(ui16), + "bswap16(%#x) != 0x3412 instead %#x\n", ui16, bswap_16(ui16)); + + ui32 = 0x12345678ul; + ATF_REQUIRE_MSG(0x78563412ul == bswap_32(ui32), + "bswap32(%#lx) != 0x78563412 instead %#lx\n", + (unsigned long)ui32, (unsigned long)bswap_32(ui32)); + + ui64 = 0x123456789abcdef0ull; + ATF_REQUIRE_MSG(0xf0debc9a78563412ull == bswap_64(ui64), + "bswap64(%#llx) != 0x3412 instead %#llx\n", + (unsigned long long)ui64, (unsigned long long)bswap_64(ui64)); + +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, byteswap); + + return atf_no_error(); +} diff --git a/tests/include/endian_sys_endian_test.c b/tests/include/endian_sys_endian_test.c new file mode 100644 index 000000000000..9c7829c94b1f --- /dev/null +++ b/tests/include/endian_sys_endian_test.c @@ -0,0 +1,13 @@ +/*- + * Copyright (c) 2021 M. Warner Losh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +/* + * Make sure this still passes if both endian.h and sys/endian.h are included + * with sys/endian.h first + */ +#include +#include +#include "sys_endian_test.c" diff --git a/tests/include/endian_test.c b/tests/include/endian_test.c new file mode 100644 index 000000000000..2431b54535a2 --- /dev/null +++ b/tests/include/endian_test.c @@ -0,0 +1,139 @@ +/*- + * Copyright (c) 2021 M. Warner Losh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include + +ATF_TC(endian); +ATF_TC_HEAD(endian, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test swapping macros in "); +} + +ATF_TC_BODY(endian, tc) +{ + /* glibc doesn't define the {__,}bswap_{16,32,64} */ +#ifdef __bswap_16 + atf_tc_fail_nonfatal("__bswap_16 improperly defined"); +#endif +#ifdef bswap_16 + atf_tc_fail_nonfatal("bswap_16 improperly defined"); +#endif +#ifdef __bswap_32 + atf_tc_fail_nonfatal("__bswap_32 improperly defined"); +#endif +#ifdef bswap_32 + atf_tc_fail_nonfatal("bswap_32 improperly defined"); +#endif +#ifdef __bswap_64 + atf_tc_fail_nonfatal("__bswap_64 improperly defined"); +#endif +#ifdef bswap_64 + atf_tc_fail_nonfatal("bswap_64 improperly defined"); +#endif + + /* glibc doesn't define bswap{16,32,64} */ +#ifdef bswap16 + atf_tc_fail_nonfatal("bswap16 improperly defined"); +#endif +#ifdef bswap32 + atf_tc_fail_nonfatal("bswap32 improperly defined"); +#endif +#ifdef bswap64 + atf_tc_fail_nonfatal("bswap64 improperly defined"); +#endif + + /* + * glibc defines with two underscores. We don't test for only one since + * that doesn't interfere. + */ +#ifndef __BIG_ENDIAN + atf_tc_fail_nonfatal("__BIG_ENDIAN not defined"); +#endif +#ifndef __LITTLE_ENDIAN + atf_tc_fail_nonfatal("__LITTLE_ENDIAN not defined"); +#endif +#ifndef __PDP_ENDIAN + atf_tc_fail_nonfatal("__PDP_ENDIAN not defined"); +#endif +#ifndef __FLOAT_WORD_ORDER + atf_tc_fail_nonfatal("__FLOAT_WORD_ORDER not defined"); +#endif +#ifndef __BYTE_ORDER + atf_tc_fail_nonfatal("__BYTE_ORDER not defined"); +#endif + + /* order to host */ +#ifdef __BYTE_ORDER +#if __BYTE_ORDER == __BIG_ENDIAN +#define H16(x) be16toh(x) +#define H32(x) be32toh(x) +#define H64(x) be64toh(x) +#define O16(x) le16toh(x) +#define O32(x) le32toh(x) +#define O64(x) le64toh(x) +#else +#define H16(x) le16toh(x) +#define H32(x) le32toh(x) +#define H64(x) le64toh(x) +#define O16(x) be16toh(x) +#define O32(x) be32toh(x) +#define O64(x) be64toh(x) +#endif +#endif + ATF_REQUIRE(H16(0x1234) == 0x1234); + ATF_REQUIRE(H32(0x12345678ul) == 0x12345678ul); + ATF_REQUIRE(H64(0x123456789abcdef0ull) == 0x123456789abcdef0ull); + ATF_REQUIRE(O16(0x1234) == __bswap16(0x1234)); + ATF_REQUIRE(O32(0x12345678ul) == __bswap32(0x12345678ul)); + ATF_REQUIRE(O64(0x123456789abcdef0ull) == __bswap64(0x123456789abcdef0ull)); +#undef H16 +#undef H32 +#undef H64 +#undef O16 +#undef O32 +#undef O64 + + /* host to order */ +#ifdef __BYTE_ORDER +#if __BYTE_ORDER == __BIG_ENDIAN +#define H16(x) htobe16(x) +#define H32(x) htobe32(x) +#define H64(x) htobe64(x) +#define O16(x) htole16(x) +#define O32(x) htole32(x) +#define O64(x) htole64(x) +#else +#define H16(x) htole16(x) +#define H32(x) htole32(x) +#define H64(x) htole64(x) +#define O16(x) htobe16(x) +#define O32(x) htobe32(x) +#define O64(x) htobe64(x) +#endif +#endif + ATF_REQUIRE(H16(0x1234) == 0x1234); + ATF_REQUIRE(H32(0x12345678ul) == 0x12345678ul); + ATF_REQUIRE(H64(0x123456789abcdef0ull) == 0x123456789abcdef0ull); + ATF_REQUIRE(O16(0x1234) == __bswap16(0x1234)); + ATF_REQUIRE(O32(0x12345678ul) == __bswap32(0x12345678ul)); + ATF_REQUIRE(O64(0x123456789abcdef0ull) == __bswap64(0x123456789abcdef0ull)); +#undef H16 +#undef H32 +#undef H64 +#undef O16 +#undef O32 +#undef O64 +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, endian); + + return atf_no_error(); +} diff --git a/tests/include/sys_endian_endian_test.c b/tests/include/sys_endian_endian_test.c new file mode 100644 index 000000000000..4d668f8d5625 --- /dev/null +++ b/tests/include/sys_endian_endian_test.c @@ -0,0 +1,12 @@ +/*- + * Copyright (c) 2021 M. Warner Losh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +/* + * Make sure this still passes if both endian.h and sys/endian.h are included + * with endian.h first. + */ +#include +#include "sys_endian_test.c" diff --git a/tests/include/sys_endian_test.c b/tests/include/sys_endian_test.c new file mode 100644 index 000000000000..5a5f77526832 --- /dev/null +++ b/tests/include/sys_endian_test.c @@ -0,0 +1,136 @@ +/*- + * Copyright (c) 2021 M. Warner Losh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include + +ATF_TC(sys_endian); +ATF_TC_HEAD(sys_endian, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test swapping macros in "); +} + +ATF_TC_BODY(sys_endian, tc) +{ + /* FreeBSD sys/endian.h doesn't define the {__,}bswap_{16,32,64} */ +#ifdef __bswap_16 + atf_tc_fail_nonfatal("__bswap_16 defined"); +#endif +#ifdef bswap_16 + atf_tc_fail_nonfatal("bswap_16 defined"); +#endif +#ifdef __bswap_32 + atf_tc_fail_nonfatal("__bswap_32 defined"); +#endif +#ifdef bswap_32 + atf_tc_fail_nonfatal("bswap_32 defined"); +#endif +#ifdef __bswap_64 + atf_tc_fail_nonfatal("__bswap_64 defined"); +#endif +#ifdef bswap_64 + atf_tc_fail_nonfatal("bswap_64 defined"); +#endif + + /* FreeBSD sys/endian.h does define bswap{16,32,64} */ +#ifndef bswap16 + atf_tc_fail_nonfatal("bswap16 not defined"); +#endif +#ifndef bswap32 + atf_tc_fail_nonfatal("bswap32 not defined"); +#endif +#ifndef bswap64 + atf_tc_fail_nonfatal("bswap64 not defined"); +#endif + + /* + * FreeBSD defines with one underscore + * We don't test for two since that doesn't interfere + */ +#ifndef _BIG_ENDIAN + atf_tc_fail_nonfatal("_BIG_ENDIAN not defined"); +#endif +#ifndef _LITTLE_ENDIAN + atf_tc_fail_nonfatal("_LITTLE_ENDIAN not defined"); +#endif +#ifndef _PDP_ENDIAN + atf_tc_fail_nonfatal("_PDP_ENDIAN not defined"); +#endif +#ifndef _BYTE_ORDER + atf_tc_fail_nonfatal("_BYTE_ORDER not defined"); +#endif + + /* order to host */ +#ifdef _BYTE_ORDER +#if _BYTE_ORDER == _BIG_ENDIAN +#define H16(x) be16toh(x) +#define H32(x) be32toh(x) +#define H64(x) be64toh(x) +#define O16(x) le16toh(x) +#define O32(x) le32toh(x) +#define O64(x) le64toh(x) +#else +#define H16(x) le16toh(x) +#define H32(x) le32toh(x) +#define H64(x) le64toh(x) +#define O16(x) be16toh(x) +#define O32(x) be32toh(x) +#define O64(x) be64toh(x) +#endif +#endif + ATF_REQUIRE(H16(0x1234) == 0x1234); + ATF_REQUIRE(H32(0x12345678ul) == 0x12345678ul); + ATF_REQUIRE(H64(0x123456789abcdef0ull) == 0x123456789abcdef0ull); + ATF_REQUIRE(O16(0x1234) == __bswap16(0x1234)); + ATF_REQUIRE(O32(0x12345678ul) == __bswap32(0x12345678ul)); + ATF_REQUIRE(O64(0x123456789abcdef0ull) == __bswap64(0x123456789abcdef0ull)); +#undef H16 +#undef H32 +#undef H64 +#undef O16 +#undef O32 +#undef O64 + + /* host to order */ +#ifdef _BYTE_ORDER +#if _BYTE_ORDER == _BIG_ENDIAN +#define H16(x) htobe16(x) +#define H32(x) htobe32(x) +#define H64(x) htobe64(x) +#define O16(x) htole16(x) +#define O32(x) htole32(x) +#define O64(x) htole64(x) +#else +#define H16(x) htole16(x) +#define H32(x) htole32(x) +#define H64(x) htole64(x) +#define O16(x) htobe16(x) +#define O32(x) htobe32(x) +#define O64(x) htobe64(x) +#endif +#endif + ATF_REQUIRE(H16(0x1234) == 0x1234); + ATF_REQUIRE(H32(0x12345678ul) == 0x12345678ul); + ATF_REQUIRE(H64(0x123456789abcdef0ull) == 0x123456789abcdef0ull); + ATF_REQUIRE(O16(0x1234) == __bswap16(0x1234)); + ATF_REQUIRE(O32(0x12345678ul) == __bswap32(0x12345678ul)); + ATF_REQUIRE(O64(0x123456789abcdef0ull) == __bswap64(0x123456789abcdef0ull)); +#undef H16 +#undef H32 +#undef H64 +#undef O16 +#undef O32 +#undef O64 +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, sys_endian); + + return atf_no_error(); +}