[libc-commits] [libc] [libc] Add inet_ntop (PR #204143)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Tue Jun 23 06:46:05 PDT 2026
================
@@ -0,0 +1,255 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for inet_ntop.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/sys_socket_macros.h"
+#include "hdr/types/struct_in6_addr.h"
+#include "hdr/types/struct_in_addr.h"
+#include "src/__support/endian_internal.h"
+#include "src/arpa/inet/inet_ntop.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcInetNtopTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcInetNtopTest, InvalidFamily) {
+ char buf[64];
+ struct in_addr addr = {0};
+ ASSERT_EQ(static_cast<const char *>(nullptr),
+ LIBC_NAMESPACE::inet_ntop(AF_INET + AF_INET6 + 1, &addr, buf,
+ sizeof(buf)));
+ ASSERT_ERRNO_EQ(EAFNOSUPPORT);
+}
+
+static void *ipv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
+ static struct in_addr addr;
+ addr.s_addr = LIBC_NAMESPACE::Endian::to_big_endian(
+ static_cast<uint32_t>(a) << 24 | static_cast<uint32_t>(b) << 16 |
+ static_cast<uint32_t>(c) << 8 | static_cast<uint32_t>(d));
+ return &addr;
+}
+
+static void *ipv6(uint16_t a, uint16_t b, uint16_t c, uint16_t d, uint16_t e,
+ uint16_t f, uint16_t g, uint16_t h) {
+ static struct in6_addr addr;
+ addr.s6_addr16[0] = LIBC_NAMESPACE::Endian::to_big_endian(a);
+ addr.s6_addr16[1] = LIBC_NAMESPACE::Endian::to_big_endian(b);
+ addr.s6_addr16[2] = LIBC_NAMESPACE::Endian::to_big_endian(c);
+ addr.s6_addr16[3] = LIBC_NAMESPACE::Endian::to_big_endian(d);
+ addr.s6_addr16[4] = LIBC_NAMESPACE::Endian::to_big_endian(e);
+ addr.s6_addr16[5] = LIBC_NAMESPACE::Endian::to_big_endian(f);
+ addr.s6_addr16[6] = LIBC_NAMESPACE::Endian::to_big_endian(g);
+ addr.s6_addr16[7] = LIBC_NAMESPACE::Endian::to_big_endian(h);
+ return &addr;
+}
+
+TEST_F(LlvmLibcInetNtopTest, IPv4Tests) {
----------------
labath wrote:
Done.
https://github.com/llvm/llvm-project/pull/204143
More information about the libc-commits
mailing list