Skip to content

Commit

Permalink
[libc][NFC] Switch sys/*.h tests over to libc_errno.
Browse files Browse the repository at this point in the history
  • Loading branch information
Siva Chandra Reddy committed Mar 13, 2023
1 parent 2aaaed3 commit af783db
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 69 deletions.
1 change: 0 additions & 1 deletion libc/test/src/sys/random/linux/CMakeLists.txt
Expand Up @@ -7,7 +7,6 @@ add_libc_unittest(
SRCS
getrandom_test.cpp
DEPENDS
libc.include.errno
libc.include.math
libc.include.sys_random
libc.src.errno.errno
Expand Down
17 changes: 13 additions & 4 deletions libc/test/src/sys/random/linux/getrandom_test.cpp
@@ -1,3 +1,12 @@
//===-- Unittests for getrandom -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/math/fabs.h"
#include "src/sys/random/getrandom.h"
#include "test/ErrnoSetterMatcher.h"
Expand All @@ -7,17 +16,17 @@ TEST(LlvmLibcGetRandomTest, InvalidFlag) {
using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
static constexpr size_t SIZE = 256;
char data[SIZE];
errno = 0;
libc_errno = 0;
ASSERT_THAT(__llvm_libc::getrandom(data, SIZE, -1), Fails(EINVAL));
errno = 0;
libc_errno = 0;
}

TEST(LlvmLibcGetRandomTest, InvalidBuffer) {
using __llvm_libc::testing::ErrnoSetterMatcher::Fails;

errno = 0;
libc_errno = 0;
ASSERT_THAT(__llvm_libc::getrandom(nullptr, 65536, 0), Fails(EFAULT));
errno = 0;
libc_errno = 0;
}

TEST(LlvmLibcGetRandomTest, PiEstimation) {
Expand Down
20 changes: 10 additions & 10 deletions libc/test/src/sys/resource/getrlimit_setrlimit_test.cpp
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/sys/resource/getrlimit.h"
#include "src/sys/resource/setrlimit.h"
Expand All @@ -15,7 +16,6 @@
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <errno.h>
#include <sys/resource.h>

TEST(LlvmLibcResourceLimitsTest, SetNoFileLimit) {
Expand All @@ -28,14 +28,14 @@ TEST(LlvmLibcResourceLimitsTest, SetNoFileLimit) {

constexpr const char *TEST_FILE1 = "testdata/resource_limits1.test";
constexpr const char *TEST_FILE2 = "testdata/resource_limits2.test";
errno = 0;
libc_errno = 0;

int fd1 = __llvm_libc::open(TEST_FILE1, O_CREAT | O_WRONLY, S_IRWXU);
ASSERT_GT(fd1, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
int fd2 = __llvm_libc::open(TEST_FILE2, O_CREAT | O_WRONLY, S_IRWXU);
ASSERT_GT(fd2, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);

ASSERT_THAT(__llvm_libc::close(fd1), Succeeds(0));
ASSERT_THAT(__llvm_libc::close(fd2), Succeeds(0));
Expand All @@ -48,22 +48,22 @@ TEST(LlvmLibcResourceLimitsTest, SetNoFileLimit) {
// One can now only open one of the files successfully.
fd1 = __llvm_libc::open(TEST_FILE1, O_RDONLY);
ASSERT_GT(fd1, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
fd2 = __llvm_libc::open(TEST_FILE2, O_RDONLY);
ASSERT_LT(fd2, 0);
ASSERT_NE(errno, 0);
ASSERT_NE(libc_errno, 0);

errno = 0;
libc_errno = 0;
ASSERT_THAT(__llvm_libc::close(fd1), Succeeds(0));

fd2 = __llvm_libc::open(TEST_FILE2, O_RDONLY);
ASSERT_GT(fd2, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
fd1 = __llvm_libc::open(TEST_FILE1, O_RDONLY);
ASSERT_LT(fd1, 0);
ASSERT_NE(errno, 0);
ASSERT_NE(libc_errno, 0);

errno = 0;
libc_errno = 0;
ASSERT_THAT(__llvm_libc::close(fd2), Succeeds(0));

ASSERT_THAT(__llvm_libc::unlink(TEST_FILE1), Succeeds(0));
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/sys/select/select_ui_test.cpp
Expand Up @@ -6,19 +6,19 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/sys/select/select.h"
#include "src/unistd/read.h"
#include "test/UnitTest/Test.h"

#include <errno.h>
#include <sys/select.h>
#include <unistd.h>

// This test is not be run automatically as part of the libc testsuite.
// Instead, one has to run it manually and press a key on the keyboard
// to make the test succeed.
TEST(LlvmLibcSelectTest, ReadStdinAfterSelect) {
errno = 0;
libc_errno = 0;
constexpr int STDIN_FD = 0;
fd_set set;
FD_ZERO(&set);
Expand Down
12 changes: 6 additions & 6 deletions libc/test/src/sys/sendfile/sendfile_test.cpp
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/sys/sendfile/sendfile.h"
#include "src/unistd/close.h"
Expand All @@ -17,7 +18,6 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

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

Expand All @@ -36,28 +36,28 @@ TEST(LlvmLibcSendfileTest, CreateAndTransfer) {
constexpr const char *OUT_FILE = "testdata/sendfile_out.test";
const char IN_DATA[] = "sendfile test";
constexpr ssize_t IN_SIZE = ssize_t(sizeof(IN_DATA));
errno = 0;
libc_errno = 0;

int in_fd = __llvm_libc::open(IN_FILE, O_CREAT | O_WRONLY, S_IRWXU);
ASSERT_GT(in_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_EQ(__llvm_libc::write(in_fd, IN_DATA, IN_SIZE), IN_SIZE);
ASSERT_THAT(__llvm_libc::close(in_fd), Succeeds(0));

in_fd = __llvm_libc::open(IN_FILE, O_RDONLY);
ASSERT_GT(in_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
int out_fd = __llvm_libc::open(OUT_FILE, O_CREAT | O_WRONLY, S_IRWXU);
ASSERT_GT(out_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ssize_t size = __llvm_libc::sendfile(in_fd, out_fd, nullptr, IN_SIZE);
ASSERT_EQ(size, IN_SIZE);
ASSERT_THAT(__llvm_libc::close(in_fd), Succeeds(0));
ASSERT_THAT(__llvm_libc::close(out_fd), Succeeds(0));

out_fd = __llvm_libc::open(OUT_FILE, O_RDONLY);
ASSERT_GT(out_fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
char buf[IN_SIZE];
ASSERT_EQ(IN_SIZE, __llvm_libc::read(out_fd, buf, IN_SIZE));
ASSERT_EQ(cpp::string_view(buf), cpp::string_view(IN_DATA));
Expand Down
14 changes: 7 additions & 7 deletions libc/test/src/sys/stat/CMakeLists.txt
Expand Up @@ -9,9 +9,9 @@ add_libc_unittest(
SRCS
chmod_test.cpp
DEPENDS
libc.include.errno
libc.include.fcntl
libc.include.sys_stat
libc.src.errno.errno
libc.src.fcntl.open
libc.src.sys.stat.chmod
libc.src.unistd.close
Expand All @@ -25,9 +25,9 @@ add_libc_unittest(
SRCS
fchmodat_test.cpp
DEPENDS
libc.include.errno
libc.include.fcntl
libc.include.sys_stat
libc.src.errno.errno
libc.src.fcntl.open
libc.src.sys.stat.fchmodat
libc.src.unistd.close
Expand All @@ -41,9 +41,9 @@ add_libc_unittest(
SRCS
fchmod_test.cpp
DEPENDS
libc.include.errno
libc.include.fcntl
libc.include.sys_stat
libc.src.errno.errno
libc.src.fcntl.open
libc.src.sys.stat.fchmod
libc.src.unistd.close
Expand All @@ -57,9 +57,9 @@ add_libc_unittest(
SRCS
mkdirat_test.cpp
DEPENDS
libc.include.errno
libc.include.fcntl
libc.include.sys_stat
libc.src.errno.errno
libc.src.sys.stat.mkdirat
libc.src.unistd.rmdir
)
Expand All @@ -71,9 +71,9 @@ add_libc_unittest(
SRCS
stat_test.cpp
DEPENDS
libc.include.errno
libc.include.fcntl
libc.include.sys_stat
libc.src.errno.errno
libc.src.sys.stat.stat
libc.src.fcntl.open
libc.src.unistd.close
Expand All @@ -87,9 +87,9 @@ add_libc_unittest(
SRCS
lstat_test.cpp
DEPENDS
libc.include.errno
libc.include.fcntl
libc.include.sys_stat
libc.src.errno.errno
libc.src.sys.stat.lstat
libc.src.fcntl.open
libc.src.unistd.close
Expand All @@ -103,9 +103,9 @@ add_libc_unittest(
SRCS
fstat_test.cpp
DEPENDS
libc.include.errno
libc.include.fcntl
libc.include.sys_stat
libc.src.errno.errno
libc.src.sys.stat.fstat
libc.src.fcntl.open
libc.src.unistd.close
Expand Down
18 changes: 9 additions & 9 deletions libc/test/src/sys/stat/chmod_test.cpp
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/sys/stat/chmod.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 @@ -29,36 +29,36 @@ TEST(LlvmLibcChmodTest, ChangeAndOpen) {
constexpr const char *TEST_FILE = "testdata/chmod.test";
const char WRITE_DATA[] = "test data";
constexpr ssize_t WRITE_SIZE = ssize_t(sizeof(WRITE_DATA));
errno = 0;
libc_errno = 0;

int fd = __llvm_libc::open(TEST_FILE, O_APPEND | O_WRONLY);
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));

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));
EXPECT_THAT(__llvm_libc::chmod(TEST_FILE, S_IRUSR), 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::chmod(TEST_FILE, S_IRWXU), Succeeds(0));
}

TEST(LlvmLibcChmodTest, NonExistentFile) {
errno = 0;
libc_errno = 0;
using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
ASSERT_THAT(__llvm_libc::chmod("non-existent-file", S_IRUSR), Fails(ENOENT));
errno = 0;
libc_errno = 0;
}
20 changes: 10 additions & 10 deletions libc/test/src/sys/stat/fchmod_test.cpp
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/sys/stat/fchmod.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 @@ -29,36 +29,36 @@ TEST(LlvmLibcChmodTest, ChangeAndOpen) {
constexpr const char *TEST_FILE = "testdata/fchmod.test";
const char WRITE_DATA[] = "test data";
constexpr ssize_t WRITE_SIZE = ssize_t(sizeof(WRITE_DATA));
errno = 0;
libc_errno = 0;

int fd = __llvm_libc::open(TEST_FILE, O_APPEND | O_WRONLY);
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));

fd = __llvm_libc::open(TEST_FILE, O_APPEND | O_WRONLY);
ASSERT_GT(fd, 0);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
EXPECT_THAT(__llvm_libc::fchmod(fd, S_IRUSR), Succeeds(0));
ASSERT_THAT(__llvm_libc::close(fd), 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::fchmod(fd, S_IRWXU), Succeeds(0));
EXPECT_THAT(__llvm_libc::close(fd), Succeeds(0));
}

TEST(LlvmLibcChmodTest, NonExistentFile) {
errno = 0;
libc_errno = 0;
ASSERT_EQ(__llvm_libc::fchmod(-1, S_IRUSR), -1);
ASSERT_NE(errno, 0);
errno = 0;
ASSERT_NE(libc_errno, 0);
libc_errno = 0;
}

0 comments on commit af783db

Please sign in to comment.