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 "test/UnitTest/ErrnoCheckingTest.h"
16#include "test/UnitTest/ErrnoSetterMatcher.h"
17#include "test/UnitTest/Test.h"
18
19#include <sys/socket.h> // For AF_UNIX and SOCK_DGRAM
20
21using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
22using LlvmLibcBindTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
23
24TEST_F(LlvmLibcBindTest, BindLocalSocket) {
25
26 const char *FILENAME = "bind_file.test";
27 auto SOCK_PATH = libc_make_test_file_path(FILENAME);
28
29 int sock = LIBC_NAMESPACE::socket(AF_UNIX, SOCK_DGRAM, 0);
30 ASSERT_GE(sock, 0);
31 ASSERT_ERRNO_SUCCESS();
32
33 struct sockaddr_un my_addr;
34
35 my_addr.sun_family = AF_UNIX;
36 unsigned int i = 0;
37 for (;
38 SOCK_PATH[i] != '\0' && (i < sizeof(sockaddr_un) - sizeof(sa_family_t));
39 ++i)
40 my_addr.sun_path[i] = SOCK_PATH[i];
41 my_addr.sun_path[i] = '\0';
42
43 // It's important that the path fits in the struct, if it doesn't then we
44 // can't try to bind to the file.
45 ASSERT_LT(
46 i, static_cast<unsigned int>(sizeof(sockaddr_un) - sizeof(sa_family_t)));
47
48 ASSERT_THAT(
49 LIBC_NAMESPACE::bind(sock, reinterpret_cast<struct sockaddr *>(&my_addr),
50 sizeof(struct sockaddr_un)),
51 Succeeds(0));
52 ASSERT_THAT(LIBC_NAMESPACE::close(sock), Succeeds(0));
53 ASSERT_THAT(LIBC_NAMESPACE::remove(SOCK_PATH), Succeeds(0));
54}
55

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of libc/test/src/sys/socket/linux/bind_test.cpp