|
| 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 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "src/sys/socket/bind.h" |
| 10 | +#include "src/sys/socket/socket.h" |
| 11 | + |
| 12 | +#include "src/stdio/remove.h" |
| 13 | +#include "src/unistd/close.h" |
| 14 | + |
| 15 | +#include "src/errno/libc_errno.h" |
| 16 | +#include "test/UnitTest/Test.h" |
| 17 | + |
| 18 | +#include <sys/socket.h> // For AF_UNIX and SOCK_DGRAM |
| 19 | + |
| 20 | +TEST(LlvmLibcSocketTest, BindLocalSocket) { |
| 21 | + |
| 22 | + const char *FILENAME = "bind_file.test"; |
| 23 | + auto SOCK_PATH = libc_make_test_file_path(FILENAME); |
| 24 | + |
| 25 | + int sock = LIBC_NAMESPACE::socket(AF_UNIX, SOCK_DGRAM, 0); |
| 26 | + ASSERT_GE(sock, 0); |
| 27 | + ASSERT_ERRNO_SUCCESS(); |
| 28 | + |
| 29 | + struct sockaddr_un my_addr; |
| 30 | + |
| 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'; |
| 38 | + |
| 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))); |
| 43 | + |
| 44 | + int result = |
| 45 | + LIBC_NAMESPACE::bind(sock, reinterpret_cast<struct sockaddr *>(&my_addr), |
| 46 | + sizeof(struct sockaddr_un)); |
| 47 | + |
| 48 | + ASSERT_EQ(result, 0); |
| 49 | + ASSERT_ERRNO_SUCCESS(); |
| 50 | + |
| 51 | + LIBC_NAMESPACE::close(sock); |
| 52 | + |
| 53 | + LIBC_NAMESPACE::remove(SOCK_PATH); |
| 54 | +} |
0 commit comments