diff --git a/.github/coverage.svg b/.github/coverage.svg
index c7f0b3f..f2b64de 100644
--- a/.github/coverage.svg
+++ b/.github/coverage.svg
@@ -1 +1 @@
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/conn_test.go b/conn_test.go
index 1134636..6775f3f 100644
--- a/conn_test.go
+++ b/conn_test.go
@@ -1,6 +1,7 @@
package sqlite3_test
import (
+ "errors"
"os"
"path/filepath"
"testing"
@@ -23,6 +24,23 @@ func TestOpen_file(t *testing.T) {
testOpen(t, filepath.Join(dir, "test.db"))
}
+func TestOpen_dir(t *testing.T) {
+ _, err := sqlite3.Open(".")
+ if err == nil {
+ t.Fatal("want error")
+ }
+ var serr *sqlite3.Error
+ if !errors.As(err, &serr) {
+ t.Fatal("want sqlite3.Error")
+ }
+ if serr.Code != sqlite3.CANTOPEN {
+ t.Fatal("want sqlite3.CANTOPEN")
+ }
+ if got := err.Error(); got != "sqlite3: unable to open database file" {
+ t.Fatal("got message: ", got)
+ }
+}
+
func testOpen(t *testing.T, name string) {
db, err := sqlite3.Open(name)
if err != nil {
@@ -51,17 +69,17 @@ func testOpen(t *testing.T, name string) {
idx := 0
for ; stmt.Step(); idx++ {
if ids[idx] != stmt.ColumnInt(0) {
- t.Errorf("expected %d got %d", ids[idx], stmt.ColumnInt(0))
+ t.Errorf("want %d got %d", ids[idx], stmt.ColumnInt(0))
}
if names[idx] != stmt.ColumnText(1) {
- t.Errorf("expected %q got %q", names[idx], stmt.ColumnText(1))
+ t.Errorf("want %q got %q", names[idx], stmt.ColumnText(1))
}
}
if err := stmt.Err(); err != nil {
t.Fatal(err)
}
if idx != 3 {
- t.Errorf("expected %d rows got %d", len(ids), idx)
+ t.Errorf("want %d rows got %d", len(ids), idx)
}
err = stmt.Close()
diff --git a/mock_test.go b/mock_test.go
new file mode 100644
index 0000000..f0bb0ed
--- /dev/null
+++ b/mock_test.go
@@ -0,0 +1,157 @@
+package sqlite3
+
+import (
+ "context"
+ "encoding/binary"
+ "math"
+
+ "github.com/tetratelabs/wazero/api"
+)
+
+var (
+ _ api.Module = &MockModule{}
+ _ api.Memory = &MockMemory{}
+)
+
+type MockModule struct {
+ memory api.Memory
+}
+
+func (m *MockModule) Memory() api.Memory { return m.memory }
+func (m *MockModule) String() string { return "MockModule" }
+func (m *MockModule) Name() string { return "MockModule" }
+
+func (m *MockModule) ExportedGlobal(name string) api.Global { return nil }
+func (m *MockModule) ExportedMemory(name string) api.Memory { return nil }
+func (m *MockModule) ExportedFunction(name string) api.Function { return nil }
+func (m *MockModule) ExportedMemoryDefinitions() map[string]api.MemoryDefinition { return nil }
+func (m *MockModule) ExportedFunctionDefinitions() map[string]api.FunctionDefinition { return nil }
+func (m *MockModule) CloseWithExitCode(ctx context.Context, exitCode uint32) error { return nil }
+func (m *MockModule) Close(context.Context) error { return nil }
+
+type MockMemory []byte
+
+func (m MockMemory) Definition() api.MemoryDefinition { return nil }
+
+func (m MockMemory) Size() uint32 { return uint32(len(m)) }
+
+func (m MockMemory) ReadByte(offset uint32) (byte, bool) {
+ if offset >= m.Size() {
+ return 0, false
+ }
+ return m[offset], true
+}
+
+func (m MockMemory) ReadUint16Le(offset uint32) (uint16, bool) {
+ if !m.hasSize(offset, 2) {
+ return 0, false
+ }
+ return binary.LittleEndian.Uint16(m[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[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[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[offset : offset+byteCount : offset+byteCount], true
+}
+
+func (m MockMemory) WriteByte(offset uint32, v byte) bool {
+ if offset >= m.Size() {
+ return false
+ }
+ m[offset] = v
+ return true
+}
+
+func (m MockMemory) WriteUint16Le(offset uint32, v uint16) bool {
+ if !m.hasSize(offset, 2) {
+ return false
+ }
+ binary.LittleEndian.PutUint16(m[offset:], v)
+ return true
+}
+
+func (m MockMemory) WriteUint32Le(offset, v uint32) bool {
+ if !m.hasSize(offset, 4) {
+ return false
+ }
+ binary.LittleEndian.PutUint32(m[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[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[offset:], val)
+ return true
+}
+
+func (m MockMemory) WriteString(offset uint32, val string) bool {
+ if !m.hasSize(offset, uint32(len(val))) {
+ return false
+ }
+ copy(m[offset:], val)
+ return true
+}
+
+func (m *MockMemory) Grow(delta uint32) (result uint32, ok bool) {
+ mem := append(*m, make([]byte, 65536)...)
+ m = &mem
+ return delta, true
+}
+
+func (m MockMemory) PageSize() (result uint32) {
+ return uint32(len(m) / 65536)
+}
+
+func (m MockMemory) hasSize(offset uint32, byteCount uint32) bool {
+ return uint64(offset)+uint64(byteCount) <= uint64(len(m))
+}
diff --git a/vfs_test.go b/vfs_test.go
new file mode 100644
index 0000000..f286b28
--- /dev/null
+++ b/vfs_test.go
@@ -0,0 +1,135 @@
+package sqlite3
+
+import (
+ "bytes"
+ "context"
+ "math/rand"
+ "testing"
+ "time"
+
+ "github.com/ncruces/julianday"
+)
+
+func Test_vfsLocaltime(t *testing.T) {
+ memory := make(MockMemory, 128)
+ module := &MockModule{&memory}
+
+ memory.Write(0, []byte("zero"))
+
+ rc := vfsLocaltime(context.TODO(), module, 0, 4)
+ if rc != 0 {
+ t.Fatal("returned", rc)
+ }
+
+ epoch := time.Unix(0, 0)
+ if z, _ := memory.Read(0, 4); !bytes.Equal(z, []byte("zero")) {
+ t.Fatal("overwrote zero address")
+ }
+ if s, _ := memory.ReadUint32Le(4 + 0*4); int(s) != epoch.Second() {
+ t.Fatal("wrong second")
+ }
+ if m, _ := memory.ReadUint32Le(4 + 1*4); int(m) != epoch.Minute() {
+ t.Fatal("wrong minute")
+ }
+ if h, _ := memory.ReadUint32Le(4 + 2*4); int(h) != epoch.Hour() {
+ t.Fatal("wrong hour")
+ }
+ if d, _ := memory.ReadUint32Le(4 + 3*4); int(d) != epoch.Day() {
+ t.Fatal("wrong day")
+ }
+ if m, _ := memory.ReadUint32Le(4 + 4*4); time.Month(1+m) != epoch.Month() {
+ t.Fatal("wrong month")
+ }
+ if y, _ := memory.ReadUint32Le(4 + 5*4); 1900+int(y) != epoch.Year() {
+ t.Fatal("wrong year")
+ }
+ if w, _ := memory.ReadUint32Le(4 + 6*4); time.Weekday(w) != epoch.Weekday() {
+ t.Fatal("wrong weekday")
+ }
+ if d, _ := memory.ReadUint32Le(4 + 7*4); int(d) != epoch.YearDay()-1 {
+ t.Fatal("wrong yearday")
+ }
+}
+
+func Test_vfsRandomness(t *testing.T) {
+ memory := make(MockMemory, 128)
+ module := &MockModule{&memory}
+
+ memory.Write(0, []byte("zero"))
+
+ rand.Seed(0)
+ rc := vfsRandomness(context.TODO(), module, 0, 16, 4)
+ if rc != 16 {
+ t.Fatal("returned", rc)
+ }
+
+ if z, _ := memory.Read(0, 4); !bytes.Equal(z, []byte("zero")) {
+ t.Fatal("overwrote zero address")
+ }
+
+ var want [16]byte
+ rand.Seed(0)
+ rand.Read(want[:])
+
+ if got, _ := memory.Read(4, 16); !bytes.Equal(got, want[:]) {
+ t.Fatalf("got %q, want %q", got, want)
+ }
+}
+
+func Test_vfsSleep(t *testing.T) {
+ start := time.Now()
+
+ rc := vfsSleep(context.TODO(), 0, 123456)
+ if rc != 0 {
+ t.Fatal("returned", rc)
+ }
+
+ want := 123456 * time.Microsecond
+ if got := time.Since(start); got < want {
+ t.Fatalf("got %v, want %v", got, want)
+ }
+}
+
+func Test_vfsCurrentTime(t *testing.T) {
+ memory := make(MockMemory, 128)
+ module := &MockModule{&memory}
+
+ memory.Write(0, []byte("zero"))
+
+ now := time.Now()
+ rc := vfsCurrentTime(context.TODO(), module, 0, 4)
+ if rc != 0 {
+ t.Fatal("returned", rc)
+ }
+
+ if z, _ := memory.Read(0, 4); !bytes.Equal(z, []byte("zero")) {
+ t.Fatal("overwrote zero address")
+ }
+ want := julianday.Float(now)
+ if got, _ := memory.ReadFloat64Le(4); float32(got) != float32(want) {
+ t.Fatalf("got %v, want %v", got, want)
+ }
+}
+
+func Test_vfsCurrentTime64(t *testing.T) {
+ memory := make(MockMemory, 128)
+ module := &MockModule{&memory}
+
+ memory.Write(0, []byte("zero"))
+
+ now := time.Now()
+ time.Sleep(time.Millisecond)
+ rc := vfsCurrentTime64(context.TODO(), module, 0, 4)
+ if rc != 0 {
+ t.Fatal("returned", rc)
+ }
+
+ if z, _ := memory.Read(0, 4); !bytes.Equal(z, []byte("zero")) {
+ t.Fatal("overwrote zero address")
+ }
+ day, nsec := julianday.Date(now)
+ want := day*86_400_000 + nsec/1_000_000
+ if got, _ := memory.ReadUint64Le(4); int64(got)-want > 100 {
+ t.Fatalf("got %v, want %v", got, want)
+ }
+}