@@ -12,15 +12,69 @@ using namespace TestCommon;
1212
1313TEST_CASE (" PathEscapesDirectory" , " [filesystem]" )
1414{
15- std::string badRelativePath = " ../../target.exe" ;
16- std::string badRelativePath2 = " test/../../target.exe" ;
17- std::string goodRelativePath = " target.exe" ;
18- std::string goodRelativePath2 = " test/../test1/target.exe" ;
19-
20- REQUIRE (PathEscapesBaseDirectory (badRelativePath));
21- REQUIRE (PathEscapesBaseDirectory (badRelativePath2));
22- REQUIRE_FALSE (PathEscapesBaseDirectory (goodRelativePath));
23- REQUIRE_FALSE (PathEscapesBaseDirectory (goodRelativePath2));
15+ SECTION (" Simple relative paths stay within the base directory" )
16+ {
17+ REQUIRE_FALSE (PathEscapesBaseDirectory (" target.exe" ));
18+ REQUIRE_FALSE (PathEscapesBaseDirectory (" test\\ target.exe" ));
19+ REQUIRE_FALSE (PathEscapesBaseDirectory (" test/subdir/target.exe" ));
20+ }
21+
22+ SECTION (" Relative paths whose '..' components resolve back inside do not escape" )
23+ {
24+ REQUIRE_FALSE (PathEscapesBaseDirectory (" test/../test1/target.exe" ));
25+ REQUIRE_FALSE (PathEscapesBaseDirectory (" ./target.exe" ));
26+ REQUIRE_FALSE (PathEscapesBaseDirectory (" a/b/../../c.exe" ));
27+ }
28+
29+ SECTION (" Paths that resolve to the base directory itself do not escape" )
30+ {
31+ REQUIRE_FALSE (PathEscapesBaseDirectory (" ." ));
32+ REQUIRE_FALSE (PathEscapesBaseDirectory (" test/.." ));
33+ }
34+
35+ SECTION (" An empty path refers to the base directory itself and does not escape" )
36+ {
37+ REQUIRE_FALSE (PathEscapesBaseDirectory (" " ));
38+ }
39+
40+ SECTION (" Relative paths that traverse above the base directory escape" )
41+ {
42+ REQUIRE (PathEscapesBaseDirectory (" ../../target.exe" ));
43+ REQUIRE (PathEscapesBaseDirectory (" test/../../target.exe" ));
44+ REQUIRE (PathEscapesBaseDirectory (" ../target.exe" ));
45+ REQUIRE (PathEscapesBaseDirectory (" .." ));
46+ REQUIRE (PathEscapesBaseDirectory (" a/../../b.exe" ));
47+
48+ // Mixed separators are still normalized correctly.
49+ REQUIRE (PathEscapesBaseDirectory (" test\\ ../..\\ target.exe" ));
50+ }
51+
52+ SECTION (" Absolute paths escape the base directory" )
53+ {
54+ REQUIRE (PathEscapesBaseDirectory (" C:\\ Windows\\ target.exe" ));
55+ REQUIRE (PathEscapesBaseDirectory (" C:/Windows/target.exe" ));
56+ }
57+
58+ SECTION (" UNC paths in their various forms escape the base directory" )
59+ {
60+ REQUIRE (PathEscapesBaseDirectory (" \\\\ server\\ share\\ target.exe" ));
61+ REQUIRE (PathEscapesBaseDirectory (" //server/share/target.exe" ));
62+
63+ // Extended-length prefix.
64+ REQUIRE (PathEscapesBaseDirectory (" \\\\ ?\\ C:\\ target.exe" ));
65+ }
66+
67+ SECTION (" Root-relative paths (no drive) resolve to the root of the base directory's drive" )
68+ {
69+ REQUIRE (PathEscapesBaseDirectory (" \\ Windows\\ target.exe" ));
70+ REQUIRE (PathEscapesBaseDirectory (" /Windows/target.exe" ));
71+ }
72+
73+ SECTION (" Drive-relative paths resolve against the current directory of the given drive" )
74+ {
75+ REQUIRE (PathEscapesBaseDirectory (" C:target.exe" ));
76+ REQUIRE (PathEscapesBaseDirectory (" C:" ));
77+ }
2478}
2579
2680TEST_CASE (" VerifySymlink" , " [filesystem]" )
0 commit comments