Dumped in posix tests.

geesefs-0-30-9
Aaron Jacobs 2015-03-06 05:02:20 +11:00
parent 0377d675dd
commit b8ea62986f
1 changed files with 176 additions and 2 deletions

View File

@ -15,6 +15,7 @@
package memfs_test
import (
"io"
"io/ioutil"
"log"
"os"
@ -79,6 +80,9 @@ func timespecToTime(ts syscall.Timespec) time.Time {
type MemFSTest struct {
clock timeutil.SimulatedClock
mfs *fuse.MountedFileSystem
// Files to close when tearing down. Nil entries are skipped.
toClose []io.Closer
}
var _ SetUpInterface = &MemFSTest{}
@ -110,6 +114,18 @@ func (t *MemFSTest) SetUp(ti *TestInfo) {
}
func (t *MemFSTest) TearDown() {
// Close any files we opened.
for _, c := range t.toClose {
if c == nil {
continue
}
err := c.Close()
if err != nil {
panic(err)
}
}
// Unmount the file system. Try again on "resource busy" errors.
delay := 10 * time.Millisecond
for {
@ -558,6 +574,164 @@ func (t *MemFSTest) FileReadsAndWrites() {
AssertTrue(false, "TODO")
}
func (t *MemFSTest) FileReadsAndWrites_BeyondEOF() {
AssertTrue(false, "TODO")
func (t *MemFSTest) WriteOverlapsEndOfFile() {
var err error
var n int
// Create a file.
f, err := os.Create(path.Join(t.mfs.Dir(), "foo"))
t.toClose = append(t.toClose, f)
AssertEq(nil, err)
// Make it 4 bytes long.
err = f.Truncate(4)
AssertEq(nil, err)
// Write the range [2, 6).
n, err = f.WriteAt([]byte("taco"), 2)
AssertEq(nil, err)
AssertEq(4, n)
// Read the full contents of the file.
contents, err := ioutil.ReadAll(f)
AssertEq(nil, err)
ExpectEq("\x00\x00taco", string(contents))
}
func (t *MemFSTest) WriteStartsAtEndOfFile() {
var err error
var n int
// Create a file.
f, err := os.Create(path.Join(t.mfs.Dir(), "foo"))
t.toClose = append(t.toClose, f)
AssertEq(nil, err)
// Make it 2 bytes long.
err = f.Truncate(2)
AssertEq(nil, err)
// Write the range [2, 6).
n, err = f.WriteAt([]byte("taco"), 2)
AssertEq(nil, err)
AssertEq(4, n)
// Read the full contents of the file.
contents, err := ioutil.ReadAll(f)
AssertEq(nil, err)
ExpectEq("\x00\x00taco", string(contents))
}
func (t *MemFSTest) WriteStartsPastEndOfFile() {
var err error
var n int
// Create a file.
f, err := os.Create(path.Join(t.mfs.Dir(), "foo"))
t.toClose = append(t.toClose, f)
AssertEq(nil, err)
// Write the range [2, 6).
n, err = f.WriteAt([]byte("taco"), 2)
AssertEq(nil, err)
AssertEq(4, n)
// Read the full contents of the file.
contents, err := ioutil.ReadAll(f)
AssertEq(nil, err)
ExpectEq("\x00\x00taco", string(contents))
}
func (t *MemFSTest) WriteAtDoesntChangeOffset_NotAppendMode() {
var err error
var n int
// Create a file.
f, err := os.Create(path.Join(t.mfs.Dir(), "foo"))
t.toClose = append(t.toClose, f)
AssertEq(nil, err)
// Make it 16 bytes long.
err = f.Truncate(16)
AssertEq(nil, err)
// Seek to offset 4.
_, err = f.Seek(4, 0)
AssertEq(nil, err)
// Write the range [10, 14).
n, err = f.WriteAt([]byte("taco"), 2)
AssertEq(nil, err)
AssertEq(4, n)
// We should still be at offset 4.
offset, err := getFileOffset(f)
AssertEq(nil, err)
ExpectEq(4, offset)
}
func (t *MemFSTest) WriteAtDoesntChangeOffset_AppendMode() {
var err error
var n int
// Create a file in append mode.
f, err := os.OpenFile(
path.Join(t.mfs.Dir(), "foo"),
os.O_RDWR|os.O_APPEND|os.O_CREATE,
0600)
t.toClose = append(t.toClose, f)
AssertEq(nil, err)
// Make it 16 bytes long.
err = f.Truncate(16)
AssertEq(nil, err)
// Seek to offset 4.
_, err = f.Seek(4, 0)
AssertEq(nil, err)
// Write the range [10, 14).
n, err = f.WriteAt([]byte("taco"), 2)
AssertEq(nil, err)
AssertEq(4, n)
// We should still be at offset 4.
offset, err := getFileOffset(f)
AssertEq(nil, err)
ExpectEq(4, offset)
}
func (t *MemFSTest) ReadsPastEndOfFile() {
var err error
var n int
buf := make([]byte, 1024)
// Create a file.
f, err := os.Create(path.Join(t.mfs.Dir(), "foo"))
t.toClose = append(t.toClose, f)
AssertEq(nil, err)
// Give it some contents.
n, err = f.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
// Read a range overlapping EOF.
n, err = f.ReadAt(buf[:4], 2)
AssertEq(io.EOF, err)
ExpectEq(2, n)
ExpectEq("co", string(buf[:n]))
// Read a range starting at EOF.
n, err = f.ReadAt(buf[:4], 4)
AssertEq(io.EOF, err)
ExpectEq(0, n)
ExpectEq("", string(buf[:n]))
// Read a range starting past EOF.
n, err = f.ReadAt(buf[:4], 100)
AssertEq(io.EOF, err)
ExpectEq(0, n)
ExpectEq("", string(buf[:n]))
}