18180ea86Smichaelrj-google //===-- Unittests for bind ------------------------------------------------===//
28180ea86Smichaelrj-google //
38180ea86Smichaelrj-google // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48180ea86Smichaelrj-google // See https://llvm.org/LICENSE.txt for license information.
58180ea86Smichaelrj-google // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68180ea86Smichaelrj-google //
78180ea86Smichaelrj-google //===----------------------------------------------------------------------===//
88180ea86Smichaelrj-google
98180ea86Smichaelrj-google #include "src/sys/socket/bind.h"
108180ea86Smichaelrj-google #include "src/sys/socket/socket.h"
118180ea86Smichaelrj-google
128180ea86Smichaelrj-google #include "src/stdio/remove.h"
138180ea86Smichaelrj-google #include "src/unistd/close.h"
148180ea86Smichaelrj-google
158180ea86Smichaelrj-google #include "src/errno/libc_errno.h"
168180ea86Smichaelrj-google #include "test/UnitTest/Test.h"
178180ea86Smichaelrj-google
188180ea86Smichaelrj-google #include <sys/socket.h> // For AF_UNIX and SOCK_DGRAM
198180ea86Smichaelrj-google
TEST(LlvmLibcSocketTest,BindLocalSocket)208180ea86Smichaelrj-google TEST(LlvmLibcSocketTest, BindLocalSocket) {
218180ea86Smichaelrj-google
228180ea86Smichaelrj-google const char *FILENAME = "bind_file.test";
238180ea86Smichaelrj-google auto SOCK_PATH = libc_make_test_file_path(FILENAME);
248180ea86Smichaelrj-google
258180ea86Smichaelrj-google int sock = LIBC_NAMESPACE::socket(AF_UNIX, SOCK_DGRAM, 0);
268180ea86Smichaelrj-google ASSERT_GE(sock, 0);
27*73874f7aSGuillaume Chatelet ASSERT_ERRNO_SUCCESS();
288180ea86Smichaelrj-google
298180ea86Smichaelrj-google struct sockaddr_un my_addr;
308180ea86Smichaelrj-google
318180ea86Smichaelrj-google my_addr.sun_family = AF_UNIX;
328180ea86Smichaelrj-google unsigned int i = 0;
338180ea86Smichaelrj-google for (;
348180ea86Smichaelrj-google SOCK_PATH[i] != '\0' && (i < sizeof(sockaddr_un) - sizeof(sa_family_t));
358180ea86Smichaelrj-google ++i)
368180ea86Smichaelrj-google my_addr.sun_path[i] = SOCK_PATH[i];
378180ea86Smichaelrj-google my_addr.sun_path[i] = '\0';
388180ea86Smichaelrj-google
398180ea86Smichaelrj-google // It's important that the path fits in the struct, if it doesn't then we
408180ea86Smichaelrj-google // can't try to bind to the file.
418180ea86Smichaelrj-google ASSERT_LT(
428180ea86Smichaelrj-google i, static_cast<unsigned int>(sizeof(sockaddr_un) - sizeof(sa_family_t)));
438180ea86Smichaelrj-google
448180ea86Smichaelrj-google int result =
458180ea86Smichaelrj-google LIBC_NAMESPACE::bind(sock, reinterpret_cast<struct sockaddr *>(&my_addr),
468180ea86Smichaelrj-google sizeof(struct sockaddr_un));
478180ea86Smichaelrj-google
488180ea86Smichaelrj-google ASSERT_EQ(result, 0);
49*73874f7aSGuillaume Chatelet ASSERT_ERRNO_SUCCESS();
508180ea86Smichaelrj-google
518180ea86Smichaelrj-google LIBC_NAMESPACE::close(sock);
528180ea86Smichaelrj-google
538180ea86Smichaelrj-google LIBC_NAMESPACE::remove(SOCK_PATH);
548180ea86Smichaelrj-google }
55