1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// 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 (
"fmt"
"internal/syscall/windows"
"internal/testenv"
"os"
"path/filepath"
"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()
}
}
func testMkdirAllAtRoot(t *testing.T, root string) {
// Create a unique-enough directory name in root.
base := fmt.Sprintf("%s-%d", t.Name(), os.Getpid())
path := filepath.Join(root, base)
if err := os.MkdirAll(path, 0777); err != nil {
t.Fatalf("MkdirAll(%q) failed: %v", path, err)
}
// Clean up
if err := os.RemoveAll(path); err != nil {
t.Fatal(err)
}
}
func TestMkdirAllExtendedLengthAtRoot(t *testing.T) {
if testenv.Builder() == "" {
t.Skipf("skipping non-hermetic test outside of Go builders")
}
const prefix = `\\?\`
vol := filepath.VolumeName(t.TempDir()) + `\`
if len(vol) < 4 || vol[:4] != prefix {
vol = prefix + vol
}
testMkdirAllAtRoot(t, vol)
}
func TestMkdirAllVolumeNameAtRoot(t *testing.T) {
if testenv.Builder() == "" {
t.Skipf("skipping non-hermetic test outside of Go builders")
}
vol, err := syscall.UTF16PtrFromString(filepath.VolumeName(t.TempDir()) + `\`)
if err != nil {
t.Fatal(err)
}
const maxVolNameLen = 50
var buf [maxVolNameLen]uint16
err = windows.GetVolumeNameForVolumeMountPoint(vol, &buf[0], maxVolNameLen)
if err != nil {
t.Fatal(err)
}
volName := syscall.UTF16ToString(buf[:])
testMkdirAllAtRoot(t, volName)
}
|