2023-02-18 02:16:11 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql/driver"
|
2025-01-24 10:46:05 +00:00
|
|
|
"slices"
|
2023-02-18 02:16:11 +00:00
|
|
|
"testing"
|
2024-12-19 14:52:25 +00:00
|
|
|
|
|
|
|
|
_ "github.com/ncruces/go-sqlite3/embed"
|
|
|
|
|
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
|
2023-02-18 02:16:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Test_namedValues(t *testing.T) {
|
|
|
|
|
want := []driver.NamedValue{
|
|
|
|
|
{Ordinal: 1, Value: true},
|
|
|
|
|
{Ordinal: 2, Value: false},
|
|
|
|
|
}
|
|
|
|
|
got := namedValues([]driver.Value{true, false})
|
2025-01-24 10:46:05 +00:00
|
|
|
if !slices.Equal(got, want) {
|
2023-02-18 02:16:11 +00:00
|
|
|
t.Errorf("got %v, want %v", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-19 14:52:25 +00:00
|
|
|
|
|
|
|
|
func Fuzz_notWhitespace(f *testing.F) {
|
|
|
|
|
f.Add("")
|
|
|
|
|
f.Add(" ")
|
|
|
|
|
f.Add(";")
|
|
|
|
|
f.Add("0")
|
|
|
|
|
f.Add("-")
|
|
|
|
|
f.Add("-0")
|
|
|
|
|
f.Add("--")
|
|
|
|
|
f.Add("--0")
|
|
|
|
|
f.Add("--\n")
|
|
|
|
|
f.Add("--0\n")
|
|
|
|
|
f.Add("/0")
|
|
|
|
|
f.Add("/*")
|
|
|
|
|
f.Add("/*/")
|
|
|
|
|
f.Add("/**")
|
|
|
|
|
f.Add("/*0")
|
|
|
|
|
f.Add("/**/")
|
|
|
|
|
f.Add("/***/")
|
|
|
|
|
f.Add("/**0/")
|
|
|
|
|
f.Add("\v")
|
|
|
|
|
f.Add(" \v")
|
|
|
|
|
f.Add("\xf0")
|
|
|
|
|
f.Add("\000")
|
|
|
|
|
|
|
|
|
|
db, err := Open(":memory:")
|
|
|
|
|
if err != nil {
|
|
|
|
|
f.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
|
|
f.Fuzz(func(t *testing.T, str string) {
|
|
|
|
|
if len(str) > 128 {
|
|
|
|
|
t.SkipNow()
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-23 12:51:36 +01:00
|
|
|
c, err := db.Conn(t.Context())
|
2024-12-19 14:52:25 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
|
|
c.Raw(func(driverConn any) error {
|
|
|
|
|
conn := driverConn.(*conn).Conn
|
|
|
|
|
stmt, tail, err := conn.Prepare(str)
|
|
|
|
|
stmt.Close()
|
|
|
|
|
|
|
|
|
|
// It's hard to be bug for bug compatible with SQLite.
|
|
|
|
|
// We settle for somewhat less:
|
|
|
|
|
// - if SQLite reports whitespace, we must too
|
|
|
|
|
// - if we report whitespace, SQLite must not parse a statement
|
|
|
|
|
if notWhitespace(str) {
|
|
|
|
|
if stmt == nil && tail == "" && err == nil {
|
|
|
|
|
t.Errorf("was whitespace: %q", str)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if stmt != nil {
|
|
|
|
|
t.Errorf("was not whitespace: %q (%v)", str, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|