1# 2010 August 19 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 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. 9# 10#*********************************************************************** 11# This file implements regression tests for SQLite library. The 12# focus of this file is testing that the current version of SQLite 13# is capable of reading and writing databases created by previous 14# versions, and vice-versa. 15# 16# To use this test, old versions of the testfixture process should be 17# copied into the working directory alongside the new version. The old 18# versions should be named "testfixtureXXX" (or testfixtureXXX.exe on 19# windows), where XXX can be any string. 20# 21# This test file uses the tcl code for controlling a second testfixture 22# process located in lock_common.tcl. See the commments in lock_common.tcl 23# for documentation of the available commands. 24# 25 26set testdir [file dirname $argv0] 27source $testdir/tester.tcl 28source $testdir/lock_common.tcl 29source $testdir/malloc_common.tcl 30db close 31 32# Search for binaries to test against. Any executable files that match 33# our naming convention are assumed to be testfixture binaries to test 34# against. 35# 36set binaries [list] 37set pattern "[file tail [info nameofexec]]?*" 38if {$tcl_platform(platform)=="windows"} { 39 set pattern [string map {\.exe {}} $pattern] 40} 41foreach file [glob -nocomplain $pattern] { 42 if {[file executable $file] && [file isfile $file]} {lappend binaries $file} 43} 44if {[llength $binaries]==0} { 45 puts "WARNING: No historical binaries to test against." 46 puts "WARNING: No backwards-compatibility tests have been run." 47 finish_test 48 return 49} 50proc get_version {binary} { 51 set chan [launch_testfixture $binary] 52 set v [testfixture $chan { sqlite3 -version }] 53 close $chan 54 set v 55} 56foreach bin $binaries { 57 puts -nonewline "Testing against $bin - " 58 flush stdout 59 puts "version [get_version $bin]" 60} 61 62proc do_backcompat_test {rv bin1 bin2 script} { 63 64 file delete -force test.db 65 66 if {$bin1 != ""} { set ::bc_chan1 [launch_testfixture $bin1] } 67 set ::bc_chan2 [launch_testfixture $bin2] 68 69 if { $rv } { 70 proc code2 {tcl} { uplevel #0 $tcl } 71 if {$bin1 != ""} { proc code2 {tcl} { testfixture $::bc_chan1 $tcl } } 72 proc code1 {tcl} { testfixture $::bc_chan2 $tcl } 73 } else { 74 proc code1 {tcl} { uplevel #0 $tcl } 75 if {$bin1 != ""} { proc code1 {tcl} { testfixture $::bc_chan1 $tcl } } 76 proc code2 {tcl} { testfixture $::bc_chan2 $tcl } 77 } 78 79 proc sql1 sql { code1 [list db eval $sql] } 80 proc sql2 sql { code2 [list db eval $sql] } 81 82 code1 { sqlite3 db test.db } 83 code2 { sqlite3 db test.db } 84 85 uplevel $script 86 87 catch { code1 { db close } } 88 catch { code2 { db close } } 89 catch { close $::bc_chan2 } 90 catch { close $::bc_chan1 } 91} 92 93array set ::incompatible [list] 94proc do_allbackcompat_test {script} { 95 96 foreach bin $::binaries { 97 set nErr [set_test_counter errors] 98 foreach dir {0 1} { 99 100 set bintag [string map {testfixture {}} $bin] 101 set bintag [string map {\.exe {}} $bintag] 102 if {$bintag == ""} {set bintag self} 103 set ::bcname ".$bintag.$dir." 104 105 rename do_test _do_test 106 proc do_test {nm sql res} { 107 set nm [regsub {\.} $nm $::bcname] 108 uplevel [list _do_test $nm $sql $res] 109 } 110 111 do_backcompat_test $dir {} $bin $script 112 113 rename do_test {} 114 rename _do_test do_test 115 } 116 if { $nErr < [set_test_counter errors] } { 117 set ::incompatible([get_version $bin]) 1 118 } 119 } 120} 121 122proc read_file {zFile} { 123 set zData {} 124 if {[file exists $zFile]} { 125 set fd [open $zFile] 126 fconfigure $fd -translation binary -encoding binary 127 128 if {[file size $zFile]<=$::sqlite_pending_byte || $zFile != "test.db"} { 129 set zData [read $fd] 130 } else { 131 set zData [read $fd $::sqlite_pending_byte] 132 append zData [string repeat x 512] 133 seek $fd [expr $::sqlite_pending_byte+512] start 134 append zData [read $fd] 135 } 136 137 close $fd 138 } 139 return $zData 140} 141proc write_file {zFile zData} { 142 set fd [open $zFile w] 143 fconfigure $fd -translation binary -encoding binary 144 puts -nonewline $fd $zData 145 close $fd 146} 147proc read_file_system {} { 148 set ret [list] 149 foreach f {test.db test.db-journal test.db-wal} { lappend ret [read_file $f] } 150 set ret 151} 152proc write_file_system {data} { 153 foreach f {test.db test.db-journal test.db-wal} d $data { 154 if {[string length $d] == 0} { 155 file delete -force $f 156 } else { 157 write_file $f $d 158 } 159 } 160} 161 162#------------------------------------------------------------------------- 163# Actual tests begin here. 164# 165# This first block of tests checks to see that the same database and 166# journal files can be used by old and new versions. WAL and wal-index 167# files are tested separately below. 168# 169do_allbackcompat_test { 170 171 # Test that database files are backwards compatible. 172 # 173 do_test backcompat-1.1.1 { sql1 { 174 CREATE TABLE t1(a PRIMARY KEY, b UNIQUE); 175 INSERT INTO t1 VALUES('abc', 'def'); 176 } } {} 177 do_test backcompat-1.1.2 { sql2 { SELECT * FROM t1; } } {abc def} 178 do_test backcompat-1.1.3 { sql2 { INSERT INTO t1 VALUES('ghi', 'jkl'); } } {} 179 do_test backcompat-1.1.4 { sql1 { SELECT * FROM t1; } } {abc def ghi jkl} 180 do_test backcompat-1.1.5 { sql1 { PRAGMA integrity_check } } {ok} 181 do_test backcompat-1.1.6 { sql2 { PRAGMA integrity_check } } {ok} 182 183 # Test that one version can roll back a hot-journal file left in the 184 # file-system by the other version. 185 # 186 # Each test case is named "backcompat-1.X...", where X is either 0 or 187 # 1. If it is 0, then the current version creates a journal file that 188 # the old versions try to read. Otherwise, if X is 1, then the old version 189 # creates the journal file and we try to read it with the current version. 190 # 191 do_test backcompat-1.2.1 { sql1 { 192 PRAGMA cache_size = 10; 193 BEGIN; 194 INSERT INTO t1 VALUES(randomblob(400), randomblob(400)); 195 INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1; 196 INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1; 197 INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1; 198 INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1; 199 COMMIT; 200 } } {} 201 set cksum1 [sql1 {SELECT md5sum(a), md5sum(b) FROM t1}] 202 set cksum2 [sql2 {SELECT md5sum(a), md5sum(b) FROM t1}] 203 do_test backcompat-1.2.2 [list string compare $cksum1 $cksum2] 0 204 205 do_test backcompat-1.2.3 { sql1 { 206 BEGIN; 207 UPDATE t1 SET a = randomblob(500); 208 } } {} 209 set data [read_file_system] 210 211 do_test backcompat-1.2.4 { sql1 { COMMIT } } {} 212 213 set same [expr {[sql2 {SELECT md5sum(a), md5sum(b) FROM t1}] == $cksum2}] 214 do_test backcompat-1.2.5 [list set {} $same] 0 215 216 code1 { db close } 217 code2 { db close } 218 write_file_system $data 219 code1 { sqlite3 db test.db } 220 code2 { sqlite3 db test.db } 221 222 set same [expr {[sql2 {SELECT md5sum(a), md5sum(b) FROM t1}] == $cksum2}] 223 do_test backcompat-1.2.6 [list set {} $same] 1 224 225 do_test backcompat-1.2.7 { sql1 { PRAGMA integrity_check } } {ok} 226 do_test backcompat-1.2.8 { sql2 { PRAGMA integrity_check } } {ok} 227} 228foreach k [lsort [array names ::incompatible]] { 229 puts "ERROR: Detected journal incompatibility with version $k" 230} 231unset ::incompatible 232 233 234#------------------------------------------------------------------------- 235# Test that WAL and wal-index files may be shared between different 236# SQLite versions. 237# 238do_allbackcompat_test { 239 if {[code1 {sqlite3 -version}] >= "3.7.0" 240 && [code2 {sqlite3 -version}] >= "3.7.0" 241 } { 242 243 do_test backcompat-2.1.1 { sql1 { 244 PRAGMA journal_mode = WAL; 245 CREATE TABLE t1(a PRIMARY KEY, b UNIQUE); 246 INSERT INTO t1 VALUES('I', 1); 247 INSERT INTO t1 VALUES('II', 2); 248 INSERT INTO t1 VALUES('III', 3); 249 SELECT * FROM t1; 250 } } {wal I 1 II 2 III 3} 251 do_test backcompat-2.1.2 { sql2 { 252 SELECT * FROM t1; 253 } } {I 1 II 2 III 3} 254 255 set data [read_file_system] 256 code1 {db close} 257 code2 {db close} 258 write_file_system $data 259 code1 {sqlite3 db test.db} 260 code2 {sqlite3 db test.db} 261 262 # The WAL file now in the file-system was created by the [code1] 263 # process. Check that the [code2] process can recover the log. 264 # 265 do_test backcompat-2.1.3 { sql2 { 266 SELECT * FROM t1; 267 } } {I 1 II 2 III 3} 268 do_test backcompat-2.1.4 { sql1 { 269 SELECT * FROM t1; 270 } } {I 1 II 2 III 3} 271 } 272} 273 274finish_test 275