class TerminalTest : public ::testing::Test {
protected:
PseudoTerminal m_pty;
+ int m_fd;
+ Terminal m_term;
void SetUp() override {
- EXPECT_THAT_ERROR(m_pty.OpenFirstAvailablePrimary(O_RDWR | O_NOCTTY),
+ ASSERT_THAT_ERROR(m_pty.OpenFirstAvailablePrimary(O_RDWR | O_NOCTTY),
llvm::Succeeded());
+ ASSERT_THAT_ERROR(m_pty.OpenSecondary(O_RDWR | O_NOCTTY),
+ llvm::Succeeded());
+ m_fd = m_pty.GetSecondaryFileDescriptor();
+ ASSERT_NE(m_fd, -1);
+ m_term.SetFileDescriptor(m_fd);
}
};
TEST_F(TerminalTest, PtyIsATerminal) {
- Terminal term{m_pty.GetPrimaryFileDescriptor()};
- EXPECT_EQ(term.IsATerminal(), true);
+ EXPECT_EQ(m_term.IsATerminal(), true);
}
TEST_F(TerminalTest, PipeIsNotATerminal) {
TEST_F(TerminalTest, SetEcho) {
struct termios terminfo;
- Terminal term{m_pty.GetPrimaryFileDescriptor()};
- ASSERT_EQ(term.SetEcho(true), true);
- ASSERT_EQ(tcgetattr(m_pty.GetPrimaryFileDescriptor(), &terminfo), 0);
+ ASSERT_EQ(m_term.SetEcho(true), true);
+ ASSERT_EQ(tcgetattr(m_fd, &terminfo), 0);
EXPECT_NE(terminfo.c_lflag & ECHO, 0U);
- ASSERT_EQ(term.SetEcho(false), true);
- ASSERT_EQ(tcgetattr(m_pty.GetPrimaryFileDescriptor(), &terminfo), 0);
+ ASSERT_EQ(m_term.SetEcho(false), true);
+ ASSERT_EQ(tcgetattr(m_fd, &terminfo), 0);
EXPECT_EQ(terminfo.c_lflag & ECHO, 0U);
}
TEST_F(TerminalTest, SetCanonical) {
struct termios terminfo;
- Terminal term{m_pty.GetPrimaryFileDescriptor()};
- ASSERT_EQ(term.SetCanonical(true), true);
- ASSERT_EQ(tcgetattr(m_pty.GetPrimaryFileDescriptor(), &terminfo), 0);
+ ASSERT_EQ(m_term.SetCanonical(true), true);
+ ASSERT_EQ(tcgetattr(m_fd, &terminfo), 0);
EXPECT_NE(terminfo.c_lflag & ICANON, 0U);
- ASSERT_EQ(term.SetCanonical(false), true);
- ASSERT_EQ(tcgetattr(m_pty.GetPrimaryFileDescriptor(), &terminfo), 0);
+ ASSERT_EQ(m_term.SetCanonical(false), true);
+ ASSERT_EQ(tcgetattr(m_fd, &terminfo), 0);
EXPECT_EQ(terminfo.c_lflag & ICANON, 0U);
}
TEST_F(TerminalTest, SaveRestoreRAII) {
struct termios orig_terminfo;
struct termios terminfo;
- ASSERT_EQ(tcgetattr(m_pty.GetPrimaryFileDescriptor(), &orig_terminfo), 0);
-
- Terminal term{m_pty.GetPrimaryFileDescriptor()};
+ ASSERT_EQ(tcgetattr(m_fd, &orig_terminfo), 0);
{
- TerminalState term_state{term};
+ TerminalState term_state{m_term};
terminfo = orig_terminfo;
// make an arbitrary change
cfsetospeed(&terminfo,
cfgetospeed(&orig_terminfo) == B9600 ? B4800 : B9600);
- ASSERT_EQ(tcsetattr(m_pty.GetPrimaryFileDescriptor(), TCSANOW, &terminfo),
+ ASSERT_EQ(tcsetattr(m_fd, TCSANOW, &terminfo),
0);
}
- ASSERT_EQ(tcgetattr(m_pty.GetPrimaryFileDescriptor(), &terminfo), 0);
+ ASSERT_EQ(tcgetattr(m_fd, &terminfo), 0);
ASSERT_EQ(memcmp(&terminfo, &orig_terminfo, sizeof(terminfo)), 0);
}
struct termios orig_terminfo;
struct termios terminfo;
- ASSERT_EQ(tcgetattr(m_pty.GetPrimaryFileDescriptor(), &orig_terminfo), 0);
+ ASSERT_EQ(tcgetattr(m_fd, &orig_terminfo), 0);
- Terminal term{m_pty.GetPrimaryFileDescriptor()};
- term_state.Save(term, false);
+ term_state.Save(m_term, false);
terminfo = orig_terminfo;
// make an arbitrary change
cfsetispeed(&terminfo, cfgetispeed(&orig_terminfo) == B9600 ? B4800 : B9600);
cfsetospeed(&terminfo, cfgetospeed(&orig_terminfo) == B9600 ? B4800 : B9600);
- ASSERT_EQ(tcsetattr(m_pty.GetPrimaryFileDescriptor(), TCSANOW, &terminfo), 0);
+ ASSERT_EQ(tcsetattr(m_fd, TCSANOW, &terminfo), 0);
term_state.Restore();
- ASSERT_EQ(tcgetattr(m_pty.GetPrimaryFileDescriptor(), &terminfo), 0);
+ ASSERT_EQ(tcgetattr(m_fd, &terminfo), 0);
ASSERT_EQ(memcmp(&terminfo, &orig_terminfo, sizeof(terminfo)), 0);
}