1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package path
20
21 import (
22 "flag"
23 "fmt"
24 goos "os"
25 "os/exec"
26 "reflect"
27 "strings"
28 "testing"
29 )
30
31 func TestWinSplitListTestsAreValid(t *testing.T) {
32 comspec := goos.Getenv("ComSpec")
33 if comspec == "" {
34 t.Fatal("%ComSpec% must be set")
35 }
36
37 for ti, tt := range winsplitlisttests {
38 testWinSplitListTestIsValid(t, ti, tt, comspec)
39 }
40 }
41
42 func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
43 comspec string) {
44
45 const (
46 cmdfile = `printdir.cmd`
47 perm = 0700
48 )
49
50 tmp := t.TempDir()
51
52 for i, d := range tt.result {
53 if d == "" {
54 continue
55 }
56 if cd := Clean(d, Windows); VolumeName(cd, Windows) != "" ||
57 cd[0] == '\\' || cd == ".." || (len(cd) >= 3 && cd[0:3] == `..\`) {
58 t.Errorf("%d,%d: %#q refers outside working directory", ti, i, d)
59 return
60 }
61 dd := Join([]string{tmp, d}, Windows)
62 if _, err := goos.Stat(dd); err == nil {
63 t.Errorf("%d,%d: %#q already exists", ti, i, d)
64 return
65 }
66 if err := goos.MkdirAll(dd, perm); err != nil {
67 t.Errorf("%d,%d: MkdirAll(%#q) failed: %v", ti, i, dd, err)
68 return
69 }
70 fn, data := Join([]string{dd, cmdfile}, Windows), []byte("@echo "+d+"\r\n")
71 if err := goos.WriteFile(fn, data, perm); err != nil {
72 t.Errorf("%d,%d: WriteFile(%#q) failed: %v", ti, i, fn, err)
73 return
74 }
75 }
76
77
78 systemRoot := goos.Getenv("SystemRoot")
79
80 for i, d := range tt.result {
81 if d == "" {
82 continue
83 }
84 exp := []byte(d + "\r\n")
85 cmd := &exec.Cmd{
86 Path: comspec,
87 Args: []string{`/c`, cmdfile},
88 Env: []string{`Path=` + systemRoot + "/System32;" + tt.list, `SystemRoot=` + systemRoot},
89 Dir: tmp,
90 }
91 out, err := cmd.CombinedOutput()
92 switch {
93 case err != nil:
94 t.Errorf("%d,%d: execution error %v\n%q", ti, i, err, out)
95 return
96 case !reflect.DeepEqual(out, exp):
97 t.Errorf("%d,%d: expected %#q, got %#q", ti, i, exp, out)
98 return
99 default:
100
101 err = goos.Remove(Join([]string{tmp, d, cmdfile}, Windows))
102 if err != nil {
103 t.Fatalf("Remove test command failed: %v", err)
104 }
105 }
106 }
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 func checkVolume8dot3Setting(vol string, enabled bool) error {
128
129
130 out, _ := exec.Command("fsutil", "8dot3name", "query", vol).CombinedOutput()
131
132 expected := "The registry state of NtfsDisable8dot3NameCreation is 2, the default (Volume level setting)"
133 if !strings.Contains(string(out), expected) {
134
135 expectedWindow10 := "The registry state is: 2 (Per volume setting - the default)"
136 if !strings.Contains(string(out), expectedWindow10) {
137 return fmt.Errorf("fsutil output should contain %q, but is %q", expected, string(out))
138 }
139 }
140
141 expected = "Based on the above two settings, 8dot3 name creation is %s on %s"
142 if enabled {
143 expected = fmt.Sprintf(expected, "enabled", vol)
144 } else {
145 expected = fmt.Sprintf(expected, "disabled", vol)
146 }
147 if !strings.Contains(string(out), expected) {
148 return fmt.Errorf("unexpected fsutil output: %q", string(out))
149 }
150 return nil
151 }
152
153 func setVolume8dot3Setting(vol string, enabled bool) error {
154 cmd := []string{"fsutil", "8dot3name", "set", vol}
155 if enabled {
156 cmd = append(cmd, "0")
157 } else {
158 cmd = append(cmd, "1")
159 }
160
161
162 out, _ := exec.Command(cmd[0], cmd[1:]...).CombinedOutput()
163 if string(out) != "\r\nSuccessfully set 8dot3name behavior.\r\n" {
164
165 expectedWindow10 := "Successfully %s 8dot3name generation on %s\r\n"
166 if enabled {
167 expectedWindow10 = fmt.Sprintf(expectedWindow10, "enabled", vol)
168 } else {
169 expectedWindow10 = fmt.Sprintf(expectedWindow10, "disabled", vol)
170 }
171 if string(out) != expectedWindow10 {
172 return fmt.Errorf("%v command failed: %q", cmd, string(out))
173 }
174 }
175 return nil
176 }
177
178 var runFSModifyTests = flag.Bool("run_fs_modify_tests", false, "run tests which modify filesystem parameters")
179
View as plain text