blob: 480f3afb3aa9415f9498939bc3d16d3aab12d70c [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
Elliott Hughes95646e62023-09-21 14:11:19 -070037#include "utils.h"
38
Elliott Hughes5da96462017-12-14 09:43:59 -080039// TODO:
40// tcdrain
41// tcflow
42// tcflush
43// tcgetattr
44// tcgetsid
45// tcsendbreak
46// tcsetattr
47
48TEST(termios, cfgetispeed_cfsetispeed) {
49 termios t = {};
50 ASSERT_EQ(0, cfsetispeed(&t, B1200));
51 ASSERT_EQ(static_cast<speed_t>(B1200), cfgetispeed(&t));
52}
53
54TEST(termios, cfsetispeed_EINVAL) {
55 termios t = {};
56 errno = 0;
57 ASSERT_EQ(-1, cfsetispeed(&t, 1200));
Elliott Hughes95646e62023-09-21 14:11:19 -070058 ASSERT_ERRNO(EINVAL);
Elliott Hughes5da96462017-12-14 09:43:59 -080059}
60
61TEST(termios, cfgetospeed_cfsetospeed) {
62 termios t = {};
63 ASSERT_EQ(0, cfsetospeed(&t, B1200));
64 ASSERT_EQ(static_cast<speed_t>(B1200), cfgetospeed(&t));
65}
66
67TEST(termios, cfsetospeed_EINVAL) {
68 termios t = {};
69 errno = 0;
70 ASSERT_EQ(-1, cfsetospeed(&t, 1200));
Elliott Hughes95646e62023-09-21 14:11:19 -070071 ASSERT_ERRNO(EINVAL);
Elliott Hughes5da96462017-12-14 09:43:59 -080072}
73
74TEST(termios, cfsetspeed) {
75 termios t = {};
76 ASSERT_EQ(0, cfsetspeed(&t, B1200));
77 ASSERT_EQ(static_cast<speed_t>(B1200), cfgetispeed(&t));
78 ASSERT_EQ(static_cast<speed_t>(B1200), cfgetospeed(&t));
79}
80
81TEST(termios, cfsetspeed_EINVAL) {
82 termios t = {};
83 errno = 0;
84 // glibc seems to allow 1200 as well as B1200 here, presumably for
85 // BSD compatibility (where Bxxx == xxx, unlike Linux).
86 ASSERT_EQ(-1, cfsetspeed(&t, 123));
Elliott Hughes95646e62023-09-21 14:11:19 -070087 ASSERT_ERRNO(EINVAL);
Elliott Hughes5da96462017-12-14 09:43:59 -080088}
89
90TEST(termios, cfmakeraw) {
91 termios t;
92 memset(&t, 0xff, sizeof(t));
93 cfmakeraw(&t);
94
95 EXPECT_EQ(0U, (t.c_iflag & (IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON)));
96 EXPECT_EQ(0U, (t.c_oflag & OPOST));
97 EXPECT_EQ(0U, (t.c_lflag & (ECHO|ECHONL|ICANON|ISIG|IEXTEN)));
98 EXPECT_EQ(0U, (t.c_cflag & PARENB));
99 EXPECT_EQ(CS8, static_cast<int>(t.c_cflag & CSIZE));
100 EXPECT_EQ(1, t.c_cc[VMIN]);
101 EXPECT_EQ(0, t.c_cc[VTIME]);
102}
Elliott Hughes647472d2023-08-28 16:55:07 -0700103
104TEST(termios, tcgetwinsize_tcsetwinsize_invalid) {
105#if !defined(__GLIBC__)
106 winsize ws = {};
107
108 errno = 0;
109 ASSERT_EQ(-1, tcgetwinsize(-1, &ws));
Elliott Hughes95646e62023-09-21 14:11:19 -0700110 ASSERT_ERRNO(EBADF);
Elliott Hughes647472d2023-08-28 16:55:07 -0700111
112 errno = 0;
113 ASSERT_EQ(-1, tcsetwinsize(-1, &ws));
Elliott Hughes95646e62023-09-21 14:11:19 -0700114 ASSERT_ERRNO(EBADF);
Elliott Hughes647472d2023-08-28 16:55:07 -0700115#else
116 GTEST_SKIP() << "glibc too old";
117#endif
118}
119
120TEST(termios, tcgetwinsize_tcsetwinsize) {
121#if !defined(__GLIBC__)
122 int pty, tty;
123 winsize ws = {123, 456, 9999, 9999};
124 ASSERT_EQ(0, openpty(&pty, &tty, nullptr, nullptr, &ws));
125
126 winsize actual = {};
127 ASSERT_EQ(0, tcgetwinsize(tty, &actual));
128 EXPECT_EQ(ws.ws_xpixel, actual.ws_xpixel);
129 EXPECT_EQ(ws.ws_ypixel, actual.ws_ypixel);
130 EXPECT_EQ(ws.ws_row, actual.ws_row);
131 EXPECT_EQ(ws.ws_col, actual.ws_col);
132
133 ws = {1, 2, 3, 4};
134 ASSERT_EQ(0, tcsetwinsize(tty, &ws));
135
136 actual = {};
137 ASSERT_EQ(0, tcgetwinsize(tty, &actual));
138 EXPECT_EQ(ws.ws_xpixel, actual.ws_xpixel);
139 EXPECT_EQ(ws.ws_ypixel, actual.ws_ypixel);
140 EXPECT_EQ(ws.ws_row, actual.ws_row);
141 EXPECT_EQ(ws.ws_col, actual.ws_col);
142
143 close(pty);
144 close(tty);
145#else
146 GTEST_SKIP() << "glibc too old";
147#endif
148}