diff --git a/go.mod b/go.mod index 6b05a56..3722ad6 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.19 require ( github.com/ncruces/julianday v0.1.5 github.com/psanford/httpreadat v0.1.0 - github.com/tetratelabs/wazero v1.1.0 + github.com/tetratelabs/wazero v1.1.1-0.20230511035210-78c35acd6e1c golang.org/x/sync v0.2.0 golang.org/x/sys v0.8.0 ) diff --git a/go.sum b/go.sum index 19e3eb0..d0bbbca 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/ncruces/julianday v0.1.5 h1:hDJ9ejiMp3DHsoZ5KW4c1lwfMjbARS7u/gbYcd0FB github.com/ncruces/julianday v0.1.5/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g= github.com/psanford/httpreadat v0.1.0 h1:VleW1HS2zO7/4c7c7zNl33fO6oYACSagjJIyMIwZLUE= github.com/psanford/httpreadat v0.1.0/go.mod h1:Zg7P+TlBm3bYbyHTKv/EdtSJZn3qwbPwpfZ/I9GKCRE= -github.com/tetratelabs/wazero v1.1.0 h1:EByoAhC+QcYpwSZJSs/aV0uokxPwBgKxfiokSUwAknQ= -github.com/tetratelabs/wazero v1.1.0/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ= +github.com/tetratelabs/wazero v1.1.1-0.20230511035210-78c35acd6e1c h1:3vKw5AyUv+16qm9tIVfvm205aDIpDmrjxPydJ87GBKY= +github.com/tetratelabs/wazero v1.1.1-0.20230511035210-78c35acd6e1c/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ= golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= diff --git a/internal/util/mem_test.go b/internal/util/mem_test.go index 4b32f70..d18a2b8 100644 --- a/internal/util/mem_test.go +++ b/internal/util/mem_test.go @@ -3,88 +3,90 @@ package util import ( "math" "testing" + + "github.com/tetratelabs/wazero/experimental/wazerotest" ) func TestView_nil(t *testing.T) { defer func() { _ = recover() }() - mock := NewMockModule(128) + mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize)) View(mock, 0, 8) t.Error("want panic") } func TestView_range(t *testing.T) { defer func() { _ = recover() }() - mock := NewMockModule(128) - View(mock, 126, 8) + mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize)) + View(mock, wazerotest.PageSize-2, 8) t.Error("want panic") } func TestView_overflow(t *testing.T) { defer func() { _ = recover() }() - mock := NewMockModule(128) + mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize)) View(mock, 1, math.MaxInt64) t.Error("want panic") } func TestReadUint32_nil(t *testing.T) { defer func() { _ = recover() }() - mock := NewMockModule(128) + mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize)) ReadUint32(mock, 0) t.Error("want panic") } func TestReadUint32_range(t *testing.T) { defer func() { _ = recover() }() - mock := NewMockModule(128) - ReadUint32(mock, 126) + mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize)) + ReadUint32(mock, wazerotest.PageSize-2) t.Error("want panic") } func TestReadUint64_nil(t *testing.T) { defer func() { _ = recover() }() - mock := NewMockModule(128) + mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize)) ReadUint64(mock, 0) t.Error("want panic") } func TestReadUint64_range(t *testing.T) { defer func() { _ = recover() }() - mock := NewMockModule(128) - ReadUint64(mock, 126) + mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize)) + ReadUint64(mock, wazerotest.PageSize-2) t.Error("want panic") } func TestWriteUint32_nil(t *testing.T) { defer func() { _ = recover() }() - mock := NewMockModule(128) + mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize)) WriteUint32(mock, 0, 1) t.Error("want panic") } func TestWriteUint32_range(t *testing.T) { defer func() { _ = recover() }() - mock := NewMockModule(128) - WriteUint32(mock, 126, 1) + mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize)) + WriteUint32(mock, wazerotest.PageSize-2, 1) t.Error("want panic") } func TestWriteUint64_nil(t *testing.T) { defer func() { _ = recover() }() - mock := NewMockModule(128) + mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize)) WriteUint64(mock, 0, 1) t.Error("want panic") } func TestWriteUint64_range(t *testing.T) { defer func() { _ = recover() }() - mock := NewMockModule(128) - WriteUint64(mock, 126, 1) + mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize)) + WriteUint64(mock, wazerotest.PageSize-2, 1) t.Error("want panic") } func TestReadString_range(t *testing.T) { defer func() { _ = recover() }() - mock := NewMockModule(128) - ReadString(mock, 130, math.MaxUint32) + mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize)) + ReadString(mock, wazerotest.PageSize+2, math.MaxUint32) t.Error("want panic") } diff --git a/internal/util/mock.go b/internal/util/mock.go deleted file mode 100644 index 0503575..0000000 --- a/internal/util/mock.go +++ /dev/null @@ -1,148 +0,0 @@ -package util - -import ( - "encoding/binary" - "math" - - "github.com/tetratelabs/wazero/api" -) - -func NewMockModule(size uint32) api.Module { - mem := mockMemory{buf: make([]byte, size)} - return mockModule{&mem, nil} -} - -type mockModule struct { - memory api.Memory - api.Module -} - -func (m mockModule) Memory() api.Memory { return m.memory } -func (m mockModule) String() string { return "mockModule" } -func (m mockModule) Name() string { return "mockModule" } - -type mockMemory struct { - buf []byte - api.Memory -} - -func (m mockMemory) Definition() api.MemoryDefinition { return nil } - -func (m mockMemory) Size() uint32 { return uint32(len(m.buf)) } - -func (m mockMemory) ReadByte(offset uint32) (byte, bool) { - if offset >= m.Size() { - return 0, false - } - return m.buf[offset], true -} - -func (m mockMemory) ReadUint16Le(offset uint32) (uint16, bool) { - if !m.hasSize(offset, 2) { - return 0, false - } - return binary.LittleEndian.Uint16(m.buf[offset : offset+2]), true -} - -func (m mockMemory) ReadUint32Le(offset uint32) (uint32, bool) { - if !m.hasSize(offset, 4) { - return 0, false - } - return binary.LittleEndian.Uint32(m.buf[offset : offset+4]), true -} - -func (m mockMemory) ReadFloat32Le(offset uint32) (float32, bool) { - v, ok := m.ReadUint32Le(offset) - if !ok { - return 0, false - } - return math.Float32frombits(v), true -} - -func (m mockMemory) ReadUint64Le(offset uint32) (uint64, bool) { - if !m.hasSize(offset, 8) { - return 0, false - } - return binary.LittleEndian.Uint64(m.buf[offset : offset+8]), true -} - -func (m mockMemory) ReadFloat64Le(offset uint32) (float64, bool) { - v, ok := m.ReadUint64Le(offset) - if !ok { - return 0, false - } - return math.Float64frombits(v), true -} - -func (m mockMemory) Read(offset, byteCount uint32) ([]byte, bool) { - if !m.hasSize(offset, byteCount) { - return nil, false - } - return m.buf[offset : offset+byteCount : offset+byteCount], true -} - -func (m mockMemory) WriteByte(offset uint32, v byte) bool { - if offset >= m.Size() { - return false - } - m.buf[offset] = v - return true -} - -func (m mockMemory) WriteUint16Le(offset uint32, v uint16) bool { - if !m.hasSize(offset, 2) { - return false - } - binary.LittleEndian.PutUint16(m.buf[offset:], v) - return true -} - -func (m mockMemory) WriteUint32Le(offset, v uint32) bool { - if !m.hasSize(offset, 4) { - return false - } - binary.LittleEndian.PutUint32(m.buf[offset:], v) - return true -} - -func (m mockMemory) WriteFloat32Le(offset uint32, v float32) bool { - return m.WriteUint32Le(offset, math.Float32bits(v)) -} - -func (m mockMemory) WriteUint64Le(offset uint32, v uint64) bool { - if !m.hasSize(offset, 8) { - return false - } - binary.LittleEndian.PutUint64(m.buf[offset:], v) - return true -} - -func (m mockMemory) WriteFloat64Le(offset uint32, v float64) bool { - return m.WriteUint64Le(offset, math.Float64bits(v)) -} - -func (m mockMemory) Write(offset uint32, val []byte) bool { - if !m.hasSize(offset, uint32(len(val))) { - return false - } - copy(m.buf[offset:], val) - return true -} - -func (m mockMemory) WriteString(offset uint32, val string) bool { - if !m.hasSize(offset, uint32(len(val))) { - return false - } - copy(m.buf[offset:], val) - return true -} - -func (m *mockMemory) Grow(delta uint32) (result uint32, ok bool) { - prev := (len(m.buf) + 65535) / 65536 - m.buf = append(m.buf, make([]byte, 65536*delta)...) - return uint32(prev), true -} - -func (m mockMemory) hasSize(offset uint32, byteCount uint32) bool { - return uint64(offset)+uint64(byteCount) <= uint64(len(m.buf)) -} diff --git a/internal/util/mock_test.go b/internal/util/mock_test.go deleted file mode 100644 index f5e0967..0000000 --- a/internal/util/mock_test.go +++ /dev/null @@ -1,242 +0,0 @@ -package util - -import ( - "math" - "testing" -) - -func Test_mockMemory_byte(t *testing.T) { - const want byte = 98 - mock := NewMockModule(128) - - _, ok := mock.Memory().ReadByte(128) - if ok { - t.Error("want error") - } - - ok = mock.Memory().WriteByte(128, 0) - if ok { - t.Error("want error") - } - - ok = mock.Memory().WriteByte(0, want) - if !ok { - t.Error("want ok") - } - - got, ok := mock.Memory().ReadByte(0) - if !ok { - t.Error("want ok") - } - if got != want { - t.Errorf("got %d, want %d", got, want) - } -} - -func Test_mockMemory_uint16(t *testing.T) { - const want uint16 = 9876 - mock := NewMockModule(128) - - _, ok := mock.Memory().ReadUint16Le(128) - if ok { - t.Error("want error") - } - - ok = mock.Memory().WriteUint16Le(128, 0) - if ok { - t.Error("want error") - } - - ok = mock.Memory().WriteUint16Le(0, want) - if !ok { - t.Error("want ok") - } - - got, ok := mock.Memory().ReadUint16Le(0) - if !ok { - t.Error("want ok") - } - if got != want { - t.Errorf("got %d, want %d", got, want) - } -} - -func Test_mockMemory_uint32(t *testing.T) { - const want uint32 = 987654321 - mock := NewMockModule(128) - - _, ok := mock.Memory().ReadUint32Le(128) - if ok { - t.Error("want error") - } - - ok = mock.Memory().WriteUint32Le(128, 0) - if ok { - t.Error("want error") - } - - ok = mock.Memory().WriteUint32Le(0, want) - if !ok { - t.Error("want ok") - } - - got, ok := mock.Memory().ReadUint32Le(0) - if !ok { - t.Error("want ok") - } - if got != want { - t.Errorf("got %d, want %d", got, want) - } -} - -func Test_mockMemory_uint64(t *testing.T) { - const want uint64 = 9876543210 - mock := NewMockModule(128) - - _, ok := mock.Memory().ReadUint64Le(128) - if ok { - t.Error("want error") - } - - ok = mock.Memory().WriteUint64Le(128, 0) - if ok { - t.Error("want error") - } - - ok = mock.Memory().WriteUint64Le(0, want) - if !ok { - t.Error("want ok") - } - - got, ok := mock.Memory().ReadUint64Le(0) - if !ok { - t.Error("want ok") - } - if got != want { - t.Errorf("got %d, want %d", got, want) - } -} - -func Test_mockMemory_float32(t *testing.T) { - const want float32 = math.Pi - mock := NewMockModule(128) - - _, ok := mock.Memory().ReadFloat32Le(128) - if ok { - t.Error("want error") - } - - ok = mock.Memory().WriteFloat32Le(128, 0) - if ok { - t.Error("want error") - } - - ok = mock.Memory().WriteFloat32Le(0, want) - if !ok { - t.Error("want ok") - } - - got, ok := mock.Memory().ReadFloat32Le(0) - if !ok { - t.Error("want ok") - } - if got != want { - t.Errorf("got %f, want %f", got, want) - } -} - -func Test_mockMemory_float64(t *testing.T) { - const want float64 = math.Pi - mock := NewMockModule(128) - - _, ok := mock.Memory().ReadFloat64Le(128) - if ok { - t.Error("want error") - } - - ok = mock.Memory().WriteFloat64Le(128, 0) - if ok { - t.Error("want error") - } - - ok = mock.Memory().WriteFloat64Le(0, want) - if !ok { - t.Error("want ok") - } - - got, ok := mock.Memory().ReadFloat64Le(0) - if !ok { - t.Error("want ok") - } - if got != want { - t.Errorf("got %f, want %f", got, want) - } -} - -func Test_mockMemory_bytes(t *testing.T) { - const want string = "\xca\xfe\xba\xbe" - mock := NewMockModule(128) - - _, ok := mock.Memory().Read(128, uint32(len(want))) - if ok { - t.Error("want error") - } - - ok = mock.Memory().Write(128, []byte(want)) - if ok { - t.Error("want error") - } - - ok = mock.Memory().WriteString(128, want) - if ok { - t.Error("want error") - } - - ok = mock.Memory().Write(0, []byte(want)) - if !ok { - t.Error("want ok") - } - - got, ok := mock.Memory().Read(0, uint32(len(want))) - if !ok { - t.Error("want ok") - } - if string(got) != want { - t.Errorf("got %q, want %q", got, want) - } - - ok = mock.Memory().WriteString(64, want) - if !ok { - t.Error("want ok") - } - - got, ok = mock.Memory().Read(64, uint32(len(want))) - if !ok { - t.Error("want ok") - } - if string(got) != want { - t.Errorf("got %q, want %q", got, want) - } -} - -func Test_mockMemory_grow(t *testing.T) { - mock := NewMockModule(128) - - _, ok := mock.Memory().ReadByte(65536) - if ok { - t.Error("want error") - } - - got, ok := mock.Memory().Grow(1) - if !ok { - t.Error("want ok") - } - if got != 1 { - t.Errorf("got %d, want 1", got) - } - - _, ok = mock.Memory().ReadByte(65536) - if !ok { - t.Error("want ok") - } -} diff --git a/sqlite3vfs/lock_test.go b/sqlite3vfs/lock_test.go index 7b98574..9aa4ebb 100644 --- a/sqlite3vfs/lock_test.go +++ b/sqlite3vfs/lock_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/ncruces/go-sqlite3/internal/util" + "github.com/tetratelabs/wazero/experimental/wazerotest" ) func Test_vfsLock(t *testing.T) { @@ -39,7 +40,7 @@ func Test_vfsLock(t *testing.T) { pFile2 = 16 pOutput = 32 ) - mod := util.NewMockModule(128) + mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) ctx, vfs := NewContext(context.TODO()) defer vfs.Close() diff --git a/sqlite3vfs/vfs_test.go b/sqlite3vfs/vfs_test.go index 56dc105..22a60aa 100644 --- a/sqlite3vfs/vfs_test.go +++ b/sqlite3vfs/vfs_test.go @@ -14,10 +14,11 @@ import ( "github.com/ncruces/go-sqlite3/internal/util" "github.com/ncruces/julianday" + "github.com/tetratelabs/wazero/experimental/wazerotest" ) func Test_vfsLocaltime(t *testing.T) { - mod := util.NewMockModule(128) + mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) ctx := context.TODO() tm := time.Now() @@ -53,7 +54,7 @@ func Test_vfsLocaltime(t *testing.T) { } func Test_vfsRandomness(t *testing.T) { - mod := util.NewMockModule(128) + mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) ctx := context.TODO() rc := vfsRandomness(ctx, mod, 0, 16, 4) @@ -68,7 +69,7 @@ func Test_vfsRandomness(t *testing.T) { } func Test_vfsSleep(t *testing.T) { - mod := util.NewMockModule(128) + mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) ctx := context.TODO() now := time.Now() @@ -84,7 +85,7 @@ func Test_vfsSleep(t *testing.T) { } func Test_vfsCurrentTime(t *testing.T) { - mod := util.NewMockModule(128) + mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) ctx := context.TODO() now := time.Now() @@ -100,7 +101,7 @@ func Test_vfsCurrentTime(t *testing.T) { } func Test_vfsCurrentTime64(t *testing.T) { - mod := util.NewMockModule(128) + mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) ctx := context.TODO() now := time.Now() @@ -118,7 +119,7 @@ func Test_vfsCurrentTime64(t *testing.T) { } func Test_vfsFullPathname(t *testing.T) { - mod := util.NewMockModule(128 + _MAX_PATHNAME) + mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) util.WriteString(mod, 4, ".") ctx := context.TODO() @@ -147,7 +148,7 @@ func Test_vfsDelete(t *testing.T) { } file.Close() - mod := util.NewMockModule(128 + _MAX_PATHNAME) + mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) util.WriteString(mod, 4, name) ctx := context.TODO() @@ -178,7 +179,7 @@ func Test_vfsAccess(t *testing.T) { t.Fatal(err) } - mod := util.NewMockModule(128 + _MAX_PATHNAME) + mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) util.WriteString(mod, 8, dir) ctx := context.TODO() @@ -218,7 +219,7 @@ func Test_vfsAccess(t *testing.T) { } func Test_vfsFile(t *testing.T) { - mod := util.NewMockModule(128) + mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) ctx, vfs := NewContext(context.TODO()) defer vfs.Close() @@ -291,7 +292,7 @@ func Test_vfsFile(t *testing.T) { } func Test_vfsFile_psow(t *testing.T) { - mod := util.NewMockModule(128) + mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) ctx, vfs := NewContext(context.TODO()) defer vfs.Close()