1 //===-- Unittests for a bunch of functions in termios.h -------------------===//
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/termios/cfgetispeed.h"
12 #include "src/termios/cfgetospeed.h"
13 #include "src/termios/cfsetispeed.h"
14 #include "src/termios/cfsetospeed.h"
15 #include "src/termios/tcgetattr.h"
16 #include "src/termios/tcgetsid.h"
17 #include "src/termios/tcsetattr.h"
18 #include "src/unistd/close.h"
19 #include "test/UnitTest/ErrnoSetterMatcher.h"
20 #include "test/UnitTest/Test.h"
24 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
25 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
27 // We just list a bunch of smoke tests here as it is not possible to
28 // test functionality at the least because we want to run the tests
29 // from ninja/make which change the terminal behavior.
31 TEST(LlvmLibcTermiosTest
, SpeedSmokeTest
) {
33 LIBC_NAMESPACE::libc_errno
= 0;
34 ASSERT_THAT(LIBC_NAMESPACE::cfsetispeed(&t
, B50
), Succeeds(0));
35 ASSERT_EQ(LIBC_NAMESPACE::cfgetispeed(&t
), speed_t(B50
));
36 ASSERT_THAT(LIBC_NAMESPACE::cfsetospeed(&t
, B75
), Succeeds(0));
37 ASSERT_EQ(LIBC_NAMESPACE::cfgetospeed(&t
), speed_t(B75
));
39 LIBC_NAMESPACE::libc_errno
= 0;
40 ASSERT_THAT(LIBC_NAMESPACE::cfsetispeed(&t
, ~CBAUD
), Fails(EINVAL
));
41 LIBC_NAMESPACE::libc_errno
= 0;
42 ASSERT_THAT(LIBC_NAMESPACE::cfsetospeed(&t
, ~CBAUD
), Fails(EINVAL
));
45 TEST(LlvmLibcTermiosTest
, GetAttrSmokeTest
) {
47 LIBC_NAMESPACE::libc_errno
= 0;
48 int fd
= LIBC_NAMESPACE::open("/dev/tty", O_RDONLY
);
50 return; // When /dev/tty is not available, no point continuing.
51 ASSERT_ERRNO_SUCCESS();
52 ASSERT_THAT(LIBC_NAMESPACE::tcgetattr(fd
, &t
), Succeeds(0));
53 ASSERT_EQ(LIBC_NAMESPACE::close(fd
), 0);
56 TEST(LlvmLibcTermiosTest
, TcGetSidSmokeTest
) {
57 LIBC_NAMESPACE::libc_errno
= 0;
58 int fd
= LIBC_NAMESPACE::open("/dev/tty", O_RDONLY
);
60 return; // When /dev/tty is not available, no point continuing.
61 ASSERT_ERRNO_SUCCESS();
62 ASSERT_GT(LIBC_NAMESPACE::tcgetsid(fd
), pid_t(0));
63 ASSERT_EQ(LIBC_NAMESPACE::close(fd
), 0);