1 //===-- Unittests for socketpair ------------------------------------------===//
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/socketpair.h"
10
11 #include "src/unistd/close.h"
12
13 #include "src/errno/libc_errno.h"
14 #include "test/UnitTest/Test.h"
15
16 #include <sys/socket.h> // For AF_UNIX and SOCK_DGRAM
17
TEST(LlvmLibcSocketPairTest,LocalSocket)18 TEST(LlvmLibcSocketPairTest, LocalSocket) {
19 int sockpair[2] = {-1, -1};
20 int result = LIBC_NAMESPACE::socketpair(AF_UNIX, SOCK_DGRAM, 0, sockpair);
21 ASSERT_EQ(result, 0);
22 ASSERT_ERRNO_SUCCESS();
23
24 ASSERT_GE(sockpair[0], 0);
25 ASSERT_GE(sockpair[1], 0);
26
27 LIBC_NAMESPACE::close(sockpair[0]);
28 LIBC_NAMESPACE::close(sockpair[1]);
29 ASSERT_ERRNO_SUCCESS();
30 }
31
TEST(LlvmLibcSocketPairTest,SocketFails)32 TEST(LlvmLibcSocketPairTest, SocketFails) {
33 int sockpair[2] = {-1, -1};
34 int result = LIBC_NAMESPACE::socketpair(-1, -1, -1, sockpair);
35 ASSERT_EQ(result, -1);
36 ASSERT_ERRNO_FAILURE();
37 }
38