-
Notifications
You must be signed in to change notification settings - Fork 2
/
fs_windows_test.go
594 lines (512 loc) · 14.8 KB
/
fs_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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//go:build windows
// +build windows
package fs
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"
"testing"
"unsafe"
)
var volumeName string
// Determine if symlinks are supported on the current volume.
func init() {
const FILE_SUPPORTS_HARD_LINKS = 0x00400000
kernel32DLL := syscall.MustLoadDLL("Kernel32.dll")
procGetVolumeInformation := kernel32DLL.MustFindProc("GetVolumeInformationW")
wd, err := os.Getwd()
if err != nil {
panic(err)
}
volumeName = filepath.VolumeName(wd)
vu, err := syscall.UTF16FromString(volumeName + `\`)
if err != nil {
panic(err)
}
var (
VolumeSerialNumber uint32
MaximumComponentLength uint32
FileSystemFlags uint32
)
r1, _, err := procGetVolumeInformation.Call(
uintptr(unsafe.Pointer(&vu[0])), 0, 0,
uintptr(unsafe.Pointer(&VolumeSerialNumber)),
uintptr(unsafe.Pointer(&MaximumComponentLength)),
uintptr(unsafe.Pointer(&FileSystemFlags)), 0, 0,
)
if err != syscall.Errno(0) {
panic(fmt.Errorf("Error: getting information for volume (%s): %s", volumeName, err))
}
if r1 == 0 {
panic(errors.New("GetVolumeInformation: returned false without an error"))
}
supportsSymlinks = FileSystemFlags&FILE_SUPPORTS_HARD_LINKS != 0
}
func longPathName() string {
var buf bytes.Buffer
for i := 0; i < 2; i++ {
for i := byte('A'); i <= 'Z'; i++ {
buf.Write(bytes.Repeat([]byte{i}, 4))
buf.WriteRune(filepath.Separator)
}
}
return filepath.Clean(buf.String())
}
func tempDir(t *testing.T) string {
path, err := ioutil.TempDir("", "fs-test")
if err != nil {
t.Fatal(err)
}
return path
}
func TestMkdir(t *testing.T) {
temp := tempDir(t)
n := MAX_PATH - len(temp) - len(string(os.PathSeparator)) - 1
path := filepath.Join(temp, strings.Repeat("A", n))
err := Mkdir(path, 0755)
if err != nil {
t.Fatal(err)
}
os.Remove(`\\?\` + path)
}
func TestMkdirAll(t *testing.T) {
temp := tempDir(t)
path := filepath.Join(temp, longPathName())
err := MkdirAll(path, 0755)
if err != nil {
t.Fatalf("TestMkdirAll: %s", err)
}
defer os.RemoveAll(`\\?\` + temp)
if _, err := Stat(path); err != nil {
t.Fatalf("TestMkdirAll: Stat failed %s", err)
}
if _, err := Lstat(path); err != nil {
t.Fatalf("TestMkdirAll: Stat failed %s", err)
}
// Make sure the handling of long paths is case-insensitive
if _, err := Stat(strings.ToLower(path)); err != nil {
t.Fatalf("TestMkdirAll: Stat failed %s", err)
}
}
func TestRemoveAll(t *testing.T) {
temp := tempDir(t)
path := filepath.Join(temp, longPathName())
err := MkdirAll(path, 0755)
if err != nil {
t.Fatalf("TestRemoveAll: %s", err)
}
defer os.RemoveAll(`\\?\` + temp)
if err := RemoveAll(temp); err != nil {
t.Fatalf("TestRemoveAll: %s", err)
}
if _, err := Stat(temp); !os.IsNotExist(err) {
t.Fatalf("TestRemoveAll: failed to remove directory: %s", temp)
}
if _, err := Stat(path); !os.IsNotExist(err) {
t.Fatalf("TestRemoveAll: failed to remove directory: %s", path)
}
}
func TestRenameLong(t *testing.T) {
oldtemp := tempDir(t)
// Create temp directory so we know it's name is unique,
// then delete it - this is our rename target.
newtemp := tempDir(t)
if err := os.RemoveAll(newtemp); err != nil {
t.Fatalf("TestRenameLong: %s", err)
}
long := longPathName()
oldpath := filepath.Join(oldtemp, long)
newpath := filepath.Join(newtemp, long)
err := MkdirAll(oldpath, 0755)
if err != nil {
t.Fatalf("TestRenameLong: %s", err)
}
defer os.RemoveAll(`\\?\` + oldtemp)
if err := Rename(oldtemp, newtemp); err != nil {
t.Fatalf("TestRenameLong: %s", err)
}
defer os.RemoveAll(`\\?\` + newtemp)
if _, err := Stat(oldpath); !os.IsNotExist(err) {
t.Fatalf("TestRenameLong: failed to rename directory: %s => %s", oldtemp, newtemp)
}
if _, err := Stat(newpath); err != nil {
t.Fatalf("TestRenameLong: failed to rename directory: %s => %s", oldtemp, newtemp)
}
}
func TestSymlinkLong(t *testing.T) {
const Content = "Hello\n"
oldtemp := tempDir(t)
defer os.RemoveAll(`\\?\` + oldtemp)
// Create temp directory so we know it's name is unique,
// then delete it - this is our rename target.
newtemp := tempDir(t)
if err := os.RemoveAll(newtemp); err != nil {
t.Fatalf("TestSymlinkLong: %s", err)
}
defer os.RemoveAll(`\\?\` + newtemp) // cleanup
long := longPathName()
oldpath := filepath.Join(oldtemp, long)
oldfile := filepath.Join(oldpath, "file.txt")
newpath := filepath.Join(newtemp, long)
newfile := filepath.Join(newpath, "file.txt")
linkedfile := filepath.Join(newpath, "linked.txt")
err := MkdirAll(oldpath, 0755)
if err != nil {
t.Fatalf("TestSymlinkLong: %s", err)
}
// create temp file
{
f, err := Create(oldfile)
if err != nil {
t.Fatalf("TestSymlinkLong: %s", err)
}
_, err = f.WriteString(Content)
f.Close() // close immediately so that cleanup does not fail
if err != nil {
t.Fatalf("TestSymlinkLong: %s", err)
}
}
testFile := func(path string) error {
f, err := Open(path)
if err != nil {
return err
}
defer f.Close()
var buf bytes.Buffer
if _, err := buf.ReadFrom(f); err != nil {
return err
}
s := buf.String()
if s != Content {
return fmt.Errorf("expected content of file (%s) to be: %s got: %s",
path, Content, s)
}
return nil
}
testDir := func() error {
if _, err := Stat(newpath); err != nil {
return fmt.Errorf("failed to rename directory: %s => %s", oldtemp, newtemp)
}
if err := testFile(newfile); err != nil {
return fmt.Errorf("failed reading file: %s", err)
}
if err := testFile(linkedfile); err != nil {
return fmt.Errorf("failed reading symlinked file: %s", err)
}
return nil
}
resetDir := func() error {
if _, err := Stat(linkedfile); err == nil {
if err := Remove(linkedfile); err != nil {
return err
}
}
if _, err := Stat(newtemp); err == nil {
if err := RemoveAll(newtemp); err != nil {
return err
}
}
return nil
}
// symlink directories (shallow)
{
if err := Symlink(oldtemp, newtemp); err != nil {
t.Fatalf("TestSymlinkLong: creating symlink: %s", err)
}
// link another file into the symlinked directory
if err := Symlink(oldfile, linkedfile); err != nil {
t.Fatalf("TestSymlinkLong: creating file symlink in symlinked directory: %s", err)
}
if err := testDir(); err != nil {
t.Fatalf("TestSymlinkLong: %s", err)
}
}
// symlink directories (deep)
{
if err := resetDir(); err != nil {
t.Fatalf("TestSymlinkLong: %s", err)
}
if err := MkdirAll(filepath.Dir(newpath), 0755); err != nil {
t.Fatalf("TestSymlinkLong: %s", err)
}
if err := Symlink(oldpath, newpath); err != nil {
t.Fatalf("TestSymlinkLong: creating symlink: %s", err)
}
if err := Symlink(oldfile, linkedfile); err != nil {
t.Fatalf("TestSymlinkLong: creating file symlink in symlinked directory: %s", err)
}
if err := testDir(); err != nil {
t.Fatalf("TestSymlinkLong: %s", err)
}
}
// symlink files
{
if err := resetDir(); err != nil {
t.Fatalf("TestSymlinkLong: %s", err)
}
if err := MkdirAll(newpath, 0755); err != nil {
t.Fatalf("TestSymlinkLong: %s", err)
}
if err := Symlink(oldfile, newfile); err != nil {
t.Fatalf("TestSymlinkLong: creating symlink: %s", err)
}
if err := Symlink(oldfile, linkedfile); err != nil {
t.Fatalf("TestSymlinkLong: creating symlink: %s", err)
}
if err := testDir(); err != nil {
t.Fatalf("TestSymlinkLong: %s", err)
}
}
// symlink a file to a symlinked file
{
// don't reset the test dir
doublelink := filepath.Join(newpath, "double.txt")
if err := Symlink(linkedfile, doublelink); err != nil {
t.Fatalf("TestSymlinkLong: creating symlink: %s", err)
}
defer os.Remove(doublelink)
if err := testFile(doublelink); err != nil {
t.Fatalf("TestSymlinkLong: %s", err)
}
}
}
func TestLeadingSpace(t *testing.T) {
const filename = " Leading Space.txt"
path := filepath.Join("./testdata/", filename)
f, err := Create(path)
if err != nil {
t.Fatal(err)
}
f.Close()
defer os.Remove(path)
fi, err := Stat(path)
if err != nil {
t.Fatal(err)
}
if name := fi.Name(); name != filename {
t.Errorf("TestLeadingSpace (%s): invalid name %s", filename, name)
}
}
func TestTrailingSpace(t *testing.T) {
const filename = "Trailing Space.txt "
path := filepath.Join("./testdata/", filename)
f, err := Create(path)
if err != nil {
t.Fatal(err)
}
f.Close()
defer os.Remove(path)
fi, err := Stat(path)
if err != nil {
t.Fatal(err)
}
if name := fi.Name(); name != filename {
t.Errorf("TestTrailingSpace (%s): invalid name %s", filename, name)
}
}
func makeLongFilePath(t *testing.T) string {
// 255 chars long, the common max component length.
const a = "0123456789abcdefg"
const s = a + a + a + a + a + a + a + a + a + a + a + a + a + a + a
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
path := filepath.Join(dir, "testdata", s)
f, err := Create(path)
if err != nil {
t.Fatalf("makeLongFilePath: error creating file (%s): %s", path, err)
}
f.Close()
return path
}
func TestLongNoVolumeName(t *testing.T) {
longpath := makeLongFilePath(t)
defer os.RemoveAll(`\\?\` + longpath)
// Remove volume name => \path\to\testdata
testpath := strings.TrimPrefix(longpath, filepath.VolumeName(longpath))
// Test with leading backslash
if _, err := Stat(testpath); err != nil {
t.Fatalf("TestLeadingSlash (%s): failed to open file (%s): %s", longpath, testpath, err)
}
// Test with forward slashes
unixpath := strings.Replace(testpath, `\`, `/`, -1)
if _, err := Stat(unixpath); err != nil {
t.Fatalf("TestLeadingSlash (%s): failed to open file with forward slashes (%s): %s", longpath, unixpath, err)
}
// Test with mixed slashes
mixedpath := make([]rune, 0, len(testpath))
n := 0
for _, r := range testpath {
if r == '\\' {
if n&1 != 0 {
r = '/'
}
n++
}
mixedpath = append(mixedpath, r)
}
if _, err := Stat(string(mixedpath)); err != nil {
t.Fatalf("TestLeadingSlash (%s): failed to open file with mixed slashes (%s): %s", longpath, string(mixedpath), err)
}
}
func TestLongUnixPath(t *testing.T) {
longpath := makeLongFilePath(t)
defer os.RemoveAll(`\\?\` + longpath)
// Remove volume name => \path\to\testdata
testpath := strings.TrimPrefix(longpath, filepath.VolumeName(longpath))
// Test with forward (unix) slashes => /path/to/testdata
unixpath := strings.Replace(testpath, `\`, `/`, -1)
if _, err := Stat(unixpath); err != nil {
t.Fatalf("TestLongUnixPath (%s): failed to open file with forward slashes (%s): %s", longpath, unixpath, err)
}
// Test with mixed slashes => \path/to\testdata
mixedpath := make([]rune, 0, len(testpath))
n := 0
for _, r := range testpath {
if r == '\\' {
if n&1 != 0 {
r = '/'
}
n++
}
mixedpath = append(mixedpath, r)
}
if _, err := Stat(string(mixedpath)); err != nil {
t.Fatalf("TestLongUnixPath (%s): failed to open file with mixed slashes (%s): %s", longpath, string(mixedpath), err)
}
}
func TestLongRelativePaths(t *testing.T) {
longpath := makeLongFilePath(t)
defer os.RemoveAll(`\\?\` + longpath)
// Remove volume name => \path\to\testdata
testpath := strings.TrimPrefix(longpath, filepath.VolumeName(longpath))
wd, err := os.Getwd()
if err != nil {
t.Error(err)
}
dirname := filepath.Base(wd)
// Test relative path
base := filepath.Base(testpath)
relativePaths := []string{
"testdata/" + base,
"./testdata/" + base,
"./testdata/../testdata/" + base,
"../" + dirname + "/testdata/" + base,
"../" + dirname + "/testdata/../testdata/" + base,
}
for _, relative := range relativePaths {
if _, err := Stat(relative); err != nil {
t.Fatalf("Relative path (%s): failed to open relative path (%s): %s",
strings.TrimSuffix(relative, base), relative, err)
}
}
}
func TestLongTrailingSpace(t *testing.T) {
longpath := makeLongFilePath(t)
defer os.RemoveAll(`\\?\` + longpath)
// test that trailing spaces are trimmed from long paths
testpath := longpath + " "
if _, err := Stat(testpath); err != nil {
t.Fatalf("TestTrailingSpaceLongPath (%s): failed to open file (%s): %s", longpath, testpath, err)
}
// relative path with a trailing space
relative := "./testdata/" + filepath.Base(longpath) + " "
if _, err := Stat(relative); err != nil {
t.Fatalf("TestTrailingSpaceLongPath (%s): failed to open relative path (%s): %s", longpath, relative, err)
}
}
type pathTest struct {
Path, Exp string
}
func (a pathTest) String() string {
return fmt.Sprintf("{Path: %q Exp: %q}", a.Path, a.Exp)
}
func TestAbsPath(t *testing.T) {
var tests = []pathTest{
{`\a\\b\\c `, volumeName + `\a\b\c`},
{`\\a\\b\\c `, volumeName + `\a\b\c`},
{`\a\\b\\c `, volumeName + `\a\b\c`},
{`\a\\b\\c`, volumeName + `\a\b\c`},
{`\a\x\..\b`, volumeName + `\a\b`},
{`\a\b\..\b\c`, volumeName + `\a\b\c`},
{volumeName + `\a\\b\\c`, volumeName + `\a\b\c`},
{volumeName + `\a\x\..\b`, volumeName + `\a\b`},
}
for _, x := range tests {
p, err := absPath(x.Path)
if err != nil {
t.Fatal(err)
}
if p != x.Exp {
t.Errorf("TestAbsPath (%+v): %q", x, p)
}
}
}
func TestWinPath(t *testing.T) {
s := "0123456789abcdef"
s = s + s + s + s + s + s + s + s + s + s + s + s + s + s
var tests = []pathTest{
// UNC paths and paths less than MAX_PATH should not be modified.
{`\\server\\b\\c`, `\\server\\b\\c`},
{`\\?\C:\\b\\c`, `\\?\C:\\b\\c`},
{`\\C:\\b\\c`, `\\C:\\b\\c`},
{`\\?\C:\` + s + `\..\` + s, `\\?\C:\` + s + `\..\` + s},
// Non UNC paths longer than MAX_PATH should be converted to long paths.
{`C:\\` + s + `\\` + s, `\\?\C:\` + s + `\` + s},
}
for _, x := range tests {
p, err := winPath(x.Path)
if err != nil {
t.Fatal(err)
}
if p != x.Exp {
t.Errorf("TestWinPath (%+v): %q", x, p)
}
}
}
// Make testing on Shared Folders easier, but do not allow the tests to pass
// unless the Soft and Hard link logic is exercised.
//
// To make testing output easier to read this should always be the last test.
//
// This MUST be the last Windows test we run.
func TestMustSupportSymlinks(t *testing.T) {
const format = `
Hard links are not supported on the current volume (%s).
Therefore, all hard and soft link tests were skipped.
To make development on VMs easier this is the last test
we run, and if this is the only error the other tests
succeeded.
The most common reason for hard and soft links not
being supported are running the tests from a network
drive or 'Shared Folder'. The easiest solution is to
run the tests from the HOMEDRIVE (%s).`
homedrive := "typically, C:"
if s := os.Getenv("HOMEDRIVE"); s != "" {
homedrive = s
}
if !supportsSymlinks {
t.Errorf(format, volumeName, homedrive)
}
}
func BenchmarkAbsPath_Relative_Short(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := absPath(`/a//b//c`); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkAbsPath_Relative_Long(b *testing.B) {
const s = `/c/Users/Administrator//go/src//github.com/charlievieth/fs/../fs/testdata`
for i := 0; i < b.N; i++ {
if _, err := absPath(s); err != nil {
b.Fatal(err)
}
}
}