blob: a8d589019b4a2d40a3481aaffed9d1b5caf76cb1 [file] [log] [blame]
Elliott Hughes5da96462017-12-14 09:43:59 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <termios.h>
30
31#include <errno.h>
Elliott Hughes647472d2023-08-28 16:55:07 -070032#include <fcntl.h>
33#include <pty.h>
Elliott Hughes5da96462017-12-14 09:43:59 -080034
35#include <gtest/gtest.h>
36
37// TODO:
38// tcdrain
39// tcflow
40// tcflush
41// tcgetattr
42// tcgetsid
43// tcsendbreak
44// tcsetattr
45
46TEST(termios, cfgetispeed_cfsetispeed) {
47 termios t = {};
48 ASSERT_EQ(0, cfsetispeed(&t, B1200));
49 ASSERT_EQ(static_cast<speed_t>(B1200), cfgetispeed(&t));
50}
51
52TEST(termios, cfsetispeed_EINVAL) {
53 termios t = {};
54 errno = 0;
55 ASSERT_EQ(-1, cfsetispeed(&t, 1200));
56 ASSERT_EQ(EINVAL, errno);
57}
58
59TEST(termios, cfgetospeed_cfsetospeed) {
60 termios t = {};
61 ASSERT_EQ(0, cfsetospeed(&t, B1200));
62 ASSERT_EQ(static_cast<speed_t>(B1200), cfgetospeed(&t));
63}
64
65TEST(termios, cfsetospeed_EINVAL) {
66 termios t = {};
67 errno = 0;
68 ASSERT_EQ(-1, cfsetospeed(&t, 1200));
69 ASSERT_EQ(EINVAL, errno);
70}
71
72TEST(termios, cfsetspeed) {
73 termios t = {};
74 ASSERT_EQ(0, cfsetspeed(&t, B1200));
75 ASSERT_EQ(static_cast<speed_t>(B1200), cfgetispeed(&t));
76 ASSERT_EQ(static_cast<speed_t>(B1200), cfgetospeed(&t));
77}
78
79TEST(termios, cfsetspeed_EINVAL) {
80 termios t = {};
81 errno = 0;
82 // glibc seems to allow 1200 as well as B1200 here, presumably for
83 // BSD compatibility (where Bxxx == xxx, unlike Linux).
84 ASSERT_EQ(-1, cfsetspeed(&t, 123));
85 ASSERT_EQ(EINVAL, errno);
86}
87
88TEST(termios, cfmakeraw) {
89 termios t;
90 memset(&t, 0xff, sizeof(t));
91 cfmakeraw(&t);
92
93 EXPECT_EQ(0U, (t.c_iflag & (IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON)));
94 EXPECT_EQ(0U, (t.c_oflag & OPOST));
95 EXPECT_EQ(0U, (t.c_lflag & (ECHO|ECHONL|ICANON|ISIG|IEXTEN)));
96 EXPECT_EQ(0U, (t.c_cflag & PARENB));
97 EXPECT_EQ(CS8, static_cast<int>(t.c_cflag & CSIZE));
98 EXPECT_EQ(1, t.c_cc[VMIN]);
99 EXPECT_EQ(0, t.c_cc[VTIME]);
100}
Elliott Hughes647472d2023-08-28 16:55:07 -0700101
102TEST(termios, tcgetwinsize_tcsetwinsize_invalid) {
103#if !defined(__GLIBC__)
104 winsize ws = {};
105
106 errno = 0;
107 ASSERT_EQ(-1, tcgetwinsize(-1, &ws));
108 ASSERT_EQ(EBADF, errno);
109
110 errno = 0;
111 ASSERT_EQ(-1, tcsetwinsize(-1, &ws));
112 ASSERT_EQ(EBADF, errno);
113#else
114 GTEST_SKIP() << "glibc too old";
115#endif
116}
117
118TEST(termios, tcgetwinsize_tcsetwinsize) {
119#if !defined(__GLIBC__)
120 int pty, tty;
121 winsize ws = {123, 456, 9999, 9999};
122 ASSERT_EQ(0, openpty(&pty, &tty, nullptr, nullptr, &ws));
123
124 winsize actual = {};
125 ASSERT_EQ(0, tcgetwinsize(tty, &actual));
126 EXPECT_EQ(ws.ws_xpixel, actual.ws_xpixel);
127 EXPECT_EQ(ws.ws_ypixel, actual.ws_ypixel);
128 EXPECT_EQ(ws.ws_row, actual.ws_row);
129 EXPECT_EQ(ws.ws_col, actual.ws_col);
130
131 ws = {1, 2, 3, 4};
132 ASSERT_EQ(0, tcsetwinsize(tty, &ws));
133
134 actual = {};
135 ASSERT_EQ(0, tcgetwinsize(tty, &actual));
136 EXPECT_EQ(ws.ws_xpixel, actual.ws_xpixel);
137 EXPECT_EQ(ws.ws_ypixel, actual.ws_ypixel);
138 EXPECT_EQ(ws.ws_row, actual.ws_row);
139 EXPECT_EQ(ws.ws_col, actual.ws_col);
140
141 close(pty);
142 close(tty);
143#else
144 GTEST_SKIP() << "glibc too old";
145#endif
146}