@@ -69,6 +69,14 @@ class test_file_canonical : public ::testing::Test {
69
69
}
70
70
};
71
71
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
+
72
80
TEST_F (test_file_canonical, canonical_path_to_regular_file) {
73
81
std::string temp_file_path = this ->make_temporary_directory () + " /temp.js" ;
74
82
write_file (temp_file_path, u8" hello\n world!\n " );
@@ -748,6 +756,56 @@ TEST_F(test_file_canonical,
748
756
}
749
757
#endif
750
758
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
+
751
809
#if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
752
810
TEST_F (test_file_canonical, canonical_path_posix_root) {
753
811
{
0 commit comments