Files
sqlite3/vfs_test.go

282 lines
5.8 KiB
Go
Raw Normal View History

2023-01-26 02:48:31 +00:00
package sqlite3
import (
"bytes"
"context"
2023-01-26 12:15:34 +00:00
"errors"
"io/fs"
"os"
2023-01-28 12:47:39 +00:00
"path/filepath"
2023-02-21 12:51:52 +00:00
"syscall"
2023-01-26 02:48:31 +00:00
"testing"
"time"
"github.com/ncruces/julianday"
)
2023-01-28 12:47:39 +00:00
func Test_vfsExit(t *testing.T) {
mem := newMemory(128)
2023-03-07 03:51:07 +00:00
ctx := context.TODO()
2023-01-28 12:47:39 +00:00
defer func() { _ = recover() }()
2023-03-07 03:51:07 +00:00
vfsExit(ctx, mem.mod, 1)
2023-02-10 16:42:49 +00:00
t.Error("want panic")
2023-01-28 12:47:39 +00:00
}
2023-01-26 02:48:31 +00:00
func Test_vfsLocaltime(t *testing.T) {
2023-01-26 14:52:38 +00:00
mem := newMemory(128)
2023-03-07 03:51:07 +00:00
ctx := context.TODO()
2023-01-26 02:48:31 +00:00
2023-03-07 03:51:07 +00:00
rc := vfsLocaltime(ctx, mem.mod, 0, 4)
2023-01-26 02:48:31 +00:00
if rc != 0 {
t.Fatal("returned", rc)
}
epoch := time.Unix(0, 0)
2023-01-26 14:52:38 +00:00
if s := mem.readUint32(4 + 0*4); int(s) != epoch.Second() {
2023-01-26 12:15:34 +00:00
t.Error("wrong second")
2023-01-26 02:48:31 +00:00
}
2023-01-26 14:52:38 +00:00
if m := mem.readUint32(4 + 1*4); int(m) != epoch.Minute() {
2023-01-26 12:15:34 +00:00
t.Error("wrong minute")
2023-01-26 02:48:31 +00:00
}
2023-01-26 14:52:38 +00:00
if h := mem.readUint32(4 + 2*4); int(h) != epoch.Hour() {
2023-01-26 12:15:34 +00:00
t.Error("wrong hour")
2023-01-26 02:48:31 +00:00
}
2023-01-26 14:52:38 +00:00
if d := mem.readUint32(4 + 3*4); int(d) != epoch.Day() {
2023-01-26 12:15:34 +00:00
t.Error("wrong day")
2023-01-26 02:48:31 +00:00
}
2023-01-26 14:52:38 +00:00
if m := mem.readUint32(4 + 4*4); time.Month(1+m) != epoch.Month() {
2023-01-26 12:15:34 +00:00
t.Error("wrong month")
2023-01-26 02:48:31 +00:00
}
2023-01-26 14:52:38 +00:00
if y := mem.readUint32(4 + 5*4); 1900+int(y) != epoch.Year() {
2023-01-26 12:15:34 +00:00
t.Error("wrong year")
2023-01-26 02:48:31 +00:00
}
2023-01-26 14:52:38 +00:00
if w := mem.readUint32(4 + 6*4); time.Weekday(w) != epoch.Weekday() {
2023-01-26 12:15:34 +00:00
t.Error("wrong weekday")
2023-01-26 02:48:31 +00:00
}
2023-01-26 14:52:38 +00:00
if d := mem.readUint32(4 + 7*4); int(d) != epoch.YearDay()-1 {
2023-01-26 12:15:34 +00:00
t.Error("wrong yearday")
2023-01-26 02:48:31 +00:00
}
}
func Test_vfsRandomness(t *testing.T) {
2023-01-26 14:52:38 +00:00
mem := newMemory(128)
2023-01-26 02:48:31 +00:00
2023-01-26 14:52:38 +00:00
rc := vfsRandomness(context.TODO(), mem.mod, 0, 16, 4)
2023-01-26 02:48:31 +00:00
if rc != 16 {
t.Fatal("returned", rc)
}
2023-02-06 01:01:17 +00:00
var zero [16]byte
if got := mem.view(4, 16); bytes.Equal(got, zero[:]) {
t.Fatal("all zero")
2023-01-26 02:48:31 +00:00
}
}
func Test_vfsSleep(t *testing.T) {
2023-03-07 03:51:07 +00:00
ctx := context.TODO()
2023-01-26 02:48:31 +00:00
2023-03-07 03:51:07 +00:00
now := time.Now()
rc := vfsSleep(ctx, 0, 123456)
2023-01-26 02:48:31 +00:00
if rc != 0 {
t.Fatal("returned", rc)
}
want := 123456 * time.Microsecond
2023-03-07 03:51:07 +00:00
if got := time.Since(now); got < want {
2023-01-26 12:15:34 +00:00
t.Errorf("got %v, want %v", got, want)
2023-01-26 02:48:31 +00:00
}
}
func Test_vfsCurrentTime(t *testing.T) {
2023-01-26 14:52:38 +00:00
mem := newMemory(128)
2023-03-07 03:51:07 +00:00
ctx := context.TODO()
2023-01-26 02:48:31 +00:00
now := time.Now()
2023-03-07 03:51:07 +00:00
rc := vfsCurrentTime(ctx, mem.mod, 0, 4)
2023-01-26 02:48:31 +00:00
if rc != 0 {
t.Fatal("returned", rc)
}
want := julianday.Float(now)
2023-01-26 14:52:38 +00:00
if got := mem.readFloat64(4); float32(got) != float32(want) {
2023-01-26 12:15:34 +00:00
t.Errorf("got %v, want %v", got, want)
2023-01-26 02:48:31 +00:00
}
}
func Test_vfsCurrentTime64(t *testing.T) {
2023-01-28 12:47:39 +00:00
mem := newMemory(128)
2023-03-07 03:51:07 +00:00
ctx := context.TODO()
2023-01-26 02:48:31 +00:00
now := time.Now()
time.Sleep(time.Millisecond)
2023-03-07 03:51:07 +00:00
rc := vfsCurrentTime64(ctx, mem.mod, 0, 4)
2023-01-26 02:48:31 +00:00
if rc != 0 {
t.Fatal("returned", rc)
}
day, nsec := julianday.Date(now)
want := day*86_400_000 + nsec/1_000_000
2023-01-28 12:47:39 +00:00
if got := mem.readUint64(4); float32(got) != float32(want) {
2023-01-26 12:15:34 +00:00
t.Errorf("got %v, want %v", got, want)
}
}
func Test_vfsFullPathname(t *testing.T) {
2023-01-29 02:11:41 +00:00
mem := newMemory(128 + _MAX_PATHNAME)
2023-01-28 12:47:39 +00:00
mem.writeString(4, ".")
2023-03-07 03:51:07 +00:00
ctx := context.TODO()
2023-01-26 12:15:34 +00:00
2023-03-07 03:51:07 +00:00
rc := vfsFullPathname(ctx, mem.mod, 0, 4, 0, 8)
2023-01-26 12:15:34 +00:00
if rc != uint32(CANTOPEN_FULLPATH) {
t.Errorf("returned %d, want %d", rc, CANTOPEN_FULLPATH)
}
2023-03-07 03:51:07 +00:00
rc = vfsFullPathname(ctx, mem.mod, 0, 4, _MAX_PATHNAME, 8)
2023-01-26 12:15:34 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-01-28 12:47:39 +00:00
want, _ := filepath.Abs(".")
if got := mem.readString(8, _MAX_PATHNAME); got != want {
t.Errorf("got %v, want %v", got, want)
}
2023-01-26 12:15:34 +00:00
}
func Test_vfsDelete(t *testing.T) {
2023-02-20 13:30:01 +00:00
name := filepath.Join(t.TempDir(), "test.db")
file, err := os.Create(name)
2023-01-26 12:15:34 +00:00
if err != nil {
t.Fatal(err)
}
file.Close()
2023-01-29 02:11:41 +00:00
mem := newMemory(128 + _MAX_PATHNAME)
mem.writeString(4, name)
2023-03-07 03:51:07 +00:00
ctx := context.TODO()
2023-01-26 12:15:34 +00:00
2023-03-07 03:51:07 +00:00
rc := vfsDelete(ctx, mem.mod, 0, 4, 1)
2023-01-26 12:15:34 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
if _, err := os.Stat(name); !errors.Is(err, fs.ErrNotExist) {
2023-01-28 12:47:39 +00:00
t.Fatal("did not delete the file")
}
2023-03-07 03:51:07 +00:00
rc = vfsDelete(ctx, mem.mod, 0, 4, 1)
2023-01-28 12:47:39 +00:00
if rc != _OK {
t.Fatal("returned", rc)
2023-01-26 12:15:34 +00:00
}
}
func Test_vfsAccess(t *testing.T) {
2023-02-21 12:51:52 +00:00
dir := t.TempDir()
file := filepath.Join(t.TempDir(), "test.db")
if f, err := os.Create(file); err != nil {
t.Fatal(err)
} else {
f.Close()
}
if err := os.Chmod(file, syscall.S_IRUSR); err != nil {
t.Fatal(err)
}
2023-01-29 02:11:41 +00:00
mem := newMemory(128 + _MAX_PATHNAME)
2023-02-21 12:51:52 +00:00
mem.writeString(8, dir)
2023-03-07 03:51:07 +00:00
ctx := context.TODO()
2023-01-26 12:15:34 +00:00
2023-03-07 03:51:07 +00:00
rc := vfsAccess(ctx, mem.mod, 0, 8, _ACCESS_EXISTS, 4)
2023-01-26 12:15:34 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-01-29 02:11:41 +00:00
if got := mem.readUint32(4); got != 1 {
2023-01-26 12:15:34 +00:00
t.Error("directory did not exist")
}
2023-03-07 03:51:07 +00:00
rc = vfsAccess(ctx, mem.mod, 0, 8, _ACCESS_READWRITE, 4)
2023-01-26 12:15:34 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-01-29 02:11:41 +00:00
if got := mem.readUint32(4); got != 1 {
2023-01-26 12:15:34 +00:00
t.Error("can't access directory")
2023-01-26 02:48:31 +00:00
}
2023-02-21 12:51:52 +00:00
mem.writeString(8, file)
2023-03-07 03:51:07 +00:00
rc = vfsAccess(ctx, mem.mod, 0, 8, _ACCESS_READWRITE, 4)
2023-02-21 12:51:52 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
if got := mem.readUint32(4); got != 0 {
t.Error("can access file")
}
2023-01-26 02:48:31 +00:00
}
2023-01-29 02:11:41 +00:00
func Test_vfsFile(t *testing.T) {
mem := newMemory(128)
2023-03-07 03:51:07 +00:00
ctx, vfs := vfsContext(context.TODO())
defer vfs.Close()
2023-01-29 02:11:41 +00:00
// Open a temporary file.
2023-03-07 03:51:07 +00:00
rc := vfsOpen(ctx, mem.mod, 0, 0, 4, OPEN_CREATE|OPEN_EXCLUSIVE|OPEN_READWRITE|OPEN_DELETEONCLOSE, 0)
2023-01-29 02:11:41 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
// Write stuff.
text := "Hello world!"
mem.writeString(16, text)
2023-03-07 03:51:07 +00:00
rc = vfsWrite(ctx, mem.mod, 4, 16, uint32(len(text)), 0)
2023-01-29 02:11:41 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
// Check file size.
2023-03-07 03:51:07 +00:00
rc = vfsFileSize(ctx, mem.mod, 4, 16)
2023-01-29 02:11:41 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
if got := mem.readUint32(16); got != uint32(len(text)) {
t.Errorf("got %d", got)
}
// Partial read at offset.
2023-03-07 03:51:07 +00:00
rc = vfsRead(ctx, mem.mod, 4, 16, uint32(len(text)), 4)
2023-01-29 02:11:41 +00:00
if rc != uint32(IOERR_SHORT_READ) {
t.Fatal("returned", rc)
}
if got := mem.readString(16, 64); got != text[4:] {
t.Errorf("got %q", got)
}
// Truncate the file.
2023-03-07 03:51:07 +00:00
rc = vfsTruncate(ctx, mem.mod, 4, 4)
2023-01-29 02:11:41 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
// Check file size.
2023-03-07 03:51:07 +00:00
rc = vfsFileSize(ctx, mem.mod, 4, 16)
2023-01-29 02:11:41 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
if got := mem.readUint32(16); got != 4 {
t.Errorf("got %d", got)
}
// Read at offset.
2023-03-07 03:51:07 +00:00
rc = vfsRead(ctx, mem.mod, 4, 32, 4, 0)
2023-01-29 02:11:41 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
if got := mem.readString(32, 64); got != text[:4] {
t.Errorf("got %q", got)
}
// Close the file.
2023-03-07 03:51:07 +00:00
rc = vfsClose(ctx, mem.mod, 4)
2023-01-29 02:11:41 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
}