-
Notifications
You must be signed in to change notification settings - Fork 17.5k
/
path_windows_test.go
415 lines (374 loc) · 11.6 KB
/
path_windows_test.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright 2013 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 filepath_test
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
)
func TestWinSplitListTestsAreValid(t *testing.T) {
comspec := os.Getenv("ComSpec")
if comspec == "" {
t.Fatal("%ComSpec% must be set")
}
for ti, tt := range winsplitlisttests {
testWinSplitListTestIsValid(t, ti, tt, comspec)
}
}
func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
comspec string) {
const (
cmdfile = `printdir.cmd`
perm os.FileMode = 0700
)
tmp, err := ioutil.TempDir("", "testWinSplitListTestIsValid")
if err != nil {
t.Fatalf("TempDir failed: %v", err)
}
defer os.RemoveAll(tmp)
for i, d := range tt.result {
if d == "" {
continue
}
if cd := filepath.Clean(d); filepath.VolumeName(cd) != "" ||
cd[0] == '\\' || cd == ".." || (len(cd) >= 3 && cd[0:3] == `..\`) {
t.Errorf("%d,%d: %#q refers outside working directory", ti, i, d)
return
}
dd := filepath.Join(tmp, d)
if _, err := os.Stat(dd); err == nil {
t.Errorf("%d,%d: %#q already exists", ti, i, d)
return
}
if err = os.MkdirAll(dd, perm); err != nil {
t.Errorf("%d,%d: MkdirAll(%#q) failed: %v", ti, i, dd, err)
return
}
fn, data := filepath.Join(dd, cmdfile), []byte("@echo "+d+"\r\n")
if err = ioutil.WriteFile(fn, data, perm); err != nil {
t.Errorf("%d,%d: WriteFile(%#q) failed: %v", ti, i, fn, err)
return
}
}
for i, d := range tt.result {
if d == "" {
continue
}
exp := []byte(d + "\r\n")
cmd := &exec.Cmd{
Path: comspec,
Args: []string{`/c`, cmdfile},
Env: []string{`Path=` + tt.list},
Dir: tmp,
}
out, err := cmd.CombinedOutput()
switch {
case err != nil:
t.Errorf("%d,%d: execution error %v\n%q", ti, i, err, out)
return
case !reflect.DeepEqual(out, exp):
t.Errorf("%d,%d: expected %#q, got %#q", ti, i, exp, out)
return
default:
// unshadow cmdfile in next directory
err = os.Remove(filepath.Join(tmp, d, cmdfile))
if err != nil {
t.Fatalf("Remove test command failed: %v", err)
}
}
}
}
// TestEvalSymlinksCanonicalNames verify that EvalSymlinks
// returns "canonical" path names on windows.
func TestEvalSymlinksCanonicalNames(t *testing.T) {
tmp, err := ioutil.TempDir("", "evalsymlinkcanonical")
if err != nil {
t.Fatal("creating temp dir:", err)
}
defer os.RemoveAll(tmp)
// ioutil.TempDir might return "non-canonical" name.
cTmpName, err := filepath.EvalSymlinks(tmp)
if err != nil {
t.Errorf("EvalSymlinks(%q) error: %v", tmp, err)
}
dirs := []string{
"test",
"test/dir",
"testing_long_dir",
"TEST2",
}
for _, d := range dirs {
dir := filepath.Join(cTmpName, d)
err := os.Mkdir(dir, 0755)
if err != nil {
t.Fatal(err)
}
cname, err := filepath.EvalSymlinks(dir)
if err != nil {
t.Errorf("EvalSymlinks(%q) error: %v", dir, err)
continue
}
if dir != cname {
t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", dir, cname, dir)
continue
}
// test non-canonical names
test := strings.ToUpper(dir)
p, err := filepath.EvalSymlinks(test)
if err != nil {
t.Errorf("EvalSymlinks(%q) error: %v", test, err)
continue
}
if p != cname {
t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", test, p, cname)
continue
}
// another test
test = strings.ToLower(dir)
p, err = filepath.EvalSymlinks(test)
if err != nil {
t.Errorf("EvalSymlinks(%q) error: %v", test, err)
continue
}
if p != cname {
t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", test, p, cname)
continue
}
}
}
// checkVolume8dot3Setting runs "fsutil 8dot3name query c:" command
// (where c: is vol parameter) to discover "8dot3 name creation state".
// The state is combination of 2 flags. The global flag controls if it
// is per volume or global setting:
// 0 - Enable 8dot3 name creation on all volumes on the system
// 1 - Disable 8dot3 name creation on all volumes on the system
// 2 - Set 8dot3 name creation on a per volume basis
// 3 - Disable 8dot3 name creation on all volumes except the system volume
// If global flag is set to 2, then per-volume flag needs to be examined:
// 0 - Enable 8dot3 name creation on this volume
// 1 - Disable 8dot3 name creation on this volume
// checkVolume8dot3Setting verifies that "8dot3 name creation" flags
// are set to 2 and 0, if enabled parameter is true, or 2 and 1, if enabled
// is false. Otherwise checkVolume8dot3Setting returns error.
func checkVolume8dot3Setting(vol string, enabled bool) error {
// It appears, on some systems "fsutil 8dot3name query ..." command always
// exits with error. Ignore exit code, and look at fsutil output instead.
out, _ := exec.Command("fsutil", "8dot3name", "query", vol).CombinedOutput()
// Check that system has "Volume level setting" set.
expected := "The registry state of NtfsDisable8dot3NameCreation is 2, the default (Volume level setting)"
if !strings.Contains(string(out), expected) {
// Windows 10 version of fsutil has different output message.
expectedWindow10 := "The registry state is: 2 (Per volume setting - the default)"
if !strings.Contains(string(out), expectedWindow10) {
return fmt.Errorf("fsutil output should contain %q, but is %q", expected, string(out))
}
}
// Now check the volume setting.
expected = "Based on the above two settings, 8dot3 name creation is %s on %s"
if enabled {
expected = fmt.Sprintf(expected, "enabled", vol)
} else {
expected = fmt.Sprintf(expected, "disabled", vol)
}
if !strings.Contains(string(out), expected) {
return fmt.Errorf("unexpected fsutil output: %q", string(out))
}
return nil
}
func setVolume8dot3Setting(vol string, enabled bool) error {
cmd := []string{"fsutil", "8dot3name", "set", vol}
if enabled {
cmd = append(cmd, "0")
} else {
cmd = append(cmd, "1")
}
// It appears, on some systems "fsutil 8dot3name set ..." command always
// exits with error. Ignore exit code, and look at fsutil output instead.
out, _ := exec.Command(cmd[0], cmd[1:]...).CombinedOutput()
if string(out) != "\r\nSuccessfully set 8dot3name behavior.\r\n" {
// Windows 10 version of fsutil has different output message.
expectedWindow10 := "Successfully %s 8dot3name generation on %s\r\n"
if enabled {
expectedWindow10 = fmt.Sprintf(expectedWindow10, "enabled", vol)
} else {
expectedWindow10 = fmt.Sprintf(expectedWindow10, "disabled", vol)
}
if string(out) != expectedWindow10 {
return fmt.Errorf("%v command failed: %q", cmd, string(out))
}
}
return nil
}
var runFSModifyTests = flag.Bool("run_fs_modify_tests", false, "run tests which modify filesystem parameters")
// This test assumes registry state of NtfsDisable8dot3NameCreation is 2,
// the default (Volume level setting).
func TestEvalSymlinksCanonicalNamesWith8dot3Disabled(t *testing.T) {
if !*runFSModifyTests {
t.Skip("skipping test that modifies file system setting; enable with -run_fs_modify_tests")
}
tempVol := filepath.VolumeName(os.TempDir())
if len(tempVol) != 2 {
t.Fatalf("unexpected temp volume name %q", tempVol)
}
err := checkVolume8dot3Setting(tempVol, true)
if err != nil {
t.Fatal(err)
}
err = setVolume8dot3Setting(tempVol, false)
if err != nil {
t.Fatal(err)
}
defer func() {
err := setVolume8dot3Setting(tempVol, true)
if err != nil {
t.Fatal(err)
}
err = checkVolume8dot3Setting(tempVol, true)
if err != nil {
t.Fatal(err)
}
}()
err = checkVolume8dot3Setting(tempVol, false)
if err != nil {
t.Fatal(err)
}
TestEvalSymlinksCanonicalNames(t)
}
func TestToNorm(t *testing.T) {
stubBase := func(path string) (string, error) {
vol := filepath.VolumeName(path)
path = path[len(vol):]
if strings.Contains(path, "/") {
return "", fmt.Errorf("invalid path is given to base: %s", vol+path)
}
if path == "" || path == "." || path == `\` {
return "", fmt.Errorf("invalid path is given to base: %s", vol+path)
}
i := strings.LastIndexByte(path, filepath.Separator)
if i == len(path)-1 { // trailing '\' is invalid
return "", fmt.Errorf("invalid path is given to base: %s", vol+path)
}
if i == -1 {
return strings.ToUpper(path), nil
}
return strings.ToUpper(path[i+1:]), nil
}
// On this test, toNorm should be same as string.ToUpper(filepath.Clean(path)) except empty string.
tests := []struct {
arg string
want string
}{
{"", ""},
{".", "."},
{"./foo/bar", `FOO\BAR`},
{"/", `\`},
{"/foo/bar", `\FOO\BAR`},
{"/foo/bar/baz/qux", `\FOO\BAR\BAZ\QUX`},
{"foo/bar", `FOO\BAR`},
{"C:/foo/bar", `C:\FOO\BAR`},
{"C:foo/bar", `C:FOO\BAR`},
{"c:/foo/bar", `C:\FOO\BAR`},
{"C:/foo/bar", `C:\FOO\BAR`},
{"C:/foo/bar/", `C:\FOO\BAR`},
{`C:\foo\bar`, `C:\FOO\BAR`},
{`C:\foo/bar\`, `C:\FOO\BAR`},
{"C:/ふー/バー", `C:\ふー\バー`},
}
for _, test := range tests {
got, err := filepath.ToNorm(test.arg, stubBase)
if err != nil {
t.Errorf("toNorm(%s) failed: %v\n", test.arg, err)
} else if got != test.want {
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 '\'
{"{{tmp}}", `{{tmpnovol}}\test\foo\bar`, `{{tmpnovol}}\test\foo\bar`},
{"{{tmp}}", `{{tmpnovol}}\.\test\foo\bar`, `{{tmpnovol}}\test\foo\bar`},
{"{{tmp}}", `{{tmpnovol}}\test\..\test\foo\bar`, `{{tmpnovol}}\test\foo\bar`},
{"{{tmp}}", `{{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):]
replacer := strings.NewReplacer("{{tmp}}", tmp, "{{tmpvol}}", tmpVol, "{{tmpnovol}}", tmpNoVol)
for _, test := range testsDir {
wd := replacer.Replace(test.wd)
arg := replacer.Replace(test.arg)
want := replacer.Replace(test.want)
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)
}
}
}