xref: /sqlite-3.40.0/test/backcompat.test (revision fc5026d3)
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 bintag [string map {testfixture {}} $bin]
95      if {$bintag == ""} {set bintag self}
96      set ::bcname ".$bintag.$dir."
97
98      rename do_test _do_test
99      proc do_test {nm sql res} {
100        set nm [regsub {\.} $nm $::bcname]
101        uplevel [list _do_test $nm $sql $res]
102      }
103
104      do_backcompat_test $dir {} $bin $script
105
106      rename do_test {}
107      rename _do_test do_test
108    }
109    if { $nErr < [set_test_counter errors] } {
110      set ::incompatible([get_version $bin]) 1
111    }
112  }
113}
114
115proc read_file {zFile} {
116  set zData {}
117  catch {
118    set fd [open $zFile]
119    fconfigure $fd -translation binary -encoding binary
120    set zData [read $fd]
121    close $fd
122  }
123  return $zData
124}
125proc write_file {zFile zData} {
126  set fd [open $zFile w]
127  fconfigure $fd -translation binary -encoding binary
128  puts -nonewline $fd $zData
129  close $fd
130}
131proc read_file_system {} {
132  set ret [list]
133  foreach f {test.db test.db-journal test.db-wal} { lappend ret [read_file $f] }
134  set ret
135}
136proc write_file_system {data} {
137  foreach f {test.db test.db-journal test.db-wal} d $data {
138    if {[string length $d] == 0} {
139      file delete -force $f
140    } else {
141      write_file $f $d
142    }
143  }
144}
145
146#-------------------------------------------------------------------------
147# Actual tests begin here.
148#
149# This first block of tests checks to see that the same database and
150# journal files can be used by old and new versions. WAL and wal-index
151# files are tested separately below.
152#
153do_allbackcompat_test {
154
155  # Test that database files are backwards compatible.
156  #
157  do_test backcompat-1.1.1 { sql1 {
158    CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
159    INSERT INTO t1 VALUES('abc', 'def');
160  } } {}
161  do_test backcompat-1.1.2 { sql2 { SELECT * FROM t1; } } {abc def}
162  do_test backcompat-1.1.3 { sql2 { INSERT INTO t1 VALUES('ghi', 'jkl'); } } {}
163  do_test backcompat-1.1.4 { sql1 { SELECT * FROM t1; } } {abc def ghi jkl}
164  do_test backcompat-1.1.5 { sql1 { PRAGMA integrity_check } } {ok}
165  do_test backcompat-1.1.6 { sql2 { PRAGMA integrity_check } } {ok}
166
167  # Test that one version can roll back a hot-journal file left in the
168  # file-system by the other version.
169  #
170  # Each test case is named "backcompat-1.X...", where X is either 0 or
171  # 1. If it is 0, then the current version creates a journal file that
172  # the old versions try to read. Otherwise, if X is 1, then the old version
173  # creates the journal file and we try to read it with the current version.
174  #
175  do_test backcompat-1.2.1 { sql1 {
176    PRAGMA cache_size = 10;
177    BEGIN;
178      INSERT INTO t1 VALUES(randomblob(400), randomblob(400));
179      INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1;
180      INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1;
181      INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1;
182      INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1;
183    COMMIT;
184  } } {}
185  set cksum1 [sql1 {SELECT md5sum(a), md5sum(b) FROM t1}]
186  set cksum2 [sql2 {SELECT md5sum(a), md5sum(b) FROM t1}]
187  do_test backcompat-1.2.2 [list string compare $cksum1 $cksum2] 0
188
189  do_test backcompat-1.2.3 { sql1 {
190    BEGIN;
191      UPDATE t1 SET a = randomblob(500);
192  } } {}
193  set data [read_file_system]
194
195  set f "test.db-journal[incr x]"
196  file copy -force test.db-journal $f
197
198  do_test backcompat-1.2.4 { sql1 { COMMIT } } {}
199
200  set same [expr {[sql2 {SELECT md5sum(a), md5sum(b) FROM t1}] == $cksum2}]
201  do_test backcompat-1.2.5 [list set {} $same] 0
202
203  code1 { db close }
204  code2 { db close }
205  write_file_system $data
206  code1 { sqlite3 db test.db }
207  code2 { sqlite3 db test.db }
208
209  set same [expr {[sql2 {SELECT md5sum(a), md5sum(b) FROM t1}] == $cksum2}]
210  do_test backcompat-1.2.6 [list set {} $same] 1
211
212  do_test backcompat-1.2.7 { sql1 { PRAGMA integrity_check } } {ok}
213  do_test backcompat-1.2.8 { sql2 { PRAGMA integrity_check } } {ok}
214}
215foreach k [lsort [array names ::incompatible]] {
216  puts "ERROR: Detected journal incompatibility with version $k"
217}
218unset ::incompatible
219
220
221#-------------------------------------------------------------------------
222# Test that WAL and wal-index files may be shared between different
223# SQLite versions.
224#
225do_allbackcompat_test {
226  if {[code1 {sqlite3 -version}] >= "3.7.0"
227   && [code2 {sqlite3 -version}] >= "3.7.0"
228  } {
229
230    do_test backcompat-2.1.1 { sql1 {
231      PRAGMA journal_mode = WAL;
232      CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
233      INSERT INTO t1 VALUES('I', 1);
234      INSERT INTO t1 VALUES('II', 2);
235      INSERT INTO t1 VALUES('III', 3);
236      SELECT * FROM t1;
237    } } {wal I 1 II 2 III 3}
238    do_test backcompat-2.1.2 { sql2 {
239      SELECT * FROM t1;
240    } } {I 1 II 2 III 3}
241
242    set data [read_file_system]
243    code1 {db close}
244    code2 {db close}
245    write_file_system $data
246    code1 {sqlite3 db test.db}
247    code2 {sqlite3 db test.db}
248
249    # The WAL file now in the file-system was created by the [code1]
250    # process. Check that the [code2] process can recover the log.
251    #
252    do_test backcompat-2.1.3 { sql2 {
253      SELECT * FROM t1;
254    } } {I 1 II 2 III 3}
255    do_test backcompat-2.1.4 { sql1 {
256      SELECT * FROM t1;
257    } } {I 1 II 2 III 3}
258  }
259}
260
261finish_test
262