1 /*
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>
32 #include <fcntl.h>
33 #include <pty.h>
34 
35 #include <gtest/gtest.h>
36 
37 #include "utils.h"
38 
39 // TODO:
40 // tcdrain
41 // tcflow
42 // tcflush
43 // tcgetattr
44 // tcgetsid
45 // tcsendbreak
46 // tcsetattr
47 
TEST(termios,cfgetispeed_cfsetispeed)48 TEST(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 
TEST(termios,cfsetispeed_EINVAL)54 TEST(termios, cfsetispeed_EINVAL) {
55   termios t = {};
56   errno = 0;
57   ASSERT_EQ(-1, cfsetispeed(&t, 1200));
58   ASSERT_ERRNO(EINVAL);
59 }
60 
TEST(termios,cfgetospeed_cfsetospeed)61 TEST(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 
TEST(termios,cfsetospeed_EINVAL)67 TEST(termios, cfsetospeed_EINVAL) {
68   termios t = {};
69   errno = 0;
70   ASSERT_EQ(-1, cfsetospeed(&t, 1200));
71   ASSERT_ERRNO(EINVAL);
72 }
73 
TEST(termios,cfsetspeed)74 TEST(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 
TEST(termios,cfsetspeed_EINVAL)81 TEST(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));
87   ASSERT_ERRNO(EINVAL);
88 }
89 
TEST(termios,cfmakeraw)90 TEST(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(static_cast<unsigned>(CS8), (t.c_cflag & CSIZE));
100   EXPECT_EQ(1, t.c_cc[VMIN]);
101   EXPECT_EQ(0, t.c_cc[VTIME]);
102 }
103 
TEST(termios,tcgetwinsize_tcsetwinsize_invalid)104 TEST(termios, tcgetwinsize_tcsetwinsize_invalid) {
105 #if !defined(__GLIBC__)
106   winsize ws = {};
107 
108   errno = 0;
109   ASSERT_EQ(-1, tcgetwinsize(-1, &ws));
110   ASSERT_ERRNO(EBADF);
111 
112   errno = 0;
113   ASSERT_EQ(-1, tcsetwinsize(-1, &ws));
114   ASSERT_ERRNO(EBADF);
115 #else
116   GTEST_SKIP() << "glibc too old";
117 #endif
118 }
119 
TEST(termios,tcgetwinsize_tcsetwinsize)120 TEST(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 }
149