Skip to content

Commit f9d91ae

Browse files
committed
various improvements to ioctl tests
1 parent 759e2f2 commit f9d91ae

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

libc/test/src/sys/ioctl/linux/ioctl_test.cpp

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,79 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
98
#include "src/errno/libc_errno.h"
9+
#include "src/fcntl/open.h"
1010
#include "src/sys/ioctl/ioctl.h"
11+
#include "src/unistd/close.h"
12+
#include "src/unistd/read.h"
1113
#include "test/UnitTest/ErrnoSetterMatcher.h"
14+
1215
#include <sys/ioctl.h>
1316

1417
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
1518
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
1619

17-
TEST(LlvmLibcSysIoctlTest, StdinFIONREAD) {
20+
TEST(LlvmLibcSysIoctlTest, NullAndTestFileFIONREAD) {
1821
LIBC_NAMESPACE::libc_errno = 0;
1922

20-
// FIONREAD reports the number of readable bytes for fd
21-
int bytes;
22-
int ret = LIBC_NAMESPACE::ioctl(0, FIONREAD, &bytes);
23+
// FIONREAD reports the number of available bytes to read for the passed fd
24+
int dev_zero_fd = LIBC_NAMESPACE::open("/dev/zero", O_RDONLY);
25+
ASSERT_GT(dev_zero_fd, 0);
26+
ASSERT_ERRNO_SUCCESS();
27+
28+
// For /dev/zero, this is always 0
29+
int dev_zero_n = -1;
30+
int ret = LIBC_NAMESPACE::ioctl(dev_zero_fd, FIONREAD, &dev_zero_n);
31+
ASSERT_GT(ret, -1);
32+
ASSERT_ERRNO_SUCCESS();
33+
ASSERT_EQ(dev_zero_n, 0);
34+
35+
ASSERT_THAT(LIBC_NAMESPACE::close(dev_zero_fd), Succeeds(0));
36+
37+
// Now, with a file known to have a non-zero size
38+
constexpr const char TEST_MSG[] = "ioctl test";
39+
constexpr ssize_t TEST_MSG_SIZE = sizeof(TEST_MSG) - 1;
40+
constexpr const char *TEST_FILE = "testdata/ioctl.test";
41+
int test_file_fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
42+
ASSERT_GT(test_file_fd, 0);
43+
ASSERT_ERRNO_SUCCESS();
44+
45+
// This reports the full size of the file, as we haven't read anything yet
46+
int test_file_n = -1;
47+
ret = LIBC_NAMESPACE::ioctl(test_file_fd, FIONREAD, &test_file_n);
48+
ASSERT_GT(ret, -1);
49+
ASSERT_ERRNO_SUCCESS();
50+
ASSERT_EQ(test_file_n, TEST_MSG_SIZE);
51+
52+
// But if we read some bytes...
53+
constexpr int READ_COUNT = 5;
54+
char buffer[READ_COUNT];
55+
ASSERT_THAT(LIBC_NAMESPACE::read(test_file_fd, buffer, READ_COUNT),
56+
Succeeds(READ_COUNT));
57+
58+
// ... n should have decreased by the number of bytes we've read
59+
int test_file_n_after_reading = -1;
60+
ret =
61+
LIBC_NAMESPACE::ioctl(test_file_fd, FIONREAD, &test_file_n_after_reading);
62+
ASSERT_GT(ret, -1);
2363
ASSERT_ERRNO_SUCCESS();
64+
ASSERT_EQ(test_file_n - READ_COUNT, test_file_n_after_reading);
65+
66+
ASSERT_THAT(LIBC_NAMESPACE::close(test_file_fd), Succeeds(0));
2467
}
2568

26-
TEST(LlvmLibcSysIoctlTest, InvalidCommandENOTTY) {
69+
TEST(LlvmLibcSysIoctlTest, InvalidIoctlCommand) {
2770
LIBC_NAMESPACE::libc_errno = 0;
2871

72+
int fd = LIBC_NAMESPACE::open("/dev/zero", O_RDONLY);
73+
ASSERT_GT(fd, 0);
74+
ASSERT_ERRNO_SUCCESS();
75+
2976
// 0xDEADBEEF is just a random nonexistent command;
3077
// calling this should always fail with ENOTTY
31-
int ret = LIBC_NAMESPACE::ioctl(3, 0xDEADBEEF, NULL);
78+
int ret = LIBC_NAMESPACE::ioctl(fd, 0xDEADBEEF, NULL);
3279
ASSERT_EQ(ret, -1);
3380
ASSERT_ERRNO_EQ(ENOTTY);
81+
82+
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
3483
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file(GENERATE OUTPUT ioctl.test CONTENT "ioctl test")

0 commit comments

Comments
 (0)