xref: /aosp_15_r20/external/llvm-libc/test/src/termios/termios_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for a bunch of functions in termios.h -------------------===//
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/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"
21 
22 #include <termios.h>
23 
24 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
25 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
26 
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.
30 
TEST(LlvmLibcTermiosTest,SpeedSmokeTest)31 TEST(LlvmLibcTermiosTest, SpeedSmokeTest) {
32   struct termios t;
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));
38 
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));
43 }
44 
TEST(LlvmLibcTermiosTest,GetAttrSmokeTest)45 TEST(LlvmLibcTermiosTest, GetAttrSmokeTest) {
46   struct termios t;
47   LIBC_NAMESPACE::libc_errno = 0;
48   int fd = LIBC_NAMESPACE::open("/dev/tty", O_RDONLY);
49   if (fd < 0)
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);
54 }
55 
TEST(LlvmLibcTermiosTest,TcGetSidSmokeTest)56 TEST(LlvmLibcTermiosTest, TcGetSidSmokeTest) {
57   LIBC_NAMESPACE::libc_errno = 0;
58   int fd = LIBC_NAMESPACE::open("/dev/tty", O_RDONLY);
59   if (fd < 0)
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);
64 }
65