github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/pkg/path/path_windows_test.go (about)

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