Skip to content

Commit 147d96b

Browse files
committed
Add more tests for canonicalize_path
We have no test coverage for lstat failing. Add a test which causes lstat to fail.
1 parent 261b694 commit 147d96b

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

test/test-file-canonical.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ class test_file_canonical : public ::testing::Test {
6969
}
7070
};
7171

72+
bool process_ignores_filesystem_permissions() noexcept {
73+
#if QLJS_HAVE_UNISTD_H
74+
return ::geteuid() == 0;
75+
#else
76+
return false;
77+
#endif
78+
}
79+
7280
TEST_F(test_file_canonical, canonical_path_to_regular_file) {
7381
std::string temp_file_path = this->make_temporary_directory() + "/temp.js";
7482
write_file(temp_file_path, u8"hello\nworld!\n");
@@ -748,6 +756,56 @@ TEST_F(test_file_canonical,
748756
}
749757
#endif
750758

759+
#if QLJS_HAVE_UNISTD_H
760+
TEST_F(test_file_canonical, unsearchable_parent_directory) {
761+
if (process_ignores_filesystem_permissions()) {
762+
GTEST_SKIP() << "cannot run test as root";
763+
}
764+
765+
std::string temp_dir = this->make_temporary_directory();
766+
create_directory(temp_dir + "/dir");
767+
write_file(temp_dir + "/dir/file", u8"hello");
768+
ASSERT_EQ(::chmod((temp_dir + "/dir").c_str(), 0600), 0)
769+
<< std::strerror(errno);
770+
771+
std::string input_path = temp_dir + "/dir/file";
772+
canonical_path_result canonical = canonicalize_path(input_path);
773+
EXPECT_FALSE(canonical.ok());
774+
std::string error = std::move(canonical).error();
775+
EXPECT_THAT(error, HasSubstr("dir/file"));
776+
EXPECT_THAT(error, HasSubstr("Permission denied"));
777+
778+
// Allow test cleanup to delete the directory.
779+
EXPECT_EQ(::chmod((temp_dir + "/dir").c_str(), 0700), 0)
780+
<< std::strerror(errno);
781+
}
782+
783+
TEST_F(test_file_canonical, unsearchable_grandparent_directory) {
784+
if (process_ignores_filesystem_permissions()) {
785+
GTEST_SKIP() << "cannot run test as root";
786+
}
787+
788+
std::string temp_dir = this->make_temporary_directory();
789+
create_directory(temp_dir + "/dir");
790+
create_directory(temp_dir + "/dir/subdir");
791+
write_file(temp_dir + "/dir/subdir/file", u8"hello");
792+
ASSERT_EQ(::chmod((temp_dir + "/dir").c_str(), 0600), 0)
793+
<< std::strerror(errno);
794+
795+
std::string input_path = temp_dir + "/dir/subdir/file";
796+
canonical_path_result canonical = canonicalize_path(input_path);
797+
EXPECT_FALSE(canonical.ok());
798+
std::string error = std::move(canonical).error();
799+
EXPECT_THAT(error, HasSubstr("dir/subdir:"));
800+
EXPECT_THAT(error, HasSubstr("dir/subdir/file"));
801+
EXPECT_THAT(error, HasSubstr("Permission denied"));
802+
803+
// Allow test cleanup to delete the directory.
804+
EXPECT_EQ(::chmod((temp_dir + "/dir").c_str(), 0700), 0)
805+
<< std::strerror(errno);
806+
}
807+
#endif
808+
751809
#if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
752810
TEST_F(test_file_canonical, canonical_path_posix_root) {
753811
{

0 commit comments

Comments
 (0)