Skip to content

Commit

Permalink
[libc][NFC] Switch unistd.h tests to libc_errno.
Browse files Browse the repository at this point in the history
  • Loading branch information
Siva Chandra Reddy committed Mar 14, 2023
1 parent 35fd371 commit 67de538
Show file tree
Hide file tree
Showing 23 changed files with 139 additions and 137 deletions.
23 changes: 20 additions & 3 deletions libc/test/src/unistd/CMakeLists.txt
Expand Up @@ -9,8 +9,8 @@ add_libc_unittest(
SRCS
access_test.cpp
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.access
libc.src.unistd.close
Expand All @@ -24,8 +24,8 @@ add_libc_unittest(
SRCS
chdir_test.cpp
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.chdir
libc.src.unistd.close
Expand All @@ -41,6 +41,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.dup
Expand All @@ -59,6 +60,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.dup2
Expand All @@ -77,6 +79,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.dup3
Expand All @@ -95,6 +98,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.fchdir
libc.src.unistd.close
Expand All @@ -110,6 +114,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.read
Expand All @@ -128,6 +133,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.fsync
Expand All @@ -147,6 +153,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.fsync
Expand All @@ -164,6 +171,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.link
Expand All @@ -179,6 +187,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.linkat
Expand All @@ -194,6 +203,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.lseek
Expand All @@ -210,6 +220,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.fcntl
libc.src.errno.errno
libc.src.sys.stat.mkdir
libc.src.unistd.rmdir
)
Expand All @@ -223,6 +234,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.unistd.readlink
libc.src.unistd.symlink
libc.src.unistd.unlink
Expand All @@ -239,6 +251,7 @@ add_libc_unittest(
libc.include.errno
libc.include.fcntl
libc.include.unistd
libc.src.errno.errno
libc.src.unistd.readlinkat
libc.src.unistd.symlink
libc.src.unistd.unlink
Expand All @@ -254,6 +267,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.symlink
Expand All @@ -269,6 +283,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.symlinkat
Expand All @@ -284,6 +299,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.read
Expand All @@ -302,6 +318,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.unistd.close
libc.src.unistd.unlink
Expand All @@ -316,6 +333,7 @@ add_libc_unittest(
DEPENDS
libc.include.errno
libc.include.unistd
libc.src.errno.errno
libc.src.fcntl.open
libc.src.fcntl.openat
libc.src.unistd.close
Expand Down Expand Up @@ -362,7 +380,6 @@ add_libc_unittest(
libc.src.unistd.isatty
libc.src.fcntl.open
libc.src.unistd.close
libc.include.errno
libc.src.errno.errno
)

Expand Down
24 changes: 12 additions & 12 deletions libc/test/src/unistd/access_test.cpp
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

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

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

TEST(LlvmLibcAccessTest, CreateAndTest) {
// The test strategy is to repeatedly create a file in different modes and
// test that it is accessable in those modes but not in others.
errno = 0;
libc_errno = 0;
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE = "testdata/access.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::close(fd), Succeeds(0));

ASSERT_EQ(__llvm_libc::access(TEST_FILE, F_OK), 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_EQ(__llvm_libc::access(TEST_FILE, X_OK | W_OK | R_OK), 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_THAT(__llvm_libc::unlink(TEST_FILE), Succeeds(0));

fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IXUSR);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
ASSERT_EQ(__llvm_libc::access(TEST_FILE, F_OK), 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_EQ(__llvm_libc::access(TEST_FILE, X_OK), 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_EQ(__llvm_libc::access(TEST_FILE, R_OK), -1);
ASSERT_EQ(errno, EACCES);
errno = 0;
ASSERT_EQ(libc_errno, EACCES);
libc_errno = 0;
ASSERT_EQ(__llvm_libc::access(TEST_FILE, W_OK), -1);
ASSERT_EQ(errno, EACCES);
errno = 0;
ASSERT_EQ(libc_errno, EACCES);
libc_errno = 0;
ASSERT_THAT(__llvm_libc::unlink(TEST_FILE), Succeeds(0));
}

Expand Down
12 changes: 6 additions & 6 deletions libc/test/src/unistd/chdir_test.cpp
Expand Up @@ -6,14 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/unistd/chdir.h"
#include "src/unistd/close.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,23 +25,23 @@ TEST(LlvmLibcChdirTest, ChangeAndOpen) {
constexpr const char *TEST_DIR = "testdata";
constexpr const char *TEST_FILE = "testdata/chdir.test";
constexpr const char *TEST_FILE_BASE = "chdir.test";
errno = 0;
libc_errno = 0;

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

ASSERT_THAT(__llvm_libc::chdir(TEST_DIR), Succeeds(0));
fd = __llvm_libc::open(TEST_FILE_BASE, O_PATH);
ASSERT_GT(fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
}

TEST(LlvmLibcChdirTest, ChangeToNonExistentDir) {
errno = 0;
libc_errno = 0;
using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
ASSERT_THAT(__llvm_libc::chdir("non-existent-dir"), Fails(ENOENT));
errno = 0;
libc_errno = 0;
}
13 changes: 6 additions & 7 deletions libc/test/src/unistd/dup2_test.cpp
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/dup2.h"
Expand All @@ -16,18 +17,16 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>

TEST(LlvmLibcdupTest, ReadAndWriteViaDup) {
constexpr int DUPFD = 0xD0;
errno = 0;
libc_errno = 0;
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE = "testdata/dup2.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::dup2(fd, DUPFD);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_EQ(dupfd, DUPFD);

// Write something via the dup
Expand All @@ -39,10 +38,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::dup2(fd, DUPFD);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_EQ(dupfd, DUPFD);

// Read the file content via the dup.
Expand Down
13 changes: 6 additions & 7 deletions libc/test/src/unistd/dup3_test.cpp
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/dup3.h"
Expand All @@ -16,24 +17,22 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>

// The tests here are exactly the same as those of dup2. We only test the
// plumbing of the dup3 syscall and not the dup3 functionality itself as it is
// a simple syscall wrapper. Testing dup3 functionality is beyond the scope of
// this test.

TEST(LlvmLibcdupTest, ReadAndWriteViaDup) {
constexpr int DUPFD = 0xD0;
errno = 0;
libc_errno = 0;
using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE = "testdata/dup3.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::dup3(fd, DUPFD, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_EQ(dupfd, DUPFD);

// Write something via the dup
Expand All @@ -45,10 +44,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::dup3(fd, DUPFD, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_EQ(dupfd, DUPFD);

// Read the file content via the dup.
Expand Down

0 comments on commit 67de538

Please sign in to comment.