[JITLink][LoongArch] Support R_LARCH_ALIGN relaxation (#122259)
[llvm-project.git] / libc / test / src / sys / socket / linux / bind_test.cpp
blobe70cbd578290ba479a4ea8cd52e6f634b706f11b
1 //===-- Unittests for bind ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "src/sys/socket/bind.h"
10 #include "src/sys/socket/socket.h"
12 #include "src/stdio/remove.h"
13 #include "src/unistd/close.h"
15 #include "src/errno/libc_errno.h"
16 #include "test/UnitTest/Test.h"
18 #include <sys/socket.h> // For AF_UNIX and SOCK_DGRAM
20 TEST(LlvmLibcSocketTest, BindLocalSocket) {
22 const char *FILENAME = "bind_file.test";
23 auto SOCK_PATH = libc_make_test_file_path(FILENAME);
25 int sock = LIBC_NAMESPACE::socket(AF_UNIX, SOCK_DGRAM, 0);
26 ASSERT_GE(sock, 0);
27 ASSERT_ERRNO_SUCCESS();
29 struct sockaddr_un my_addr;
31 my_addr.sun_family = AF_UNIX;
32 unsigned int i = 0;
33 for (;
34 SOCK_PATH[i] != '\0' && (i < sizeof(sockaddr_un) - sizeof(sa_family_t));
35 ++i)
36 my_addr.sun_path[i] = SOCK_PATH[i];
37 my_addr.sun_path[i] = '\0';
39 // It's important that the path fits in the struct, if it doesn't then we
40 // can't try to bind to the file.
41 ASSERT_LT(
42 i, static_cast<unsigned int>(sizeof(sockaddr_un) - sizeof(sa_family_t)));
44 int result =
45 LIBC_NAMESPACE::bind(sock, reinterpret_cast<struct sockaddr *>(&my_addr),
46 sizeof(struct sockaddr_un));
48 ASSERT_EQ(result, 0);
49 ASSERT_ERRNO_SUCCESS();
51 LIBC_NAMESPACE::close(sock);
53 LIBC_NAMESPACE::remove(SOCK_PATH);