Instrument Neutral Distributed Interface INDI 2.2.5
test_tty_eof.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3#include <gtest/gtest.h>
4
5#include "indicom.h"
6
7#include <array>
8#include <sys/socket.h>
9#include <unistd.h>
10
11namespace
12{
13class TtyEofTest : public ::testing::Test
14{
15 protected:
16 void SetUp() override
17 {
18 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, m_fds));
19 // An EOF must not consume stale buffer contents as a received reply.
20 m_buffer.fill('A');
21 m_buffer[16] = '#';
22 }
23
24 void TearDown() override
25 {
26 for (int &fd : m_fds)
27 {
28 if (fd >= 0)
29 {
30 EXPECT_EQ(0, close(fd));
31 fd = -1;
32 }
33 }
34 }
35
36 void closeWriter()
37 {
38 const int writer = m_fds[1];
39 m_fds[1] = -1;
40 ASSERT_EQ(0, close(writer));
41 }
42
43 int m_fds[2] { -1, -1 };
44 std::array<char, 64> m_buffer {};
45};
46
47TEST_F(TtyEofTest, ReadSectionEmptyEof)
48{
49 closeWriter();
50 int nbytesRead = -1;
51 EXPECT_EQ(TTY_READ_ERROR, tty_read_section(m_fds[0], m_buffer.data(), '#', 1, &nbytesRead));
52 EXPECT_EQ(0, nbytesRead);
53}
54
55TEST_F(TtyEofTest, ReadSectionPartialReplyThenEof)
56{
57 ASSERT_EQ(2, write(m_fds[1], "AB", 2));
58 closeWriter();
59 int nbytesRead = -1;
60 EXPECT_EQ(TTY_READ_ERROR, tty_read_section(m_fds[0], m_buffer.data(), '#', 1, &nbytesRead));
61 EXPECT_EQ(2, nbytesRead);
62}
63
64TEST_F(TtyEofTest, ReadSectionValidDelimitedReply)
65{
66 ASSERT_EQ(3, write(m_fds[1], "OK#", 3));
67 int nbytesRead = -1;
68 EXPECT_EQ(TTY_OK, tty_read_section(m_fds[0], m_buffer.data(), '#', 1, &nbytesRead));
69 EXPECT_EQ(3, nbytesRead);
70}
71
72TEST_F(TtyEofTest, NReadSectionEmptyEof)
73{
74 closeWriter();
75 int nbytesRead = -1;
76 EXPECT_EQ(TTY_READ_ERROR, tty_nread_section(m_fds[0], m_buffer.data(), m_buffer.size(), '#', 1, &nbytesRead));
77 EXPECT_EQ(0, nbytesRead);
78}
79
80TEST_F(TtyEofTest, NReadSectionPartialReplyThenEof)
81{
82 ASSERT_EQ(2, write(m_fds[1], "AB", 2));
83 closeWriter();
84 int nbytesRead = -1;
85 EXPECT_EQ(TTY_READ_ERROR, tty_nread_section(m_fds[0], m_buffer.data(), m_buffer.size(), '#', 1, &nbytesRead));
86 EXPECT_EQ(2, nbytesRead);
87}
88
89TEST_F(TtyEofTest, NReadSectionValidDelimitedReply)
90{
91 ASSERT_EQ(3, write(m_fds[1], "OK#", 3));
92 int nbytesRead = -1;
93 EXPECT_EQ(TTY_OK, tty_nread_section(m_fds[0], m_buffer.data(), m_buffer.size(), '#', 1, &nbytesRead));
94 EXPECT_EQ(3, nbytesRead);
95}
96}
int tty_read_section(int fd, char *buf, char stop_char, int timeout, int *nbytes_read)
read buffer from terminal with a delimiter
Definition indicom.c:653
int tty_nread_section(int fd, char *buf, int nsize, char stop_char, int timeout, int *nbytes_read)
read buffer from terminal with a delimiter
Definition indicom.c:759
Implementations for common driver routines.
@ TTY_OK
Definition indicom.h:54
@ TTY_READ_ERROR
Definition indicom.h:55
int fd
TEST_F(AlignmentPluginTest, Test_BuiltIn_ErrorRecovery)