Fix Mounts::ResolvePath for dir nodes.

PiperOrigin-RevId: 368390904
Change-Id: I4f59e8d74b0d81497255cb0838d6d3132cae160b
This commit is contained in:
Wiktor Garbacz 2021-04-14 02:45:15 -07:00 committed by Copybara-Service
parent 2efaa463c9
commit bc6bb0c7e5
2 changed files with 54 additions and 1 deletions

View File

@ -278,7 +278,6 @@ absl::StatusOr<std::string> Mounts::ResolvePath(absl::string_view path) const {
std::pair<absl::string_view, absl::string_view> parts = std::pair<absl::string_view, absl::string_view> parts =
absl::StrSplit(tail, absl::MaxSplits('/', 1)); absl::StrSplit(tail, absl::MaxSplits('/', 1));
const std::string cur(parts.first); const std::string cur(parts.first);
tail = parts.second;
const auto it = curtree->entries().find(cur); const auto it = curtree->entries().find(cur);
if (it == curtree->entries().end()) { if (it == curtree->entries().end()) {
if (curtree->node().has_dir_node()) { if (curtree->node().has_dir_node()) {
@ -287,6 +286,7 @@ absl::StatusOr<std::string> Mounts::ResolvePath(absl::string_view path) const {
return absl::NotFoundError("Path could not be resolved in the mounts"); return absl::NotFoundError("Path could not be resolved in the mounts");
} }
curtree = &it->second; curtree = &it->second;
tail = parts.second;
} }
switch (curtree->node().node_case()) { switch (curtree->node().node_case()) {
case MountTree::Node::kFileNode: case MountTree::Node::kFileNode:

View File

@ -39,6 +39,7 @@ using ::sapi::GetTestTempPath;
using ::sapi::IsOk; using ::sapi::IsOk;
using ::sapi::StatusIs; using ::sapi::StatusIs;
using ::testing::Eq; using ::testing::Eq;
using ::testing::StrEq;
using ::testing::UnorderedElementsAreArray; using ::testing::UnorderedElementsAreArray;
constexpr size_t kTmpfsSize = 1024; constexpr size_t kTmpfsSize = 1024;
@ -243,5 +244,57 @@ TEST(MountTreeTest, TestList) {
// clang-format on // clang-format on
} }
TEST(MountsResolvePathTest, Files) {
Mounts mounts;
ASSERT_THAT(mounts.AddFileAt("/A", "/a"), IsOk());
ASSERT_THAT(mounts.AddFileAt("/B", "/d/b"), IsOk());
ASSERT_THAT(mounts.AddFileAt("/C/D/E", "/d/c/e/f/h"), IsOk());
std::string resolved;
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/a"));
EXPECT_THAT(resolved, StrEq("/A"));
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/b"));
EXPECT_THAT(resolved, StrEq("/B"));
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/c/e/f/h"));
EXPECT_THAT(resolved, StrEq("/C/D/E"));
ASSERT_THAT(mounts.ResolvePath("/f"), StatusIs(absl::StatusCode::kNotFound));
ASSERT_THAT(mounts.ResolvePath("/d"), StatusIs(absl::StatusCode::kNotFound));
ASSERT_THAT(mounts.ResolvePath("/d/c/e/f"),
StatusIs(absl::StatusCode::kNotFound));
ASSERT_THAT(mounts.ResolvePath("/d/d"),
StatusIs(absl::StatusCode::kNotFound));
}
TEST(MountsResolvePathTest, Dirs) {
Mounts mounts;
ASSERT_THAT(mounts.AddDirectoryAt("/A", "/a"), IsOk());
ASSERT_THAT(mounts.AddDirectoryAt("/B", "/d/b"), IsOk());
ASSERT_THAT(mounts.AddDirectoryAt("/C/D/E", "/d/c/e/f/h"), IsOk());
ASSERT_THAT(mounts.AddFileAt("/J/G/H", "/d/c/e/f/h/j"), IsOk());
ASSERT_THAT(mounts.AddDirectoryAt("/K/L/M", "/d/c/e/f/h/k"), IsOk());
std::string resolved;
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/a"));
EXPECT_THAT(resolved, StrEq("/A"));
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/a/b/c/d/e"));
EXPECT_THAT(resolved, StrEq("/A/b/c/d/e"));
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/b"));
EXPECT_THAT(resolved, StrEq("/B"));
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/c/e/f/h"));
EXPECT_THAT(resolved, StrEq("/C/D/E"));
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/c/e/f/h/i"));
EXPECT_THAT(resolved, StrEq("/C/D/E/i"));
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/c/e/f/h/j"));
EXPECT_THAT(resolved, StrEq("/J/G/H"));
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/c/e/f/h/k"));
EXPECT_THAT(resolved, StrEq("/K/L/M"));
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/c/e/f/h/k/a"));
EXPECT_THAT(resolved, StrEq("/K/L/M/a"));
ASSERT_THAT(mounts.ResolvePath("/f"), StatusIs(absl::StatusCode::kNotFound));
ASSERT_THAT(mounts.ResolvePath("/d"), StatusIs(absl::StatusCode::kNotFound));
ASSERT_THAT(mounts.ResolvePath("/d/c/e/f"),
StatusIs(absl::StatusCode::kNotFound));
ASSERT_THAT(mounts.ResolvePath("/d/d"),
StatusIs(absl::StatusCode::kNotFound));
}
} // namespace } // namespace
} // namespace sandbox2 } // namespace sandbox2