3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
12 # This file implements tests for the appendvfs extension.
15 # avfs-1.0. Test that an appendvfs DB can be added to an empty (ZLF) file.
16 # avfs-1.1. Test that the DB can be read with correct content upon reopen.
17 # avfs-1.2. Test that an appendvfs DB can be added to a simple text file.
18 # avfs-1.3. Test that the DB can be read with correct content upon reopen.
19 # avfs-1.4. Test that appended DB is aligned to default page boundary.
20 # avfs-2.1. Test that the simple text file retains its initial text.
21 # avfs-3.1. Test that the appendvfs can grow and shrink, remaining intact.
22 # avfs-3.2. Test that appendvfs is intact after grow/shrink/close/reopen.
23 # avfs-3.3. Test that appendvfs can grow by many pages and be written.
24 # avfs-3.4. Test that grown appendvfs can be reopened and appear intact.
25 # avfs-3.5. Test that much grown appendvfs can shrink and reopen intact.
26 # avfs-4.1. Test shell's ability to append to a non-appendvfs file.
27 # avfs-4.2. Test shell's ability to append to empty or nonexistent file.
28 # avfs-4.3. Test shell's ability to reopen and alter an appendvfs file.
29 # avfs-5.1. Test appendvfs refusal to open too-tiny DB appended onto ZLF.
30 # avfs-5.2. Test appendvfs refusal to open too-tiny DB appended on other.
34 set testdir [file dirname $argv0]
35 source $testdir/tester.tcl
38 # Do not attempt this test if SQLITE_OMIT_VIRTUALTABLE is defined.
45 set CLI [test_find_cli]
49 load_static_extension db appendvfs
53 forcedelete $::fa $::fza
57 set shdo "sh_app1.sql"
62 set res [catchcmd "-batch -cmd \".read $shdo\""]
63 return [regexp {^.archive} [lindex $res 1]]
66 set ::vf "&vfs=apndvfs"
68 # Return file offset of appendvfs portion of a file, or {} if none such.
69 proc fosAvfs {fname} {
70 if {[file size $fname] < 25} {
73 if {[catch {set fd [open $fname rb]}]} {
80 if {$am ne "Start-Of-SQLite3-"} {
83 binary scan $ao "W" rvo
89 set out [open $::fza wb]
91 sqlite3 adb "file:$::fza?mode=rwc$::vf" -uri 1
93 PRAGMA page_size=1024;
95 CREATE TABLE t1(a TEXT);
96 INSERT INTO t1 VALUES ('dog'),('cat');
97 SELECT group_concat(a) as pets FROM (SELECT a FROM t1 ORDER BY a);
98 } { lappend results $pets }
100 lappend results [fosAvfs $fza]
101 set ::result [join $results " | "]
106 sqlite3 adb "file:$::fza?mode=rw$::vf" -uri 1
108 SELECT group_concat(a) as pets FROM (SELECT a FROM t1 ORDER BY a DESC);
109 } { lappend results $pets }
111 set ::result [join $results " | "]
116 set out [open $::fa wb]
117 set ::tlo { "Just some text," "and more text," "ending at 3 lines." }
118 puts $out [join $::tlo "\n"]
120 set adbSz [file size $::fa]
121 sqlite3 adb "file:$::fa?mode=rwc$::vf" -uri 1
123 PRAGMA auto_vacuum = 0;
124 PRAGMA page_size=512;
126 CREATE TABLE t1(a TEXT);
127 INSERT INTO t1 VALUES ('dog'),('cat'),('pig');
128 SELECT group_concat(a) as pets FROM (SELECT a FROM t1 ORDER BY a);
129 } { lappend results $pets }
131 set adaSz [file size $::fa]
132 lappend results "Bytes before/after $adbSz/$adaSz"
133 set ::result [join $results " | "]
134 } {cat,dog,pig | Bytes before/after 50/5145}
138 sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
140 SELECT group_concat(a) as pets FROM (SELECT a FROM t1 ORDER BY a DESC);
141 } { lappend results $pets }
143 set ::result [join $results " | "]
147 set ::result [fosAvfs $fa]
151 set in [open $::fa r]
153 for {set i [llength $::tlo]} {$i > 0} {incr i -1} {
154 lappend tli [gets $in]
157 if { [join $tli ":"] ne [join $::tlo ":"] } {
158 set ::result "Appendee changed."
160 set ::result "Appendee intact."
164 # Set of repeatable random integers for a couple tests.
167 return [::tcl::mathfunc::int [expr $v * 100000]]
169 array set ::randints [list 0 [rint [::tcl::mathfunc::srand 0]]]
170 for {set i 1} {$i < $::nrint} {incr i} {
171 set ::randints($i) [rint [::tcl::mathfunc::rand]]
176 sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
179 PRAGMA cache_size=10;
180 CREATE TABLE ri (i INTEGER);
183 for {set i 0} {$i < $::nrint} {incr i} {
184 set r $::randints($i)
185 set s $::randints([incr i])
186 set t $::randints([incr i])
187 set u $::randints([incr i])
188 set v $::randints([incr i])
190 INSERT INTO ri VALUES ($r),($s),($t),($u),($v)
195 SELECT integrity_check as ic FROM pragma_integrity_check();
196 } { lappend results $ic }
197 set adbSz [file size $::fa]
200 SELECT count(*) as ic FROM ri;
201 DELETE FROM ri WHERE (i % 50) <> 25;
202 SELECT integrity_check as ic FROM pragma_integrity_check();
204 SELECT integrity_check as ic FROM pragma_integrity_check();
205 SELECT count(*) as ic FROM ri;
208 set adaSz [file size $::fa]
209 set adba [expr ($adbSz + 0.1)/$adaSz]
210 # lappend results $adba
211 set results [concat $results [lrange $qr 0 2]]
212 lappend results [expr {$adba > 10.0}]
213 set ::result [join $results " | "]
214 } "ok | $::nrint | ok | ok | 1"
218 sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
220 SELECT integrity_check as ic FROM pragma_integrity_check();
221 } { lappend results $ic }
223 set ::result [join $results " | "]
226 # avfs-3.3. Test that appendvfs can grow by many pages and be written.
229 sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
232 while {$npages > 0} {
233 adb eval { INSERT INTO ri VALUES (randomblob(1500)) }
238 SELECT integrity_check as ic FROM pragma_integrity_check();
239 } { lappend results $ic }
241 set adaSzr [expr [file size $::fa] / 300.0 / 1500 ]
242 set okSzr [expr $adaSzr > 1.0 && $adaSzr < 1.3 ]
243 lappend results $okSzr
244 set ::result [join $results " | "]
247 # avfs-3.4. Test that grown appendvfs can be reopened and appear intact.
250 sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
252 SELECT integrity_check as ic FROM pragma_integrity_check();
253 } { lappend results $ic }
258 # avfs-3.5. Test that much grown appendvfs can shrink and reopen intact.
261 set adbsz [file size $::fa]
262 sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
264 DELETE FROM ri WHERE rowid % 8 <> 0;
265 SELECT integrity_check as ic FROM pragma_integrity_check();
267 SELECT integrity_check as ic FROM pragma_integrity_check();
268 } { lappend results $ic }
270 set adasz [file size $::fa]
271 lappend results [expr {$adbsz/$adasz > 5}]
272 sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
274 SELECT integrity_check as ic FROM pragma_integrity_check();
275 } { lappend results $ic }
277 set ::result [join $results " | "]
280 set ::cliDoesAr [shellDoesAr]
283 set shdo "sh_app1.sql"
284 set shod "sh_app1.adb"
285 forcedelete $shdo $shod
286 set ofd [open $shdo w]
290 puts $ofd "pragma page_size=512;"
291 puts $ofd "create table sqlar (a);"
296 set ofd [open $shod wb]
297 puts $ofd "Some text."
299 set res [catchcmd "-append -batch -init $shdo $shod" ""]
300 lappend res [fosAvfs $shod]
301 forcedelete $shdo $shod
302 set ::result [join $res " | "]
306 set shdo "sh_app1.sql"
307 set shod "sh_app1.adb"
308 forcedelete $shdo $shod
309 set ofd [open $shdo w]
313 puts $ofd "pragma page_size=512;"
314 puts $ofd "create table sqlar (a);"
319 set ofd [open $shod wb]
321 set res [catchcmd "-append -batch -init $shdo $shod" ""]
322 lappend res [fosAvfs $shod]
323 forcedelete $shdo ; # Leave $shod for next test.
324 set ::result [join $res " | "]
328 set shdo "sh_app1.sql"
329 set shod "sh_app1.adb" ; # Same as test 4.2, reusing ADB.
331 set ofd [open $shdo w]
333 puts $ofd ".ar -u $shdo"
334 puts $ofd "select count(*) from sqlar where name = '$shdo';"
336 puts $ofd "insert into sqlar values (1);"
337 puts $ofd "select count(*) from sqlar;"
341 set res [catchcmd "-append -batch -init $shdo $shod" ""]
342 sqlite3 adb "file:$shod?mode=rw$::vf" -uri 1
344 SELECT count(*) as n FROM sqlar
347 forcedelete $shdo $shod;
348 set ::result [join $res " | "]
352 set fake "faketiny.sdb"
354 set ofd [open $fake wb]
355 puts -nonewline $ofd "SQLite format 3"
356 puts -nonewline $ofd [binary format "c" 0]
357 puts -nonewline $ofd "Start-Of-SQLite3-"
358 puts -nonewline $ofd [binary format "W" 0]
360 if {[catch {sqlite3 adb "file:$fake?mode=rw$::vf" -uri 1}]} {
361 set res "Open failed."
364 set res "Opened when should not."
371 set fake "faketiny.sdb"
373 set ofd [open $fake wb]
374 set fakeAppendee "Dog ate my homework.\n"
375 puts -nonewline $ofd $fakeAppendee
376 puts -nonewline $ofd "SQLite format 3"
377 puts -nonewline $ofd [binary format "c" 0]
378 puts -nonewline $ofd "Start-Of-SQLite3-"
379 puts -nonewline $ofd [binary format "W" [string length $fakeAppendee]]
381 if {[catch {sqlite3 adb "file:$fake?mode=rw$::vf" -uri 1}]} {
382 set res "Open failed."
385 set res "Opened when should not."
391 forcedelete $::fa $::fza
393 unset -nocomplain ::fa ::fza ::tlo ::result ::randints ::nrint ::cliDoesAr