1// Copyright 2020 CUE Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Copyright 2013 The Go Authors. All rights reserved.
16// Use of this source code is governed by a BSD-style
17// license that can be found in the LICENSE file.
18
19package path
20
21import (
22 "flag"
23 "fmt"
24 goos "os"
25 "os/exec"
26 "reflect"
27 "strings"
28 "testing"
29)
30
31func 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
42func 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 // on some systems, SystemRoot is required for cmd to work
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 // unshadow cmdfile in next directory
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// checkVolume8dot3Setting runs "fsutil 8dot3name query c:" command
110// (where c: is vol parameter) to discover "8dot3 name creation state".
111// The state is combination of 2 flags. The global flag controls if it
112// is per volume or global setting:
113//
114// 0 - Enable 8dot3 name creation on all volumes on the system
115// 1 - Disable 8dot3 name creation on all volumes on the system
116// 2 - Set 8dot3 name creation on a per volume basis
117// 3 - Disable 8dot3 name creation on all volumes except the system volume
118//
119// If global flag is set to 2, then per-volume flag needs to be examined:
120//
121// 0 - Enable 8dot3 name creation on this volume
122// 1 - Disable 8dot3 name creation on this volume
123//
124// checkVolume8dot3Setting verifies that "8dot3 name creation" flags
125// are set to 2 and 0, if enabled parameter is true, or 2 and 1, if enabled
126// is false. Otherwise checkVolume8dot3Setting returns error.
127func checkVolume8dot3Setting(vol string, enabled bool) error {
128 // It appears, on some systems "fsutil 8dot3name query ..." command always
129 // exits with error. Ignore exit code, and look at fsutil output instead.
130 out, _ := exec.Command("fsutil", "8dot3name", "query", vol).CombinedOutput()
131 // Check that system has "Volume level setting" set.
132 expected := "The registry state of NtfsDisable8dot3NameCreation is 2, the default (Volume level setting)"
133 if !strings.Contains(string(out), expected) {
134 // Windows 10 version of fsutil has different output message.
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 // Now check the volume setting.
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
153func 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 // It appears, on some systems "fsutil 8dot3name set ..." command always
161 // exits with error. Ignore exit code, and look at fsutil output instead.
162 out, _ := exec.Command(cmd[0], cmd[1:]...).CombinedOutput()
163 if string(out) != "\r\nSuccessfully set 8dot3name behavior.\r\n" {
164 // Windows 10 version of fsutil has different output message.
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
178var runFSModifyTests = flag.Bool("run_fs_modify_tests", false, "run tests which modify filesystem parameters")