1 //===-- Unittests for bind ------------------------------------------------===//
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
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);
27 ASSERT_ERRNO_SUCCESS();
29 struct sockaddr_un my_addr
;
31 my_addr
.sun_family
= AF_UNIX
;
34 SOCK_PATH
[i
] != '\0' && (i
< sizeof(sockaddr_un
) - sizeof(sa_family_t
));
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.
42 i
, static_cast<unsigned int>(sizeof(sockaddr_un
) - sizeof(sa_family_t
)));
45 LIBC_NAMESPACE::bind(sock
, reinterpret_cast<struct sockaddr
*>(&my_addr
),
46 sizeof(struct sockaddr_un
));
49 ASSERT_ERRNO_SUCCESS();
51 LIBC_NAMESPACE::close(sock
);
53 LIBC_NAMESPACE::remove(SOCK_PATH
);