aboutsummaryrefslogtreecommitdiff
path: root/src/path/filepath/path_windows_test.go
diff options
context:
space:
mode:
authorDamien Neil <dneil@google.com>2022-10-19 14:41:03 -0700
committerDamien Neil <dneil@google.com>2022-11-09 22:06:00 +0000
commitbe9d78c9c5905fbc10d8cd6a4714dd4ad1c91674 (patch)
treea512b557f9b117dbfbf5ab027268e7a304131d9c /src/path/filepath/path_windows_test.go
parent575964d42c7b3001c09f2676d0ee9d520debb5eb (diff)
downloadgo-be9d78c9c5905fbc10d8cd6a4714dd4ad1c91674.tar.xz
path/filepath: detect all forms of \\ volume paths on Windows
Previously, the volumeNameLen function checked for UNC paths starting with two slashes, a non-'.' character, and another slash. This misses volume names such as "\\.\C:\". The previous check for volume names rejects paths beginning with "\\.". This is incorrect, because while these names are not UNC paths, "\\.\C:\" is a DOS device path prefix indicating the C: device. It also misses UNC path prefixes in the form "\\.\UNC\server\share\". The previous check for UNC paths also rejects any path with an empty or missing host or share component. This leads to a number of possibly-incorrect behaviors, such as Clean(`\\a`) returning `\a`. Converting the semantically-significant `\\` prefix to a single `\` seems wrong. Consistently treat paths beginning with two separators as having a volume prefix. Update VolumeName to detect DOS device paths (`\\.\` or `\\?\`), DOS device paths linking to UNC paths (`\\.\UNC\Server\Share` or `\\?\UNC\Server\Share`), and UNC paths (`\\Server\Share\`). Clean(`\\a`) = `\\a` Join(`\\`, `a`, `b`) = `\\a\b` In addition, normalize path separators in VolumeName for consistency with other functions which Clean their result. Fixes #56336 Change-Id: Id01c33029585bfffc313dcf0ad42ff6ac7ce42fd Reviewed-on: https://go-review.googlesource.com/c/go/+/444280 Run-TryBot: Damien Neil <dneil@google.com> Reviewed-by: Quim Muntal <quimmuntal@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Roland Shoemaker <roland@golang.org>
Diffstat (limited to 'src/path/filepath/path_windows_test.go')
-rw-r--r--src/path/filepath/path_windows_test.go20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/path/filepath/path_windows_test.go b/src/path/filepath/path_windows_test.go
index 9e6c0ec81d..e37dddcead 100644
--- a/src/path/filepath/path_windows_test.go
+++ b/src/path/filepath/path_windows_test.go
@@ -560,3 +560,23 @@ func TestIssue52476(t *testing.T) {
}
}
}
+
+func TestAbsWindows(t *testing.T) {
+ for _, test := range []struct {
+ path string
+ want string
+ }{
+ {`C:\foo`, `C:\foo`},
+ {`\\host\share\foo`, `\\host\share\foo`},
+ {`\\host`, `\\host`},
+ {`\\.\NUL`, `\\.\NUL`},
+ {`NUL`, `\\.\NUL`},
+ {`COM1`, `\\.\COM1`},
+ {`a/NUL`, `\\.\NUL`},
+ } {
+ got, err := filepath.Abs(test.path)
+ if err != nil || got != test.want {
+ t.Errorf("Abs(%q) = %q, %v; want %q, nil", test.path, got, err, test.want)
+ }
+ }
+}