llvm-c166/libc/test/include/netinet_in_test.cpp
Pavel Labath 5a530a22fa
[libc] Reimplement IN6_IS_ADDR_* macros using statement expressions (#209772)
This patch fixes three issues with the previous implementation:
- the macro argument was being evaluated more than once
- casting to other types (uint32_t in particular) was an aliasing
violation
- it was casting to non-const pointers, resulting in compiler errors in
cases where the user passes a const ptr.

A statement expression fixes the first issue by using a temporary
variable. The second issue is fixed by using the appropriate members of
struct in6_addr. The last issue is fixed by dropping the cast
completely. This requires the user to pass a correctly types pointer (as
POSIX requires). Implementations keep it for compatibility with old code
passing void * and similar, but a quick survey shows that most modern
code passes the correct types. If this turns out to be an issue, we can
easily add the cast (to a const type) back.

Implementation notes:
- IN6_IS_ADDR_MULTICAST doesn't use the statement expression because the
argument is evaluated only once
- I use private helper macros (__IN6_IS_ADDR_UNSPECIFIED and
__IN6_IS_ADDR_LOOPBACK) so IN6_IS_ADDR_V4COMPAT does not nest statement
expressions
- I considered using private entrypoints (like we do with cpuset macros)
instead of statement expressions, but decided against it because these
macros are simpler and would thus result in a higher
boilerplate-to-useful-code ratio (particularly given our restriction on
calling other entrypoints)

Assisted by Gemini.
2026-07-16 08:35:53 +02:00

79 lines
2.3 KiB
C++

//===-- Unittests for netinet/in macro ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "include/llvm-libc-macros/netinet-in-macros.h"
#include "test/UnitTest/Test.h"
TEST(LlvmLibcNetinetInTest, IN6Macro) {
struct in6_addr addr = {};
const struct in6_addr *const_addr = &addr;
EXPECT_TRUE(IN6_IS_ADDR_UNSPECIFIED(&addr));
EXPECT_TRUE(IN6_IS_ADDR_UNSPECIFIED(const_addr));
for (int i = 0; i < 16; ++i) {
addr.s6_addr[i] = 1;
EXPECT_FALSE(IN6_IS_ADDR_UNSPECIFIED(&addr));
addr.s6_addr[i] = 0;
}
EXPECT_FALSE(IN6_IS_ADDR_LOOPBACK(&addr));
addr.s6_addr[15] = 1;
EXPECT_TRUE(IN6_IS_ADDR_LOOPBACK(&addr));
addr.s6_addr[15] = 0;
EXPECT_FALSE(IN6_IS_ADDR_MULTICAST(&addr));
addr.s6_addr[0] = 0xff;
EXPECT_TRUE(IN6_IS_ADDR_MULTICAST(&addr));
addr.s6_addr[0] = 0;
addr.s6_addr[0] = 0xfe;
addr.s6_addr[1] = 0x80;
EXPECT_TRUE(IN6_IS_ADDR_LINKLOCAL(&addr));
addr.s6_addr[0] = 0xff;
addr.s6_addr[1] = 0x80;
EXPECT_FALSE(IN6_IS_ADDR_LINKLOCAL(&addr));
addr.s6_addr[0] = 0xfe;
addr.s6_addr[1] = 0xc0;
EXPECT_TRUE(IN6_IS_ADDR_SITELOCAL(&addr));
addr.s6_addr[0] = 0xff;
addr.s6_addr[1] = 0x80;
EXPECT_FALSE(IN6_IS_ADDR_SITELOCAL(&addr));
addr.s6_addr[0] = 0xff;
addr.s6_addr[1] = 0x1;
EXPECT_TRUE(IN6_IS_ADDR_MC_NODELOCAL(&addr));
addr.s6_addr[1] = 0x2;
EXPECT_TRUE(IN6_IS_ADDR_MC_LINKLOCAL(&addr));
addr.s6_addr[1] = 0x5;
EXPECT_TRUE(IN6_IS_ADDR_MC_SITELOCAL(&addr));
addr.s6_addr[1] = 0x8;
EXPECT_TRUE(IN6_IS_ADDR_MC_ORGLOCAL(&addr));
addr.s6_addr[1] = 0xe;
EXPECT_TRUE(IN6_IS_ADDR_MC_GLOBAL(&addr));
addr.s6_addr[1] = 0;
addr.s6_addr[0] = 0;
EXPECT_FALSE(IN6_IS_ADDR_V4MAPPED(&addr));
addr.s6_addr[10] = 0xff;
addr.s6_addr[11] = 0xff;
EXPECT_TRUE(IN6_IS_ADDR_V4MAPPED(&addr));
addr.s6_addr[10] = 0;
addr.s6_addr[11] = 0;
for (int i = 12; i < 16; ++i) {
addr.s6_addr[i] ^= 42;
EXPECT_TRUE(IN6_IS_ADDR_V4COMPAT(&addr));
addr.s6_addr[i] ^= 42;
}
for (int i = 0; i < 12; ++i) {
addr.s6_addr[i] ^= 42;
EXPECT_FALSE(IN6_IS_ADDR_V4COMPAT(&addr));
addr.s6_addr[i] ^= 42;
}
}