-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathvfs_test.go
346 lines (296 loc) · 7.96 KB
/
vfs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package vfs
import (
"bytes"
"context"
"errors"
"io/fs"
"os"
"os/user"
"path/filepath"
"syscall"
"testing"
"time"
"github.com/tetratelabs/wazero/experimental/wazerotest"
"github.com/ncruces/go-sqlite3/internal/util"
"github.com/ncruces/julianday"
)
func Test_vfsLocaltime(t *testing.T) {
mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize))
ctx := context.TODO()
tm := time.Now()
rc := vfsLocaltime(ctx, mod, 4, tm.Unix())
if rc != 0 {
t.Fatal("returned", rc)
}
if s := util.Read32[int32](mod, 4+0*4); int(s) != tm.Second() {
t.Error("wrong second")
}
if m := util.Read32[int32](mod, 4+1*4); int(m) != tm.Minute() {
t.Error("wrong minute")
}
if h := util.Read32[int32](mod, 4+2*4); int(h) != tm.Hour() {
t.Error("wrong hour")
}
if d := util.Read32[int32](mod, 4+3*4); int(d) != tm.Day() {
t.Error("wrong day")
}
if m := util.Read32[int32](mod, 4+4*4); time.Month(1+m) != tm.Month() {
t.Error("wrong month")
}
if y := util.Read32[int32](mod, 4+5*4); 1900+int(y) != tm.Year() {
t.Error("wrong year")
}
if w := util.Read32[int32](mod, 4+6*4); time.Weekday(w) != tm.Weekday() {
t.Error("wrong weekday")
}
if d := util.Read32[int32](mod, 4+7*4); int(d) != tm.YearDay()-1 {
t.Error("wrong yearday")
}
}
func Test_vfsRandomness(t *testing.T) {
mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize))
ctx := context.TODO()
rc := vfsRandomness(ctx, mod, 0, 16, 4)
if rc != 16 {
t.Fatal("returned", rc)
}
var zero [16]byte
if got := util.View(mod, 4, 16); bytes.Equal(got, zero[:]) {
t.Fatal("all zero")
}
}
func Test_vfsSleep(t *testing.T) {
mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize))
ctx := context.TODO()
now := time.Now()
rc := vfsSleep(ctx, mod, 0, 123456)
if rc != 0 {
t.Fatal("returned", rc)
}
want := 123456 * time.Microsecond
if got := time.Since(now); got < want {
t.Errorf("got %v, want %v", got, want)
}
}
func Test_vfsCurrentTime64(t *testing.T) {
mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize))
ctx := context.TODO()
now := time.Now()
time.Sleep(time.Millisecond)
rc := vfsCurrentTime64(ctx, mod, 0, 4)
if rc != 0 {
t.Fatal("returned", rc)
}
day, nsec := julianday.Date(now)
want := day*86_400_000 + nsec/1_000_000
if got := util.Read64[int64](mod, 4); float32(got) != float32(want) {
t.Errorf("got %v, want %v", got, want)
}
}
func Test_vfsFullPathname(t *testing.T) {
mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize))
util.WriteString(mod, 4, ".")
ctx := context.TODO()
rc := vfsFullPathname(ctx, mod, 0, 4, 0, 8)
if rc != _CANTOPEN_FULLPATH {
t.Errorf("returned %d, want %d", rc, _CANTOPEN_FULLPATH)
}
rc = vfsFullPathname(ctx, mod, 0, 4, _MAX_PATHNAME, 8)
if rc != _OK {
t.Fatal("returned", rc)
}
want, _ := filepath.Abs(".")
if got := util.ReadString(mod, 8, _MAX_PATHNAME); got != want {
t.Errorf("got %v, want %v", got, want)
}
}
func Test_vfsDelete(t *testing.T) {
name := filepath.Join(t.TempDir(), "test.db")
file, err := os.Create(name)
if err != nil {
t.Fatal(err)
}
file.Close()
mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize))
util.WriteString(mod, 4, name)
ctx := context.TODO()
rc := vfsDelete(ctx, mod, 0, 4, 1)
if rc != _OK {
t.Fatal("returned", rc)
}
if _, err := os.Stat(name); !errors.Is(err, fs.ErrNotExist) {
t.Fatal("did not delete the file")
}
rc = vfsDelete(ctx, mod, 0, 4, 1)
if rc != _IOERR_DELETE_NOENT {
t.Fatal("returned", rc)
}
}
func Test_vfsAccess(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, "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)
}
mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize))
util.WriteString(mod, 8, dir)
ctx := context.TODO()
rc := vfsAccess(ctx, mod, 0, 8, ACCESS_EXISTS, 4)
if rc != _OK {
t.Fatal("returned", rc)
}
if got := util.Read32[int32](mod, 4); got != 1 {
t.Error("directory did not exist")
}
rc = vfsAccess(ctx, mod, 0, 8, ACCESS_READWRITE, 4)
if rc != _OK {
t.Fatal("returned", rc)
}
if got := util.Read32[int32](mod, 4); got != 1 {
t.Error("can't access directory")
}
util.WriteString(mod, 8, file)
rc = vfsAccess(ctx, mod, 0, 8, ACCESS_READ, 4)
if rc != _OK {
t.Fatal("returned", rc)
}
if got := util.Read32[int32](mod, 4); got != 1 {
t.Error("can't access file")
}
if fi, err := os.Stat(file); err != nil {
t.Fatal(err)
} else if fi.Mode().Perm()&0700 != syscall.S_IRUSR {
t.Skip("skipping due to permissions")
}
if usr, err := user.Current(); err == nil && usr.Uid == "0" {
t.Skip("skipping as root")
}
util.WriteString(mod, 8, file)
rc = vfsAccess(ctx, mod, 0, 8, ACCESS_READWRITE, 4)
if rc != _OK {
t.Fatal("returned", rc)
}
if got := util.Read32[int32](mod, 4); got != 0 {
t.Error("can access file")
}
}
func Test_vfsFile(t *testing.T) {
mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize))
ctx := util.NewContext(context.TODO())
// Open a temporary file.
rc := vfsOpen(ctx, mod, 0, 0, 4, OPEN_CREATE|OPEN_EXCLUSIVE|OPEN_READWRITE|OPEN_DELETEONCLOSE, 0, 0)
if rc != _OK {
t.Fatal("returned", rc)
}
// Check sector size.
if size := vfsSectorSize(ctx, mod, 4); size != _DEFAULT_SECTOR_SIZE {
t.Fatal("returned", size)
}
// Write stuff.
text := "Hello world!"
util.WriteString(mod, 16, text)
rc = vfsWrite(ctx, mod, 4, 16, int32(len(text)), 0)
if rc != _OK {
t.Fatal("returned", rc)
}
// Check file size.
rc = vfsFileSize(ctx, mod, 4, 16)
if rc != _OK {
t.Fatal("returned", rc)
}
if got := util.Read64[int64](mod, 16); got != int64(len(text)) {
t.Errorf("got %d", got)
}
// Partial read at offset.
rc = vfsRead(ctx, mod, 4, 16, int32(len(text)), 4)
if rc != _IOERR_SHORT_READ {
t.Fatal("returned", rc)
}
if got := util.ReadString(mod, 16, 64); got != text[4:] {
t.Errorf("got %q", got)
}
// Truncate the file.
rc = vfsTruncate(ctx, mod, 4, 4)
if rc != _OK {
t.Fatal("returned", rc)
}
// Check file size.
rc = vfsFileSize(ctx, mod, 4, 16)
if rc != _OK {
t.Fatal("returned", rc)
}
if got := util.Read64[int64](mod, 16); got != 4 {
t.Errorf("got %d", got)
}
// Read at offset.
rc = vfsRead(ctx, mod, 4, 32, 4, 0)
if rc != _OK {
t.Fatal("returned", rc)
}
if got := util.ReadString(mod, 32, 64); got != text[:4] {
t.Errorf("got %q", got)
}
// Close the file.
rc = vfsClose(ctx, mod, 4)
if rc != _OK {
t.Fatal("returned", rc)
}
}
func Test_vfsFile_psow(t *testing.T) {
mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize))
ctx := util.NewContext(context.TODO())
// Open a temporary file.
rc := vfsOpen(ctx, mod, 0, 0, 4, OPEN_CREATE|OPEN_EXCLUSIVE|OPEN_READWRITE|OPEN_DELETEONCLOSE, 0, 0)
if rc != _OK {
t.Fatal("returned", rc)
}
// Read powersafe overwrite.
util.Write32(mod, 16, int32(-1))
rc = vfsFileControl(ctx, mod, 4, _FCNTL_POWERSAFE_OVERWRITE, 16)
if rc != _OK {
t.Fatal("returned", rc)
}
if got := util.Read32[int32](mod, 16); got == 0 {
t.Error("psow disabled")
}
// Unset powersafe overwrite.
util.Write32(mod, 16, int32(0))
rc = vfsFileControl(ctx, mod, 4, _FCNTL_POWERSAFE_OVERWRITE, 16)
if rc != _OK {
t.Fatal("returned", rc)
}
// Read powersafe overwrite.
util.Write32(mod, 16, int32(-1))
rc = vfsFileControl(ctx, mod, 4, _FCNTL_POWERSAFE_OVERWRITE, 16)
if rc != _OK {
t.Fatal("returned", rc)
}
if got := util.Read32[int32](mod, 16); got != 0 {
t.Error("psow enabled")
}
// Set powersafe overwrite.
util.Write32(mod, 16, int32(1))
rc = vfsFileControl(ctx, mod, 4, _FCNTL_POWERSAFE_OVERWRITE, 16)
if rc != _OK {
t.Fatal("returned", rc)
}
// Read powersafe overwrite.
util.Write32(mod, 16, int32(-1))
rc = vfsFileControl(ctx, mod, 4, _FCNTL_POWERSAFE_OVERWRITE, 16)
if rc != _OK {
t.Fatal("returned", rc)
}
if got := util.Read32[int32](mod, 16); got == 0 {
t.Error("psow disabled")
}
// Close the file.
rc = vfsClose(ctx, mod, 4)
if rc != _OK {
t.Fatal("returned", rc)
}
}