18 changes: 9 additions & 9 deletions libc/test/src/sys/stat/fchmodat_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/sys/stat/fchmodat.h"
#include "src/unistd/close.h"
Expand All @@ -14,7 +15,6 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>

Expand All @@ -31,29 +31,29 @@ TEST(LlvmLibcFchmodatTest, ChangeAndOpen) {
constexpr const char *TEST_FILE_BASENAME = "fchmodat.test";
const char WRITE_DATA[] = "fchmodat test";
constexpr ssize_t WRITE_SIZE = ssize_t(sizeof(WRITE_DATA));
errno = 0;
libc_errno = 0;

int fd = __llvm_libc::open(TEST_FILE, O_CREAT | O_WRONLY, S_IRWXU);
ASSERT_GT(fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_EQ(__llvm_libc::write(fd, WRITE_DATA, sizeof(WRITE_DATA)), WRITE_SIZE);
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));

int dirfd = __llvm_libc::open(TEST_DIR, O_DIRECTORY);
ASSERT_GT(dirfd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

EXPECT_THAT(__llvm_libc::fchmodat(dirfd, TEST_FILE_BASENAME, S_IRUSR, 0),
Succeeds(0));

// Opening for writing should fail.
EXPECT_EQ(__llvm_libc::open(TEST_FILE, O_APPEND | O_WRONLY), -1);
EXPECT_NE(errno, 0);
errno = 0;
EXPECT_NE(libc_errno, 0);
libc_errno = 0;
// But opening for reading should succeed.
fd = __llvm_libc::open(TEST_FILE, O_APPEND | O_RDONLY);
EXPECT_GT(fd, 0);
EXPECT_EQ(errno, 0);
EXPECT_EQ(libc_errno, 0);

EXPECT_THAT(__llvm_libc::close(fd), Succeeds(0));
EXPECT_THAT(__llvm_libc::fchmodat(dirfd, TEST_FILE_BASENAME, S_IRWXU, 0),
Expand All @@ -63,9 +63,9 @@ TEST(LlvmLibcFchmodatTest, ChangeAndOpen) {
}

TEST(LlvmLibcFchmodatTest, NonExistentFile) {
errno = 0;
libc_errno = 0;
using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
ASSERT_THAT(__llvm_libc::fchmodat(AT_FDCWD, "non-existent-file", S_IRUSR, 0),
Fails(ENOENT));
errno = 0;
libc_errno = 0;
}
10 changes: 5 additions & 5 deletions libc/test/src/sys/stat/fstat_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/sys/stat/fstat.h"
#include "src/unistd/close.h"
Expand All @@ -14,7 +15,6 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>

Expand All @@ -27,11 +27,11 @@ TEST(LlvmLibcFStatTest, CreatAndReadMode) {
// make it readonly using chmod. We test that chmod actually succeeded by
// trying to open the file for writing and failing.
constexpr const char *TEST_FILE = "testdata/fstat.test";
errno = 0;
libc_errno = 0;

int fd = __llvm_libc::open(TEST_FILE, O_CREAT | O_WRONLY, S_IRWXU);
ASSERT_GT(fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

struct stat statbuf;
ASSERT_THAT(__llvm_libc::fstat(fd, &statbuf), Succeeds(0));
Expand All @@ -43,9 +43,9 @@ TEST(LlvmLibcFStatTest, CreatAndReadMode) {
}

TEST(LlvmLibcFStatTest, NonExistentFile) {
errno = 0;
libc_errno = 0;
using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
struct stat statbuf;
ASSERT_THAT(__llvm_libc::fstat(-1, &statbuf), Fails(EBADF));
errno = 0;
libc_errno = 0;
}
1 change: 0 additions & 1 deletion libc/test/src/sys/stat/mkdirat_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

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

TEST(LlvmLibcMkdiratTest, CreateAndRemove) {
Expand Down
10 changes: 5 additions & 5 deletions libc/test/src/sys/stat/stat_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/sys/stat/stat.h"
#include "src/unistd/close.h"
Expand All @@ -14,7 +15,6 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>

Expand All @@ -27,11 +27,11 @@ TEST(LlvmLibcStatTest, CreatAndReadMode) {
// make it readonly using chmod. We test that chmod actually succeeded by
// trying to open the file for writing and failing.
constexpr const char *TEST_FILE = "testdata/stat.test";
errno = 0;
libc_errno = 0;

int fd = __llvm_libc::open(TEST_FILE, O_CREAT | O_WRONLY, S_IRWXU);
ASSERT_GT(fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));

struct stat statbuf;
Expand All @@ -43,9 +43,9 @@ TEST(LlvmLibcStatTest, CreatAndReadMode) {
}

TEST(LlvmLibcStatTest, NonExistentFile) {
errno = 0;
libc_errno = 0;
using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
struct stat statbuf;
ASSERT_THAT(__llvm_libc::stat("non-existent-file", &statbuf), Fails(ENOENT));
errno = 0;
libc_errno = 0;
}