aboutsummaryrefslogtreecommitdiff
path: root/src/path/filepath/path_windows_test.go
diff options
context:
space:
mode:
authorHiroshi Ioka <hirochachacha@gmail.com>2016-08-19 09:37:19 +0900
committerBrad Fitzpatrick <bradfitz@golang.org>2016-08-30 20:01:49 +0000
commit7722d0f90383750784377bb395a8c799868bbab8 (patch)
tree4ee9e8e7e44a929a3fe8f366a3555bf724e09ab8 /src/path/filepath/path_windows_test.go
parent1319a0ffc79e6a3f278ce39bee90bf6823c647be (diff)
downloadgo-7722d0f90383750784377bb395a8c799868bbab8.tar.xz
path/filepath: handle ".." in normalizing a path on Windows
Current code assumes there are not ".." in the Clean(path). That's not true. Clean doesn't handle leading "..", so we need to stop normalization if we see "..". Fixes #16793 Change-Id: I0a7901bedac17f1210b134d593ebd9f5e8483775 Reviewed-on: https://go-review.googlesource.com/27410 Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Alex Brainman <alex.brainman@gmail.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/path/filepath/path_windows_test.go')
-rw-r--r--src/path/filepath/path_windows_test.go101
1 files changed, 99 insertions, 2 deletions
diff --git a/src/path/filepath/path_windows_test.go b/src/path/filepath/path_windows_test.go
index 180c2e90af..2be200cb7d 100644
--- a/src/path/filepath/path_windows_test.go
+++ b/src/path/filepath/path_windows_test.go
@@ -309,9 +309,106 @@ func TestToNorm(t *testing.T) {
for _, test := range tests {
got, err := filepath.ToNorm(test.arg, stubBase)
if err != nil {
- t.Errorf("unexpected toNorm error, arg: %s, err: %v\n", test.arg, err)
+ t.Errorf("toNorm(%s) failed: %v\n", test.arg, err)
} else if got != test.want {
- t.Errorf("toNorm error, arg: %s, want: %s, got: %s\n", test.arg, test.want, got)
+ t.Errorf("toNorm(%s) returns %s, but %s expected\n", test.arg, got, test.want)
+ }
+ }
+
+ testPath := `{{tmp}}\test\foo\bar`
+
+ testsDir := []struct {
+ wd string
+ arg string
+ want string
+ }{
+ // test absolute paths
+ {".", `{{tmp}}\test\foo\bar`, `{{tmp}}\test\foo\bar`},
+ {".", `{{tmp}}\.\test/foo\bar`, `{{tmp}}\test\foo\bar`},
+ {".", `{{tmp}}\test\..\test\foo\bar`, `{{tmp}}\test\foo\bar`},
+ {".", `{{tmp}}\TEST\FOO\BAR`, `{{tmp}}\test\foo\bar`},
+
+ // test relative paths begin with drive letter
+ {`{{tmp}}\test`, `{{tmpvol}}.`, `{{tmpvol}}.`},
+ {`{{tmp}}\test`, `{{tmpvol}}..`, `{{tmpvol}}..`},
+ {`{{tmp}}\test`, `{{tmpvol}}foo\bar`, `{{tmpvol}}foo\bar`},
+ {`{{tmp}}\test`, `{{tmpvol}}.\foo\bar`, `{{tmpvol}}foo\bar`},
+ {`{{tmp}}\test`, `{{tmpvol}}foo\..\foo\bar`, `{{tmpvol}}foo\bar`},
+ {`{{tmp}}\test`, `{{tmpvol}}FOO\BAR`, `{{tmpvol}}foo\bar`},
+
+ // test relative paths begin with '\'
+ {".", `{{tmpnovol}}\test\foo\bar`, `{{tmpnovol}}\test\foo\bar`},
+ {".", `{{tmpnovol}}\.\test\foo\bar`, `{{tmpnovol}}\test\foo\bar`},
+ {".", `{{tmpnovol}}\test\..\test\foo\bar`, `{{tmpnovol}}\test\foo\bar`},
+ {".", `{{tmpnovol}}\TEST\FOO\BAR`, `{{tmpnovol}}\test\foo\bar`},
+
+ // test relative paths begin without '\'
+ {`{{tmp}}\test`, ".", `.`},
+ {`{{tmp}}\test`, "..", `..`},
+ {`{{tmp}}\test`, `foo\bar`, `foo\bar`},
+ {`{{tmp}}\test`, `.\foo\bar`, `foo\bar`},
+ {`{{tmp}}\test`, `foo\..\foo\bar`, `foo\bar`},
+ {`{{tmp}}\test`, `FOO\BAR`, `foo\bar`},
+ }
+
+ cwd, err := os.Getwd()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ defer func() {
+ err := os.Chdir(cwd)
+ if err != nil {
+ t.Fatal(err)
+ }
+ }()
+
+ tmp, err := ioutil.TempDir("", "testToNorm")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(tmp)
+
+ // ioutil.TempDir might return "non-canonical" name.
+ tmp, err = filepath.EvalSymlinks(tmp)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = os.MkdirAll(strings.Replace(testPath, "{{tmp}}", tmp, -1), 0777)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ tmpVol := filepath.VolumeName(tmp)
+ tmpNoVol := tmp[len(tmpVol):]
+
+ for _, test := range testsDir {
+ wd := strings.Replace(strings.Replace(strings.Replace(test.wd, "{{tmp}}", tmp, -1), "{{tmpvol}}", tmpVol, -1), "{{tmpnovol}}", tmpNoVol, -1)
+ arg := strings.Replace(strings.Replace(strings.Replace(test.arg, "{{tmp}}", tmp, -1), "{{tmpvol}}", tmpVol, -1), "{{tmpnovol}}", tmpNoVol, -1)
+ want := strings.Replace(strings.Replace(strings.Replace(test.want, "{{tmp}}", tmp, -1), "{{tmpvol}}", tmpVol, -1), "{{tmpnovol}}", tmpNoVol, -1)
+
+ if test.wd == "." {
+ err := os.Chdir(cwd)
+ if err != nil {
+ t.Error(err)
+
+ continue
+ }
+ } else {
+ err := os.Chdir(wd)
+ if err != nil {
+ t.Error(err)
+
+ continue
+ }
+ }
+
+ got, err := filepath.ToNorm(arg, filepath.NormBase)
+ if err != nil {
+ t.Errorf("toNorm(%s) failed: %v (wd=%s)\n", arg, err, wd)
+ } else if got != want {
+ t.Errorf("toNorm(%s) returns %s, but %s expected (wd=%s)\n", arg, got, want, wd)
}
}
}