github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/sqlite_test.go (about)

     1  package sqlite3
     2  
     3  import (
     4  	"bytes"
     5  	"math"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/ncruces/go-sqlite3/internal/util"
    10  	"github.com/tetratelabs/wazero"
    11  )
    12  
    13  func init() {
    14  	Path = "./embed/sqlite3.wasm"
    15  	RuntimeConfig = wazero.NewRuntimeConfig().WithMemoryLimitPages(1024)
    16  }
    17  
    18  func Test_sqlite_error_OOM(t *testing.T) {
    19  	t.Parallel()
    20  
    21  	sqlite, err := instantiateSQLite()
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	defer sqlite.close()
    26  
    27  	defer func() { _ = recover() }()
    28  	sqlite.error(uint64(NOMEM), 0)
    29  	t.Error("want panic")
    30  }
    31  
    32  func Test_sqlite_call_closed(t *testing.T) {
    33  	t.Parallel()
    34  
    35  	sqlite, err := instantiateSQLite()
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	sqlite.close()
    40  
    41  	defer func() { _ = recover() }()
    42  	sqlite.call("free")
    43  	t.Error("want panic")
    44  }
    45  
    46  func Test_sqlite_new(t *testing.T) {
    47  	t.Parallel()
    48  
    49  	sqlite, err := instantiateSQLite()
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	defer sqlite.close()
    54  
    55  	t.Run("MaxUint32", func(t *testing.T) {
    56  		defer func() { _ = recover() }()
    57  		sqlite.new(math.MaxUint32)
    58  		t.Error("want panic")
    59  	})
    60  
    61  	t.Run("_MAX_ALLOCATION_SIZE", func(t *testing.T) {
    62  		if testing.Short() {
    63  			t.Skip("skipping in short mode")
    64  		}
    65  		if os.Getenv("CI") != "" {
    66  			t.Skip("skipping in CI")
    67  		}
    68  		defer func() { _ = recover() }()
    69  		sqlite.new(_MAX_ALLOCATION_SIZE)
    70  		sqlite.new(_MAX_ALLOCATION_SIZE)
    71  		t.Error("want panic")
    72  	})
    73  }
    74  
    75  func Test_sqlite_newArena(t *testing.T) {
    76  	t.Parallel()
    77  
    78  	sqlite, err := instantiateSQLite()
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	defer sqlite.close()
    83  
    84  	arena := sqlite.newArena(16)
    85  	defer arena.free()
    86  
    87  	const title = "Lorem ipsum"
    88  	ptr := arena.string(title)
    89  	if ptr == 0 {
    90  		t.Fatalf("got nullptr")
    91  	}
    92  	if got := util.ReadString(sqlite.mod, ptr, math.MaxUint32); got != title {
    93  		t.Errorf("got %q, want %q", got, title)
    94  	}
    95  
    96  	const body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
    97  	ptr = arena.string(body)
    98  	if ptr == 0 {
    99  		t.Fatalf("got nullptr")
   100  	}
   101  	if got := util.ReadString(sqlite.mod, ptr, math.MaxUint32); got != body {
   102  		t.Errorf("got %q, want %q", got, body)
   103  	}
   104  
   105  	ptr = arena.bytes(nil)
   106  	if ptr != 0 {
   107  		t.Errorf("want nullptr")
   108  	}
   109  	ptr = arena.bytes([]byte(title))
   110  	if ptr == 0 {
   111  		t.Fatalf("got nullptr")
   112  	}
   113  	if got := util.View(sqlite.mod, ptr, uint64(len(title))); string(got) != title {
   114  		t.Errorf("got %q, want %q", got, title)
   115  	}
   116  
   117  	arena.free()
   118  }
   119  
   120  func Test_sqlite_newBytes(t *testing.T) {
   121  	t.Parallel()
   122  
   123  	sqlite, err := instantiateSQLite()
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  	defer sqlite.close()
   128  
   129  	ptr := sqlite.newBytes(nil)
   130  	if ptr != 0 {
   131  		t.Errorf("got %#x, want nullptr", ptr)
   132  	}
   133  
   134  	buf := []byte("sqlite3")
   135  	ptr = sqlite.newBytes(buf)
   136  	if ptr == 0 {
   137  		t.Fatal("got nullptr, want a pointer")
   138  	}
   139  
   140  	want := buf
   141  	if got := util.View(sqlite.mod, ptr, uint64(len(want))); !bytes.Equal(got, want) {
   142  		t.Errorf("got %q, want %q", got, want)
   143  	}
   144  
   145  	ptr = sqlite.newBytes(buf[:0])
   146  	if ptr == 0 {
   147  		t.Fatal("got nullptr, want a pointer")
   148  	}
   149  
   150  	if got := util.View(sqlite.mod, ptr, 0); got != nil {
   151  		t.Errorf("got %q, want nil", got)
   152  	}
   153  }
   154  
   155  func Test_sqlite_newString(t *testing.T) {
   156  	t.Parallel()
   157  
   158  	sqlite, err := instantiateSQLite()
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  	defer sqlite.close()
   163  
   164  	ptr := sqlite.newString("")
   165  	if ptr == 0 {
   166  		t.Error("got nullptr, want a pointer")
   167  	}
   168  
   169  	str := "sqlite3\000sqlite3"
   170  	ptr = sqlite.newString(str)
   171  	if ptr == 0 {
   172  		t.Fatal("got nullptr, want a pointer")
   173  	}
   174  
   175  	want := str + "\000"
   176  	if got := util.View(sqlite.mod, ptr, uint64(len(want))); string(got) != want {
   177  		t.Errorf("got %q, want %q", got, want)
   178  	}
   179  }
   180  
   181  func Test_sqlite_getString(t *testing.T) {
   182  	t.Parallel()
   183  
   184  	sqlite, err := instantiateSQLite()
   185  	if err != nil {
   186  		t.Fatal(err)
   187  	}
   188  	defer sqlite.close()
   189  
   190  	ptr := sqlite.newString("")
   191  	if ptr == 0 {
   192  		t.Error("got nullptr, want a pointer")
   193  	}
   194  
   195  	str := "sqlite3" + "\000 drop this"
   196  	ptr = sqlite.newString(str)
   197  	if ptr == 0 {
   198  		t.Fatal("got nullptr, want a pointer")
   199  	}
   200  
   201  	want := "sqlite3"
   202  	if got := util.ReadString(sqlite.mod, ptr, math.MaxUint32); got != want {
   203  		t.Errorf("got %q, want %q", got, want)
   204  	}
   205  	if got := util.ReadString(sqlite.mod, ptr, 0); got != "" {
   206  		t.Errorf("got %q, want empty", got)
   207  	}
   208  
   209  	func() {
   210  		defer func() { _ = recover() }()
   211  		util.ReadString(sqlite.mod, ptr, uint32(len(want)/2))
   212  		t.Error("want panic")
   213  	}()
   214  
   215  	func() {
   216  		defer func() { _ = recover() }()
   217  		util.ReadString(sqlite.mod, 0, math.MaxUint32)
   218  		t.Error("want panic")
   219  	}()
   220  }
   221  
   222  func Test_sqlite_free(t *testing.T) {
   223  	t.Parallel()
   224  
   225  	sqlite, err := instantiateSQLite()
   226  	if err != nil {
   227  		t.Fatal(err)
   228  	}
   229  	defer sqlite.close()
   230  
   231  	sqlite.free(0)
   232  
   233  	ptr := sqlite.new(1)
   234  	if ptr == 0 {
   235  		t.Error("got nullptr, want a pointer")
   236  	}
   237  
   238  	sqlite.free(ptr)
   239  }