sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | # 2017 September 15 |
| 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. |
| 12 | # |
| 13 | |
| 14 | set testdir [file dirname $argv0] |
| 15 | source $testdir/tester.tcl |
| 16 | set testprefix mjournal |
| 17 | |
| 18 | if {[permutation]=="inmemory_journal"} { |
| 19 | finish_test |
| 20 | return |
| 21 | } |
| 22 | |
| 23 | # Test that nothing bad happens if a journal file contains a pointer to |
| 24 | # a master journal file that does not have a "-" in the name. At one point |
| 25 | # this was causing a segfault on unix. |
| 26 | # |
| 27 | do_execsql_test 1.0 { |
| 28 | CREATE TABLE t1(a, b); |
| 29 | } |
| 30 | |
| 31 | do_test 1.1 { |
| 32 | forcedelete test.db2journal test.db-journal |
| 33 | |
| 34 | close [open test.db-journal w] |
| 35 | |
| 36 | hexio_write test.db-journal 0 746573742e6462326a6f75726e616c00 |
| 37 | hexio_write test.db-journal 16 00000010 |
| 38 | hexio_write test.db-journal 20 000005e1 |
| 39 | hexio_write test.db-journal 24 d9d505f920a163d7 |
| 40 | |
| 41 | close [open test.db2journal w] |
| 42 | hexio_write test.db2journal 0 abcd |
| 43 | } {2} |
| 44 | |
| 45 | do_execsql_test 1.2 { |
| 46 | SELECT * FROM t1; |
| 47 | } |
| 48 | |
| 49 | do_test 1.3 { |
| 50 | forcedelete test0db2journal test.db-journal |
| 51 | close [open test.db-journal w] |
| 52 | hexio_write test.db-journal 0 74657374306462326a6f75726e616c00 |
| 53 | hexio_write test.db-journal 16 00000010 |
| 54 | hexio_write test.db-journal 20 000005e3 |
| 55 | hexio_write test.db-journal 24 d9d505f920a163d7 |
| 56 | |
| 57 | close [open test0db2journal w] |
| 58 | hexio_write test0db2journal 0 abcd |
| 59 | } {2} |
| 60 | |
| 61 | do_execsql_test 1.4 { |
| 62 | SELECT * FROM t1; |
| 63 | } |
| 64 | |
| 65 | # And now test that nothing bad happens if a master journal contains a |
| 66 | # pointer to a journal file that does not have a "-" in the name. |
| 67 | # |
| 68 | do_test 1.5 { |
| 69 | forcedelete test.db2-master test.db-journal test1 |
| 70 | close [open test.db-journal w] |
| 71 | hexio_write test.db-journal 0 746573742e6462322d6d617374657200 |
| 72 | hexio_write test.db-journal 16 00000010 |
| 73 | hexio_write test.db-journal 20 0000059f |
| 74 | hexio_write test.db-journal 24 d9d505f920a163d7 |
| 75 | |
| 76 | close [open test.db2-master w] |
| 77 | hexio_write test.db2-master 0 746573743100 |
| 78 | |
| 79 | close [open test1 w] |
| 80 | hexio_write test1 0 abcd |
| 81 | } {2} |
| 82 | |
| 83 | do_execsql_test 1.6 { |
| 84 | SELECT * FROM t1; |
| 85 | } |
| 86 | |
| 87 | #------------------------------------------------------------------------- |
| 88 | # Check that master journals are not created if the transaction involves |
| 89 | # multiple temp files. |
| 90 | # |
| 91 | db close |
| 92 | testvfs tvfs |
| 93 | tvfs filter xOpen |
| 94 | tvfs script open_cb |
| 95 | set ::open "" |
| 96 | proc open_cb {method file arglist} { |
| 97 | lappend ::open $file |
| 98 | } |
| 99 | |
| 100 | proc contains_mj {} { |
| 101 | foreach f $::open { |
| 102 | set t [file tail $f] |
| 103 | if {[string match *mj* $t]} { return 1 } |
| 104 | } |
| 105 | return 0 |
| 106 | } |
| 107 | |
| 108 | # Like [do_execsql_test], except that a boolean indicating whether or |
| 109 | # not a master journal file was opened ([file tail] contains "mj") or |
| 110 | # not. Example: |
| 111 | # |
| 112 | # do_hasmj_test 1.0 { SELECT 'a', 'b' } {0 a b} |
| 113 | # |
| 114 | proc do_hasmj_test {tn sql expected} { |
| 115 | set ::open [list] |
| 116 | uplevel [list do_test $tn [subst -nocommands { |
| 117 | set res [execsql "$sql"] |
| 118 | concat [contains_mj] [set res] |
| 119 | }] [list {*}$expected]] |
| 120 | } |
| 121 | |
| 122 | forcedelete test.db |
| 123 | forcedelete test.db2 |
| 124 | forcedelete test.db3 |
| 125 | sqlite3 db test.db -vfs tvfs |
| 126 | |
| 127 | do_execsql_test 2.0 { |
| 128 | ATTACH 'test.db2' AS dbfile; |
| 129 | ATTACH '' AS dbtemp; |
| 130 | ATTACH ':memory:' AS dbmem; |
| 131 | |
| 132 | CREATE TABLE t1(x); |
| 133 | CREATE TABLE dbfile.t2(x); |
| 134 | CREATE TABLE dbtemp.t3(x); |
| 135 | CREATE TABLE dbmem.t4(x); |
| 136 | } |
| 137 | |
| 138 | # Two real files. |
| 139 | do_hasmj_test 2.1 { |
| 140 | BEGIN; |
| 141 | INSERT INTO t1 VALUES(1); |
| 142 | INSERT INTO t2 VALUES(1); |
| 143 | COMMIT; |
| 144 | } {1} |
| 145 | |
| 146 | # One real, one temp file. |
| 147 | do_hasmj_test 2.2 { |
| 148 | BEGIN; |
| 149 | INSERT INTO t1 VALUES(1); |
| 150 | INSERT INTO t3 VALUES(1); |
| 151 | COMMIT; |
| 152 | } {0} |
| 153 | |
| 154 | # One file, one :memory: db. |
| 155 | do_hasmj_test 2.3 { |
| 156 | BEGIN; |
| 157 | INSERT INTO t1 VALUES(1); |
| 158 | INSERT INTO t4 VALUES(1); |
| 159 | COMMIT; |
| 160 | } {0} |
| 161 | |
| 162 | #------------------------------------------------------------------------- |
| 163 | reset_db |
| 164 | |
| 165 | do_execsql_test 3.0 { |
| 166 | PRAGMA synchronous = OFF; |
| 167 | CREATE TABLE t1(x, y); |
| 168 | } |
| 169 | |
| 170 | do_execsql_test 3.1 { |
| 171 | BEGIN; |
| 172 | INSERT INTO t1 VALUES(100, 200); |
| 173 | } |
| 174 | |
| 175 | db_save |
| 176 | db close |
| 177 | db_restore |
| 178 | |
| 179 | do_test 3.2 { |
| 180 | |
| 181 | # Append super-journal name to test.db-journal |
| 182 | # |
| 183 | set mjname [file join [pwd] test.db-super] |
| 184 | binary scan $mjname c* bytes |
| 185 | set cksum 0 |
| 186 | foreach b $bytes { incr cksum $b } |
| 187 | set fd [open test.db-journal a+] |
| 188 | fconfigure $fd -translation binary -encoding iso8859-1 |
| 189 | puts -nonewline $fd $mjname |
| 190 | puts -nonewline $fd [binary format I [string length $mjname]] |
| 191 | puts -nonewline $fd [binary format I $cksum] |
| 192 | puts -nonewline $fd [binary decode hex "d9d505f920a163d7"] |
| 193 | close $fd |
| 194 | |
| 195 | # Create super-journal |
| 196 | # |
| 197 | set fd [open test.db-super w] |
| 198 | fconfigure $fd -translation binary -encoding iso8859-1 |
| 199 | puts -nonewline $fd "test2.db-journal\0test.db-journal\0" |
| 200 | close $fd |
| 201 | |
| 202 | # Create corrupt child journal |
| 203 | # |
| 204 | set fd [open test2.db-journal w] |
| 205 | fconfigure $fd -translation binary -encoding iso8859-1 |
| 206 | puts -nonewline $fd [binary decode hex "00200001[string repeat 41 512]"] |
| 207 | puts -nonewline $fd [binary decode hex "0000020000008200d9d505f920a163d7"] |
| 208 | close $fd |
| 209 | } {} |
| 210 | |
| 211 | sqlite3 db test.db |
| 212 | do_execsql_test 3.3 { |
| 213 | SELECT type, name, tbl_name, sql FROM sqlite_schema |
| 214 | } {table t1 t1 {CREATE TABLE t1(x, y)}} |
| 215 | |
| 216 | #------------------------------------------------------------------------- |
| 217 | reset_db |
| 218 | do_execsql_test 4.0 { |
| 219 | CREATE TABLE t1(x INTEGER PRIMARY KEY); |
| 220 | } |
| 221 | db_save_and_close |
| 222 | |
| 223 | proc append_super_journal {jrnl mjname} { |
| 224 | binary scan $mjname c* bytes |
| 225 | set cksum 0 |
| 226 | foreach b $bytes { incr cksum $b } |
| 227 | set fd [open $jrnl a+] |
| 228 | fconfigure $fd -translation binary -encoding iso8859-1 |
| 229 | puts -nonewline $fd $mjname |
| 230 | puts -nonewline $fd [binary format I [string length $mjname]] |
| 231 | puts -nonewline $fd [binary format I $cksum] |
| 232 | puts -nonewline $fd [binary decode hex "d9d505f920a163d7"] |
| 233 | close $fd |
| 234 | } |
| 235 | |
| 236 | set c 0 |
| 237 | ifcapable 8_3_names { set c 1 } |
| 238 | |
| 239 | set tests [subst { |
| 240 | 1 notamasterjournal 0 |
| 241 | 2 master.9FF $c |
| 242 | 3 master-mj1234569AA 1 |
| 243 | 4 master-mj123456_AA 0 |
| 244 | 5 abc 0 |
| 245 | 6 masterr9FF 0 |
| 246 | 7 master-fj123456_AA 0 |
| 247 | 8 -mj1234569AA 1 |
| 248 | 9 1-mj1234569AA 1 |
| 249 | 10 .9AB 0 |
| 250 | 11 master.9X2 0 |
| 251 | 12 master.92X 0 |
| 252 | 13 master-mj12G4569AA 0 |
| 253 | }] |
| 254 | |
| 255 | foreach {tn mjname bDel} $tests { |
| 256 | for {set content 0} {$content<2} {incr content} { |
| 257 | db_restore_and_reopen |
| 258 | execsql { |
| 259 | PRAGMA synchronous = OFF; |
| 260 | BEGIN; |
| 261 | INSERT INTO t1 DEFAULT VALUES; |
| 262 | } |
| 263 | |
| 264 | db_save_and_close |
| 265 | db_restore |
| 266 | |
| 267 | append_super_journal test.db-journal $mjname |
| 268 | set fd [open $mjname w] |
| 269 | if {$content} { |
| 270 | set jname [file normalize test.db-journal] |
| 271 | if {$::tcl_platform(platform) eq "windows"} { |
| 272 | set jname [string map [list / \\] $jname] |
| 273 | } |
| 274 | puts -nonewline $fd $jname |
| 275 | puts -nonewline $fd "\0" |
| 276 | set mjexists [expr !$bDel] |
| 277 | } else { |
| 278 | puts -nonewline $fd "helloworld" |
| 279 | set mjexists 1 |
| 280 | } |
| 281 | close $fd |
| 282 | |
| 283 | sqlite3 db test.db |
| 284 | do_test 4.$tn.$content.1 { |
| 285 | execsql { SELECT * FROM t1 } |
| 286 | file exists $mjname |
| 287 | } $mjexists |
| 288 | do_test 4.$tn.$content.2 { |
| 289 | file exists test.db-journal |
| 290 | } {0} |
| 291 | |
| 292 | forcedelete $mjname |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | finish_test |
| 297 | |
| 298 |