From 00549b4fcdde7de69f9fe618000e49c13db8224b Mon Sep 17 00:00:00 2001 From: Laszlo Csomor Date: Wed, 11 Jan 2017 09:12:10 +0000 Subject: Bazel client: split CanAccess to specific methods The new methods (CanReadFile, CanExecuteFile, CanAccessDirectory) are a lot easier to implement on Windows than a generic CanAccess. On POSIX these methods are just a wrapper around the now static-visible CanAccess(). See https://github.com/bazelbuild/bazel/issues/2107 -- PiperOrigin-RevId: 144176710 MOS_MIGRATED_REVID=144176710 --- src/test/cpp/util/file_posix_test.cc | 69 ++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 31 deletions(-) (limited to 'src/test/cpp/util') diff --git a/src/test/cpp/util/file_posix_test.cc b/src/test/cpp/util/file_posix_test.cc index 12030f7474..b100016076 100644 --- a/src/test/cpp/util/file_posix_test.cc +++ b/src/test/cpp/util/file_posix_test.cc @@ -226,41 +226,48 @@ TEST(FilePosixTest, PathExists) { } TEST(FilePosixTest, CanAccess) { - for (int i = 0; i < 8; ++i) { - ASSERT_FALSE(CanAccess("/this/should/not/exist/mkay", i & 1, i & 2, i & 4)); - ASSERT_FALSE(CanAccess("non.existent", i & 1, i & 2, i & 4)); - } - - for (int i = 0; i < 4; ++i) { - // /usr/bin/yes exists on Linux, Darwin, and MSYS - ASSERT_TRUE(CanAccess("/", i & 1, false, i & 2)); - ASSERT_TRUE(CanAccess("/usr", i & 1, false, i & 2)); - ASSERT_TRUE(CanAccess("/usr/", i & 1, false, i & 2)); - ASSERT_TRUE(CanAccess("/usr/bin/yes", i & 1, false, i & 2)); - } - - char* tmpdir_cstr = getenv("TEST_TMPDIR"); - ASSERT_FALSE(tmpdir_cstr == NULL); + ASSERT_FALSE(CanReadFile("/this/should/not/exist/mkay")); + ASSERT_FALSE(CanExecuteFile("/this/should/not/exist/mkay")); + ASSERT_FALSE(CanAccessDirectory("/this/should/not/exist/mkay")); - string tmpdir(tmpdir_cstr); - ASSERT_NE("", tmpdir); + ASSERT_FALSE(CanReadFile("non.existent")); + ASSERT_FALSE(CanExecuteFile("non.existent")); + ASSERT_FALSE(CanAccessDirectory("non.existent")); - string mock_file = tmpdir + (tmpdir.back() == '/' ? "" : "/") + - "FilePosixTest.CanAccess.mock_file"; - int fd = open(mock_file.c_str(), O_CREAT, 0500); - ASSERT_GT(fd, 0); - close(fd); + const char* tmpdir = getenv("TEST_TMPDIR"); + ASSERT_NE(nullptr, tmpdir); + ASSERT_NE(0, *tmpdir); - // Sanity check: assert that we successfully created the file with the given - // permissions. - ASSERT_EQ(0, access(mock_file.c_str(), R_OK | X_OK)); - ASSERT_NE(0, access(mock_file.c_str(), R_OK | W_OK | X_OK)); + string dir(JoinPath(tmpdir, "canaccesstest")); + ASSERT_EQ(0, mkdir(dir.c_str(), 0700)); - // Actual assertion - for (int i = 0; i < 4; ++i) { - ASSERT_TRUE(CanAccess(mock_file, i & 1, false, i & 2)); - ASSERT_FALSE(CanAccess(mock_file, i & 1, true, i & 2)); - } + ASSERT_FALSE(CanReadFile(dir)); + ASSERT_FALSE(CanExecuteFile(dir)); + ASSERT_TRUE(CanAccessDirectory(dir)); + + string file(JoinPath(dir, "foo.txt")); + FILE* fh = fopen(file.c_str(), "wt"); + ASSERT_NE(nullptr, fh); + ASSERT_LT(0, fprintf(fh, "hello")); + fclose(fh); + + ASSERT_TRUE(CanReadFile(file)); + ASSERT_FALSE(CanExecuteFile(file)); + ASSERT_FALSE(CanAccessDirectory(file)); + + ASSERT_EQ(0, chmod(file.c_str(), 0100)); + ASSERT_FALSE(CanReadFile(file)); + ASSERT_TRUE(CanExecuteFile(file)); + ASSERT_FALSE(CanAccessDirectory(file)); + + ASSERT_EQ(0, chmod(dir.c_str(), 0500)); + ASSERT_FALSE(CanReadFile(dir)); + ASSERT_FALSE(CanExecuteFile(dir)); + ASSERT_FALSE(CanAccessDirectory(dir)); + ASSERT_EQ(0, chmod(dir.c_str(), 0700)); + + ASSERT_EQ(0, unlink(file.c_str())); + ASSERT_EQ(0, rmdir(dir.c_str())); } TEST(FilePosixTest, GetCwd) { -- cgit v1.2.3