diff options
author | Edward Thomson <ethomson@microsoft.com> | 2014-11-26 02:13:00 +0300 |
---|---|---|
committer | Edward Thomson <ethomson@microsoft.com> | 2014-12-16 19:08:53 +0300 |
commit | a64119e3963fcd358ba914c9e1a81a890666d15a (patch) | |
tree | f81788b6b64bc56b84f59da979895078ec93b246 /tests/path | |
parent | 0d388adc86946f3c8cddc2493b470d47a653c4a5 (diff) |
checkout: disallow bad paths on win32
Disallow:
1. paths with trailing dot
2. paths with trailing space
3. paths with trailing colon
4. paths that are 8.3 short names of .git folders ("GIT~1")
5. paths that are reserved path names (COM1, LPT1, etc).
6. paths with reserved DOS characters (colons, asterisks, etc)
These paths would (without \\?\ syntax) be elided to other paths - for
example, ".git." would be written as ".git". As a result, writing these
paths literally (using \\?\ syntax) makes them hard to operate with from
the shell, Windows Explorer or other tools. Disallow these.
Diffstat (limited to 'tests/path')
-rw-r--r-- | tests/path/core.c | 194 | ||||
-rw-r--r-- | tests/path/win32.c | 24 |
2 files changed, 218 insertions, 0 deletions
diff --git a/tests/path/core.c b/tests/path/core.c index 45f54df29..7cc111800 100644 --- a/tests/path/core.c +++ b/tests/path/core.c @@ -51,3 +51,197 @@ void test_path_core__make_relative(void) test_make_relative("/path", "/path", "pathtofoo", GIT_ENOTFOUND); test_make_relative("path", "path", "pathtofoo", GIT_ENOTFOUND); } + +void test_path_core__isvalid_standard(void) +{ + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/bar", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/bar/file.txt", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/bar/.file", 0)); +} + +void test_path_core__isvalid_empty_dir_component(void) +{ + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo//bar", 0)); + + /* leading slash */ + cl_assert_equal_b(false, git_path_isvalid(NULL, "/", 0)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "/foo", 0)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "/foo/bar", 0)); + + /* trailing slash */ + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo/", 0)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo/bar/", 0)); +} + +void test_path_core__isvalid_dot_and_dotdot(void) +{ + cl_assert_equal_b(true, git_path_isvalid(NULL, ".", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "./foo", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/.", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "./foo", 0)); + + cl_assert_equal_b(true, git_path_isvalid(NULL, "..", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "../foo", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/..", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "../foo", 0)); + + cl_assert_equal_b(false, git_path_isvalid(NULL, ".", GIT_PATH_REJECT_TRAVERSAL)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "./foo", GIT_PATH_REJECT_TRAVERSAL)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo/.", GIT_PATH_REJECT_TRAVERSAL)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "./foo", GIT_PATH_REJECT_TRAVERSAL)); + + cl_assert_equal_b(false, git_path_isvalid(NULL, "..", GIT_PATH_REJECT_TRAVERSAL)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "../foo", GIT_PATH_REJECT_TRAVERSAL)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo/..", GIT_PATH_REJECT_TRAVERSAL)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "../foo", GIT_PATH_REJECT_TRAVERSAL)); +} + +void test_path_core__isvalid_dot_git(void) +{ + cl_assert_equal_b(true, git_path_isvalid(NULL, ".git", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, ".git/foo", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/.git", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/.git/bar", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/.GIT/bar", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/bar/.Git", 0)); + + cl_assert_equal_b(false, git_path_isvalid(NULL, ".git", GIT_PATH_REJECT_DOT_GIT)); + cl_assert_equal_b(false, git_path_isvalid(NULL, ".git/foo", GIT_PATH_REJECT_DOT_GIT)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo/.git", GIT_PATH_REJECT_DOT_GIT)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo/.git/bar", GIT_PATH_REJECT_DOT_GIT)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo/.GIT/bar", GIT_PATH_REJECT_DOT_GIT)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo/bar/.Git", GIT_PATH_REJECT_DOT_GIT)); + + cl_assert_equal_b(true, git_path_isvalid(NULL, "!git", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/!git", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "!git/bar", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, ".tig", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/.tig", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, ".tig/bar", 0)); +} + +void test_path_core__isvalid_backslash(void) +{ + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo\\file.txt", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/bar\\file.txt", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/bar\\", 0)); + + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo\\file.txt", GIT_PATH_REJECT_BACKSLASH)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo/bar\\file.txt", GIT_PATH_REJECT_BACKSLASH)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo/bar\\", GIT_PATH_REJECT_BACKSLASH)); +} + +void test_path_core__isvalid_trailing_dot(void) +{ + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo.", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo...", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/bar.", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo./bar", 0)); + + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo.", GIT_PATH_REJECT_TRAILING_DOT)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo...", GIT_PATH_REJECT_TRAILING_DOT)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo/bar.", GIT_PATH_REJECT_TRAILING_DOT)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo./bar", GIT_PATH_REJECT_TRAILING_DOT)); +} + +void test_path_core__isvalid_trailing_space(void) +{ + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo ", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo ", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/bar ", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, " ", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo /bar", 0)); + + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo ", GIT_PATH_REJECT_TRAILING_SPACE)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo ", GIT_PATH_REJECT_TRAILING_SPACE)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo/bar ", GIT_PATH_REJECT_TRAILING_SPACE)); + cl_assert_equal_b(false, git_path_isvalid(NULL, " ", GIT_PATH_REJECT_TRAILING_SPACE)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo /bar", GIT_PATH_REJECT_TRAILING_SPACE)); +} + +void test_path_core__isvalid_trailing_colon(void) +{ + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo:", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo/bar:", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, ":", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "foo:/bar", 0)); + + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo:", GIT_PATH_REJECT_TRAILING_COLON)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo/bar:", GIT_PATH_REJECT_TRAILING_COLON)); + cl_assert_equal_b(false, git_path_isvalid(NULL, ":", GIT_PATH_REJECT_TRAILING_COLON)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "foo:/bar", GIT_PATH_REJECT_TRAILING_COLON)); +} + +void test_path_core__isvalid_dos_git_shortname(void) +{ + cl_assert_equal_b(true, git_path_isvalid(NULL, "git~1", 0)); + + cl_assert_equal_b(false, git_path_isvalid(NULL, "git~1", GIT_PATH_REJECT_DOS_GIT_SHORTNAME)); +} + +void test_path_core__isvalid_dos_paths(void) +{ + cl_assert_equal_b(true, git_path_isvalid(NULL, "aux", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "aux.", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "aux:", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "aux.asdf", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "aux.asdf\\zippy", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "aux:asdf\\foobar", 0)); + + cl_assert_equal_b(false, git_path_isvalid(NULL, "aux", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "aux.", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "aux:", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "aux.asdf", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "aux.asdf\\zippy", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "aux:asdf\\foobar", GIT_PATH_REJECT_DOS_PATHS)); + + cl_assert_equal_b(true, git_path_isvalid(NULL, "aux1", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "aux1", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "auxn", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "aux\\foo", GIT_PATH_REJECT_DOS_PATHS)); +} + +void test_path_core__isvalid_dos_paths_withnum(void) +{ + cl_assert_equal_b(true, git_path_isvalid(NULL, "com1", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "com1.", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "com1:", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "com1.asdf", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "com1.asdf\\zippy", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "com1:asdf\\foobar", 0)); + + cl_assert_equal_b(false, git_path_isvalid(NULL, "com1", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "com1.", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "com1:", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "com1.asdf", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "com1.asdf\\zippy", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "com1:asdf\\foobar", GIT_PATH_REJECT_DOS_PATHS)); + + cl_assert_equal_b(true, git_path_isvalid(NULL, "com10", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "com10", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "comn", GIT_PATH_REJECT_DOS_PATHS)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "com1\\foo", GIT_PATH_REJECT_DOS_PATHS)); +} + +void test_core_path__isvalid_nt_chars(void) +{ + cl_assert_equal_b(true, git_path_isvalid(NULL, "asdf\001foo", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "asdf\037bar", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "asdf<bar", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "asdf>foo", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "asdf:foo", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "asdf\"bar", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "asdf|foo", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "asdf?bar", 0)); + cl_assert_equal_b(true, git_path_isvalid(NULL, "asdf*bar", 0)); + + cl_assert_equal_b(false, git_path_isvalid(NULL, "asdf\001foo", GIT_PATH_REJECT_NT_CHARS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "asdf\037bar", GIT_PATH_REJECT_NT_CHARS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "asdf<bar", GIT_PATH_REJECT_NT_CHARS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "asdf>foo", GIT_PATH_REJECT_NT_CHARS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "asdf:foo", GIT_PATH_REJECT_NT_CHARS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "asdf\"bar", GIT_PATH_REJECT_NT_CHARS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "asdf|foo", GIT_PATH_REJECT_NT_CHARS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "asdf?bar", GIT_PATH_REJECT_NT_CHARS)); + cl_assert_equal_b(false, git_path_isvalid(NULL, "asdf*bar", GIT_PATH_REJECT_NT_CHARS)); +} diff --git a/tests/path/win32.c b/tests/path/win32.c index ef0b5d2f2..22742f82d 100644 --- a/tests/path/win32.c +++ b/tests/path/win32.c @@ -188,3 +188,27 @@ void test_path_win32__canonicalize(void) test_canonicalize(L"\\\\server\\..\\..\\share\\.\\foo", L"\\\\server\\share\\foo"); #endif } + +void test_path_win32__8dot3_name(void) +{ +#ifdef GIT_WIN32 + char *shortname; + + /* Some guaranteed short names */ + cl_assert_equal_s("PROGRA~1", (shortname = git_win32_path_8dot3_name("C:\\Program Files"))); + git__free(shortname); + + cl_assert_equal_s("WINDOWS", (shortname = git_win32_path_8dot3_name("C:\\WINDOWS"))); + git__free(shortname); + + /* Create some predictible short names */ + cl_must_pass(p_mkdir(".foo", 0777)); + cl_assert_equal_s("FOO~1", (shortname = git_win32_path_8dot3_name(".foo"))); + git__free(shortname); + + cl_git_write2file("bar~1", "foobar\n", 7, O_RDWR|O_CREAT, 0666); + cl_must_pass(p_mkdir(".bar", 0777)); + cl_assert_equal_s("BAR~2", (shortname = git_win32_path_8dot3_name(".bar"))); + git__free(shortname); +#endif +} |