17f607066Sdan# 2020-12-16 27f607066Sdan# 37f607066Sdan# The author disclaims copyright to this source code. In place of 47f607066Sdan# a legal notice, here is a blessing: 57f607066Sdan# 67f607066Sdan# May you do good and not evil. 77f607066Sdan# May you find forgiveness for yourself and forgive others. 87f607066Sdan# May you share freely, never taking more than you give. 97f607066Sdan# 107f607066Sdan#*********************************************************************** 117f607066Sdan# 127f607066Sdan# 137f607066Sdan 147f607066Sdanset testdir [file dirname $argv0] 157f607066Sdansource $testdir/tester.tcl 167f607066Sdanset testprefix corruptN 177f607066Sdan 187f607066Sdan# These tests deal with corrupt database files 197f607066Sdan# 207f607066Sdandatabase_may_be_corrupt 217f607066Sdan 227f607066Sdanreset_db 237f607066Sdando_test 1.0 { 247f607066Sdan sqlite3 db {} 257f607066Sdan db deserialize [decode_hexdb { 267f607066Sdan.open --hexdb 277f607066Sdan| size 4096 pagesize 512 filename sql024239.txt.db 287f607066Sdan| page 1 offset 0 297f607066Sdan| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. 307f607066Sdan| 16: 02 00 01 01 00 40 20 20 00 00 00 0c 00 00 00 07 .....@ ........ 317f607066Sdan| 32: 00 00 00 00 00 00 00 00 00 00 00 08 00 00 00 04 ................ 327f607066Sdan| 48: 00 00 00 00 89 00 00 04 00 10 00 01 0a 00 00 01 ................ 337f607066Sdan| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0c ................ 347f607066Sdan| 96: 00 2e 2c 50 0d 00 00 00 06 01 06 00 01 da 01 b0 ..,P............ 357f607066Sdan| 112: 01 56 01 86 01 2a 01 06 00 00 00 00 00 00 00 00 .V...*.......... 367f607066Sdan| 256: 00 00 00 00 00 00 22 07 06 17 11 11 01 31 74 61 .............1ta 377f607066Sdan| 272: 62 6c 65 74 34 74 34 07 43 52 45 41 54 45 20 54 blet4t4.CREATE T 387f607066Sdan| 288: 41 42 4c 45 20 74 34 28 78 29 2a 06 06 17 13 11 ABLE t4(x)*..... 397f607066Sdan| 304: 01 3f 69 6e 64 65 78 74 33 78 74 33 05 43 52 45 .?indext3xt3.CRE 407f607066Sdan| 320: 41 54 45 20 49 4e 44 45 58 20 74 33 78 20 4f 4e ATE INDEX t3x ON 417f607066Sdan| 336: 20 74 33 28 78 29 2e 04 06 17 15 11 01 45 69 6e t3(x).......Ein 427f607066Sdan| 352: 64 65 78 74 32 63 64 74 32 05 43 52 45 41 54 45 dext2cdt2.CREATE 437f607066Sdan| 368: 20 49 4e 44 45 58 20 74 32 63 64 20 4f 4e 20 74 INDEX t2cd ON t 447f607066Sdan| 384: 32 28 63 2c 64 29 28 05 06 17 11 11 01 3d 74 61 2(c,d)(......=ta 457f607066Sdan| 400: 62 6c 65 74 33 74 33 07 43 52 45 41 54 45 20 54 blet3t3.CREATE T 467f607066Sdan| 416: 41 42 4c 45 20 74 33 28 63 2c 78 2c 65 2c 66 29 ABLE t3(c,x,e,f) 477f607066Sdan| 432: 28 02 06 17 11 11 01 3d 74 61 62 6c 65 74 32 74 (......=tablet2t 487f607066Sdan| 448: 32 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 2.CREATE TABLE t 497f607066Sdan| 464: 32 28 63 2c 64 2c 65 2c 66 29 24 01 06 17 11 11 2(c,d,e,f)$..... 507f607066Sdan| 480: 01 35 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 .5tablet1t1.CREA 517f607066Sdan| 496: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 29 TE TABLE t1(a,b) 527f607066Sdan| page 2 offset 512 537f607066Sdan| 0: 0d 00 00 00 04 01 41 00 01 fa 01 f3 01 de 01 cf ......A......... 547f607066Sdan| 160: 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 .. ............. 557f607066Sdan| 448: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0d ................ 567f607066Sdan| 464: 04 03 17 17 73 65 76 65 6e 65 69 67 68 74 13 03 ....seveneight.. 577f607066Sdan| 480: 03 07 07 40 14 00 00 00 00 00 00 40 18 00 00 00 ...@.......@.... 587f607066Sdan| 496: 00 00 00 05 02 03 01 01 03 04 04 01 03 09 01 02 ................ 597f607066Sdan| page 3 offset 1024 607f607066Sdan| 0: 0d 00 00 00 08 01 54 00 01 f7 01 ec 01 c5 01 aa ......T......... 617f607066Sdan| 16: 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 627f607066Sdan| 112: 00 00 dd 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 637f607066Sdan| 336: 00 00 00 00 19 08 05 17 17 17 17 65 69 67 68 74 ...........eight 647f607066Sdan| 352: 65 69 67 68 74 73 65 76 65 6e 73 65 76 65 6e 25 eightsevenseven% 657f607066Sdan| 368: 07 05 07 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 667f607066Sdan| 432: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ 677f607066Sdan| 480: 00 00 0f 04 17 17 01 65 69 67 68 74 65 69 67 68 .......eighteigh 687f607066Sdan| 496: 74 08 15 04 07 07 01 40 18 00 00 00 00 00 00 40 t......@.......@ 697f607066Sdan| page 4 offset 1536 707f607066Sdan| 0: 18 00 00 00 00 00 00 07 07 04 01 01 01 04 04 06 ................ 717f607066Sdan| 16: 07 04 01 01 01 02 02 05 0f 04 17 17 01 73 6d 76 .............smv 727f607066Sdan| 32: 65 6e 65 69 67 68 74 04 15 04 07 07 01 40 14 00 eneight......@.. 737f607066Sdan| page 5 offset 2048 747f607066Sdan| 0: 0a 00 00 00 08 01 96 00 01 fa 01 c4 01 f2 01 bc ................ 757f607066Sdan| 16: 01 dc 01 e1 01 96 01 cc 00 00 00 00 00 00 00 00 ................ 767f607066Sdan| 160: 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00 00 ......2......... 777f607066Sdan| 368: 00 00 00 08 00 00 00 00 00 00 00 00 00 00 00 00 ................ 787f607066Sdan| 400: 00 00 00 00 00 00 0f 04 17 17 01 85 69 67 68 74 ............ight 797f607066Sdan| 416: 65 69 67 68 74 08 15 04 07 07 01 40 18 00 00 00 eight......@.... 807f607066Sdan| 432: 00 00 00 40 18 00 00 00 00 00 00 07 07 04 01 01 ...@............ 817f607066Sdan| 448: 01 04 04 06 07 04 01 01 01 02 02 05 0f 04 17 17 ................ 827f607066Sdan| 464: 01 73 6d 76 65 6e 65 69 67 68 74 04 15 04 07 07 .smveneight..... 837f607066Sdan| 480: 01 40 14 00 00 00 00 00 00 40 18 00 00 00 00 00 .@.......@...... 847f607066Sdan| 496: 00 03 07 04 01 01 01 03 04 02 05 04 03 01 09 02 ................ 857f607066Sdan| page 6 offset 2560 867f607066Sdan| 0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 877f607066Sdan| 16: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 00 ................ 887f607066Sdan| 304: 00 00 00 26 00 00 00 00 00 00 00 00 00 00 00 00 ...&............ 897f607066Sdan| page 7 offset 3072 907f607066Sdan| 0: 0d 00 00 00 08 01 c2 00 01 fb 01 f6 01 f1 01 ec ................ 917f607066Sdan| 16: 01 e0 01 d4 01 cb 01 c2 00 00 00 00 00 00 00 00 ................ 927f607066Sdan| 128: 00 00 00 00 00 00 00 00 00 00 00 00 00 20 00 04 ............. .. 937f607066Sdan| 384: 00 00 00 00 00 00 00 00 00 07 08 02 17 65 69 fc .............ei. 947f607066Sdan| 400: 68 74 07 07 02 17 65 69 67 68 74 0a fb fd f8 bf ht....eight..... 957f607066Sdan| 416: e7 ff ff ff 00 00 00 0a 05 02 07 40 18 00 00 00 ...........@.... 967f607066Sdan| 432: 00 00 00 03 04 02 01 04 03 03 02 01 04 03 02 01 ................ 977f607066Sdan| 448: ff ff ff ff ff ff 00 00 00 00 00 00 00 00 00 00 ................ 987f607066Sdan| end sql024239.txt.db 997f607066Sdan}]} {} 1007f607066Sdan 1017f607066Sdando_catchsql_test 1.1 { 1027f607066Sdan VACUUM; 1037f607066Sdan} {1 {database disk image is malformed}} 1047f607066Sdan 105319deefdSdrh# 2021-04-05 dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 106319deefdSdrhdo_test 2.0 { 107319deefdSdrh sqlite3 db {} 108319deefdSdrh db deserialize [decode_hexdb { 109319deefdSdrh| size 16384 pagesize 4096 filename c-b92b.txt.db 110319deefdSdrh| page 1 offset 0 111319deefdSdrh| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. 112319deefdSdrh| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 04 .....@ ........ 113319deefdSdrh| 32: 00 00 00 00 00 00 00 00 00 00 00 03 00 00 00 04 ................ 114319deefdSdrh| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ 115319deefdSdrh| 96: 00 00 00 00 0d 0f f8 00 04 0f 12 00 0f 91 0f d3 ................ 116319deefdSdrh| 112: 0f 67 0f 12 00 00 00 00 00 00 00 00 00 00 00 00 .g.............. 117319deefdSdrh| 3856: 00 00 53 04 07 1b 13 11 08 81 0d 74 72 69 67 67 ..S........trigg 118319deefdSdrh| 3872: 65 72 74 72 30 74 31 43 52 45 41 54 45 20 54 52 ertr0t1CREATE TR 119319deefdSdrh| 3888: 49 47 47 45 52 20 74 72 30 20 44 45 4c 45 54 45 IGGER tr0 DELETE 120319deefdSdrh| 3904: 20 4f 4e 20 74 31 20 42 45 47 49 4e 0a 20 20 55 ON t1 BEGIN. U 121319deefdSdrh| 3920: 50 44 41 54 45 20 74 31 20 53 45 54 20 62 20 3d PDATE t1 SET b = 122319deefdSdrh| 3936: 20 61 3b 0a 45 4e 44 28 03 06 17 11 11 01 3d 69 a;.END(......=i 123319deefdSdrh| 3952: 6e 64 65 78 69 30 74 31 04 43 52 45 41 54 45 20 ndexi0t1.CREATE 124319deefdSdrh| 3968: 49 4e 44 45 58 20 69 30 20 4f 4e 20 74 31 28 62 INDEX i0 ON t1(b 125319deefdSdrh| 3984: 29 40 01 06 17 11 11 01 6d 74 61 62 6c 65 74 31 )@......mtablet1 126319deefdSdrh| 4000: 74 31 02 43 52 45 41 54 45 20 54 41 42 4c 45 20 t1.CREATE TABLE 127319deefdSdrh| 4016: 74 31 28 61 20 55 4e 49 51 55 45 20 4f 4e 20 43 t1(a UNIQUE ON C 128319deefdSdrh| 4032: 4f 4e 46 4c 49 43 54 20 52 45 50 4c 41 43 45 2c ONFLICT REPLACE, 129319deefdSdrh| 4048: 20 62 29 23 02 06 17 37 11 01 00 69 6e 64 65 78 b)#...7...index 130319deefdSdrh| 4064: 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 sqlite_autoindex 131319deefdSdrh| 4080: 5f 74 31 5f 31 74 31 03 00 00 00 08 00 00 00 00 _t1_1t1......... 132319deefdSdrh| page 2 offset 4096 133319deefdSdrh| 0: 0d 00 00 00 02 0f 00 00 00 00 00 00 00 00 00 00 ................ 134319deefdSdrh| 4080: 00 00 05 02 03 01 01 09 0d 05 01 03 01 01 04 0c ................ 135319deefdSdrh| page 3 offset 8192 136319deefdSdrh| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ 137319deefdSdrh| 4080: 00 00 00 00 00 05 03 01 01 09 02 04 03 01 09 04 ................ 138319deefdSdrh| page 4 offset 12288 139319deefdSdrh| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ 140319deefdSdrh| 4080: 00 00 00 00 00 05 03 01 01 0d 02 04 03 00 00 00 ................ 141319deefdSdrh| end c-b92b.txt.db 142319deefdSdrh}]} {} 143319deefdSdrh 144*0f201fccSdrh# This test only works with the legacy RC4 PRNG 145*0f201fccSdrhif 0 { 146319deefdSdrh prng_seed 0 db 147319deefdSdrh do_catchsql_test 2.1 { 148319deefdSdrh SELECT count(*) FROM sqlite_schema; 149319deefdSdrh WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000) 150319deefdSdrh INSERT INTO t1(a) SELECT randomblob(null) FROM c; 151319deefdSdrh } {1 {database disk image is malformed}} 152*0f201fccSdrh} 153319deefdSdrh 154d4b64699Sdanreset_db 15599bd5525Slarrybrif {![info exists ::G(perm:presql)]} { 156d4b64699Sdan do_execsql_test 3.0 { 157d4b64699Sdan CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y); 158d4b64699Sdan PRAGMA writable_schema = 1; 159d4b64699Sdan UPDATE sqlite_schema 160d4b64699Sdan SET sql = 'CREATE TABLE sqlite_sequence(name-seq)' 161d4b64699Sdan WHERE name = 'sqlite_sequence'; 162d4b64699Sdan } 163d4b64699Sdan db close 164d4b64699Sdan sqlite3 db test.db 165d4b64699Sdan do_catchsql_test 3.1 { 166d4b64699Sdan PRAGMA writable_schema = 1; 167d4b64699Sdan INSERT INTO t1(y) VALUES('abc'); 168d4b64699Sdan } {1 {database disk image is malformed}} 169112501feSdan reset_db 17099bd5525Slarrybr 171112501feSdan do_execsql_test 4.1 { 172112501feSdan CREATE TABLE x1(a INTEGER PRIMARY KEY, b UNIQUE, c UNIQUE); 173112501feSdan INSERT INTO x1 VALUES(1, 1, 2); 174112501feSdan INSERT INTO x1 VALUES(2, 2, 3); 175112501feSdan INSERT INTO x1 VALUES(3, 3, 4); 176112501feSdan INSERT INTO x1 VALUES(4, 5, 6); 177112501feSdan PRAGMA writable_schema = 1; 178112501feSdan 179112501feSdan UPDATE sqlite_schema SET rootpage = ( 180112501feSdan SELECT rootpage FROM sqlite_schema WHERE name = 'sqlite_autoindex_x1_2' 181112501feSdan ) WHERE name = 'sqlite_autoindex_x1_1'; 182112501feSdan } 183112501feSdan 184112501feSdan db close 185112501feSdan sqlite3 db test.db 186112501feSdan breakpoint 18799bd5525Slarrybr do_catchsql_test 4.2 { 188112501feSdan PRAGMA writable_schema = 1; 189112501feSdan REPLACE INTO x1 VALUES(5, 2, 3); 190715e002dSdan } {0 {}} 191112501feSdan 19299bd5525Slarrybr} 19399bd5525Slarrybr 194bb0eec43Sdan#------------------------------------------------------------------------- 195bb0eec43Sdan 196bb0eec43Sdanreset_db 19799bd5525Slarrybr 19899bd5525Slarrybrifcapable json1&&vtab { 199bb0eec43Sdan db func strreplace strreplace 200bb0eec43Sdan proc strreplace {orig a b} { 201bb0eec43Sdan string map [list $a $b] $orig 202bb0eec43Sdan } 203bb0eec43Sdan 20499bd5525Slarrybr do_execsql_test 5.0 { 205bb0eec43Sdan CREATE TABLE t1(a, b); 206bb0eec43Sdan CREATE INDEX t1a ON t1(a); 207bb0eec43Sdan CREATE INDEX t1b ON t1(b); 208bb0eec43Sdan 209bb0eec43Sdan PRAGMA writable_schema = 1; 210bb0eec43Sdan UPDATE sqlite_schema 211bb0eec43Sdan SET sql = strreplace(sql, 't1', 'json_each') 212bb0eec43Sdan WHERE type='index'; 213bb0eec43Sdan } 214bb0eec43Sdan 21509612a5bSdan # Do not run this tests if there is any presql (SQL run from within 21609612a5bSdan # the [sqlite3] command) configured. In this case the schema is parsed 21709612a5bSdan # before the "PRAGMA writable_schema" command is executed and the 21809612a5bSdan # script throws and exception. 21909612a5bSdan if {[info exists ::G(perm:presql)]==0 || $::G(perm:presql)==""} { 220bb0eec43Sdan db close 221bb0eec43Sdan sqlite3 db test.db 222bb0eec43Sdan 22399bd5525Slarrybr do_execsql_test 5.1 { 224bb0eec43Sdan PRAGMA writable_schema = 1; 225bb0eec43Sdan SELECT * FROM t1 226112501feSdan } 22709612a5bSdan } 22899bd5525Slarrybr}; # ifcapable json1&&vtab 229319deefdSdrh 2301273d69cSdan#------------------------------------------------------------------------- 2311273d69cSdanreset_db 2321273d69cSdan 2331273d69cSdando_execsql_test 6.0 { 234de3fd373Sdan PRAGMA auto_vacuum = 0; 2351273d69cSdan PRAGMA page_size=1024; 2361273d69cSdan CREATE TABLE t1(a INTEGER PRIMARY KEY, b); 2371273d69cSdan INSERT INTO t1(b) VALUES(zeroblob(300)),(zeroblob(300)),(zeroblob(300)),(zeroblob(300)); 2381273d69cSdan CREATE TABLE t2(a); 2391273d69cSdan CREATE TRIGGER t1tr BEFORE UPDATE ON t1 BEGIN DELETE FROM t2; END; 2401273d69cSdan PRAGMA writable_schema=ON; 2411273d69cSdan UPDATE sqlite_schema SET rootpage=3 WHERE rowid=2; 2421273d69cSdan PRAGMA writable_schema=RESET; 2431273d69cSdan INSERT INTO t2 VALUES('active'),('boomer'),('atom'),('atomic'), 2441273d69cSdan ('alpha channel backup abandon test aback boomer atom alpha active'); 2451273d69cSdan} 2461273d69cSdando_catchsql_test 6.1 { 2471273d69cSdan UPDATE t1 SET b=zeroblob(299); 2481273d69cSdan} {1 {database disk image is malformed}} 2491273d69cSdan 2501273d69cSdanreset_db 2511273d69cSdando_execsql_test 6.2 { 2521273d69cSdan -- Make "t1" a large table. Large enough that the children of the root 2531273d69cSdan -- node are interior nodes. 2541273d69cSdan PRAGMA page_size = 1024; 255de3fd373Sdan PRAGMA auto_vacuum = 0; 2561273d69cSdan CREATE TABLE t1(x); 2571273d69cSdan WITH s(i) AS ( 2581273d69cSdan SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<500 2591273d69cSdan ) 2601273d69cSdan INSERT INTO t1 SELECT zeroblob(300) FROM s; 2611273d69cSdan 2621273d69cSdan CREATE TABLE t2(y); 2631273d69cSdan CREATE TRIGGER tr BEFORE UPDATE ON t1 BEGIN 2641273d69cSdan DELETE FROM t2; 2651273d69cSdan END; 2661273d69cSdan 2671273d69cSdan -- Set the root of table t2 to 137 - the leftmost child of the root of t1. 2681273d69cSdan PRAGMA writable_schema = ON; 2691273d69cSdan UPDATE sqlite_schema SET rootpage = 137 WHERE name='t2'; 2701273d69cSdan PRAGMA writable_schema = RESET; 2711273d69cSdan} 2721273d69cSdan 2731273d69cSdando_catchsql_test 6.3 { 2741273d69cSdan -- Run an UPDATE on t1 that will hit a child of page 136. Have the trigger 2751273d69cSdan -- clear page 136 and its children. Assert fails. 2761273d69cSdan UPDATE t1 SET x='hello world' WHERE rowid=1; 2771273d69cSdan} {1 {database disk image is malformed}} 2787f607066Sdan 2797f607066Sdanfinish_test 280