13 changes: 6 additions & 7 deletions libc/test/src/unistd/dup_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/dup.h"
Expand All @@ -16,17 +17,15 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>

TEST(LlvmLibcdupTest, ReadAndWriteViaDup) {
errno = 0;
libc_errno = 0;
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE = "testdata/dup.test";
int fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
int dupfd = __llvm_libc::dup(fd);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(dupfd, 0);

// Write something via the dup
Expand All @@ -38,10 +37,10 @@ TEST(LlvmLibcdupTest, ReadAndWriteViaDup) {

// Reopen the file for reading and create a dup.
fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
dupfd = __llvm_libc::dup(fd);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(dupfd, 0);

// Read the file content via the dup.
Expand Down
16 changes: 8 additions & 8 deletions libc/test/src/unistd/fchdir_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/fchdir.h"
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>
#include <fcntl.h>

TEST(LlvmLibcChdirTest, ChangeAndOpen) {
Expand All @@ -25,28 +25,28 @@ TEST(LlvmLibcChdirTest, ChangeAndOpen) {
constexpr const char *TEST_DIR = "testdata";
constexpr const char *TEST_FILE = "testdata/fchdir.test";
constexpr const char *TEST_FILE_BASE = "fchdir.test";
errno = 0;
libc_errno = 0;

int dir_fd = __llvm_libc::open(TEST_DIR, O_DIRECTORY);
ASSERT_GT(dir_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
int file_fd = __llvm_libc::open(TEST_FILE, O_PATH);
ASSERT_GT(file_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_THAT(__llvm_libc::close(file_fd), Succeeds(0));

ASSERT_THAT(__llvm_libc::fchdir(dir_fd), Succeeds(0));
file_fd = __llvm_libc::open(TEST_FILE_BASE, O_PATH);
ASSERT_GT(file_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_THAT(__llvm_libc::close(file_fd), Succeeds(0));
ASSERT_THAT(__llvm_libc::close(dir_fd), Succeeds(0));
}

TEST(LlvmLibcChdirTest, ChangeToNonExistentDir) {
using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
errno = 0;
libc_errno = 0;
ASSERT_EQ(__llvm_libc::fchdir(0), -1);
ASSERT_NE(errno, 0);
errno = 0;
ASSERT_NE(libc_errno, 0);
libc_errno = 0;
}
13 changes: 6 additions & 7 deletions libc/test/src/unistd/ftruncate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/string_view.h"
#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/ftruncate.h"
Expand All @@ -16,8 +17,6 @@
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <errno.h>

namespace cpp = __llvm_libc::cpp;

TEST(LlvmLibcFtruncateTest, CreateAndTruncate) {
Expand All @@ -32,16 +31,16 @@ TEST(LlvmLibcFtruncateTest, CreateAndTruncate) {
// 2. Read it to make sure what was written is actually in the file.
// 3. Truncate to 1 byte.
// 4. Try to read more than 1 byte and fail.
errno = 0;
libc_errno = 0;
int fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
ASSERT_EQ(ssize_t(WRITE_SIZE),
__llvm_libc::write(fd, WRITE_DATA, WRITE_SIZE));
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));

fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
ASSERT_EQ(ssize_t(WRITE_SIZE), __llvm_libc::read(fd, buf, WRITE_SIZE));
ASSERT_EQ(cpp::string_view(buf), cpp::string_view(WRITE_DATA));
Expand All @@ -51,12 +50,12 @@ TEST(LlvmLibcFtruncateTest, CreateAndTruncate) {
// writing.
fd = __llvm_libc::open(TEST_FILE, O_WRONLY);
ASSERT_GT(fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_THAT(__llvm_libc::ftruncate(fd, off_t(1)), Succeeds(0));
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));

fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
ASSERT_EQ(ssize_t(1), __llvm_libc::read(fd, buf, WRITE_SIZE));
ASSERT_EQ(buf[0], WRITE_DATA[0]);
Expand Down
15 changes: 7 additions & 8 deletions libc/test/src/unistd/isatty_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/isatty.h"
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <errno.h>

using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;

TEST(LlvmLibcIsATTYTest, StdInOutTests) {
// If stdin is connected to a terminal, assume that all of the standard i/o
// fds are.
errno = 0;
libc_errno = 0;
if (__llvm_libc::isatty(0)) {
EXPECT_THAT(__llvm_libc::isatty(0), Succeeds(1)); // stdin
EXPECT_THAT(__llvm_libc::isatty(1), Succeeds(1)); // stdout
Expand All @@ -33,26 +32,26 @@ TEST(LlvmLibcIsATTYTest, StdInOutTests) {
}

TEST(LlvmLibcIsATTYTest, BadFdTest) {
errno = 0;
libc_errno = 0;
EXPECT_THAT(__llvm_libc::isatty(-1), Fails(EBADF, 0)); // invalid fd
}

TEST(LlvmLibcIsATTYTest, DevTTYTest) {
constexpr const char *TTY_FILE = "/dev/tty";
errno = 0;
libc_errno = 0;
int fd = __llvm_libc::open(TTY_FILE, O_RDONLY);
if (fd > 0) {
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
EXPECT_THAT(__llvm_libc::isatty(fd), Succeeds(1));
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
}
}

TEST(LlvmLibcIsATTYTest, FileTest) {
constexpr const char *TEST_FILE = "testdata/isatty.test";
errno = 0;
libc_errno = 0;
int fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
EXPECT_THAT(__llvm_libc::isatty(fd), Fails(ENOTTY, 0));
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
Expand Down
9 changes: 4 additions & 5 deletions libc/test/src/unistd/link_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/link.h"
#include "src/unistd/unlink.h"
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <errno.h>

TEST(LlvmLibcLinkTest, CreateAndUnlink) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE = "testdata/link.test";
Expand All @@ -25,16 +24,16 @@ TEST(LlvmLibcLinkTest, CreateAndUnlink) {
// 2. Create a link to that file.
// 3. Open the link to check that the link was created.
// 4. Cleanup the file and its link.
errno = 0;
libc_errno = 0;
int write_fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(write_fd, 0);
ASSERT_THAT(__llvm_libc::close(write_fd), Succeeds(0));
ASSERT_THAT(__llvm_libc::link(TEST_FILE, TEST_FILE_LINK), Succeeds(0));

int link_fd = __llvm_libc::open(TEST_FILE_LINK, O_PATH);
ASSERT_GT(link_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_THAT(__llvm_libc::close(link_fd), Succeeds(0));

ASSERT_THAT(__llvm_libc::unlink(TEST_FILE), Succeeds(0));
Expand Down
9 changes: 4 additions & 5 deletions libc/test/src/unistd/linkat_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/linkat.h"
#include "src/unistd/unlink.h"
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <errno.h>

TEST(LlvmLibcLinkatTest, CreateAndUnlink) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_DIR = "testdata";
Expand All @@ -28,9 +27,9 @@ TEST(LlvmLibcLinkatTest, CreateAndUnlink) {
// 2. Create a link to that file.
// 3. Open the link to check that the link was created.
// 4. Cleanup the file and its link.
errno = 0;
libc_errno = 0;
int write_fd = __llvm_libc::open(TEST_FILE_PATH, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(write_fd, 0);
ASSERT_THAT(__llvm_libc::close(write_fd), Succeeds(0));

Expand All @@ -40,7 +39,7 @@ TEST(LlvmLibcLinkatTest, CreateAndUnlink) {

int link_fd = __llvm_libc::open(TEST_FILE_LINK_PATH, O_PATH);
ASSERT_GT(link_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_THAT(__llvm_libc::close(link_fd), Succeeds(0));

ASSERT_THAT(__llvm_libc::unlink(TEST_FILE_PATH), Succeeds(0));
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/unistd/lseek_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/lseek.h"
Expand All @@ -14,14 +15,13 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>
#include <unistd.h>

TEST(LlvmLibcUniStd, LseekTest) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE = "testdata/lseek.test";
int fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
constexpr const char LSEEK_TEST[] = "lseek test";
constexpr int LSEEK_TEST_SIZE = sizeof(LSEEK_TEST) - 1;
Expand Down Expand Up @@ -54,7 +54,7 @@ TEST(LlvmLibcUniStd, LseekFailsTest) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE = "testdata/lseek.test";
int fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
EXPECT_THAT(__llvm_libc::lseek(fd, -1, SEEK_CUR), Fails(EINVAL));
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
Expand Down
9 changes: 4 additions & 5 deletions libc/test/src/unistd/pread_pwrite_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/fsync.h"
Expand All @@ -17,8 +18,6 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>

TEST(LlvmLibcUniStd, PWriteAndPReadBackTest) {
// The strategy here is that we first create a file and write to it. Next,
// we open that file again and write at an offset. Finally, we open the
Expand All @@ -34,22 +33,22 @@ TEST(LlvmLibcUniStd, PWriteAndPReadBackTest) {

constexpr const char *TEST_FILE = "testdata/pread_pwrite.test";
int fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
ASSERT_THAT(__llvm_libc::write(fd, HELLO, HELLO_SIZE), Succeeds(HELLO_SIZE));
ASSERT_THAT(__llvm_libc::fsync(fd), Succeeds(0));
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));

fd = __llvm_libc::open(TEST_FILE, O_WRONLY);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
ASSERT_THAT(__llvm_libc::pwrite(fd, HELLO, HELLO_SIZE, OFFSET),
Succeeds(HELLO_SIZE));
ASSERT_THAT(__llvm_libc::fsync(fd), Succeeds(0));
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));

fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
char read_buf[OFFSET_TEXT_SIZE];
ASSERT_THAT(__llvm_libc::pread(fd, read_buf, HELLO_SIZE, OFFSET),
Expand Down
7 changes: 3 additions & 4 deletions libc/test/src/unistd/read_write_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/fsync.h"
Expand All @@ -15,13 +16,11 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>

TEST(LlvmLibcUniStd, WriteAndReadBackTest) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE = "__unistd_read_write.test";
int write_fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(write_fd, 0);
constexpr const char HELLO[] = "hello";
constexpr int HELLO_SIZE = sizeof(HELLO);
Expand All @@ -31,7 +30,7 @@ TEST(LlvmLibcUniStd, WriteAndReadBackTest) {
ASSERT_THAT(__llvm_libc::close(write_fd), Succeeds(0));

int read_fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(read_fd, 0);
char read_buf[10];
ASSERT_THAT(__llvm_libc::read(read_fd, read_buf, HELLO_SIZE),
Expand Down
7 changes: 3 additions & 4 deletions libc/test/src/unistd/readlink_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/string_view.h"
#include "src/errno/libc_errno.h"
#include "src/unistd/readlink.h"
#include "src/unistd/symlink.h"
#include "src/unistd/unlink.h"
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <errno.h>

namespace cpp = __llvm_libc::cpp;

TEST(LlvmLibcReadlinkTest, CreateAndUnlink) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char LINK_VAL[] = "readlink_test_value";
constexpr const char LINK[] = "testdata/readlink.test.link";
errno = 0;
libc_errno = 0;

// The test strategy is as follows:
// 1. Create a symlink with value LINK_VAL.
Expand All @@ -31,7 +30,7 @@ TEST(LlvmLibcReadlinkTest, CreateAndUnlink) {

char buf[sizeof(LINK_VAL)];
ssize_t len = __llvm_libc::readlink(LINK, buf, sizeof(buf));
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_EQ(cpp::string_view(buf, len), cpp::string_view(LINK_VAL));

ASSERT_THAT(__llvm_libc::unlink(LINK), Succeeds(0));
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/unistd/readlinkat_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/string_view.h"
#include "src/errno/libc_errno.h"
#include "src/unistd/readlinkat.h"
#include "src/unistd/symlink.h"
#include "src/unistd/unlink.h"
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <errno.h>
#include <fcntl.h>

namespace cpp = __llvm_libc::cpp;
Expand All @@ -22,7 +22,7 @@ TEST(LlvmLibcReadlinkatTest, CreateAndUnlink) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char LINK_VAL[] = "readlinkat_test_value";
constexpr const char LINK[] = "testdata/readlinkat.test.link";
errno = 0;
libc_errno = 0;

// The test strategy is as follows:
// 1. Create a symlink with value LINK_VAL.
Expand All @@ -32,7 +32,7 @@ TEST(LlvmLibcReadlinkatTest, CreateAndUnlink) {

char buf[sizeof(LINK_VAL)];
ssize_t len = __llvm_libc::readlinkat(AT_FDCWD, LINK, buf, sizeof(buf));
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_EQ(cpp::string_view(buf, len), cpp::string_view(LINK_VAL));

ASSERT_THAT(__llvm_libc::unlink(LINK), Succeeds(0));
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/unistd/rmdir_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/sys/stat/mkdir.h"
#include "src/unistd/rmdir.h"
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>
#include <fcntl.h>

TEST(LlvmLibcRmdirTest, CreateAndRemove) {
Expand Down
9 changes: 4 additions & 5 deletions libc/test/src/unistd/symlink_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/symlink.h"
#include "src/unistd/unlink.h"
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <errno.h>

TEST(LlvmLibcSymlinkTest, CreateAndUnlink) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE_BASE = "symlink.test";
Expand All @@ -26,9 +25,9 @@ TEST(LlvmLibcSymlinkTest, CreateAndUnlink) {
// 2. Create a symlink to that file.
// 3. Open the symlink to check that the symlink was created.
// 4. Cleanup the file and its symlink.
errno = 0;
libc_errno = 0;
int write_fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(write_fd, 0);
ASSERT_THAT(__llvm_libc::close(write_fd), Succeeds(0));

Expand All @@ -37,7 +36,7 @@ TEST(LlvmLibcSymlinkTest, CreateAndUnlink) {

int symlink_fd = __llvm_libc::open(TEST_FILE_LINK, O_PATH);
ASSERT_GT(symlink_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_THAT(__llvm_libc::close(symlink_fd), Succeeds(0));

ASSERT_THAT(__llvm_libc::unlink(TEST_FILE), Succeeds(0));
Expand Down
9 changes: 4 additions & 5 deletions libc/test/src/unistd/symlinkat_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/symlinkat.h"
#include "src/unistd/unlink.h"
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <errno.h>

TEST(LlvmLibcSymlinkatTest, CreateAndUnlink) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_DIR = "testdata";
Expand All @@ -28,9 +27,9 @@ TEST(LlvmLibcSymlinkatTest, CreateAndUnlink) {
// 2. Create a link to that file.
// 3. Open the link to check that the link was created.
// 4. Cleanup the file and its link.
errno = 0;
libc_errno = 0;
int write_fd = __llvm_libc::open(TEST_FILE_PATH, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(write_fd, 0);
ASSERT_THAT(__llvm_libc::close(write_fd), Succeeds(0));

Expand All @@ -40,7 +39,7 @@ TEST(LlvmLibcSymlinkatTest, CreateAndUnlink) {

int link_fd = __llvm_libc::open(TEST_FILE_LINK_PATH, O_PATH);
ASSERT_GT(link_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_THAT(__llvm_libc::close(link_fd), Succeeds(0));

ASSERT_THAT(__llvm_libc::close(dir_fd), Succeeds(0));
Expand Down
36 changes: 18 additions & 18 deletions libc/test/src/unistd/syscall_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/unistd/syscall.h"
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <errno.h>
#include <fcntl.h>
#include <sys/syscall.h> // For syscall numbers.
#include <unistd.h>
Expand All @@ -26,26 +26,26 @@ using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
// because the macro generates a call to the actual internal function
// (__llvm_libc_syscall) which is inside the namespace.
TEST(LlvmLibcSyscallTest, TrivialCall) {
errno = 0;
libc_errno = 0;

ASSERT_GE(__llvm_libc::syscall(SYS_gettid), 0l);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
}

TEST(LlvmLibcSyscallTest, SymlinkCreateDestroy) {
constexpr const char LINK_VAL[] = "syscall_readlink_test_value";
constexpr const char LINK[] = "testdata/syscall_readlink.test.link";

ASSERT_GE(__llvm_libc::syscall(SYS_symlink, LINK_VAL, LINK), 0l);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

char buf[sizeof(LINK_VAL)];

ASSERT_GE(__llvm_libc::syscall(SYS_readlink, LINK, buf, sizeof(buf)), 0l);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

ASSERT_GE(__llvm_libc::syscall(SYS_unlink, LINK), 0l);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
}

TEST(LlvmLibcSyscallTest, FileReadWrite) {
Expand All @@ -57,16 +57,16 @@ TEST(LlvmLibcSyscallTest, FileReadWrite) {
int fd =
__llvm_libc::syscall(SYS_open, TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_GT(fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

ASSERT_GE(__llvm_libc::syscall(SYS_pwrite64, fd, HELLO, HELLO_SIZE, 0), 0l);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

ASSERT_GE(__llvm_libc::syscall(SYS_fsync, fd), 0l);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

ASSERT_GE(__llvm_libc::syscall(SYS_close, fd), 0l);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
}

TEST(LlvmLibcSyscallTest, FileLinkCreateDestroy) {
Expand All @@ -86,30 +86,30 @@ TEST(LlvmLibcSyscallTest, FileLinkCreateDestroy) {
int write_fd = __llvm_libc::syscall(SYS_open, TEST_FILE_PATH,
O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_GT(write_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

ASSERT_GE(__llvm_libc::syscall(SYS_close, write_fd), 0l);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

int dir_fd = __llvm_libc::syscall(SYS_open, TEST_DIR, O_DIRECTORY, 0);
ASSERT_GT(dir_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

ASSERT_GE(__llvm_libc::syscall(SYS_linkat, dir_fd, TEST_FILE, dir_fd,
TEST_FILE_LINK, 0),
0l);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

int link_fd = __llvm_libc::syscall(SYS_open, TEST_FILE_LINK_PATH, O_PATH, 0);
ASSERT_GT(link_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

ASSERT_GE(__llvm_libc::syscall(SYS_unlink, TEST_FILE_PATH), 0l);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

ASSERT_GE(__llvm_libc::syscall(SYS_unlink, TEST_FILE_LINK_PATH), 0l);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

ASSERT_GE(__llvm_libc::syscall(SYS_close, dir_fd), 0l);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
}
11 changes: 5 additions & 6 deletions libc/test/src/unistd/truncate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/string_view.h"
#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/read.h"
Expand All @@ -16,8 +17,6 @@
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <errno.h>

namespace cpp = __llvm_libc::cpp;

TEST(LlvmLibcTruncateTest, CreateAndTruncate) {
Expand All @@ -32,16 +31,16 @@ TEST(LlvmLibcTruncateTest, CreateAndTruncate) {
// 2. Read it to make sure what was written is actually in the file.
// 3. Truncate to 1 byte.
// 4. Try to read more than 1 byte and fail.
errno = 0;
libc_errno = 0;
int fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
ASSERT_EQ(ssize_t(WRITE_SIZE),
__llvm_libc::write(fd, WRITE_DATA, WRITE_SIZE));
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));

fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
ASSERT_EQ(ssize_t(WRITE_SIZE), __llvm_libc::read(fd, buf, WRITE_SIZE));
ASSERT_EQ(cpp::string_view(buf), cpp::string_view(WRITE_DATA));
Expand All @@ -50,7 +49,7 @@ TEST(LlvmLibcTruncateTest, CreateAndTruncate) {
ASSERT_THAT(__llvm_libc::truncate(TEST_FILE, off_t(1)), Succeeds(0));

fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
ASSERT_EQ(ssize_t(1), __llvm_libc::read(fd, buf, WRITE_SIZE));
ASSERT_EQ(buf[0], WRITE_DATA[0]);
Expand Down
5 changes: 2 additions & 3 deletions libc/test/src/unistd/unlink_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "src/unistd/unlink.h"
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>

TEST(LlvmLibcUnlinkTest, CreateAndUnlink) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE = "testdata/unlink.test";
int write_fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(write_fd, 0);
ASSERT_THAT(__llvm_libc::close(write_fd), Succeeds(0));
ASSERT_THAT(__llvm_libc::unlink(TEST_FILE), Succeeds(0));
Expand Down
9 changes: 4 additions & 5 deletions libc/test/src/unistd/unlinkat_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/fcntl/openat.h"
#include "src/unistd/close.h"
Expand All @@ -14,18 +15,16 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>

TEST(LlvmLibcUnlinkatTest, CreateAndDeleteTest) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_DIR = "testdata";
constexpr const char *TEST_FILE = "openat.test";
int dir_fd = __llvm_libc::open(TEST_DIR, O_DIRECTORY);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(dir_fd, 0);
int write_fd =
__llvm_libc::openat(dir_fd, TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(write_fd, 0);
ASSERT_THAT(__llvm_libc::close(write_fd), Succeeds(0));
ASSERT_THAT(__llvm_libc::unlinkat(dir_fd, TEST_FILE, 0), Succeeds(0));
Expand All @@ -35,7 +34,7 @@ TEST(LlvmLibcUnlinkatTest, CreateAndDeleteTest) {
TEST(LlvmLibcUnlinkatTest, UnlinkatNonExistentFile) {
constexpr const char *TEST_DIR = "testdata";
int dir_fd = __llvm_libc::open(TEST_DIR, O_DIRECTORY);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(dir_fd, 0);
using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
Expand Down