More tests.

This commit is contained in:
Nuno Cruces
2023-01-26 11:12:00 +00:00
parent 591828031e
commit 09c7c7af3f
4 changed files with 154 additions and 13 deletions

31
.github/coverage.html vendored
View File

@@ -59,7 +59,7 @@
<option value="file1">github.com/ncruces/go-sqlite3/compile.go (54.5%)</option>
<option value="file2">github.com/ncruces/go-sqlite3/conn.go (73.9%)</option>
<option value="file2">github.com/ncruces/go-sqlite3/conn.go (86.8%)</option>
<option value="file3">github.com/ncruces/go-sqlite3/error.go (54.5%)</option>
@@ -233,6 +233,7 @@ func compile() <span class="cov8" title="1">{
import (
"bytes"
"context"
"math"
"strconv"
"github.com/tetratelabs/wazero"
@@ -378,7 +379,7 @@ func (c *Conn) error(rc uint64) error <span class="cov8" title="1">{
}
func (c *Conn) free(ptr uint32) <span class="cov8" title="1">{
if ptr == 0 </span><span class="cov0" title="0">{
if ptr == 0 </span><span class="cov8" title="1">{
return
}</span>
<span class="cov8" title="1">_, err := c.api.free.Call(c.ctx, uint64(ptr))
@@ -393,18 +394,18 @@ func (c *Conn) new(len uint32) uint32 <span class="cov8" title="1">{
panic(err)</span>
}
<span class="cov8" title="1">ptr := uint32(r[0])
if ptr == 0 || ptr &gt;= c.memory.Size() </span><span class="cov0" title="0">{
if ptr == 0 || ptr &gt;= c.memory.Size() </span><span class="cov8" title="1">{
panic(oomErr)</span>
}
<span class="cov8" title="1">return ptr</span>
}
func (c *Conn) newBytes(s []byte) uint32 <span class="cov0" title="0">{
if s == nil </span><span class="cov0" title="0">{
func (c *Conn) newBytes(s []byte) uint32 <span class="cov8" title="1">{
if s == nil </span><span class="cov8" title="1">{
return 0
}</span>
<span class="cov0" title="0">siz := uint32(len(s))
<span class="cov8" title="1">siz := uint32(len(s))
ptr := c.new(siz)
mem, ok := c.memory.Read(ptr, siz)
if !ok </span><span class="cov0" title="0">{
@@ -412,7 +413,7 @@ func (c *Conn) newBytes(s []byte) uint32 <span class="cov0" title="0">{
panic(rangeErr)</span>
}
<span class="cov0" title="0">copy(mem, s)
<span class="cov8" title="1">copy(mem, s)
return ptr</span>
}
@@ -435,17 +436,25 @@ func (c *Conn) getString(ptr, maxlen uint32) string <span class="cov8" title="1"
}</span>
func getString(memory api.Memory, ptr, maxlen uint32) string <span class="cov8" title="1">{
if ptr == 0 </span><span class="cov0" title="0">{
if ptr == 0 </span><span class="cov8" title="1">{
panic(nilErr)</span>
}
<span class="cov8" title="1">mem, ok := memory.Read(ptr, maxlen+1)
if !ok </span><span class="cov0" title="0">{
<span class="cov8" title="1">switch maxlen </span>{
case 0:<span class="cov8" title="1">
return ""</span>
case math.MaxUint32:<span class="cov8" title="1"></span>
//
default:<span class="cov8" title="1">
maxlen = maxlen + 1</span>
}
<span class="cov8" title="1">mem, ok := memory.Read(ptr, maxlen)
if !ok </span><span class="cov8" title="1">{
mem, ok = memory.Read(ptr, memory.Size()-ptr)
if !ok </span><span class="cov0" title="0">{
panic(rangeErr)</span>
}
}
<span class="cov8" title="1">if i := bytes.IndexByte(mem, 0); i &lt; 0 </span><span class="cov0" title="0">{
<span class="cov8" title="1">if i := bytes.IndexByte(mem, 0); i &lt; 0 </span><span class="cov8" title="1">{
panic(noNulErr)</span>
} else<span class="cov8" title="1"> {
return string(mem[:i])

View File

@@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="108" height="20" role="img" aria-label="coverage: 56.6%"><title>coverage: 56.6%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="108" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="61" height="20" fill="#555"/><rect x="61" width="47" height="20" fill="#fe7d37"/><rect width="108" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">coverage</text><text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="510">coverage</text><text aria-hidden="true" x="835" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="370">56.6%</text><text x="835" y="140" transform="scale(.1)" fill="#fff" textLength="370">56.6%</text></g></svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="108" height="20" role="img" aria-label="coverage: 59.0%"><title>coverage: 59.0%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="108" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="61" height="20" fill="#555"/><rect x="61" width="47" height="20" fill="#fe7d37"/><rect width="108" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">coverage</text><text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="510">coverage</text><text aria-hidden="true" x="835" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="370">59.0%</text><text x="835" y="140" transform="scale(.1)" fill="#fff" textLength="370">59.0%</text></g></svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

11
conn.go
View File

@@ -3,6 +3,7 @@ package sqlite3
import (
"bytes"
"context"
"math"
"strconv"
"github.com/tetratelabs/wazero"
@@ -208,7 +209,15 @@ func getString(memory api.Memory, ptr, maxlen uint32) string {
if ptr == 0 {
panic(nilErr)
}
mem, ok := memory.Read(ptr, maxlen+1)
switch maxlen {
case 0:
return ""
case math.MaxUint32:
//
default:
maxlen = maxlen + 1
}
mem, ok := memory.Read(ptr, maxlen)
if !ok {
mem, ok = memory.Read(ptr, memory.Size()-ptr)
if !ok {

123
conn_test.go Normal file
View File

@@ -0,0 +1,123 @@
package sqlite3
import (
"bytes"
"math"
"testing"
)
func TestConn_new(t *testing.T) {
db, err := Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
defer func() { _ = recover() }()
db.new(math.MaxUint32)
t.Errorf("should have panicked")
}
func TestConn_newBytes(t *testing.T) {
db, err := Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
ptr := db.newBytes(nil)
if ptr != 0 {
t.Errorf("want nullptr got %x", ptr)
}
buf := []byte("sqlite3")
ptr = db.newBytes(buf)
if ptr == 0 {
t.Errorf("want a pointer got nullptr")
}
want := buf
if got, ok := db.memory.Read(ptr, uint32(len(want))); !ok || !bytes.Equal(want, got) {
t.Errorf("want %q got %q", want, got)
}
}
func TestConn_newString(t *testing.T) {
db, err := Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
ptr := db.newString("")
if ptr == 0 {
t.Errorf("want a pointer got nullptr")
}
str := "sqlite3\000sqlite3"
ptr = db.newString(str)
if ptr == 0 {
t.Errorf("want a pointer got nullptr")
}
want := str + "\000"
if got, ok := db.memory.Read(ptr, uint32(len(want))); !ok || want != string(got) {
t.Errorf("want %q got %q", want, got)
}
}
func TestConn_getString(t *testing.T) {
db, err := Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
ptr := db.newString("")
if ptr == 0 {
t.Errorf("want a pointer got nullptr")
}
str := "sqlite3" + "\000 drop this"
ptr = db.newString(str)
if ptr == 0 {
t.Errorf("want a pointer got nullptr")
}
want := "sqlite3"
if got := db.getString(ptr, math.MaxUint32); want != got {
t.Errorf("want %q got %q", want, got)
}
if got := db.getString(ptr, 0); got != "" {
t.Errorf("want empty got %q", got)
}
func() {
defer func() { _ = recover() }()
db.getString(ptr, uint32(len(want)/2))
t.Errorf("should have panicked")
}()
func() {
defer func() { _ = recover() }()
db.getString(0, math.MaxUint32)
t.Errorf("should have panicked")
}()
}
func TestConn_free(t *testing.T) {
db, err := Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
db.free(0)
ptr := db.new(0)
if ptr == 0 {
t.Errorf("want a pointer got nullptr")
}
db.free(ptr)
}