| 1 | //===-- Interactive unittests for select ----------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "src/sys/select/select.h" |
| 10 | #include "src/unistd/read.h" |
| 11 | #include "test/UnitTest/ErrnoCheckingTest.h" |
| 12 | #include "test/UnitTest/ErrnoSetterMatcher.h" |
| 13 | #include "test/UnitTest/Test.h" |
| 14 | |
| 15 | #include <sys/select.h> |
| 16 | #include <unistd.h> |
| 17 | |
| 18 | using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; |
| 19 | using LlvmLibcSelectTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
| 20 | |
| 21 | // This test is not be run automatically as part of the libc testsuite. |
| 22 | // Instead, one has to run it manually and press a key on the keyboard |
| 23 | // to make the test succeed. |
| 24 | TEST_F(LlvmLibcSelectTest, ReadStdinAfterSelect) { |
| 25 | constexpr int STDIN_FD = 0; |
| 26 | fd_set set; |
| 27 | FD_ZERO(&set); |
| 28 | FD_SET(STDIN_FD, &set); |
| 29 | struct timeval zero { |
| 30 | .tv_sec: 0, .tv_usec: 0 |
| 31 | }; // No wait |
| 32 | struct timeval hr { |
| 33 | .tv_sec: 3600, .tv_usec: 0 |
| 34 | }; // Wait for an hour. |
| 35 | |
| 36 | // Zero timeout means we don't wait for input. So, select should return |
| 37 | // immediately. |
| 38 | ASSERT_THAT( |
| 39 | LIBC_NAMESPACE::select(STDIN_FD + 1, &set, nullptr, nullptr, &zero), |
| 40 | Succeeds(0)); |
| 41 | // The set should indicate that stdin is NOT ready for reading. |
| 42 | ASSERT_EQ(0, FD_ISSET(STDIN_FD, &set)); |
| 43 | |
| 44 | FD_SET(STDIN_FD, &set); |
| 45 | // Wait for an hour and give the user a chance to hit a key. |
| 46 | ASSERT_THAT(LIBC_NAMESPACE::select(STDIN_FD + 1, &set, nullptr, nullptr, &hr), |
| 47 | Succeeds(1)); |
| 48 | // The set should indicate that stdin is ready for reading. |
| 49 | ASSERT_EQ(1, FD_ISSET(STDIN_FD, &set)); |
| 50 | |
| 51 | // Verify that atleast one character can be read. |
| 52 | char c; |
| 53 | ASSERT_THAT(LIBC_NAMESPACE::read(STDIN_FD, &c, 1), Succeeds(ssize_t(1))); |
| 54 | } |
| 55 | |