HardenedBSD/tests/include/byteswap_test.c
Warner Losh ac77b26215 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
2024-10-15 17:14:42 -06:00

77 lines
1.7 KiB
C

/*-
* Copyright (c) 2021 M. Warner Losh <imp@FreeBSD.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <byteswap.h>
#include <atf-c.h>
ATF_TC(byteswap);
ATF_TC_HEAD(byteswap, tc)
{
atf_tc_set_md_var(tc, "descr", "Test swapping macros in <byteswap.h>");
}
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();
}