github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/path/filepath/path_windows_test.go (about)

     1  // Copyright 2013 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 filepath_test
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"internal/testenv"
    11  	"io/ioutil"
    12  	"os"
    13  	"os/exec"
    14  	"path/filepath"
    15  	"reflect"
    16  	"runtime/debug"
    17  	"strings"
    18  	"testing"
    19  )
    20  
    21  func TestWinSplitListTestsAreValid(t *testing.T) {
    22  	comspec := os.Getenv("ComSpec")
    23  	if comspec == "" {
    24  		t.Fatal("%ComSpec% must be set")
    25  	}
    26  
    27  	for ti, tt := range winsplitlisttests {
    28  		testWinSplitListTestIsValid(t, ti, tt, comspec)
    29  	}
    30  }
    31  
    32  func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
    33  	comspec string) {
    34  
    35  	const (
    36  		cmdfile             = `printdir.cmd`
    37  		perm    os.FileMode = 0700
    38  	)
    39  
    40  	tmp, err := ioutil.TempDir("", "testWinSplitListTestIsValid")
    41  	if err != nil {
    42  		t.Fatalf("TempDir failed: %v", err)
    43  	}
    44  	defer os.RemoveAll(tmp)
    45  
    46  	for i, d := range tt.result {
    47  		if d == "" {
    48  			continue
    49  		}
    50  		if cd := filepath.Clean(d); filepath.VolumeName(cd) != "" ||
    51  			cd[0] == '\\' || cd == ".." || (len(cd) >= 3 && cd[0:3] == `..\`) {
    52  			t.Errorf("%d,%d: %#q refers outside working directory", ti, i, d)
    53  			return
    54  		}
    55  		dd := filepath.Join(tmp, d)
    56  		if _, err := os.Stat(dd); err == nil {
    57  			t.Errorf("%d,%d: %#q already exists", ti, i, d)
    58  			return
    59  		}
    60  		if err = os.MkdirAll(dd, perm); err != nil {
    61  			t.Errorf("%d,%d: MkdirAll(%#q) failed: %v", ti, i, dd, err)
    62  			return
    63  		}
    64  		fn, data := filepath.Join(dd, cmdfile), []byte("@echo "+d+"\r\n")
    65  		if err = ioutil.WriteFile(fn, data, perm); err != nil {
    66  			t.Errorf("%d,%d: WriteFile(%#q) failed: %v", ti, i, fn, err)
    67  			return
    68  		}
    69  	}
    70  
    71  	// on some systems, SystemRoot is required for cmd to work
    72  	systemRoot := os.Getenv("SystemRoot")
    73  
    74  	for i, d := range tt.result {
    75  		if d == "" {
    76  			continue
    77  		}
    78  		exp := []byte(d + "\r\n")
    79  		cmd := &exec.Cmd{
    80  			Path: comspec,
    81  			Args: []string{`/c`, cmdfile},
    82  			Env:  []string{`Path=` + tt.list, `SystemRoot=` + systemRoot},
    83  			Dir:  tmp,
    84  		}
    85  		out, err := cmd.CombinedOutput()
    86  		switch {
    87  		case err != nil:
    88  			t.Errorf("%d,%d: execution error %v\n%q", ti, i, err, out)
    89  			return
    90  		case !reflect.DeepEqual(out, exp):
    91  			t.Errorf("%d,%d: expected %#q, got %#q", ti, i, exp, out)
    92  			return
    93  		default:
    94  			// unshadow cmdfile in next directory
    95  			err = os.Remove(filepath.Join(tmp, d, cmdfile))
    96  			if err != nil {
    97  				t.Fatalf("Remove test command failed: %v", err)
    98  			}
    99  		}
   100  	}
   101  }
   102  
   103  // TestEvalSymlinksCanonicalNames verify that EvalSymlinks
   104  // returns "canonical" path names on windows.
   105  func TestEvalSymlinksCanonicalNames(t *testing.T) {
   106  	tmp, err := ioutil.TempDir("", "evalsymlinkcanonical")
   107  	if err != nil {
   108  		t.Fatal("creating temp dir:", err)
   109  	}
   110  	defer os.RemoveAll(tmp)
   111  
   112  	// ioutil.TempDir might return "non-canonical" name.
   113  	cTmpName, err := filepath.EvalSymlinks(tmp)
   114  	if err != nil {
   115  		t.Errorf("EvalSymlinks(%q) error: %v", tmp, err)
   116  	}
   117  
   118  	dirs := []string{
   119  		"test",
   120  		"test/dir",
   121  		"testing_long_dir",
   122  		"TEST2",
   123  	}
   124  
   125  	for _, d := range dirs {
   126  		dir := filepath.Join(cTmpName, d)
   127  		err := os.Mkdir(dir, 0755)
   128  		if err != nil {
   129  			t.Fatal(err)
   130  		}
   131  		cname, err := filepath.EvalSymlinks(dir)
   132  		if err != nil {
   133  			t.Errorf("EvalSymlinks(%q) error: %v", dir, err)
   134  			continue
   135  		}
   136  		if dir != cname {
   137  			t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", dir, cname, dir)
   138  			continue
   139  		}
   140  		// test non-canonical names
   141  		test := strings.ToUpper(dir)
   142  		p, err := filepath.EvalSymlinks(test)
   143  		if err != nil {
   144  			t.Errorf("EvalSymlinks(%q) error: %v", test, err)
   145  			continue
   146  		}
   147  		if p != cname {
   148  			t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", test, p, cname)
   149  			continue
   150  		}
   151  		// another test
   152  		test = strings.ToLower(dir)
   153  		p, err = filepath.EvalSymlinks(test)
   154  		if err != nil {
   155  			t.Errorf("EvalSymlinks(%q) error: %v", test, err)
   156  			continue
   157  		}
   158  		if p != cname {
   159  			t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", test, p, cname)
   160  			continue
   161  		}
   162  	}
   163  }
   164  
   165  // checkVolume8dot3Setting runs "fsutil 8dot3name query c:" command
   166  // (where c: is vol parameter) to discover "8dot3 name creation state".
   167  // The state is combination of 2 flags. The global flag controls if it
   168  // is per volume or global setting:
   169  //   0 - Enable 8dot3 name creation on all volumes on the system
   170  //   1 - Disable 8dot3 name creation on all volumes on the system
   171  //   2 - Set 8dot3 name creation on a per volume basis
   172  //   3 - Disable 8dot3 name creation on all volumes except the system volume
   173  // If global flag is set to 2, then per-volume flag needs to be examined:
   174  //   0 - Enable 8dot3 name creation on this volume
   175  //   1 - Disable 8dot3 name creation on this volume
   176  // checkVolume8dot3Setting verifies that "8dot3 name creation" flags
   177  // are set to 2 and 0, if enabled parameter is true, or 2 and 1, if enabled
   178  // is false. Otherwise checkVolume8dot3Setting returns error.
   179  func checkVolume8dot3Setting(vol string, enabled bool) error {
   180  	// It appears, on some systems "fsutil 8dot3name query ..." command always
   181  	// exits with error. Ignore exit code, and look at fsutil output instead.
   182  	out, _ := exec.Command("fsutil", "8dot3name", "query", vol).CombinedOutput()
   183  	// Check that system has "Volume level setting" set.
   184  	expected := "The registry state of NtfsDisable8dot3NameCreation is 2, the default (Volume level setting)"
   185  	if !strings.Contains(string(out), expected) {
   186  		// Windows 10 version of fsutil has different output message.
   187  		expectedWindow10 := "The registry state is: 2 (Per volume setting - the default)"
   188  		if !strings.Contains(string(out), expectedWindow10) {
   189  			return fmt.Errorf("fsutil output should contain %q, but is %q", expected, string(out))
   190  		}
   191  	}
   192  	// Now check the volume setting.
   193  	expected = "Based on the above two settings, 8dot3 name creation is %s on %s"
   194  	if enabled {
   195  		expected = fmt.Sprintf(expected, "enabled", vol)
   196  	} else {
   197  		expected = fmt.Sprintf(expected, "disabled", vol)
   198  	}
   199  	if !strings.Contains(string(out), expected) {
   200  		return fmt.Errorf("unexpected fsutil output: %q", string(out))
   201  	}
   202  	return nil
   203  }
   204  
   205  func setVolume8dot3Setting(vol string, enabled bool) error {
   206  	cmd := []string{"fsutil", "8dot3name", "set", vol}
   207  	if enabled {
   208  		cmd = append(cmd, "0")
   209  	} else {
   210  		cmd = append(cmd, "1")
   211  	}
   212  	// It appears, on some systems "fsutil 8dot3name set ..." command always
   213  	// exits with error. Ignore exit code, and look at fsutil output instead.
   214  	out, _ := exec.Command(cmd[0], cmd[1:]...).CombinedOutput()
   215  	if string(out) != "\r\nSuccessfully set 8dot3name behavior.\r\n" {
   216  		// Windows 10 version of fsutil has different output message.
   217  		expectedWindow10 := "Successfully %s 8dot3name generation on %s\r\n"
   218  		if enabled {
   219  			expectedWindow10 = fmt.Sprintf(expectedWindow10, "enabled", vol)
   220  		} else {
   221  			expectedWindow10 = fmt.Sprintf(expectedWindow10, "disabled", vol)
   222  		}
   223  		if string(out) != expectedWindow10 {
   224  			return fmt.Errorf("%v command failed: %q", cmd, string(out))
   225  		}
   226  	}
   227  	return nil
   228  }
   229  
   230  var runFSModifyTests = flag.Bool("run_fs_modify_tests", false, "run tests which modify filesystem parameters")
   231  
   232  // This test assumes registry state of NtfsDisable8dot3NameCreation is 2,
   233  // the default (Volume level setting).
   234  func TestEvalSymlinksCanonicalNamesWith8dot3Disabled(t *testing.T) {
   235  	if !*runFSModifyTests {
   236  		t.Skip("skipping test that modifies file system setting; enable with -run_fs_modify_tests")
   237  	}
   238  	tempVol := filepath.VolumeName(os.TempDir())
   239  	if len(tempVol) != 2 {
   240  		t.Fatalf("unexpected temp volume name %q", tempVol)
   241  	}
   242  
   243  	err := checkVolume8dot3Setting(tempVol, true)
   244  	if err != nil {
   245  		t.Fatal(err)
   246  	}
   247  	err = setVolume8dot3Setting(tempVol, false)
   248  	if err != nil {
   249  		t.Fatal(err)
   250  	}
   251  	defer func() {
   252  		err := setVolume8dot3Setting(tempVol, true)
   253  		if err != nil {
   254  			t.Fatal(err)
   255  		}
   256  		err = checkVolume8dot3Setting(tempVol, true)
   257  		if err != nil {
   258  			t.Fatal(err)
   259  		}
   260  	}()
   261  	err = checkVolume8dot3Setting(tempVol, false)
   262  	if err != nil {
   263  		t.Fatal(err)
   264  	}
   265  	TestEvalSymlinksCanonicalNames(t)
   266  }
   267  
   268  func TestToNorm(t *testing.T) {
   269  	stubBase := func(path string) (string, error) {
   270  		vol := filepath.VolumeName(path)
   271  		path = path[len(vol):]
   272  
   273  		if strings.Contains(path, "/") {
   274  			return "", fmt.Errorf("invalid path is given to base: %s", vol+path)
   275  		}
   276  
   277  		if path == "" || path == "." || path == `\` {
   278  			return "", fmt.Errorf("invalid path is given to base: %s", vol+path)
   279  		}
   280  
   281  		i := strings.LastIndexByte(path, filepath.Separator)
   282  		if i == len(path)-1 { // trailing '\' is invalid
   283  			return "", fmt.Errorf("invalid path is given to base: %s", vol+path)
   284  		}
   285  		if i == -1 {
   286  			return strings.ToUpper(path), nil
   287  		}
   288  
   289  		return strings.ToUpper(path[i+1:]), nil
   290  	}
   291  
   292  	// On this test, toNorm should be same as string.ToUpper(filepath.Clean(path)) except empty string.
   293  	tests := []struct {
   294  		arg  string
   295  		want string
   296  	}{
   297  		{"", ""},
   298  		{".", "."},
   299  		{"./foo/bar", `FOO\BAR`},
   300  		{"/", `\`},
   301  		{"/foo/bar", `\FOO\BAR`},
   302  		{"/foo/bar/baz/qux", `\FOO\BAR\BAZ\QUX`},
   303  		{"foo/bar", `FOO\BAR`},
   304  		{"C:/foo/bar", `C:\FOO\BAR`},
   305  		{"C:foo/bar", `C:FOO\BAR`},
   306  		{"c:/foo/bar", `C:\FOO\BAR`},
   307  		{"C:/foo/bar", `C:\FOO\BAR`},
   308  		{"C:/foo/bar/", `C:\FOO\BAR`},
   309  		{`C:\foo\bar`, `C:\FOO\BAR`},
   310  		{`C:\foo/bar\`, `C:\FOO\BAR`},
   311  		{"C:/ふー/バー", `C:\ふー\バー`},
   312  	}
   313  
   314  	for _, test := range tests {
   315  		got, err := filepath.ToNorm(test.arg, stubBase)
   316  		if err != nil {
   317  			t.Errorf("toNorm(%s) failed: %v\n", test.arg, err)
   318  		} else if got != test.want {
   319  			t.Errorf("toNorm(%s) returns %s, but %s expected\n", test.arg, got, test.want)
   320  		}
   321  	}
   322  
   323  	testPath := `{{tmp}}\test\foo\bar`
   324  
   325  	testsDir := []struct {
   326  		wd   string
   327  		arg  string
   328  		want string
   329  	}{
   330  		// test absolute paths
   331  		{".", `{{tmp}}\test\foo\bar`, `{{tmp}}\test\foo\bar`},
   332  		{".", `{{tmp}}\.\test/foo\bar`, `{{tmp}}\test\foo\bar`},
   333  		{".", `{{tmp}}\test\..\test\foo\bar`, `{{tmp}}\test\foo\bar`},
   334  		{".", `{{tmp}}\TEST\FOO\BAR`, `{{tmp}}\test\foo\bar`},
   335  
   336  		// test relative paths begin with drive letter
   337  		{`{{tmp}}\test`, `{{tmpvol}}.`, `{{tmpvol}}.`},
   338  		{`{{tmp}}\test`, `{{tmpvol}}..`, `{{tmpvol}}..`},
   339  		{`{{tmp}}\test`, `{{tmpvol}}foo\bar`, `{{tmpvol}}foo\bar`},
   340  		{`{{tmp}}\test`, `{{tmpvol}}.\foo\bar`, `{{tmpvol}}foo\bar`},
   341  		{`{{tmp}}\test`, `{{tmpvol}}foo\..\foo\bar`, `{{tmpvol}}foo\bar`},
   342  		{`{{tmp}}\test`, `{{tmpvol}}FOO\BAR`, `{{tmpvol}}foo\bar`},
   343  
   344  		// test relative paths begin with '\'
   345  		{"{{tmp}}", `{{tmpnovol}}\test\foo\bar`, `{{tmpnovol}}\test\foo\bar`},
   346  		{"{{tmp}}", `{{tmpnovol}}\.\test\foo\bar`, `{{tmpnovol}}\test\foo\bar`},
   347  		{"{{tmp}}", `{{tmpnovol}}\test\..\test\foo\bar`, `{{tmpnovol}}\test\foo\bar`},
   348  		{"{{tmp}}", `{{tmpnovol}}\TEST\FOO\BAR`, `{{tmpnovol}}\test\foo\bar`},
   349  
   350  		// test relative paths begin without '\'
   351  		{`{{tmp}}\test`, ".", `.`},
   352  		{`{{tmp}}\test`, "..", `..`},
   353  		{`{{tmp}}\test`, `foo\bar`, `foo\bar`},
   354  		{`{{tmp}}\test`, `.\foo\bar`, `foo\bar`},
   355  		{`{{tmp}}\test`, `foo\..\foo\bar`, `foo\bar`},
   356  		{`{{tmp}}\test`, `FOO\BAR`, `foo\bar`},
   357  	}
   358  
   359  	tmp, err := ioutil.TempDir("", "testToNorm")
   360  	if err != nil {
   361  		t.Fatal(err)
   362  	}
   363  	defer func() {
   364  		err := os.RemoveAll(tmp)
   365  		if err != nil {
   366  			t.Fatal(err)
   367  		}
   368  	}()
   369  
   370  	// ioutil.TempDir might return "non-canonical" name.
   371  	ctmp, err := filepath.EvalSymlinks(tmp)
   372  	if err != nil {
   373  		t.Fatal(err)
   374  	}
   375  
   376  	err = os.MkdirAll(strings.Replace(testPath, "{{tmp}}", ctmp, -1), 0777)
   377  	if err != nil {
   378  		t.Fatal(err)
   379  	}
   380  
   381  	cwd, err := os.Getwd()
   382  	if err != nil {
   383  		t.Fatal(err)
   384  	}
   385  	defer func() {
   386  		err := os.Chdir(cwd)
   387  		if err != nil {
   388  			t.Fatal(err)
   389  		}
   390  	}()
   391  
   392  	tmpVol := filepath.VolumeName(ctmp)
   393  	if len(tmpVol) != 2 {
   394  		t.Fatalf("unexpected temp volume name %q", tmpVol)
   395  	}
   396  
   397  	tmpNoVol := ctmp[len(tmpVol):]
   398  
   399  	replacer := strings.NewReplacer("{{tmp}}", ctmp, "{{tmpvol}}", tmpVol, "{{tmpnovol}}", tmpNoVol)
   400  
   401  	for _, test := range testsDir {
   402  		wd := replacer.Replace(test.wd)
   403  		arg := replacer.Replace(test.arg)
   404  		want := replacer.Replace(test.want)
   405  
   406  		if test.wd == "." {
   407  			err := os.Chdir(cwd)
   408  			if err != nil {
   409  				t.Error(err)
   410  
   411  				continue
   412  			}
   413  		} else {
   414  			err := os.Chdir(wd)
   415  			if err != nil {
   416  				t.Error(err)
   417  
   418  				continue
   419  			}
   420  		}
   421  
   422  		got, err := filepath.ToNorm(arg, filepath.NormBase)
   423  		if err != nil {
   424  			t.Errorf("toNorm(%s) failed: %v (wd=%s)\n", arg, err, wd)
   425  		} else if got != want {
   426  			t.Errorf("toNorm(%s) returns %s, but %s expected (wd=%s)\n", arg, got, want, wd)
   427  		}
   428  	}
   429  }
   430  
   431  func TestUNC(t *testing.T) {
   432  	// Test that this doesn't go into an infinite recursion.
   433  	// See golang.org/issue/15879.
   434  	defer debug.SetMaxStack(debug.SetMaxStack(1e6))
   435  	filepath.Glob(`\\?\c:\*`)
   436  }
   437  
   438  func testWalkMklink(t *testing.T, linktype string) {
   439  	output, _ := exec.Command("cmd", "/c", "mklink", "/?").Output()
   440  	if !strings.Contains(string(output), fmt.Sprintf(" /%s ", linktype)) {
   441  		t.Skipf(`skipping test; mklink does not supports /%s parameter`, linktype)
   442  	}
   443  	testWalkSymlink(t, func(target, link string) error {
   444  		output, err := exec.Command("cmd", "/c", "mklink", "/"+linktype, link, target).CombinedOutput()
   445  		if err != nil {
   446  			return fmt.Errorf(`"mklink /%s %v %v" command failed: %v\n%v`, linktype, link, target, err, string(output))
   447  		}
   448  		return nil
   449  	})
   450  }
   451  
   452  func TestWalkDirectoryJunction(t *testing.T) {
   453  	testenv.MustHaveSymlink(t)
   454  	testWalkMklink(t, "J")
   455  }
   456  
   457  func TestWalkDirectorySymlink(t *testing.T) {
   458  	testenv.MustHaveSymlink(t)
   459  	testWalkMklink(t, "D")
   460  }