-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite_vfs_lowlevel_test.v
282 lines (213 loc) · 6.41 KB
/
sqlite_vfs_lowlevel_test.v
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
import sqlite
import rand
const max_file_name_len = 256
fn test_vfs_register() {
org_default_vfs := sqlite.get_default_vfs()!
assert org_default_vfs.zName != 0
vfs_name := 'sometest'
mut vfs_descr := &sqlite.Sqlite3_vfs{
zName: vfs_name.str
iVersion: 2
}
if _ := sqlite.get_vfs(vfs_name) {
panic('expected that vfs is not known')
}
vfs_descr.register_as_nondefault() or { panic('vfs register failed ${err}') }
sqlite.get_vfs(vfs_name)!
now_default_vfs := sqlite.get_default_vfs()!
assert now_default_vfs.zName == org_default_vfs.zName
vfs_descr.unregister() or { panic('vfs unregister failed ${err}') }
if _ := sqlite.get_vfs(vfs_name) {
panic('vfs supposedly unregistered yet somehow still foundable')
}
}
// minimal vfs based on example https://www.sqlite.org/src/doc/trunk/src/test_demovfs.c
fn test_verify_vfs_is_actually_used() {
wrapped := sqlite.get_default_vfs()!
vfs_name := 'sometest'
mut vfs_state := &ExampleVfsState{
log: []string{cap: 100}
}
mut vfs_descr := &sqlite.Sqlite3_vfs{
iVersion: 2
szOsFile: int(sizeof(ExampleVfsOpenedFile))
mxPathname: max_file_name_len
zName: vfs_name.str
pAppData: vfs_state
xOpen: example_vfs_open
xDelete: example_vfs_delete
xAccess: example_vfs_access
xFullPathname: example_vfs_fullpathname
xDlOpen: wrapped.xDlOpen
xDlError: wrapped.xDlError
xDlSym: wrapped.xDlSym
xDlClose: wrapped.xDlClose
xRandomness: wrapped.xRandomness
xSleep: wrapped.xSleep
xCurrentTime: wrapped.xCurrentTime
xGetLastError: example_vfs_getlasterror
xCurrentTimeInt64: wrapped.xCurrentTimeInt64
}
vfs_descr.register_as_nondefault()!
// normally this would be written to disk
mut db := sqlite.connect_full('foo.db', [.readwrite, .create], vfs_name)!
assert ['fullpathname from=foo.db to=foo.db}', 'open temp?=false name=foo.db', 'read file=foo.db'] == vfs_state.log
vfs_state.log.clear()
db.close()!
assert ['close file=foo.db'] == vfs_state.log
}
struct ExampleVfsState {
mut:
log []string
}
struct ExampleVfsOpenedFile {
mut:
base sqlite.Sqlite3_file
name string
vfs_state &ExampleVfsState
}
fn to_vfsstate(t &sqlite.Sqlite3_vfs) &ExampleVfsState {
unsafe {
p := t.pAppData
if p == 0 {
assert false, 'p should not be 0'
}
return &ExampleVfsState(p)
}
}
fn to_vfsopenedfile(t &sqlite.Sqlite3_file) &ExampleVfsOpenedFile {
unsafe {
if t == 0 {
assert false, 't should not be 0'
}
return &ExampleVfsOpenedFile(t)
}
}
fn example_vfs_fullpathname(vfs &sqlite.Sqlite3_vfs, input &char, size_of_output int, output &char) int {
println('fullpathname called')
mut vfs_state := to_vfsstate(vfs)
from := unsafe { cstring_to_vstring(input) }
unsafe {
vmemcpy(output, input, from.len)
output[from.len] = u8(0)
}
result := unsafe { cstring_to_vstring(output) }
vfs_state.log << 'fullpathname from=${from} to=${result}}'
return sqlite.sqlite_ok
}
fn example_vfs_access(vfs &sqlite.Sqlite3_vfs, zPath &char, flags int, pResOut &int) int {
println('access called')
mut vfs_state := &ExampleVfsState{}
unsafe {
assert 0 != vfs.pAppData
vfs_state = &ExampleVfsState(vfs.pAppData)
}
vfs_state.log << 'accessed'
return sqlite.sqlite_ok
}
fn example_vfs_open(vfs &sqlite.Sqlite3_vfs, file_name_or_null_for_tempfile &char, vfs_opened_file &sqlite.Sqlite3_file, in_flags int, out_flags &int) int {
println('open called')
mut is_temp := false
mut file_name := ''
unsafe {
if file_name_or_null_for_tempfile == nil {
is_temp = true
file_name = rand.uuid_v4()
} else {
file_name = cstring_to_vstring(file_name_or_null_for_tempfile)
}
}
mut vfs_state := to_vfsstate(vfs)
unsafe {
mut outp := to_vfsopenedfile(vfs_opened_file)
outp.base.pMethods = &sqlite.Sqlite3_io_methods{
iVersion: 1
xClose: example_vfsfile_close
xRead: example_vfsfile_read
xWrite: example_vfsfile_write
xTruncate: example_vfsfile_truncate
xSync: example_vfsfile_sync
xFileSize: example_vfsfile_size
xLock: example_vfsfile_lock
xUnlock: example_vfsfile_unlock
xCheckReservedLock: example_vfsfile_checkreservedlock
xFileControl: example_vfsfile_filecontrol
xSectorSize: example_vfsfile_sectorsize
xDeviceCharacteristics: example_vfsfile_devicecharacteristics
}
outp.name = file_name.clone()
outp.vfs_state = vfs_state
}
vfs_state.log << 'open temp?=${is_temp} name=${file_name}'
return sqlite.sqlite_ok
}
fn example_vfsfile_checkreservedlock(file &sqlite.Sqlite3_file, pResOut &int) int {
println('file checkreservedlock')
unsafe {
*pResOut = 0
}
return sqlite.sqlite_ok
}
fn example_vfsfile_filecontrol(file &sqlite.Sqlite3_file, op int, arg voidptr) int {
println('file filecontrol')
return 0
}
fn example_vfsfile_devicecharacteristics(file &sqlite.Sqlite3_file) int {
println('file devicecharacteristics')
return 0
}
fn example_vfsfile_size(file &sqlite.Sqlite3_file, result &i64) int {
println('file size')
return sqlite.sqlite_ok
}
fn example_vfsfile_read(file &sqlite.Sqlite3_file, output voidptr, amount int, offset i64) int {
println('file read')
assert amount > 0
mut vfsfile := to_vfsopenedfile(file)
vfsfile.vfs_state.log << 'read file=${vfsfile.name}'
unsafe {
vmemset(output, 0, amount)
}
return sqlite.sqlite_ioerr_short_read
}
fn example_vfsfile_truncate(file &sqlite.Sqlite3_file, size i64) int {
println('file truncate')
return sqlite.sqlite_ok
}
fn example_vfsfile_sectorsize(file &sqlite.Sqlite3_file) int {
println('file sectorsize')
return 0
}
fn example_vfsfile_sync(file &sqlite.Sqlite3_file, flags int) int {
println('file sync called')
return sqlite.sqlite_ok
}
fn example_vfsfile_lock(file &sqlite.Sqlite3_file, elock int) int {
println('file lock called')
return sqlite.sqlite_ok
}
fn example_vfsfile_unlock(file &sqlite.Sqlite3_file, elock int) int {
println('file unlock called')
return sqlite.sqlite_ok
}
fn example_vfsfile_write(file &sqlite.Sqlite3_file, buf voidptr, amount int, offset i64) int {
println('file write called')
return sqlite.sqlite_ok
}
fn example_vfsfile_close(file &sqlite.Sqlite3_file) int {
println('file close called')
mut vfsfile := to_vfsopenedfile(file)
vfsfile.vfs_state.log << 'close file=${vfsfile.name}'
return sqlite.sqlite_ok
}
fn example_vfs_delete(vfs &sqlite.Sqlite3_vfs, name &char, sync_dir int) int {
println('vfs delete called')
return sqlite.sqlite_ok
}
fn example_vfs_getlasterror(vfs &sqlite.Sqlite3_vfs, i int, o &char) int {
println('vfs getlasterror called')
unsafe {
*o = 0
}
return sqlite.sqlite_ok
}