summaryrefslogtreecommitdiffstats
path: root/src/util_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/util_test.cc')
-rw-r--r--src/util_test.cc213
1 files changed, 209 insertions, 4 deletions
diff --git a/src/util_test.cc b/src/util_test.cc
index b58d15e..8ca7f56 100644
--- a/src/util_test.cc
+++ b/src/util_test.cc
@@ -16,6 +16,15 @@
#include "test.h"
+namespace {
+
+bool CanonicalizePath(string* path, string* err) {
+ unsigned int unused;
+ return ::CanonicalizePath(path, &unused, err);
+}
+
+} // namespace
+
TEST(CanonicalizePath, PathSamples) {
string path;
string err;
@@ -84,6 +93,201 @@ TEST(CanonicalizePath, PathSamples) {
EXPECT_EQ("", path);
}
+#ifdef _WIN32
+TEST(CanonicalizePath, PathSamplesWindows) {
+ string path;
+ string err;
+
+ EXPECT_FALSE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("empty path", err);
+
+ path = "foo.h"; err = "";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("foo.h", path);
+
+ path = ".\\foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("foo.h", path);
+
+ path = ".\\foo\\.\\bar.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("foo/bar.h", path);
+
+ path = ".\\x\\foo\\..\\bar.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("x/bar.h", path);
+
+ path = ".\\x\\foo\\..\\..\\bar.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("bar.h", path);
+
+ path = "foo\\\\bar";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("foo/bar", path);
+
+ path = "foo\\\\.\\\\..\\\\\\bar";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("bar", path);
+
+ path = ".\\x\\..\\foo\\..\\..\\bar.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("../bar.h", path);
+
+ path = "foo\\.\\.";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("foo", path);
+
+ path = "foo\\bar\\..";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("foo", path);
+
+ path = "foo\\.hidden_bar";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("foo/.hidden_bar", path);
+
+ path = "\\foo";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("/foo", path);
+
+ path = "\\\\foo";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("//foo", path);
+
+ path = "\\";
+ EXPECT_TRUE(CanonicalizePath(&path, &err));
+ EXPECT_EQ("", path);
+}
+
+TEST(CanonicalizePath, SlashTracking) {
+ string path;
+ string err;
+ unsigned int slash_bits;
+
+ path = "foo.h"; err = "";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("foo.h", path);
+ EXPECT_EQ(0, slash_bits);
+
+ path = "a\\foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("a/foo.h", path);
+ EXPECT_EQ(1, slash_bits);
+
+ path = "a/bcd/efh\\foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("a/bcd/efh/foo.h", path);
+ EXPECT_EQ(4, slash_bits);
+
+ path = "a\\bcd/efh\\foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("a/bcd/efh/foo.h", path);
+ EXPECT_EQ(5, slash_bits);
+
+ path = "a\\bcd\\efh\\foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("a/bcd/efh/foo.h", path);
+ EXPECT_EQ(7, slash_bits);
+
+ path = "a/bcd/efh/foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("a/bcd/efh/foo.h", path);
+ EXPECT_EQ(0, slash_bits);
+
+ path = "a\\./efh\\foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("a/efh/foo.h", path);
+ EXPECT_EQ(3, slash_bits);
+
+ path = "a\\../efh\\foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("efh/foo.h", path);
+ EXPECT_EQ(1, slash_bits);
+
+ path = "a\\b\\c\\d\\e\\f\\g\\foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("a/b/c/d/e/f/g/foo.h", path);
+ EXPECT_EQ(127, slash_bits);
+
+ path = "a\\b\\c\\..\\..\\..\\g\\foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("g/foo.h", path);
+ EXPECT_EQ(1, slash_bits);
+
+ path = "a\\b/c\\../../..\\g\\foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("g/foo.h", path);
+ EXPECT_EQ(1, slash_bits);
+
+ path = "a\\b/c\\./../..\\g\\foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("a/g/foo.h", path);
+ EXPECT_EQ(3, slash_bits);
+
+ path = "a\\b/c\\./../..\\g/foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("a/g/foo.h", path);
+ EXPECT_EQ(1, slash_bits);
+
+ path = "a\\\\\\foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("a/foo.h", path);
+ EXPECT_EQ(1, slash_bits);
+
+ path = "a/\\\\foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("a/foo.h", path);
+ EXPECT_EQ(0, slash_bits);
+
+ path = "a\\//foo.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ("a/foo.h", path);
+ EXPECT_EQ(1, slash_bits);
+}
+
+TEST(CanonicalizePath, CanonicalizeNotExceedingLen) {
+ // Make sure searching \/ doesn't go past supplied len.
+ char buf[] = "foo/bar\\baz.h\\"; // Last \ past end.
+ unsigned int slash_bits;
+ string err;
+ size_t size = 13;
+ EXPECT_TRUE(::CanonicalizePath(buf, &size, &slash_bits, &err));
+ EXPECT_EQ(0, strncmp("foo/bar/baz.h", buf, size));
+ EXPECT_EQ(2, slash_bits); // Not including the trailing one.
+}
+
+TEST(CanonicalizePath, TooManyComponents) {
+ string path;
+ string err;
+ unsigned int slash_bits;
+
+ // 32 is OK.
+ path = "a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./x.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+
+ // Backslashes version.
+ path =
+ "a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\."
+ "\\a\\.\\a\\.\\a\\.\\a\\.\\x.h";
+ EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ(slash_bits, 0xffff);
+
+ // 33 is not.
+ err = "";
+ path =
+ "a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/x.h";
+ EXPECT_FALSE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ(err, "too many path components");
+
+ // Backslashes version.
+ err = "";
+ path =
+ "a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\."
+ "\\a\\.\\a\\.\\a\\.\\a\\.\\a\\x.h";
+ EXPECT_FALSE(CanonicalizePath(&path, &slash_bits, &err));
+ EXPECT_EQ(err, "too many path components");
+}
+#endif
+
TEST(CanonicalizePath, EmptyResult) {
string path;
string err;
@@ -122,26 +326,27 @@ TEST(CanonicalizePath, NotNullTerminated) {
string path;
string err;
size_t len;
+ unsigned int unused;
path = "foo/. bar/.";
len = strlen("foo/."); // Canonicalize only the part before the space.
- EXPECT_TRUE(CanonicalizePath(&path[0], &len, &err));
+ EXPECT_TRUE(CanonicalizePath(&path[0], &len, &unused, &err));
EXPECT_EQ(strlen("foo"), len);
EXPECT_EQ("foo/. bar/.", string(path));
path = "foo/../file bar/.";
len = strlen("foo/../file");
- EXPECT_TRUE(CanonicalizePath(&path[0], &len, &err));
+ EXPECT_TRUE(CanonicalizePath(&path[0], &len, &unused, &err));
EXPECT_EQ(strlen("file"), len);
EXPECT_EQ("file ./file bar/.", string(path));
}
TEST(PathEscaping, TortureTest) {
string result;
-
+
GetWin32EscapedString("foo bar\\\"'$@d!st!c'\\path'\\", &result);
EXPECT_EQ("\"foo bar\\\\\\\"'$@d!st!c'\\path'\\\\\"", result);
- result.clear();
+ result.clear();
GetShellEscapedString("foo bar\"/'$@d!st!c'/path'", &result);
EXPECT_EQ("'foo bar\"/'\\''$@d!st!c'\\''/path'\\'''", result);