Source file src/os/path_windows_test.go

     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 (
     8  	"os"
     9  	"strings"
    10  	"syscall"
    11  	"testing"
    12  )
    13  
    14  func TestFixLongPath(t *testing.T) {
    15  	if os.CanUseLongPaths {
    16  		return
    17  	}
    18  	// 248 is long enough to trigger the longer-than-248 checks in
    19  	// fixLongPath, but short enough not to make a path component
    20  	// longer than 255, which is illegal on Windows. (which
    21  	// doesn't really matter anyway, since this is purely a string
    22  	// function we're testing, and it's not actually being used to
    23  	// do a system call)
    24  	veryLong := "l" + strings.Repeat("o", 248) + "ng"
    25  	for _, test := range []struct{ in, want string }{
    26  		// Short; unchanged:
    27  		{`C:\short.txt`, `C:\short.txt`},
    28  		{`C:\`, `C:\`},
    29  		{`C:`, `C:`},
    30  		// The "long" substring is replaced by a looooooong
    31  		// string which triggers the rewriting. Except in the
    32  		// cases below where it doesn't.
    33  		{`C:\long\foo.txt`, `\\?\C:\long\foo.txt`},
    34  		{`C:/long/foo.txt`, `\\?\C:\long\foo.txt`},
    35  		{`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz`},
    36  		{`\\unc\path`, `\\unc\path`},
    37  		{`long.txt`, `long.txt`},
    38  		{`C:long.txt`, `C:long.txt`},
    39  		{`c:\long\..\bar\baz`, `c:\long\..\bar\baz`},
    40  		{`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`},
    41  		{`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`},
    42  	} {
    43  		in := strings.ReplaceAll(test.in, "long", veryLong)
    44  		want := strings.ReplaceAll(test.want, "long", veryLong)
    45  		if got := os.FixLongPath(in); got != want {
    46  			got = strings.ReplaceAll(got, veryLong, "long")
    47  			t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want)
    48  		}
    49  	}
    50  }
    51  
    52  func TestMkdirAllLongPath(t *testing.T) {
    53  	tmpDir := t.TempDir()
    54  	path := tmpDir
    55  	for i := 0; i < 100; i++ {
    56  		path += `\another-path-component`
    57  	}
    58  	if err := os.MkdirAll(path, 0777); err != nil {
    59  		t.Fatalf("MkdirAll(%q) failed; %v", path, err)
    60  	}
    61  	if err := os.RemoveAll(tmpDir); err != nil {
    62  		t.Fatalf("RemoveAll(%q) failed; %v", tmpDir, err)
    63  	}
    64  }
    65  
    66  func TestMkdirAllExtendedLength(t *testing.T) {
    67  	tmpDir := t.TempDir()
    68  
    69  	const prefix = `\\?\`
    70  	if len(tmpDir) < 4 || tmpDir[:4] != prefix {
    71  		fullPath, err := syscall.FullPath(tmpDir)
    72  		if err != nil {
    73  			t.Fatalf("FullPath(%q) fails: %v", tmpDir, err)
    74  		}
    75  		tmpDir = prefix + fullPath
    76  	}
    77  	path := tmpDir + `\dir\`
    78  	if err := os.MkdirAll(path, 0777); err != nil {
    79  		t.Fatalf("MkdirAll(%q) failed: %v", path, err)
    80  	}
    81  
    82  	path = path + `.\dir2`
    83  	if err := os.MkdirAll(path, 0777); err == nil {
    84  		t.Fatalf("MkdirAll(%q) should have failed, but did not", path)
    85  	}
    86  }
    87  
    88  func TestOpenRootSlash(t *testing.T) {
    89  	tests := []string{
    90  		`/`,
    91  		`\`,
    92  	}
    93  
    94  	for _, test := range tests {
    95  		dir, err := os.Open(test)
    96  		if err != nil {
    97  			t.Fatalf("Open(%q) failed: %v", test, err)
    98  		}
    99  		dir.Close()
   100  	}
   101  }
   102  

View as plain text