Skip to content

Commit 98ef42e

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

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

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

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,77 @@
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"
12-
#include <sys/ioctl.h>
1314

1415
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
1516
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
1617

17-
TEST(LlvmLibcSysIoctlTest, StdinFIONREAD) {
18+
TEST(LlvmLibcSysIoctlTest, NullAndTestFileFIONREAD) {
1819
LIBC_NAMESPACE::libc_errno = 0;
1920

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

26-
TEST(LlvmLibcSysIoctlTest, InvalidCommandENOTTY) {
67+
TEST(LlvmLibcSysIoctlTest, InvalidIoctlCommand) {
2768
LIBC_NAMESPACE::libc_errno = 0;
2869

70+
int fd = LIBC_NAMESPACE::open("/dev/zero", O_RDONLY);
71+
ASSERT_GT(fd, 0);
72+
ASSERT_ERRNO_SUCCESS();
73+
2974
// 0xDEADBEEF is just a random nonexistent command;
3075
// calling this should always fail with ENOTTY
31-
int ret = LIBC_NAMESPACE::ioctl(3, 0xDEADBEEF, NULL);
76+
int ret = LIBC_NAMESPACE::ioctl(fd, 0xDEADBEEF, NULL);
3277
ASSERT_EQ(ret, -1);
3378
ASSERT_ERRNO_EQ(ENOTTY);
79+
80+
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
3481
}
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)