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

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Wed Apr 22 05:57:42 PDT 2026


================
@@ -0,0 +1,92 @@
+//===-- 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/common.h"
+#include "src/string/strlen.h"
+#include "src/string/strncmp.h"
+#include "src/string/strncpy.h"
+#include "test/UnitTest/LibcTest.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace testing {
+
+LIBC_INLINE bool make_sockaddr_un(const char *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 (strlen(path) + 1 > sizeof(sun.sun_path))
+    return false;
+  strncpy(sun.sun_path, path, sizeof(sun.sun_path));
+  return true;
+}
+
+struct SocketAddress {
+  struct sockaddr_un addr;
+  socklen_t addrlen;
+};
+
+class SocketAddressMatcher : public Matcher<SocketAddress> {
+  const char *expected_path;
+  struct sockaddr_un actual_addr;
+  socklen_t actual_addrlen;
+
+public:
+  explicit SocketAddressMatcher(const char *path) : expected_path(path) {}
----------------
kaladron wrote:

Keep in mind that as we get more and more complicated tests, we'll need to evolve the test framework appropriately.  So if something should be changed, we should do that.  (Either before or after the commit)

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


More information about the libc-commits mailing list