68 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/__support/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/ErrnoCheckingTest.h"20#include "test/UnitTest/ErrnoSetterMatcher.h"21#include "test/UnitTest/Test.h"22 23#include <termios.h>24 25using LlvmLibcTermiosTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;26using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;27 28// We just list a bunch of smoke tests here as it is not possible to29// test functionality at the least because we want to run the tests30// from ninja/make which change the terminal behavior.31 32TEST_F(LlvmLibcTermiosTest, SpeedSmokeTest) {33 struct termios t;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 ASSERT_THAT(LIBC_NAMESPACE::cfsetispeed(&t, ~CBAUD), Fails(EINVAL));40 ASSERT_THAT(LIBC_NAMESPACE::cfsetospeed(&t, ~CBAUD), Fails(EINVAL));41}42 43TEST_F(LlvmLibcTermiosTest, GetAttrSmokeTest) {44 struct termios t;45 int fd = LIBC_NAMESPACE::open("/dev/tty", O_RDONLY);46 if (fd < 0) {47 // When /dev/tty is not available, no point continuing48 libc_errno = 0;49 return;50 }51 ASSERT_ERRNO_SUCCESS();52 ASSERT_THAT(LIBC_NAMESPACE::tcgetattr(fd, &t), Succeeds(0));53 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));54}55 56TEST_F(LlvmLibcTermiosTest, TcGetSidSmokeTest) {57 int fd = LIBC_NAMESPACE::open("/dev/tty", O_RDONLY);58 if (fd < 0) {59 // When /dev/tty is not available, no point continuing60 libc_errno = 0;61 return;62 }63 ASSERT_ERRNO_SUCCESS();64 ASSERT_THAT(LIBC_NAMESPACE::tcgetsid(fd),65 returns(GT(pid_t(0))).with_errno(EQ(0)));66 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));67}68