]>
Commit | Line | Data |
---|---|---|
c2047754 ILT |
1 | // Copyright 2016 The Go Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style | |
3 | // license that can be found in the LICENSE file. | |
4 | ||
5 | package os_test | |
6 | ||
7 | import ( | |
dd931d9b | 8 | "io/ioutil" |
c2047754 ILT |
9 | "os" |
10 | "strings" | |
dd931d9b | 11 | "syscall" |
c2047754 ILT |
12 | "testing" |
13 | ) | |
14 | ||
15 | func TestFixLongPath(t *testing.T) { | |
16 | // 248 is long enough to trigger the longer-than-248 checks in | |
17 | // fixLongPath, but short enough not to make a path component | |
18 | // longer than 255, which is illegal on Windows. (which | |
19 | // doesn't really matter anyway, since this is purely a string | |
20 | // function we're testing, and it's not actually being used to | |
21 | // do a system call) | |
22 | veryLong := "l" + strings.Repeat("o", 248) + "ng" | |
23 | for _, test := range []struct{ in, want string }{ | |
24 | // Short; unchanged: | |
25 | {`C:\short.txt`, `C:\short.txt`}, | |
26 | {`C:\`, `C:\`}, | |
27 | {`C:`, `C:`}, | |
28 | // The "long" substring is replaced by a looooooong | |
29 | // string which triggers the rewriting. Except in the | |
30 | // cases below where it doesn't. | |
31 | {`C:\long\foo.txt`, `\\?\C:\long\foo.txt`}, | |
32 | {`C:/long/foo.txt`, `\\?\C:\long\foo.txt`}, | |
33 | {`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz`}, | |
34 | {`\\unc\path`, `\\unc\path`}, | |
35 | {`long.txt`, `long.txt`}, | |
36 | {`C:long.txt`, `C:long.txt`}, | |
37 | {`c:\long\..\bar\baz`, `c:\long\..\bar\baz`}, | |
38 | {`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`}, | |
39 | {`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`}, | |
40 | } { | |
4f4a855d ILT |
41 | in := strings.ReplaceAll(test.in, "long", veryLong) |
42 | want := strings.ReplaceAll(test.want, "long", veryLong) | |
c2047754 | 43 | if got := os.FixLongPath(in); got != want { |
4f4a855d | 44 | got = strings.ReplaceAll(got, veryLong, "long") |
c2047754 ILT |
45 | t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want) |
46 | } | |
47 | } | |
48 | } | |
dd931d9b ILT |
49 | |
50 | func TestMkdirAllExtendedLength(t *testing.T) { | |
51 | tmpDir, err := ioutil.TempDir("", "TestMkdirAllExtendedLength") | |
52 | if err != nil { | |
53 | t.Fatal(err) | |
54 | } | |
55 | defer os.RemoveAll(tmpDir) | |
56 | ||
57 | const prefix = `\\?\` | |
58 | if len(tmpDir) < 4 || tmpDir[:4] != prefix { | |
59 | fullPath, err := syscall.FullPath(tmpDir) | |
60 | if err != nil { | |
61 | t.Fatalf("FullPath(%q) fails: %v", tmpDir, err) | |
62 | } | |
63 | tmpDir = prefix + fullPath | |
64 | } | |
65 | path := tmpDir + `\dir\` | |
66 | err = os.MkdirAll(path, 0777) | |
67 | if err != nil { | |
68 | t.Fatalf("MkdirAll(%q) failed: %v", path, err) | |
69 | } | |
70 | ||
71 | path = path + `.\dir2` | |
72 | err = os.MkdirAll(path, 0777) | |
73 | if err == nil { | |
74 | t.Fatalf("MkdirAll(%q) should have failed, but did not", path) | |
75 | } | |
76 | } | |
5a8ea165 ILT |
77 | |
78 | func TestOpenRootSlash(t *testing.T) { | |
79 | tests := []string{ | |
80 | `/`, | |
81 | `\`, | |
82 | } | |
83 | ||
84 | for _, test := range tests { | |
85 | dir, err := os.Open(test) | |
86 | if err != nil { | |
87 | t.Fatalf("Open(%q) failed: %v", test, err) | |
88 | } | |
89 | dir.Close() | |
90 | } | |
91 | } |