11
11
#include " src/sys/ioctl/ioctl.h"
12
12
#include " src/unistd/close.h"
13
13
#include " src/unistd/read.h"
14
+ #include " src/unistd/write.h"
15
+
14
16
#include " test/UnitTest/ErrnoSetterMatcher.h"
17
+ #include " test/UnitTest/Test.h"
15
18
16
19
#include " hdr/sys_ioctl_macros.h"
17
20
18
- using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
19
21
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
20
22
21
- TEST (LlvmLibcSysIoctlTest, TestFileFIONREAD ) {
23
+ TEST (LlvmLibcSysIoctlTest, InvalidCommandAndFIONREAD ) {
22
24
LIBC_NAMESPACE::libc_errno = 0 ;
23
25
24
- constexpr const char TEST_MSG[] = " ioctl test" ;
26
+ // Setup the test file
27
+ constexpr const char *TEST_FILE_NAME = " testdata/ioctl.test" ;
28
+ constexpr const char *TEST_MSG = " ioctl test" ;
25
29
constexpr int TEST_MSG_SIZE = sizeof (TEST_MSG) - 1 ;
26
- constexpr const char *TEST_FILE = " testdata/ioctl.test" ;
30
+ auto TEST_FILE = libc_make_test_file_path (TEST_FILE_NAME);
31
+ int new_test_file_fd =
32
+ LIBC_NAMESPACE::open (TEST_FILE, O_CREAT | O_WRONLY, 0644 );
33
+ ASSERT_THAT (
34
+ (int )LIBC_NAMESPACE::write (new_test_file_fd, TEST_MSG, TEST_MSG_SIZE),
35
+ Succeeds (TEST_MSG_SIZE));
36
+ ASSERT_ERRNO_SUCCESS ();
37
+ ASSERT_THAT (LIBC_NAMESPACE::close (new_test_file_fd), Succeeds (0 ));
38
+ ASSERT_ERRNO_SUCCESS ();
39
+
40
+ // Reopen the file for testing
27
41
int fd = LIBC_NAMESPACE::open (TEST_FILE, O_RDONLY);
28
42
ASSERT_ERRNO_SUCCESS ();
29
43
ASSERT_GT (fd, 0 );
@@ -49,21 +63,11 @@ TEST(LlvmLibcSysIoctlTest, TestFileFIONREAD) {
49
63
ASSERT_GT (ret, -1 );
50
64
ASSERT_EQ (n - READ_COUNT, n_after_reading);
51
65
52
- ASSERT_THAT (LIBC_NAMESPACE::close (fd), Succeeds (0 ));
53
- }
54
-
55
- TEST (LlvmLibcSysIoctlTest, InvalidIoctlCommand) {
56
- LIBC_NAMESPACE::libc_errno = 0 ;
57
-
58
- int fd = LIBC_NAMESPACE::open (" /dev/zero" , O_RDONLY);
59
- ASSERT_GT (fd, 0 );
60
- ASSERT_ERRNO_SUCCESS ();
61
-
62
66
// 0xDEADBEEF is just a random nonexistent command;
63
67
// calling this should always fail with ENOTTY
64
- int ret = LIBC_NAMESPACE::ioctl (fd, 0xDEADBEEF , NULL );
65
- ASSERT_EQ (ret, -1 );
68
+ ret = LIBC_NAMESPACE::ioctl (fd, 0xDEADBEEF , NULL );
66
69
ASSERT_ERRNO_EQ (ENOTTY);
70
+ ASSERT_EQ (ret, -1 );
67
71
68
72
ASSERT_THAT (LIBC_NAMESPACE::close (fd), Succeeds (0 ));
69
73
}
0 commit comments