Home | History | Annotate | Download | only in tests
      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 
     33 #include <gtest/gtest.h>
     34 
     35 // TODO:
     36 // tcdrain
     37 // tcflow
     38 // tcflush
     39 // tcgetattr
     40 // tcgetsid
     41 // tcsendbreak
     42 // tcsetattr
     43 
     44 TEST(termios, cfgetispeed_cfsetispeed) {
     45   termios t = {};
     46   ASSERT_EQ(0, cfsetispeed(&t, B1200));
     47   ASSERT_EQ(static_cast<speed_t>(B1200), cfgetispeed(&t));
     48 }
     49 
     50 TEST(termios, cfsetispeed_EINVAL) {
     51   termios t = {};
     52   errno = 0;
     53   ASSERT_EQ(-1, cfsetispeed(&t, 1200));
     54   ASSERT_EQ(EINVAL, errno);
     55 }
     56 
     57 TEST(termios, cfgetospeed_cfsetospeed) {
     58   termios t = {};
     59   ASSERT_EQ(0, cfsetospeed(&t, B1200));
     60   ASSERT_EQ(static_cast<speed_t>(B1200), cfgetospeed(&t));
     61 }
     62 
     63 TEST(termios, cfsetospeed_EINVAL) {
     64   termios t = {};
     65   errno = 0;
     66   ASSERT_EQ(-1, cfsetospeed(&t, 1200));
     67   ASSERT_EQ(EINVAL, errno);
     68 }
     69 
     70 TEST(termios, cfsetspeed) {
     71   termios t = {};
     72   ASSERT_EQ(0, cfsetspeed(&t, B1200));
     73   ASSERT_EQ(static_cast<speed_t>(B1200), cfgetispeed(&t));
     74   ASSERT_EQ(static_cast<speed_t>(B1200), cfgetospeed(&t));
     75 }
     76 
     77 TEST(termios, cfsetspeed_EINVAL) {
     78   termios t = {};
     79   errno = 0;
     80   // glibc seems to allow 1200 as well as B1200 here, presumably for
     81   // BSD compatibility (where Bxxx == xxx, unlike Linux).
     82   ASSERT_EQ(-1, cfsetspeed(&t, 123));
     83   ASSERT_EQ(EINVAL, errno);
     84 }
     85 
     86 TEST(termios, cfmakeraw) {
     87   termios t;
     88   memset(&t, 0xff, sizeof(t));
     89   cfmakeraw(&t);
     90 
     91   EXPECT_EQ(0U, (t.c_iflag & (IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON)));
     92   EXPECT_EQ(0U, (t.c_oflag & OPOST));
     93   EXPECT_EQ(0U, (t.c_lflag & (ECHO|ECHONL|ICANON|ISIG|IEXTEN)));
     94   EXPECT_EQ(0U, (t.c_cflag & PARENB));
     95   EXPECT_EQ(CS8, static_cast<int>(t.c_cflag & CSIZE));
     96   EXPECT_EQ(1, t.c_cc[VMIN]);
     97   EXPECT_EQ(0, t.c_cc[VTIME]);
     98 }
     99