Skip to content

Commit 02b329d

Browse files
okunzcopybara-github
authored andcommitted
Adds additional test cases for sapi::file::CleanPath and updates
the documentation. No behaviour changes are introduced. PiperOrigin-RevId: 721539154 Change-Id: I52a96055614518d169908f15a4c79f2cb6b5825b
1 parent 9865502 commit 02b329d

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

sandboxed_api/util/path.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ std::pair<absl::string_view, absl::string_view> SplitPath(
4848
// Collapses duplicate "/"s, resolve ".." and "." path elements, removes
4949
// trailing "/".
5050
//
51-
// NOTE: This respects relative vs. absolute paths, but does not
52-
// invoke any system calls in order to resolve relative paths to the actual
53-
// working directory. That is, this is purely a string manipulation, completely
54-
// independent of process state.
51+
// This is purely a string manipulation and does not invoke any system calls in
52+
// order to resolve relative paths to the actual working directory.
53+
//
54+
// For absolute paths, this is equivalent to the following:
55+
// /path/to/./foo/../bar/../baz/ -> /path/to/bar/baz
56+
//
57+
// For relative paths, this is equivalent to the following:
58+
// ./path/to/./foo/../bar/../baz/ -> path/to/bar/baz
59+
// ../path/to/./foo/../bar/../baz/ -> ../path/to/bar/baz
5560
std::string CleanPath(absl::string_view path);
5661

5762
} // namespace sapi::file

sandboxed_api/util/path_test.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ TEST(PathTest, CleanPath) {
9696
EXPECT_THAT(file::CleanPath("../../a/b/../c"), StrEq("../../a/c"));
9797
EXPECT_THAT(file::CleanPath("../../a/b/../c/../.."), StrEq("../.."));
9898
EXPECT_THAT(file::CleanPath("foo/../../../bar"), StrEq("../../bar"));
99+
EXPECT_THAT(file::CleanPath("/path/to/./foo/../bar/../baz/"),
100+
StrEq("/path/to/baz"));
101+
EXPECT_THAT(file::CleanPath("./path/to/./foo/../bar/../baz/"),
102+
StrEq("path/to/baz"));
103+
EXPECT_THAT(file::CleanPath("../path/to/./foo/../bar/../baz/"),
104+
StrEq("../path/to/baz"));
99105
}
100106

101107
} // namespace

0 commit comments

Comments
 (0)