| 1 |
|
| 2 |
# 2011 December 13 |
| 3 |
# |
| 4 |
# The author disclaims copyright to this source code. In place of |
| 5 |
# a legal notice, here is a blessing: |
| 6 |
# |
| 7 |
# May you do good and not evil. |
| 8 |
# May you find forgiveness for yourself and forgive others. |
| 9 |
# May you share freely, never taking more than you give. |
| 10 |
# |
| 11 |
#*********************************************************************** |
| 12 |
# |
| 13 |
# This file contains tests for error (IO, OOM etc.) handling when using |
| 14 |
# the multiplexor extension with 8.3 filenames. |
| 15 |
# |
| 16 |
|
| 17 |
set testdir [file dirname $argv0] |
| 18 |
source $testdir/tester.tcl |
| 19 |
source $testdir/malloc_common.tcl |
| 20 |
set ::testprefix multiplex3 |
| 21 |
|
| 22 |
ifcapable !8_3_names { |
| 23 |
puts -nonewline "SQLite compiled without SQLITE_ENABLE_8_3_NAMES. " |
| 24 |
puts "Skipping tests multiplex3-*." |
| 25 |
finish_test |
| 26 |
return |
| 27 |
} |
| 28 |
|
| 29 |
db close |
| 30 |
sqlite3_shutdown |
| 31 |
sqlite3_config_uri 1 |
| 32 |
autoinstall_test_functions |
| 33 |
|
| 34 |
sqlite3_multiplex_initialize "" 1 |
| 35 |
|
| 36 |
proc destroy_vfs_stack {} { |
| 37 |
generic_unregister stack |
| 38 |
sqlite3_multiplex_shutdown |
| 39 |
} |
| 40 |
|
| 41 |
proc multiplex_delete_db {} { |
| 42 |
forcedelete test.db |
| 43 |
for {set i 1} {$i <= 1000} {incr i} { |
| 44 |
forcedelete test.[format %03d $i] |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
# Procs to save and restore the current muliplexed database. |
| 49 |
# |
| 50 |
proc multiplex_save_db {} { |
| 51 |
foreach f [glob -nocomplain sv_test.*] { forcedelete $f } |
| 52 |
foreach f [glob -nocomplain test.*] { forcecopy $f "sv_$f" } |
| 53 |
} |
| 54 |
proc multiplex_restore_db {} { |
| 55 |
foreach f [glob -nocomplain test.*] {forcedelete $f} |
| 56 |
foreach f [glob -nocomplain sv_test.*] {forcecopy $f [string range $f 3 end]} } |
| 57 |
|
| 58 |
proc setup_and_save_db {} { |
| 59 |
multiplex_delete_db |
| 60 |
sqlite3 db file:test.db?8_3_names=1 |
| 61 |
sqlite3_multiplex_control db main chunk_size [expr 256*1024] |
| 62 |
execsql { |
| 63 |
CREATE TABLE t1(a PRIMARY KEY, b); |
| 64 |
INSERT INTO t1 VALUES(randomblob(15), randomblob(2000)); |
| 65 |
INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 2 |
| 66 |
INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 4 |
| 67 |
INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 8 |
| 68 |
INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 16 |
| 69 |
INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 32 |
| 70 |
INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 64 |
| 71 |
INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 128 |
| 72 |
INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 256 |
| 73 |
INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 512 |
| 74 |
} |
| 75 |
set ::cksum1 [execsql {SELECT md5sum(a, b) FROM t1 ORDER BY a}] |
| 76 |
db close |
| 77 |
multiplex_save_db |
| 78 |
} |
| 79 |
|
| 80 |
do_test 1.0 { setup_and_save_db } {} |
| 81 |
do_faultsim_test 1 -prep { |
| 82 |
multiplex_restore_db |
| 83 |
sqlite3 db file:test.db?8_3_names=1 |
| 84 |
sqlite3_multiplex_control db main chunk_size [expr 256*1024] |
| 85 |
execsql { PRAGMA journal_mode = truncate } |
| 86 |
execsql { PRAGMA synchronous = off } |
| 87 |
} -body { |
| 88 |
execsql { |
| 89 |
UPDATE t1 SET a=randomblob(12), b=randomblob(1500) WHERE (rowid%32)=0 |
| 90 |
} |
| 91 |
} -test { |
| 92 |
faultsim_test_result {0 {}} |
| 93 |
if {$testrc!=0} { |
| 94 |
set cksum2 [execsql {SELECT md5sum(a, b) FROM t1 ORDER BY a}] |
| 95 |
if {$cksum2 != $::cksum1} { error "data mismatch" } |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
#------------------------------------------------------------------------- |
| 100 |
# The following tests verify that hot-journal rollback works. As follows: |
| 101 |
# |
| 102 |
# 1. Create a large database. |
| 103 |
# 2. Set the pager cache to be very small. |
| 104 |
# 3. Open a transaction. |
| 105 |
# 4. Run the following 100 times: |
| 106 |
# a. Update a row. |
| 107 |
# b. Copy all files on disk to a new db location, including the journal. |
| 108 |
# c. Verify that the new db can be opened and that the content matches |
| 109 |
# the database created in step 1 (proving the journal was rolled |
| 110 |
# back). |
| 111 |
|
| 112 |
do_test 2.0 { |
| 113 |
setup_and_save_db |
| 114 |
multiplex_restore_db |
| 115 |
sqlite3 db file:test.db?8_3_names=1 |
| 116 |
execsql { PRAGMA cache_size = 10 } |
| 117 |
execsql { BEGIN } |
| 118 |
} {} |
| 119 |
|
| 120 |
for {set iTest 1} {$iTest<=100} {incr iTest} { |
| 121 |
do_test 2.$iTest { |
| 122 |
execsql { |
| 123 |
UPDATE t1 SET a=randomblob(12), b=randomblob(1400) WHERE rowid=5*$iTest |
| 124 |
} |
| 125 |
foreach f [glob -nocomplain test.*] {forcecopy $f "xx_$f"} |
| 126 |
sqlite3 db2 file:xx_test.db?8_3_names=1 |
| 127 |
execsql {SELECT md5sum(a, b) FROM t1 ORDER BY a} db2 |
| 128 |
} $::cksum1 |
| 129 |
|
| 130 |
db2 close |
| 131 |
} |
| 132 |
catch { db close } |
| 133 |
|
| 134 |
|
| 135 |
do_test 3.0 { setup_and_save_db } {} |
| 136 |
do_faultsim_test 3 -faults ioerr-trans* -prep { |
| 137 |
|
| 138 |
forcedelete test2.db |
| 139 |
set fd [open test2.wal w] |
| 140 |
seek $fd 4095 |
| 141 |
puts -nonewline $fd x |
| 142 |
close $fd |
| 143 |
|
| 144 |
multiplex_restore_db |
| 145 |
sqlite3 db file:test.db?8_3_names=1 |
| 146 |
sqlite3 db2 file:test2.db?8_3_names=1 |
| 147 |
sqlite3_multiplex_control db main chunk_size [expr 256*1024] |
| 148 |
sqlite3_multiplex_control db2 main chunk_size [expr 256*1024] |
| 149 |
} -body { |
| 150 |
sqlite3_backup B db2 main db main |
| 151 |
B step 100000 |
| 152 |
set rc [B finish] |
| 153 |
if { [string match SQLITE_IOERR_* $rc] } {error "disk I/O error"} |
| 154 |
set rc |
| 155 |
} -test { |
| 156 |
faultsim_test_result {0 SQLITE_OK} |
| 157 |
if {$testrc==0} { |
| 158 |
set cksum2 [execsql {SELECT md5sum(a, b) FROM t1 ORDER BY a} db2] |
| 159 |
if {$cksum2 != $::cksum1} { error "data mismatch" } |
| 160 |
} |
| 161 |
catch { B finish } |
| 162 |
catch { db close } |
| 163 |
catch { db2 close } |
| 164 |
} |
| 165 |
|
| 166 |
catch { db close } |
| 167 |
sqlite3_multiplex_shutdown |
| 168 |
finish_test |