summaryrefslogtreecommitdiffstats
path: root/src/os/path_windows_test.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:19:13 +0000
commitccd992355df7192993c666236047820244914598 (patch)
treef00fea65147227b7743083c6148396f74cd66935 /src/os/path_windows_test.go
parentInitial commit. (diff)
downloadgolang-1.21-ccd992355df7192993c666236047820244914598.tar.xz
golang-1.21-ccd992355df7192993c666236047820244914598.zip
Adding upstream version 1.21.8.upstream/1.21.8
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/os/path_windows_test.go')
-rw-r--r--src/os/path_windows_test.go108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/os/path_windows_test.go b/src/os/path_windows_test.go
new file mode 100644
index 0000000..2506b4f
--- /dev/null
+++ b/src/os/path_windows_test.go
@@ -0,0 +1,108 @@
+// Copyright 2016 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package os_test
+
+import (
+ "os"
+ "strings"
+ "syscall"
+ "testing"
+)
+
+func TestFixLongPath(t *testing.T) {
+ if os.CanUseLongPaths {
+ return
+ }
+ t.Parallel()
+
+ // 248 is long enough to trigger the longer-than-248 checks in
+ // fixLongPath, but short enough not to make a path component
+ // longer than 255, which is illegal on Windows. (which
+ // doesn't really matter anyway, since this is purely a string
+ // function we're testing, and it's not actually being used to
+ // do a system call)
+ veryLong := "l" + strings.Repeat("o", 248) + "ng"
+ for _, test := range []struct{ in, want string }{
+ // Short; unchanged:
+ {`C:\short.txt`, `C:\short.txt`},
+ {`C:\`, `C:\`},
+ {`C:`, `C:`},
+ // The "long" substring is replaced by a looooooong
+ // string which triggers the rewriting. Except in the
+ // cases below where it doesn't.
+ {`C:\long\foo.txt`, `\\?\C:\long\foo.txt`},
+ {`C:/long/foo.txt`, `\\?\C:\long\foo.txt`},
+ {`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz`},
+ {`\\unc\path`, `\\unc\path`},
+ {`long.txt`, `long.txt`},
+ {`C:long.txt`, `C:long.txt`},
+ {`c:\long\..\bar\baz`, `c:\long\..\bar\baz`},
+ {`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`},
+ {`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`},
+ } {
+ in := strings.ReplaceAll(test.in, "long", veryLong)
+ want := strings.ReplaceAll(test.want, "long", veryLong)
+ if got := os.FixLongPath(in); got != want {
+ got = strings.ReplaceAll(got, veryLong, "long")
+ t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want)
+ }
+ }
+}
+
+func TestMkdirAllLongPath(t *testing.T) {
+ t.Parallel()
+
+ tmpDir := t.TempDir()
+ path := tmpDir
+ for i := 0; i < 100; i++ {
+ path += `\another-path-component`
+ }
+ if err := os.MkdirAll(path, 0777); err != nil {
+ t.Fatalf("MkdirAll(%q) failed; %v", path, err)
+ }
+ if err := os.RemoveAll(tmpDir); err != nil {
+ t.Fatalf("RemoveAll(%q) failed; %v", tmpDir, err)
+ }
+}
+
+func TestMkdirAllExtendedLength(t *testing.T) {
+ t.Parallel()
+ tmpDir := t.TempDir()
+
+ const prefix = `\\?\`
+ if len(tmpDir) < 4 || tmpDir[:4] != prefix {
+ fullPath, err := syscall.FullPath(tmpDir)
+ if err != nil {
+ t.Fatalf("FullPath(%q) fails: %v", tmpDir, err)
+ }
+ tmpDir = prefix + fullPath
+ }
+ path := tmpDir + `\dir\`
+ if err := os.MkdirAll(path, 0777); err != nil {
+ t.Fatalf("MkdirAll(%q) failed: %v", path, err)
+ }
+
+ path = path + `.\dir2`
+ if err := os.MkdirAll(path, 0777); err == nil {
+ t.Fatalf("MkdirAll(%q) should have failed, but did not", path)
+ }
+}
+
+func TestOpenRootSlash(t *testing.T) {
+ t.Parallel()
+
+ tests := []string{
+ `/`,
+ `\`,
+ }
+
+ for _, test := range tests {
+ dir, err := os.Open(test)
+ if err != nil {
+ t.Fatalf("Open(%q) failed: %v", test, err)
+ }
+ dir.Close()
+ }
+}