[libc-commits] [libc] [libc] Add socket test support library (PR #193207)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Fri May 1 03:05:58 PDT 2026


================
@@ -0,0 +1,99 @@
+//===-- Helpers for socket tests --------------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TEST_SRC_SYS_SOCKET_LINUX_SOCKET_TEST_SUPPORT_H
+#define LLVM_LIBC_TEST_SRC_SYS_SOCKET_LINUX_SOCKET_TEST_SUPPORT_H
+
+#include "hdr/sys_socket_macros.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/socklen_t.h"
+#include "hdr/types/struct_sockaddr_un.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/common.h"
+#include "src/string/strncpy.h"
+#include "src/string/strnlen.h"
+#include "test/UnitTest/LibcTest.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace testing {
+
+[[nodiscard]] LIBC_INLINE bool make_sockaddr_un(cpp::string_view path,
+                                                struct sockaddr_un &sun) {
+  sun.sun_family = AF_UNIX;
+  // The kernel accepts addresses which fill the entire sun_path buffer (without
+  // the terminating '\0' character), but we don't do that as it makes matching
+  // the returned values more difficult.
+  if (path.size() + 1 > sizeof(sun.sun_path))
+    return false;
+  strncpy(sun.sun_path, path.data(), sizeof(sun.sun_path));
+  return true;
+}
+
+struct SocketAddress {
+  struct sockaddr_un addr;
+  socklen_t addrlen;
+};
+
+class SocketAddressMatcher : public Matcher<SocketAddress> {
+  cpp::string_view expected_path;
+  struct sockaddr_un actual_addr;
+  socklen_t actual_addrlen;
+
+public:
+  explicit SocketAddressMatcher(cpp::string_view path) : expected_path(path) {}
+
+  bool match(const SocketAddress &actual) {
+    actual_addr = actual.addr;
+    actual_addrlen = actual.addrlen;
+    if (actual_addr.sun_family != AF_UNIX)
+      return false;
+    if (actual_addrlen > sizeof(actual_addr))
+      return false;
+    size_t expected_path_len = expected_path.size();
+    if (expected_path_len + 1 + sizeof(actual_addr.sun_family) > actual_addrlen)
----------------
kaladron wrote:

I wonder if you should be using offsetof.  Do something like this at the top:

constexpr size_t SUN_PATH_OFFSET = offsetof(struct sockaddr_un, sun_path);

instead of sizeof(actual_addr.sun_family) (here and below) since this correctly shows what we're looking for (and also protects against padding, etc).

https://github.com/llvm/llvm-project/pull/193207


More information about the libc-commits mailing list