From be185c73e2b392894099ea357636c9e6160485ee Mon Sep 17 00:00:00 2001 From: Hao Zheng Date: Mon, 12 Aug 2024 17:43:01 -0700 Subject: [PATCH] Test readdir_r symbolic file handling b/302730696 Change-Id: I6e68ecdc43fdbc637674d85a74aa38fe04b581de Change-Id: I17a5497628d1bfabdf8f626a44c416951a2267ce --- .../posix_directory_get_next_test.cc | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/starboard/nplb/posix_compliance/posix_directory_get_next_test.cc b/starboard/nplb/posix_compliance/posix_directory_get_next_test.cc index 43c39b25d474..bbacc5621b67 100644 --- a/starboard/nplb/posix_compliance/posix_directory_get_next_test.cc +++ b/starboard/nplb/posix_compliance/posix_directory_get_next_test.cc @@ -14,6 +14,7 @@ #include +#include #include #include #include @@ -216,6 +217,44 @@ TEST(PosixDirectoryGetNextTest, FailureOnInsufficientSize) { EXPECT_TRUE(closedir(directory) == 0); } +TEST(PosixDirectoryGetNextTest, SymlinkHandling) { + // std::string path = GetTempDir(); this temp dir caches many other files. + std::string path = GetTempDir(); + path += "/test_sym"; + + int mkdir_result = mkdir(path.c_str(), 0777); + EXPECT_TRUE(mkdir_result == 0); + + std::string symlink_path = path + "/symlink"; + std::string target_path = path + "/target"; + + FILE* target_file = fopen(target_path.c_str(), "w"); + EXPECT_TRUE(target_file != nullptr); + fclose(target_file); + + int result = symlink(target_path.c_str(), symlink_path.c_str()); + EXPECT_EQ(result, 0) << "Failed to create symlink: " << strerror(errno); + + DIR* directory = opendir(path.c_str()); + EXPECT_TRUE(directory) << "Can't open: " << path; + + while (true) { + struct dirent dirent_buffer; + struct dirent* dirent; + result = readdir_r(directory, &dirent_buffer, &dirent); + if (result || !dirent) { + break; + } + + SB_LOG(INFO) << "dirent d_name: " << dirent->d_name; + // Check if the entry is a symlink. + if (dirent->d_type == DT_LNK) { + EXPECT_STREQ(dirent->d_name, "symlink"); + } + } + + EXPECT_TRUE(closedir(directory) == 0); +} } // namespace } // namespace nplb } // namespace starboard