diff options
| author | michaelrj-google <71531609+michaelrj-google@users.noreply.github.com> | 2023-12-12 13:36:11 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-12 13:36:11 -0800 |
| commit | 8180ea8694cbc58f105f27f2044dabc3febbcf6a (patch) | |
| tree | 3f1dc906dc1e9eaa4e22a96e1ea9db3bb585a629 /libc/test/src/sys/socket/linux | |
| parent | cd9a641613eddf25d4b25eaa96b2c393d401d42c (diff) | |
[libc] Add bind function (#74014)
This patch adds the bind function to go with the socket function. It
also cleans up a lot of socket related data structures.
Diffstat (limited to 'libc/test/src/sys/socket/linux')
| -rw-r--r-- | libc/test/src/sys/socket/linux/CMakeLists.txt | 16 | ||||
| -rw-r--r-- | libc/test/src/sys/socket/linux/bind_test.cpp | 55 | ||||
| -rw-r--r-- | libc/test/src/sys/socket/linux/socket_test.cpp | 4 |
3 files changed, 73 insertions, 2 deletions
diff --git a/libc/test/src/sys/socket/linux/CMakeLists.txt b/libc/test/src/sys/socket/linux/CMakeLists.txt index 4380597e5515..666dc28c7e4e 100644 --- a/libc/test/src/sys/socket/linux/CMakeLists.txt +++ b/libc/test/src/sys/socket/linux/CMakeLists.txt @@ -12,3 +12,19 @@ add_libc_unittest( libc.src.sys.socket.socket libc.src.unistd.close ) + + +add_libc_unittest( + bind_test + SUITE + libc_sys_socket_unittests + SRCS + bind_test.cpp + DEPENDS + libc.include.sys_socket + libc.src.errno.errno + libc.src.sys.socket.socket + libc.src.sys.socket.bind + libc.src.stdio.remove + libc.src.unistd.close +) diff --git a/libc/test/src/sys/socket/linux/bind_test.cpp b/libc/test/src/sys/socket/linux/bind_test.cpp new file mode 100644 index 000000000000..5a3a1c227c9b --- /dev/null +++ b/libc/test/src/sys/socket/linux/bind_test.cpp @@ -0,0 +1,55 @@ +//===-- Unittests for bind ------------------------------------------------===// +// +// 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 "src/sys/socket/bind.h" +#include "src/sys/socket/socket.h" + +#include "src/stdio/remove.h" +#include "src/unistd/close.h" + +#include "src/errno/libc_errno.h" +#include "test/UnitTest/LibcTest.h" +#include "test/UnitTest/Test.h" + +#include <sys/socket.h> // For AF_UNIX and SOCK_DGRAM + +TEST(LlvmLibcSocketTest, BindLocalSocket) { + + const char *FILENAME = "bind_file.test"; + auto SOCK_PATH = libc_make_test_file_path(FILENAME); + + int sock = LIBC_NAMESPACE::socket(AF_UNIX, SOCK_DGRAM, 0); + ASSERT_GE(sock, 0); + ASSERT_EQ(libc_errno, 0); + + struct sockaddr_un my_addr; + + my_addr.sun_family = AF_UNIX; + unsigned int i = 0; + for (; + SOCK_PATH[i] != '\0' && (i < sizeof(sockaddr_un) - sizeof(sa_family_t)); + ++i) + my_addr.sun_path[i] = SOCK_PATH[i]; + my_addr.sun_path[i] = '\0'; + + // It's important that the path fits in the struct, if it doesn't then we + // can't try to bind to the file. + ASSERT_LT( + i, static_cast<unsigned int>(sizeof(sockaddr_un) - sizeof(sa_family_t))); + + int result = + LIBC_NAMESPACE::bind(sock, reinterpret_cast<struct sockaddr *>(&my_addr), + sizeof(struct sockaddr_un)); + + ASSERT_EQ(result, 0); + ASSERT_EQ(libc_errno, 0); + + LIBC_NAMESPACE::close(sock); + + LIBC_NAMESPACE::remove(SOCK_PATH); +} diff --git a/libc/test/src/sys/socket/linux/socket_test.cpp b/libc/test/src/sys/socket/linux/socket_test.cpp index 9037888441a3..9d5bfacde0a4 100644 --- a/libc/test/src/sys/socket/linux/socket_test.cpp +++ b/libc/test/src/sys/socket/linux/socket_test.cpp @@ -13,10 +13,10 @@ #include "src/errno/libc_errno.h" #include "test/UnitTest/Test.h" -#include <sys/socket.h> // For AF_LOCAL and SOCK_DGRAM +#include <sys/socket.h> // For AF_UNIX and SOCK_DGRAM TEST(LlvmLibcSocketTest, LocalSocket) { - int sock = LIBC_NAMESPACE::socket(AF_LOCAL, SOCK_DGRAM, 0); + int sock = LIBC_NAMESPACE::socket(AF_UNIX, SOCK_DGRAM, 0); ASSERT_GE(sock, 0); ASSERT_EQ(libc_errno, 0); |
