Files
sqlite3/sqlite_test.go

240 lines
4.6 KiB
Go
Raw Normal View History

2023-01-26 11:12:00 +00:00
package sqlite3
import (
"bytes"
"math"
2023-10-16 12:26:25 +01:00
"os"
2023-01-26 11:12:00 +00:00
"testing"
2023-03-29 15:01:25 +01:00
"github.com/ncruces/go-sqlite3/internal/util"
2024-04-24 01:07:17 +01:00
"github.com/tetratelabs/wazero"
2023-01-26 11:12:00 +00:00
)
2023-03-29 15:01:25 +01:00
func init() {
Path = "./embed/sqlite3.wasm"
2024-04-24 01:07:17 +01:00
RuntimeConfig = wazero.NewRuntimeConfig().WithMemoryLimitPages(1024)
2023-03-29 15:01:25 +01:00
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_error_OOM(t *testing.T) {
2023-02-27 03:20:23 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-02-27 03:20:23 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-02-27 03:20:23 +00:00
defer func() { _ = recover() }()
2023-07-04 02:29:38 +01:00
sqlite.error(uint64(NOMEM), 0)
2023-02-27 03:20:23 +00:00
t.Error("want panic")
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_call_closed(t *testing.T) {
2023-02-24 17:49:16 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-02-24 17:49:16 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
sqlite.close()
2023-02-24 17:49:16 +00:00
defer func() { _ = recover() }()
2023-11-30 17:52:35 +00:00
sqlite.call("free")
2023-02-24 17:49:16 +00:00
t.Error("want panic")
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_new(t *testing.T) {
2023-02-15 16:24:34 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-01-26 11:12:00 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-01-26 11:12:00 +00:00
2023-04-14 00:09:40 +01:00
t.Run("MaxUint32", func(t *testing.T) {
2023-02-27 03:20:23 +00:00
defer func() { _ = recover() }()
2023-07-04 02:29:38 +01:00
sqlite.new(math.MaxUint32)
2023-02-27 03:20:23 +00:00
t.Error("want panic")
2023-04-14 00:09:40 +01:00
})
2023-02-27 03:20:23 +00:00
2023-04-14 00:09:40 +01:00
t.Run("_MAX_ALLOCATION_SIZE", func(t *testing.T) {
2023-10-16 12:26:25 +01:00
if testing.Short() {
t.Skip("skipping in short mode")
}
if os.Getenv("CI") != "" {
t.Skip("skipping in CI")
}
2023-04-14 00:09:40 +01:00
defer func() { _ = recover() }()
2023-07-04 02:29:38 +01:00
sqlite.new(_MAX_ALLOCATION_SIZE)
sqlite.new(_MAX_ALLOCATION_SIZE)
2023-04-14 00:09:40 +01:00
t.Error("want panic")
})
2023-01-26 11:12:00 +00:00
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_newArena(t *testing.T) {
2023-02-15 16:24:34 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-02-14 11:34:24 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-02-14 11:34:24 +00:00
2023-07-04 02:29:38 +01:00
arena := sqlite.newArena(16)
2023-02-24 14:31:41 +00:00
defer arena.free()
2023-02-14 11:34:24 +00:00
const title = "Lorem ipsum"
ptr := arena.string(title)
if ptr == 0 {
t.Fatalf("got nullptr")
}
2023-07-04 02:29:38 +01:00
if got := util.ReadString(sqlite.mod, ptr, math.MaxUint32); got != title {
2023-02-14 11:34:24 +00:00
t.Errorf("got %q, want %q", got, title)
}
const body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
ptr = arena.string(body)
if ptr == 0 {
t.Fatalf("got nullptr")
}
2023-07-04 02:29:38 +01:00
if got := util.ReadString(sqlite.mod, ptr, math.MaxUint32); got != body {
2023-02-14 11:34:24 +00:00
t.Errorf("got %q, want %q", got, body)
}
2023-05-29 16:50:48 +01:00
ptr = arena.bytes(nil)
if ptr != 0 {
t.Errorf("want nullptr")
}
ptr = arena.bytes([]byte(title))
if ptr == 0 {
t.Fatalf("got nullptr")
}
2023-07-04 02:29:38 +01:00
if got := util.View(sqlite.mod, ptr, uint64(len(title))); string(got) != title {
2023-05-29 16:50:48 +01:00
t.Errorf("got %q, want %q", got, title)
}
2023-02-24 14:31:41 +00:00
arena.free()
2023-02-14 11:34:24 +00:00
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_newBytes(t *testing.T) {
2023-02-15 16:24:34 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-01-26 11:12:00 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-01-26 11:12:00 +00:00
2023-07-04 02:29:38 +01:00
ptr := sqlite.newBytes(nil)
2023-01-26 11:12:00 +00:00
if ptr != 0 {
2023-02-10 16:42:49 +00:00
t.Errorf("got %#x, want nullptr", ptr)
2023-01-26 11:12:00 +00:00
}
buf := []byte("sqlite3")
2023-07-04 02:29:38 +01:00
ptr = sqlite.newBytes(buf)
2023-01-26 11:12:00 +00:00
if ptr == 0 {
2023-01-26 12:15:34 +00:00
t.Fatal("got nullptr, want a pointer")
2023-01-26 11:12:00 +00:00
}
want := buf
2023-07-04 02:29:38 +01:00
if got := util.View(sqlite.mod, ptr, uint64(len(want))); !bytes.Equal(got, want) {
2023-01-26 12:15:34 +00:00
t.Errorf("got %q, want %q", got, want)
2023-01-26 11:12:00 +00:00
}
2023-10-13 17:06:05 +01:00
ptr = sqlite.newBytes(buf[:0])
if ptr == 0 {
t.Fatal("got nullptr, want a pointer")
}
if got := util.View(sqlite.mod, ptr, 0); got != nil {
t.Errorf("got %q, want nil", got)
}
2023-01-26 11:12:00 +00:00
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_newString(t *testing.T) {
2023-02-15 16:24:34 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-01-26 11:12:00 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-01-26 11:12:00 +00:00
2023-07-04 02:29:38 +01:00
ptr := sqlite.newString("")
2023-01-26 11:12:00 +00:00
if ptr == 0 {
2023-01-26 12:15:34 +00:00
t.Error("got nullptr, want a pointer")
2023-01-26 11:12:00 +00:00
}
str := "sqlite3\000sqlite3"
2023-07-04 02:29:38 +01:00
ptr = sqlite.newString(str)
2023-01-26 11:12:00 +00:00
if ptr == 0 {
2023-01-26 12:15:34 +00:00
t.Fatal("got nullptr, want a pointer")
2023-01-26 11:12:00 +00:00
}
want := str + "\000"
2023-07-04 02:29:38 +01:00
if got := util.View(sqlite.mod, ptr, uint64(len(want))); string(got) != want {
2023-01-26 12:15:34 +00:00
t.Errorf("got %q, want %q", got, want)
2023-01-26 11:12:00 +00:00
}
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_getString(t *testing.T) {
2023-02-15 16:24:34 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-01-26 11:12:00 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-01-26 11:12:00 +00:00
2023-07-04 02:29:38 +01:00
ptr := sqlite.newString("")
2023-01-26 11:12:00 +00:00
if ptr == 0 {
2023-01-26 12:15:34 +00:00
t.Error("got nullptr, want a pointer")
2023-01-26 11:12:00 +00:00
}
str := "sqlite3" + "\000 drop this"
2023-07-04 02:29:38 +01:00
ptr = sqlite.newString(str)
2023-01-26 11:12:00 +00:00
if ptr == 0 {
2023-01-26 12:15:34 +00:00
t.Fatal("got nullptr, want a pointer")
2023-01-26 11:12:00 +00:00
}
want := "sqlite3"
2023-07-04 02:29:38 +01:00
if got := util.ReadString(sqlite.mod, ptr, math.MaxUint32); got != want {
2023-01-26 12:15:34 +00:00
t.Errorf("got %q, want %q", got, want)
2023-01-26 11:12:00 +00:00
}
2023-07-04 02:29:38 +01:00
if got := util.ReadString(sqlite.mod, ptr, 0); got != "" {
2023-01-26 12:15:34 +00:00
t.Errorf("got %q, want empty", got)
2023-01-26 11:12:00 +00:00
}
func() {
defer func() { _ = recover() }()
2023-07-04 02:29:38 +01:00
util.ReadString(sqlite.mod, ptr, uint32(len(want)/2))
2023-02-10 16:42:49 +00:00
t.Error("want panic")
2023-01-26 11:12:00 +00:00
}()
func() {
defer func() { _ = recover() }()
2023-07-04 02:29:38 +01:00
util.ReadString(sqlite.mod, 0, math.MaxUint32)
2023-02-10 16:42:49 +00:00
t.Error("want panic")
2023-01-26 11:12:00 +00:00
}()
}
2023-07-04 02:29:38 +01:00
func Test_sqlite_free(t *testing.T) {
2023-02-15 16:24:34 +00:00
t.Parallel()
2023-07-04 02:29:38 +01:00
sqlite, err := instantiateSQLite()
2023-01-26 11:12:00 +00:00
if err != nil {
t.Fatal(err)
}
2023-07-04 02:29:38 +01:00
defer sqlite.close()
2023-01-26 11:12:00 +00:00
2023-07-04 02:29:38 +01:00
sqlite.free(0)
2023-01-26 11:12:00 +00:00
2023-07-04 02:29:38 +01:00
ptr := sqlite.new(1)
2023-01-26 11:12:00 +00:00
if ptr == 0 {
2023-01-26 12:15:34 +00:00
t.Error("got nullptr, want a pointer")
2023-01-26 11:12:00 +00:00
}
2023-07-04 02:29:38 +01:00
sqlite.free(ptr)
2023-01-26 11:12:00 +00:00
}