Finish open coverage.

master
Ben Johnson 2014-01-13 08:25:56 -07:00
parent 47224c4387
commit f1d7fe5b08
7 changed files with 189 additions and 23 deletions

4
db.go
View File

@ -149,7 +149,7 @@ func (db *DB) mmap() error {
// Determine the map size based on the file size.
var size int
if info, err := db.os.Stat(db.path); err != nil {
if info, err := db.file.Stat(); err != nil {
return err
} else if info.Size() < int64(db.pageSize*2) {
return &Error{"file size too small", nil}
@ -188,7 +188,7 @@ func (db *DB) init() error {
for i := 0; i < 2; i++ {
p := db.page(buf[:], i)
p.id = pgno(i)
p.initMeta(db.pageSize)
p.init(db.pageSize)
}
// Write the buffer to our data file.

View File

@ -1,11 +1,17 @@
package bolt
import (
"errors"
"io"
"io/ioutil"
"os"
"syscall"
"testing"
"time"
"unsafe"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// Ensure that a database can be opened without error.
@ -28,9 +34,9 @@ func TestDBReopen(t *testing.T) {
// Ensure that the database returns an error if the file handle cannot be open.
func TestDBOpenFileError(t *testing.T) {
withMockDB(func(db *DB, mockos *mockos, path string) {
withMockDB(func(db *DB, mockos *mockos, mocksyscall *mocksyscall, path string) {
exp := &os.PathError{}
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return((*os.File)(nil), exp)
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return((*mockfile)(nil), exp)
err := db.Open(path, 0666)
assert.Equal(t, err, exp)
})
@ -38,28 +44,141 @@ func TestDBOpenFileError(t *testing.T) {
// Ensure that the database returns an error if the meta file handle cannot be open.
func TestDBOpenMetaFileError(t *testing.T) {
withMockDB(func(db *DB, mockos *mockos, path string) {
withMockDB(func(db *DB, mockos *mockos, mocksyscall *mocksyscall, path string) {
exp := &os.PathError{}
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return(&os.File{}, nil)
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return((*os.File)(nil), exp)
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return(&mockfile{}, nil)
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return((*mockfile)(nil), exp)
err := db.Open(path, 0666)
assert.Equal(t, err, exp)
})
}
// Ensure that the database limits the upper bound of the page size.
/*
func TestDBLimitPageSize(t *testing.T) {
withMockDB(func(db *DB, mockos *mockos, path string) {
buf := make([]byte, 4096)
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return(&mockfile{}, nil)
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return(&mockfile{}, nil)
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return(&mockfile{}, nil)
withMockDB(func(db *DB, mockos *mockos, mocksyscall *mocksyscall, path string) {
b := make([]byte, 0x10000)
p0, p1 := (*page)(unsafe.Pointer(&b[0x0000])), (*page)(unsafe.Pointer(&b[0x8000]))
p0.init(0x8000)
p1.init(0x8000)
file, metafile := &mockfile{}, &mockfile{}
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return(file, nil)
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return(metafile, nil)
mockos.On("Getpagesize").Return(0x10000)
file.On("ReadAt", mock.Anything, int64(0)).Return(0, nil)
file.On("Stat").Return(&mockfileinfo{"", 0x10000, 0666, time.Now(), false, nil}, nil)
metafile.On("WriteAt", mock.Anything, int64(0)).Return(0, nil)
mocksyscall.On("Mmap", 0, int64(0), 0x10000, syscall.PROT_READ, syscall.MAP_SHARED).Return(b, nil)
db.Open(path, 0666)
assert.Equal(t, db.pageSize, maxPageSize)
})
}
// Ensure that write errors to the meta file handler during initialization are returned.
func TestDBMetaInitWriteError(t *testing.T) {
withMockDB(func(db *DB, mockos *mockos, mocksyscall *mocksyscall, path string) {
file, metafile := &mockfile{}, &mockfile{}
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return(file, nil)
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return(metafile, nil)
mockos.On("Getpagesize").Return(0x10000)
file.On("ReadAt", mock.Anything, int64(0)).Return(0, nil)
file.On("Stat").Return(&mockfileinfo{"", 0x10000, 0666, time.Now(), false, nil}, nil)
metafile.On("WriteAt", mock.Anything, int64(0)).Return(0, io.ErrShortWrite)
err := db.Open(path, 0666)
assert.Equal(t, err, io.ErrShortWrite)
})
}
// Ensure that a database that is too small returns an error.
func TestDBFileTooSmall(t *testing.T) {
withMockDB(func(db *DB, mockos *mockos, mocksyscall *mocksyscall, path string) {
file, metafile := &mockfile{}, &mockfile{}
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return(file, nil)
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return(metafile, nil)
mockos.On("Getpagesize").Return(0x1000)
file.On("ReadAt", mock.Anything, int64(0)).Return(0, nil)
file.On("Stat").Return(&mockfileinfo{"", 0x1000, 0666, time.Now(), false, nil}, nil)
metafile.On("WriteAt", mock.Anything, int64(0)).Return(0, nil)
err := db.Open(path, 0666)
assert.Equal(t, err, &Error{"file size too small", nil})
})
}
// Ensure that stat errors during mmap get returned.
func TestDBMmapStatError(t *testing.T) {
withMockDB(func(db *DB, mockos *mockos, mocksyscall *mocksyscall, path string) {
exp := &os.PathError{}
file, metafile := &mockfile{}, &mockfile{}
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return(file, nil)
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return(metafile, nil)
mockos.On("Getpagesize").Return(0x1000)
file.On("ReadAt", mock.Anything, int64(0)).Return(0, nil)
file.On("Stat").Return((*mockfileinfo)(nil), exp)
metafile.On("WriteAt", mock.Anything, int64(0)).Return(0, nil)
err := db.Open(path, 0666)
assert.Equal(t, err, exp)
})
}
*/
// Ensure that mmap errors get returned.
func TestDBMmapError(t *testing.T) {
withMockDB(func(db *DB, mockos *mockos, mocksyscall *mocksyscall, path string) {
exp := errors.New("")
file, metafile := &mockfile{}, &mockfile{}
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return(file, nil)
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return(metafile, nil)
mockos.On("Getpagesize").Return(0x1000)
file.On("ReadAt", mock.Anything, int64(0)).Return(0, nil)
file.On("Stat").Return(&mockfileinfo{"", 0x2000, 0666, time.Now(), false, nil}, nil)
metafile.On("WriteAt", mock.Anything, int64(0)).Return(0, nil)
mocksyscall.On("Mmap", 0, int64(0), 0x2000, syscall.PROT_READ, syscall.MAP_SHARED).Return(([]byte)(nil), exp)
err := db.Open(path, 0666)
assert.Equal(t, err, exp)
})
}
// Ensure that corrupt meta0 page errors get returned.
func TestDBCorruptMeta0(t *testing.T) {
withMockDB(func(db *DB, mockos *mockos, mocksyscall *mocksyscall, path string) {
b := make([]byte, 0x10000)
p0, p1 := (*page)(unsafe.Pointer(&b[0x0000])), (*page)(unsafe.Pointer(&b[0x8000]))
p0.init(0x8000)
p1.init(0x8000)
m, _ := p0.meta()
m.magic = 0
file, metafile := &mockfile{}, &mockfile{}
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return(file, nil)
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return(metafile, nil)
mockos.On("Getpagesize").Return(0x10000)
file.On("ReadAt", mock.Anything, int64(0)).Return(0, nil)
file.On("Stat").Return(&mockfileinfo{"", 0x10000, 0666, time.Now(), false, nil}, nil)
metafile.On("WriteAt", mock.Anything, int64(0)).Return(0, nil)
mocksyscall.On("Mmap", 0, int64(0), 0x10000, syscall.PROT_READ, syscall.MAP_SHARED).Return(b, nil)
err := db.Open(path, 0666)
assert.Equal(t, err, &Error{"meta0 error", InvalidError})
})
}
// Ensure that corrupt meta1 page errors get returned.
func TestDBCorruptMeta1(t *testing.T) {
withMockDB(func(db *DB, mockos *mockos, mocksyscall *mocksyscall, path string) {
b := make([]byte, 0x10000)
p0, p1 := (*page)(unsafe.Pointer(&b[0x0000])), (*page)(unsafe.Pointer(&b[0x8000]))
p0.init(0x8000)
p1.init(0x8000)
m, _ := p1.meta()
m.version = 100
file, metafile := &mockfile{}, &mockfile{}
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return(file, nil)
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return(metafile, nil)
mockos.On("Getpagesize").Return(0x10000)
file.On("ReadAt", mock.Anything, int64(0)).Return(0, nil)
file.On("Stat").Return(&mockfileinfo{"", 0x10000, 0666, time.Now(), false, nil}, nil)
metafile.On("WriteAt", mock.Anything, int64(0)).Return(0, nil)
mocksyscall.On("Mmap", 0, int64(0), 0x10000, syscall.PROT_READ, syscall.MAP_SHARED).Return(b, nil)
err := db.Open(path, 0666)
assert.Equal(t, err, &Error{"meta1 error", VersionMismatchError})
})
}
// withDB executes a function with a database reference.
func withDB(fn func(*DB, string)) {
@ -74,9 +193,10 @@ func withDB(fn func(*DB, string)) {
}
// withMockDB executes a function with a database reference and a mock filesystem.
func withMockDB(fn func(*DB, *mockos, string)) {
os := &mockos{}
func withMockDB(fn func(*DB, *mockos, *mocksyscall, string)) {
os, syscall := &mockos{}, &mocksyscall{}
db := NewDB()
db.os = os
fn(db, os, "/mock/db")
db.syscall = syscall
fn(db, os, syscall, "/mock/db")
}

View File

@ -1,7 +1,12 @@
package bolt
import (
"os"
)
type file interface {
Fd() uintptr
ReadAt(b []byte, off int64) (n int, err error)
Stat() (fi os.FileInfo, err error)
WriteAt(b []byte, off int64) (n int, err error)
}

View File

@ -1,6 +1,9 @@
package bolt
import (
"os"
"time"
"github.com/stretchr/testify/mock"
)
@ -18,7 +21,45 @@ func (m *mockfile) ReadAt(b []byte, off int64) (n int, err error) {
return args.Int(0), args.Error(1)
}
func (m *mockfile) Stat() (os.FileInfo, error) {
args := m.Called()
return args.Get(0).(os.FileInfo), args.Error(1)
}
func (m *mockfile) WriteAt(b []byte, off int64) (n int, err error) {
args := m.Called(b, off)
return args.Int(0), args.Error(1)
}
type mockfileinfo struct {
name string
size int64
mode os.FileMode
modTime time.Time
isDir bool
sys interface{}
}
func (m *mockfileinfo) Name() string {
return m.name
}
func (m *mockfileinfo) Size() int64 {
return m.size
}
func (m *mockfileinfo) Mode() os.FileMode {
return m.mode
}
func (m *mockfileinfo) ModTime() time.Time {
return m.modTime
}
func (m *mockfileinfo) IsDir() bool {
return m.isDir
}
func (m *mockfileinfo) Sys() interface{} {
return m.sys
}

4
os.go
View File

@ -5,14 +5,14 @@ import (
)
type _os interface {
OpenFile(name string, flag int, perm os.FileMode) (file *os.File, err error)
OpenFile(name string, flag int, perm os.FileMode) (file file, err error)
Stat(name string) (fi os.FileInfo, err error)
Getpagesize() int
}
type sysos struct{}
func (o *sysos) OpenFile(name string, flag int, perm os.FileMode) (file *os.File, err error) {
func (o *sysos) OpenFile(name string, flag int, perm os.FileMode) (file file, err error) {
return os.OpenFile(name, flag, perm)
}

View File

@ -10,9 +10,9 @@ type mockos struct {
mock.Mock
}
func (m *mockos) OpenFile(name string, flag int, perm os.FileMode) (file *os.File, err error) {
func (m *mockos) OpenFile(name string, flag int, perm os.FileMode) (file file, err error) {
args := m.Called(name, flag, perm)
return args.Get(0).(*os.File), args.Error(1)
return args.Get(0).(*mockfile), args.Error(1)
}
func (m *mockos) Stat(name string) (fi os.FileInfo, err error) {

View File

@ -78,8 +78,8 @@ func (p *page) meta() (*meta, error) {
return m, nil
}
// initMeta initializes a page as a new meta page.
func (p *page) initMeta(pageSize int) {
// init initializes a page as a new meta page.
func (p *page) init(pageSize int) {
p.flags = p_meta
m := (*meta)(unsafe.Pointer(&p.ptr))
m.magic = magic