mirror of https://github.com/hak5/bolt.git
Finish open coverage.
parent
47224c4387
commit
f1d7fe5b08
4
db.go
4
db.go
|
@ -149,7 +149,7 @@ func (db *DB) mmap() error {
|
||||||
|
|
||||||
// Determine the map size based on the file size.
|
// Determine the map size based on the file size.
|
||||||
var size int
|
var size int
|
||||||
if info, err := db.os.Stat(db.path); err != nil {
|
if info, err := db.file.Stat(); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if info.Size() < int64(db.pageSize*2) {
|
} else if info.Size() < int64(db.pageSize*2) {
|
||||||
return &Error{"file size too small", nil}
|
return &Error{"file size too small", nil}
|
||||||
|
@ -188,7 +188,7 @@ func (db *DB) init() error {
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
p := db.page(buf[:], i)
|
p := db.page(buf[:], i)
|
||||||
p.id = pgno(i)
|
p.id = pgno(i)
|
||||||
p.initMeta(db.pageSize)
|
p.init(db.pageSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the buffer to our data file.
|
// Write the buffer to our data file.
|
||||||
|
|
150
db_test.go
150
db_test.go
|
@ -1,11 +1,17 @@
|
||||||
package bolt
|
package bolt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Ensure that a database can be opened without error.
|
// 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.
|
// Ensure that the database returns an error if the file handle cannot be open.
|
||||||
func TestDBOpenFileError(t *testing.T) {
|
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{}
|
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)
|
err := db.Open(path, 0666)
|
||||||
assert.Equal(t, err, exp)
|
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.
|
// Ensure that the database returns an error if the meta file handle cannot be open.
|
||||||
func TestDBOpenMetaFileError(t *testing.T) {
|
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{}
|
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_CREATE, os.FileMode(0666)).Return(&mockfile{}, 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_SYNC, os.FileMode(0666)).Return((*mockfile)(nil), exp)
|
||||||
err := db.Open(path, 0666)
|
err := db.Open(path, 0666)
|
||||||
assert.Equal(t, err, exp)
|
assert.Equal(t, err, exp)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that the database limits the upper bound of the page size.
|
// Ensure that the database limits the upper bound of the page size.
|
||||||
/*
|
|
||||||
func TestDBLimitPageSize(t *testing.T) {
|
func TestDBLimitPageSize(t *testing.T) {
|
||||||
withMockDB(func(db *DB, mockos *mockos, path string) {
|
withMockDB(func(db *DB, mockos *mockos, mocksyscall *mocksyscall, path string) {
|
||||||
buf := make([]byte, 4096)
|
b := make([]byte, 0x10000)
|
||||||
mockos.On("OpenFile", path, os.O_RDWR|os.O_CREATE, os.FileMode(0666)).Return(&mockfile{}, nil)
|
p0, p1 := (*page)(unsafe.Pointer(&b[0x0000])), (*page)(unsafe.Pointer(&b[0x8000]))
|
||||||
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return(&mockfile{}, nil)
|
p0.init(0x8000)
|
||||||
mockos.On("OpenFile", path, os.O_RDWR|os.O_SYNC, os.FileMode(0666)).Return(&mockfile{}, nil)
|
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)
|
err := db.Open(path, 0666)
|
||||||
assert.Equal(t, err, exp)
|
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.
|
// withDB executes a function with a database reference.
|
||||||
func withDB(fn func(*DB, string)) {
|
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.
|
// withMockDB executes a function with a database reference and a mock filesystem.
|
||||||
func withMockDB(fn func(*DB, *mockos, string)) {
|
func withMockDB(fn func(*DB, *mockos, *mocksyscall, string)) {
|
||||||
os := &mockos{}
|
os, syscall := &mockos{}, &mocksyscall{}
|
||||||
db := NewDB()
|
db := NewDB()
|
||||||
db.os = os
|
db.os = os
|
||||||
fn(db, os, "/mock/db")
|
db.syscall = syscall
|
||||||
|
fn(db, os, syscall, "/mock/db")
|
||||||
}
|
}
|
||||||
|
|
5
file.go
5
file.go
|
@ -1,7 +1,12 @@
|
||||||
package bolt
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
type file interface {
|
type file interface {
|
||||||
Fd() uintptr
|
Fd() uintptr
|
||||||
ReadAt(b []byte, off int64) (n int, err error)
|
ReadAt(b []byte, off int64) (n int, err error)
|
||||||
|
Stat() (fi os.FileInfo, err error)
|
||||||
WriteAt(b []byte, off int64) (n int, err error)
|
WriteAt(b []byte, off int64) (n int, err error)
|
||||||
}
|
}
|
||||||
|
|
41
file_test.go
41
file_test.go
|
@ -1,6 +1,9 @@
|
||||||
package bolt
|
package bolt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/mock"
|
"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)
|
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) {
|
func (m *mockfile) WriteAt(b []byte, off int64) (n int, err error) {
|
||||||
args := m.Called(b, off)
|
args := m.Called(b, off)
|
||||||
return args.Int(0), args.Error(1)
|
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
4
os.go
|
@ -5,14 +5,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type _os interface {
|
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)
|
Stat(name string) (fi os.FileInfo, err error)
|
||||||
Getpagesize() int
|
Getpagesize() int
|
||||||
}
|
}
|
||||||
|
|
||||||
type sysos struct{}
|
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)
|
return os.OpenFile(name, flag, perm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,9 @@ type mockos struct {
|
||||||
mock.Mock
|
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)
|
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) {
|
func (m *mockos) Stat(name string) (fi os.FileInfo, err error) {
|
||||||
|
|
4
page.go
4
page.go
|
@ -78,8 +78,8 @@ func (p *page) meta() (*meta, error) {
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// initMeta initializes a page as a new meta page.
|
// init initializes a page as a new meta page.
|
||||||
func (p *page) initMeta(pageSize int) {
|
func (p *page) init(pageSize int) {
|
||||||
p.flags = p_meta
|
p.flags = p_meta
|
||||||
m := (*meta)(unsafe.Pointer(&p.ptr))
|
m := (*meta)(unsafe.Pointer(&p.ptr))
|
||||||
m.magic = magic
|
m.magic = magic
|
||||||
|
|
Loading…
Reference in New Issue