1 //===-- Unittests for isatty ----------------------------------------------===//
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/errno/libc_errno.h"
10 #include "src/fcntl/open.h"
11 #include "src/unistd/close.h"
12 #include "src/unistd/isatty.h"
13 #include "test/UnitTest/ErrnoSetterMatcher.h"
14 #include "test/UnitTest/Test.h"
16 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
17 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
19 TEST(LlvmLibcIsATTYTest
, StdInOutTests
) {
20 // If stdin is connected to a terminal, assume that all of the standard i/o
23 if (LIBC_NAMESPACE::isatty(0)) {
24 EXPECT_THAT(LIBC_NAMESPACE::isatty(0), Succeeds(1)); // stdin
25 EXPECT_THAT(LIBC_NAMESPACE::isatty(1), Succeeds(1)); // stdout
26 EXPECT_THAT(LIBC_NAMESPACE::isatty(2), Succeeds(1)); // stderr
28 EXPECT_THAT(LIBC_NAMESPACE::isatty(0), Fails(ENOTTY
, 0)); // stdin
29 EXPECT_THAT(LIBC_NAMESPACE::isatty(1), Fails(ENOTTY
, 0)); // stdout
30 EXPECT_THAT(LIBC_NAMESPACE::isatty(2), Fails(ENOTTY
, 0)); // stderr
34 TEST(LlvmLibcIsATTYTest
, BadFdTest
) {
36 EXPECT_THAT(LIBC_NAMESPACE::isatty(-1), Fails(EBADF
, 0)); // invalid fd
39 TEST(LlvmLibcIsATTYTest
, DevTTYTest
) {
40 constexpr const char *TTY_FILE
= "/dev/tty";
42 int fd
= LIBC_NAMESPACE::open(TTY_FILE
, O_RDONLY
);
44 ASSERT_EQ(libc_errno
, 0);
45 EXPECT_THAT(LIBC_NAMESPACE::isatty(fd
), Succeeds(1));
46 ASSERT_THAT(LIBC_NAMESPACE::close(fd
), Succeeds(0));
50 TEST(LlvmLibcIsATTYTest
, FileTest
) {
51 constexpr const char *TEST_FILE
= "testdata/isatty.test";
53 int fd
= LIBC_NAMESPACE::open(TEST_FILE
, O_WRONLY
| O_CREAT
, S_IRWXU
);
54 ASSERT_EQ(libc_errno
, 0);
56 EXPECT_THAT(LIBC_NAMESPACE::isatty(fd
), Fails(ENOTTY
, 0));
57 ASSERT_THAT(LIBC_NAMESPACE::close(fd
), Succeeds(0));