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]]*" 38foreach file [glob $pattern] { 39 if {[file executable $file]} {lappend binaries $file} 40} 41if {[llength $binaries]==0} { 42 puts "WARNING: No binaries to test against. No tests have been run." 43 finish_test 44 return 45} 46proc get_version {binary} { 47 set chan [launch_testfixture $binary] 48 set v [testfixture $chan { sqlite3 -version }] 49 close $chan 50 set v 51} 52foreach bin $binaries { 53 puts "Testing against $bin - version [get_version $bin]" 54} 55 56proc do_backcompat_test {rv bin1 bin2 script} { 57 58 file delete -force test.db 59 60 if {$bin1 != ""} { set ::bc_chan1 [launch_testfixture $bin1] } 61 set ::bc_chan2 [launch_testfixture $bin2] 62 63 if { $rv } { 64 proc code2 {tcl} { uplevel #0 $tcl } 65 if {$bin1 != ""} { proc code2 {tcl} { testfixture $::bc_chan1 $tcl } } 66 proc code1 {tcl} { testfixture $::bc_chan2 $tcl } 67 } else { 68 proc code1 {tcl} { uplevel #0 $tcl } 69 if {$bin1 != ""} { proc code1 {tcl} { testfixture $::bc_chan1 $tcl } } 70 proc code2 {tcl} { testfixture $::bc_chan2 $tcl } 71 } 72 73 proc sql1 sql { code1 [list db eval $sql] } 74 proc sql2 sql { code2 [list db eval $sql] } 75 76 code1 { sqlite3 db test.db } 77 code2 { sqlite3 db test.db } 78 79 uplevel $script 80 81 catch { code1 { db close } } 82 catch { code2 { db close } } 83 catch { close $::bc_chan2 } 84 catch { close $::bc_chan1 } 85} 86 87array set ::incompatible [list] 88proc do_allbackcompat_test {script} { 89 90 foreach bin $::binaries { 91 set nErr [set_test_counter errors] 92 foreach dir {0 1} { 93 94 set ::bcname ".[string map {testfixture {}} $bin].$dir." 95 96 rename do_test _do_test 97 proc do_test {nm sql res} { 98 set nm [regsub {\.} $nm $::bcname] 99 uplevel [list _do_test $nm $sql $res] 100 } 101 102 do_backcompat_test $dir {} $bin $script 103 104 rename do_test {} 105 rename _do_test do_test 106 } 107 if { $nErr < [set_test_counter errors] } { 108 set ::incompatible([get_version $bin]) 1 109 } 110 } 111} 112 113proc read_file {zFile} { 114 set zData {} 115 catch { 116 set fd [open $zFile] 117 fconfigure $fd -translation binary -encoding binary 118 set zData [read $fd] 119 close $fd 120 } 121 return $zData 122} 123proc write_file {zFile zData} { 124 set fd [open $zFile w] 125 fconfigure $fd -translation binary -encoding binary 126 puts -nonewline $fd $zData 127 close $fd 128} 129proc read_file_system {} { 130 set ret [list] 131 foreach f {test.db test.db-journal test.db-wal} { lappend ret [read_file $f] } 132 set ret 133} 134proc write_file_system {data} { 135 foreach f {test.db test.db-journal test.db-wal} d $data { 136 if {[string length $d] == 0} { 137 file delete -force $f 138 } else { 139 write_file $f $d 140 } 141 } 142} 143 144#------------------------------------------------------------------------- 145# Actual tests begin here. 146# 147do_allbackcompat_test { 148 149 # Test that database files are backwards compatible. 150 # 151 do_test backcompat-1.1.1 { sql1 { 152 CREATE TABLE t1(a PRIMARY KEY, b UNIQUE); 153 INSERT INTO t1 VALUES('abc', 'def'); 154 } } {} 155 do_test backcompat-1.1.2 { sql2 { SELECT * FROM t1; } } {abc def} 156 do_test backcompat-1.1.3 { sql2 { INSERT INTO t1 VALUES('ghi', 'jkl'); } } {} 157 do_test backcompat-1.1.4 { sql1 { SELECT * FROM t1; } } {abc def ghi jkl} 158 do_test backcompat-1.1.5 { sql1 { PRAGMA integrity_check } } {ok} 159 do_test backcompat-1.1.6 { sql2 { PRAGMA integrity_check } } {ok} 160 161 # Test that one version can roll back a hot-journal file left in the 162 # file-system by the other version. 163 # 164 # Each test case is named "backcompat-1.X...", where X is either 0 or 165 # 1. If it is 0, then the current version creates a journal file that 166 # the old versions try to read. Otherwise, if X is 1, then the old version 167 # creates the journal file and we try to read it with the current version. 168 # 169 do_test backcompat-1.2.1 { sql1 { 170 PRAGMA cache_size = 10; 171 BEGIN; 172 INSERT INTO t1 VALUES(randomblob(400), randomblob(400)); 173 INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1; 174 INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1; 175 INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1; 176 INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1; 177 COMMIT; 178 } } {} 179 set cksum1 [sql1 {SELECT md5sum(a), md5sum(b) FROM t1}] 180 set cksum2 [sql2 {SELECT md5sum(a), md5sum(b) FROM t1}] 181 do_test backcompat-1.2.2 [list string compare $cksum1 $cksum2] 0 182 183 do_test backcompat-1.2.3 { sql1 { 184 BEGIN; 185 UPDATE t1 SET a = randomblob(500); 186 } } {} 187 set data [read_file_system] 188 189 set f "test.db-journal[incr x]" 190 file copy -force test.db-journal $f 191 192 do_test backcompat-1.2.4 { sql1 { COMMIT } } {} 193 194 set same [expr {[sql2 {SELECT md5sum(a), md5sum(b) FROM t1}] == $cksum2}] 195 do_test backcompat-1.2.5 [list set {} $same] 0 196 197 code1 { db close } 198 code2 { db close } 199 write_file_system $data 200 code1 { sqlite3 db test.db } 201 code2 { sqlite3 db test.db } 202 203 set same [expr {[sql2 {SELECT md5sum(a), md5sum(b) FROM t1}] == $cksum2}] 204 do_test backcompat-1.2.6 [list set {} $same] 1 205 206 do_test backcompat-1.2.7 { sql1 { PRAGMA integrity_check } } {ok} 207 do_test backcompat-1.2.8 { sql2 { PRAGMA integrity_check } } {ok} 208} 209 210foreach k [lsort [array names ::incompatible]] { 211 puts "ERROR: Detected incompatibility with version $k" 212} 213 214finish_test 215