diff options
Diffstat (limited to 'libgo/go/os/os_test.go')
-rw-r--r-- | libgo/go/os/os_test.go | 156 |
1 files changed, 128 insertions, 28 deletions
diff --git a/libgo/go/os/os_test.go b/libgo/go/os/os_test.go index a3885a899c3..10a74c2062a 100644 --- a/libgo/go/os/os_test.go +++ b/libgo/go/os/os_test.go @@ -25,8 +25,6 @@ import ( "time" ) -var supportsSymlinks = true - var dot = []string{ "dir.go", "env.go", @@ -229,6 +227,28 @@ func TestRead0(t *testing.T) { } } +// Reading a closed file should should return ErrClosed error +func TestReadClosed(t *testing.T) { + path := sfdir + "/" + sfname + file, err := Open(path) + if err != nil { + t.Fatal("open failed:", err) + } + file.Close() // close immediately + + b := make([]byte, 100) + _, err = file.Read(b) + + e, ok := err.(*PathError) + if !ok { + t.Fatalf("Read: %T(%v), want PathError", e, e) + } + + if e.Err != ErrClosed { + t.Errorf("Read: %v, want PathError(ErrClosed)", e) + } +} + func testReaddirnames(dir string, contents []string, t *testing.T) { file, err := Open(dir) if err != nil { @@ -578,15 +598,8 @@ func TestReaddirOfFile(t *testing.T) { } func TestHardLink(t *testing.T) { - if runtime.GOOS == "plan9" { - t.Skip("skipping on plan9, hardlinks not supported") - } - // From Android release M (Marshmallow), hard linking files is blocked - // and an attempt to call link() on a file will return EACCES. - // - https://code.google.com/p/android-developer-preview/issues/detail?id=3150 - if runtime.GOOS == "android" { - t.Skip("skipping on android, hardlinks not supported") - } + testenv.MustHaveLink(t) + defer chtmpdir(t)() from, to := "hardlinktestfrom", "hardlinktestto" Remove(from) // Just in case. @@ -650,14 +663,8 @@ func chtmpdir(t *testing.T) func() { } func TestSymlink(t *testing.T) { - switch runtime.GOOS { - case "android", "nacl", "plan9": - t.Skipf("skipping on %s", runtime.GOOS) - case "windows": - if !supportsSymlinks { - t.Skipf("skipping on %s", runtime.GOOS) - } - } + testenv.MustHaveSymlink(t) + defer chtmpdir(t)() from, to := "symlinktestfrom", "symlinktestto" Remove(from) // Just in case. @@ -717,14 +724,8 @@ func TestSymlink(t *testing.T) { } func TestLongSymlink(t *testing.T) { - switch runtime.GOOS { - case "android", "plan9", "nacl": - t.Skipf("skipping on %s", runtime.GOOS) - case "windows": - if !supportsSymlinks { - t.Skipf("skipping on %s", runtime.GOOS) - } - } + testenv.MustHaveSymlink(t) + defer chtmpdir(t)() s := "0123456789abcdef" // Long, but not too long: a common limit is 255. @@ -840,6 +841,39 @@ func TestRenameFailed(t *testing.T) { } } +func TestRenameToDirFailed(t *testing.T) { + defer chtmpdir(t)() + from, to := "renamefrom", "renameto" + + Remove(from) + Remove(to) + Mkdir(from, 0777) + Mkdir(to, 0777) + defer Remove(from) + defer Remove(to) + + err := Rename(from, to) + switch err := err.(type) { + case *LinkError: + if err.Op != "rename" { + t.Errorf("rename %q, %q: err.Op: want %q, got %q", from, to, "rename", err.Op) + } + if err.Old != from { + t.Errorf("rename %q, %q: err.Old: want %q, got %q", from, to, from, err.Old) + } + if err.New != to { + t.Errorf("rename %q, %q: err.New: want %q, got %q", from, to, to, err.New) + } + case nil: + t.Errorf("rename %q, %q: expected error, got nil", from, to) + + // cleanup whatever was placed in "renameto" + Remove(to) + default: + t.Errorf("rename %q, %q: expected %T, got %T %v", from, to, new(LinkError), err, err) + } +} + func exec(t *testing.T, dir, cmd string, args []string, expect string) { r, w, err := Pipe() if err != nil { @@ -1028,7 +1062,7 @@ func testChtimes(t *testing.T, name string) { } if !pmt.Before(mt) { - t.Errorf("ModTime didn't go backwards; was=%d, after=%d", mt, pmt) + t.Errorf("ModTime didn't go backwards; was=%v, after=%v", mt, pmt) } } @@ -1665,6 +1699,72 @@ func TestReadAtEOF(t *testing.T) { } } +func TestLongPath(t *testing.T) { + tmpdir := newDir("TestLongPath", t) + defer func(d string) { + if err := RemoveAll(d); err != nil { + t.Fatalf("RemoveAll failed: %v", err) + } + }(tmpdir) + + // Test the boundary of 247 and fewer bytes (normal) and 248 and more bytes (adjusted). + sizes := []int{247, 248, 249, 400} + for len(tmpdir) < 400 { + tmpdir += "/dir3456789" + } + for _, sz := range sizes { + t.Run(fmt.Sprintf("length=%d", sz), func(t *testing.T) { + sizedTempDir := tmpdir[:sz-1] + "x" // Ensure it does not end with a slash. + + // The various sized runs are for this call to trigger the boundary + // condition. + if err := MkdirAll(sizedTempDir, 0755); err != nil { + t.Fatalf("MkdirAll failed: %v", err) + } + data := []byte("hello world\n") + if err := ioutil.WriteFile(sizedTempDir+"/foo.txt", data, 0644); err != nil { + t.Fatalf("ioutil.WriteFile() failed: %v", err) + } + if err := Rename(sizedTempDir+"/foo.txt", sizedTempDir+"/bar.txt"); err != nil { + t.Fatalf("Rename failed: %v", err) + } + mtime := time.Now().Truncate(time.Minute) + if err := Chtimes(sizedTempDir+"/bar.txt", mtime, mtime); err != nil { + t.Fatalf("Chtimes failed: %v", err) + } + names := []string{"bar.txt"} + if testenv.HasSymlink() { + if err := Symlink(sizedTempDir+"/bar.txt", sizedTempDir+"/symlink.txt"); err != nil { + t.Fatalf("Symlink failed: %v", err) + } + names = append(names, "symlink.txt") + } + if testenv.HasLink() { + if err := Link(sizedTempDir+"/bar.txt", sizedTempDir+"/link.txt"); err != nil { + t.Fatalf("Link failed: %v", err) + } + names = append(names, "link.txt") + } + for _, wantSize := range []int64{int64(len(data)), 0} { + for _, name := range names { + path := sizedTempDir + "/" + name + dir, err := Stat(path) + if err != nil { + t.Fatalf("Stat(%q) failed: %v", path, err) + } + filesize := size(path, t) + if dir.Size() != filesize || filesize != wantSize { + t.Errorf("Size(%q) is %d, len(ReadFile()) is %d, want %d", path, dir.Size(), filesize, wantSize) + } + } + if err := Truncate(sizedTempDir+"/bar.txt", 0); err != nil { + t.Fatalf("Truncate failed: %v", err) + } + } + }) + } +} + func testKillProcess(t *testing.T, processKiller func(p *Process)) { testenv.MustHaveExec(t) |