Skip to content

Commit

Permalink
Test readdir_r symbolic file handling
Browse files Browse the repository at this point in the history
b/302730696
Change-Id: I6e68ecdc43fdbc637674d85a74aa38fe04b581de

Change-Id: I17a5497628d1bfabdf8f626a44c416951a2267ce
  • Loading branch information
haozheng-cobalt committed Aug 13, 2024
1 parent 9545024 commit be185c7
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions starboard/nplb/posix_compliance/posix_directory_get_next_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <dirent.h>

#include <unistd.h>
#include <queue>
#include <set>
#include <string>
Expand Down Expand Up @@ -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

0 comments on commit be185c7

Please sign in to comment.