Skip to content

Commit

Permalink
Enable DEFENSIVE mode by default for tests. This requires lots of cas…
Browse files Browse the repository at this point in the history
…e of

turning DEFENSIVE off in order to dodgy things to the database for testing
purposes.  No all of those cases are yet handled, so "make test" does not run
to completion.
  • Loading branch information
D. Richard Hipp committed Nov 6, 2018
1 parent 1583bf0 commit a929f1f
Show file tree
Hide file tree
Showing 32 changed files with 90 additions and 10 deletions.
4 changes: 3 additions & 1 deletion ext/fts5/test/fts5corrupt.test
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ db_save
do_execsql_test 1.2 { INSERT INTO t1(t1) VALUES('integrity-check') }
set segid [lindex [fts5_level_segids t1] 0]

sqlite3_db_config db DEFENSIVE 0
do_test 1.3 {
execsql {
DELETE FROM t1_data WHERE rowid = fts5_rowid('segment', $segid, 4);
Expand All @@ -50,6 +51,7 @@ do_test 1.3 {

do_test 1.4 {
db_restore_and_reopen
sqlite3_db_config db DEFENSIVE 0
execsql {
UPDATE t1_data set block = X'00000000' || substr(block, 5) WHERE
rowid = fts5_rowid('segment', $segid, 4);
Expand Down Expand Up @@ -89,7 +91,7 @@ do_execsql_test 3.0 {
do_execsql_test 3.1 {
SELECT * FROM t3 WHERE t3 MATCH 'o'
} {{one o} {three o} {five o}}

sqlite3_db_config db DEFENSIVE 0
do_catchsql_test 3.1 {
DELETE FROM t3_content WHERE rowid = 3;
SELECT * FROM t3 WHERE t3 MATCH 'o';
Expand Down
2 changes: 2 additions & 0 deletions ext/fts5/test/fts5corrupt2.test
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ foreach {tno stmt} {
set lrowid [db one {SELECT max(rowid) FROM t1_data WHERE (rowid & $mask)=0}]
set nbyte [db one {SELECT length(block) FROM t1_data WHERE rowid=$lrowid}]
set all [db eval {SELECT rowid FROM t1}]
sqlite3_db_config db DEFENSIVE 0
for {set i [expr $nbyte-2]} {$i>=0} {incr i -1} {
do_execsql_test 2.$i.1 {
BEGIN;
Expand Down Expand Up @@ -248,6 +249,7 @@ foreach {tn hdr} {

#--------------------------------------------------------------------
reset_db
sqlite3_db_config db DEFENSIVE 0
do_execsql_test 6.1 {
CREATE VIRTUAL TABLE x5 USING fts5(tt);
INSERT INTO x5 VALUES('a');
Expand Down
9 changes: 9 additions & 0 deletions ext/fts5/test/fts5corrupt3.test
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ do_test 1.1 {
set {} {}
} {}

sqlite3_db_config db DEFENSIVE 0
for {set i 0} {$i < $L} {incr i} {
do_test 1.2.$i {
catchsql {
Expand Down Expand Up @@ -86,6 +87,7 @@ do_execsql_test 2.2 {
#
reset_db
do_test 3.0 { create_t1 } {}
sqlite3_db_config db DEFENSIVE 0

do_execsql_test 3.1 {
SELECT count(*) FROM t1_data;
Expand Down Expand Up @@ -158,6 +160,7 @@ do_3_test 3.10
# Test that segments that end unexpectedly are identified as corruption.
#
reset_db
sqlite3_db_config db DEFENSIVE 0
do_test 4.0 {
execsql {
CREATE VIRTUAL TABLE t1 USING fts5(x);
Expand All @@ -182,6 +185,7 @@ for {set i 1} {1} {incr i} {

db close
sqlite3 db test.db
sqlite3_db_config db DEFENSIVE 0

db eval {
BEGIN;
Expand Down Expand Up @@ -257,6 +261,7 @@ foreach rowid [db eval {SELECT rowid FROM x1_data WHERE rowid>100}] {
#------------------------------------------------------------------------
#
reset_db
sqlite3_db_config db DEFENSIVE 0
do_execsql_test 6.1.0 {
CREATE VIRTUAL TABLE t1 USING fts5(a);
INSERT INTO t1 VALUES('bbbbb ccccc');
Expand All @@ -273,6 +278,7 @@ do_catchsql_test 6.1.2 {

#-------
reset_db
sqlite3_db_config db DEFENSIVE 0
do_execsql_test 6.2.0 {
CREATE VIRTUAL TABLE t1 USING fts5(a);
INSERT INTO t1(t1, rank) VALUES('pgsz', 32);
Expand All @@ -288,6 +294,7 @@ do_catchsql_test 6.2.2 {

#-------
reset_db
sqlite3_db_config db DEFENSIVE 0
do_execsql_test 6.3.0 {
CREATE VIRTUAL TABLE t1 USING fts5(a);
INSERT INTO t1 VALUES('abc abcdef abcdefghi');
Expand Down Expand Up @@ -362,6 +369,7 @@ do_test 7.0 {
}
} {}

sqlite3_db_config db DEFENSIVE 0
do_test 7.1 {
foreach i [db eval { SELECT rowid FROM t5_data WHERE rowid>100 }] {
db eval BEGIN
Expand All @@ -383,6 +391,7 @@ do_execsql_test 8.1 {
INSERT INTO t1 VALUES('one', 'two');
}

sqlite3_db_config db DEFENSIVE 0
do_test 9.1.1 {
set blob "12345678" ;# cookie
append blob "0105" ;# 1 level, total of 5 segments
Expand Down
1 change: 1 addition & 0 deletions ext/fts5/test/fts5integrity.test
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ do_execsql_test 4.1 {
INSERT INTO aa(aa) VALUES('integrity-check');
}

sqlite3_db_config db DEFENSIVE 0
do_catchsql_test 4.2 {
BEGIN;
UPDATE aa_docsize SET sz = X'44' WHERE rowid = 3;
Expand Down
2 changes: 2 additions & 0 deletions ext/fts5/test/fts5rebuild.test
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ do_execsql_test 1.4 {
INSERT INTO f1(f1) VALUES('integrity-check');
} {}

sqlite3_db_config db DEFENSIVE 0
do_execsql_test 1.5 {
DELETE FROM f1_data;
} {}
sqlite3_db_config db DEFENSIVE 1

do_catchsql_test 1.6 {
INSERT INTO f1(f1) VALUES('integrity-check');
Expand Down
2 changes: 2 additions & 0 deletions ext/fts5/test/fts5rowid.test
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ set res [db one {SELECT count(*) FROM x1_data}]
do_execsql_test 2.3 {
SELECT count(fts5_decode(rowid, block)) FROM x1_data;
} $res
sqlite3_db_config db DEFENSIVE 0
do_execsql_test 2.4 {
UPDATE x1_data SET block = X'';
SELECT count(fts5_decode(rowid, block)) FROM x1_data;
} $res
sqlite3_db_config db DEFENSIVE 1

do_execsql_test 2.5 {
INSERT INTO x1(x1, rank) VALUES('pgsz', 1024);
Expand Down
4 changes: 3 additions & 1 deletion ext/fts5/test/fts5version.test
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ do_execsql_test 1.3 {
SELECT rowid FROM t1 WHERE t1 MATCH 'a';
} {1}

sqlite3_db_config db DEFENSIVE 0
do_execsql_test 1.4 {
UPDATE t1_config set v=5 WHERE k='version';
}
}

do_test 1.5 {
db close
Expand All @@ -53,6 +54,7 @@ do_test 1.6 {
} {1 {invalid fts5 file format (found 5, expected 4) - run 'rebuild'}}

do_test 1.7 {
sqlite3_db_config db DEFENSIVE 0
execsql { DELETE FROM t1_config WHERE k='version' }
db close
sqlite3 db test.db
Expand Down
2 changes: 1 addition & 1 deletion ext/fts5/test/fts5vocab.test
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ if {[detail_is_none]} { set resc [row_to_col $resr] }
do_execsql_test 8.1.1 { SELECT * FROM x1_r; } $resr
do_execsql_test 8.1.2 { SELECT * FROM x1_c } $resc

sqlite3_db_config db DEFENSIVE 0
do_execsql_test 8.2 {
PRAGMA writable_schema = 1;
UPDATE sqlite_master
Expand Down Expand Up @@ -481,4 +482,3 @@ do_test 9.6 {


finish_test

4 changes: 4 additions & 0 deletions ext/rtree/rtree8.test
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ do_test rtree8-1.3.5 {
#
populate_t1 50
do_execsql_test rtree8-2.1.1 { SELECT max(nodeno) FROM t1_node } {5}
sqlite3_db_config db DEFENSIVE 0
do_execsql_test rtree8-2.1.2 { DELETE FROM t1_node } {}
sqlite3_db_config db DEFENSIVE 1
for {set i 1} {$i <= 50} {incr i} {
do_catchsql_test rtree8-2.1.3.$i {
SELECT * FROM t1 WHERE id = $i
Expand All @@ -121,9 +123,11 @@ do_execsql_test rtree8-2.1.6 {


populate_t1 50
sqlite3_db_config db DEFENSIVE 0
do_execsql_test rtree8-2.2.1 {
DELETE FROM t1_parent
} {}
sqlite3_db_config db DEFENSIVE 1
do_catchsql_test rtree8-2.2.2 {
DELETE FROM t1 WHERE id=25
} {1 {database disk image is malformed}}
Expand Down
3 changes: 2 additions & 1 deletion ext/rtree/rtreeA.test
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ proc populate_t1 {} {
execsql { INSERT INTO t1 VALUES($i, $i, $x2, $i, $y2) }
}
execsql COMMIT
sqlite3_db_config db DEFENSIVE 0
}

proc truncate_node {nodeno nTrunc} {
Expand Down Expand Up @@ -246,6 +247,7 @@ do_execsql_test rtreeA-6.2 {
create_t1
populate_t1
sqlite3 db test.db
sqlite3_db_config db DEFENSIVE 0
do_execsql_test rtreeA-7.100 {
UPDATE t1_node SET data=x'' WHERE rowid=1;
} {}
Expand All @@ -258,4 +260,3 @@ do_test rtreeA-7.120 {


finish_test

4 changes: 3 additions & 1 deletion ext/rtree/rtreecheck.test
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ proc setup_simple_db {{module rtree}} {
INSERT INTO r1 VALUES(4, 8, 8, 8, 8); -- 21
INSERT INTO r1 VALUES(5, 9, 9, 9, 9); -- 27
"
sqlite3_db_config db DEFENSIVE 0
}

setup_simple_db
Expand Down Expand Up @@ -112,6 +113,7 @@ do_execsql_test 3.1 {
SELECT rtreecheck('r2')
} {ok}

sqlite3_db_config db DEFENSIVE 0
do_execsql_test 3.2 {
BEGIN;
UPDATE r2_node SET data = X'123456';
Expand Down Expand Up @@ -140,6 +142,7 @@ do_execsql_test 5.0 {
)
INSERT INTO r3 SELECT i, i, i, i, i FROM x;
}
sqlite3_db_config db DEFENSIVE 0
do_execsql_test 5.1 {
BEGIN;
UPDATE r3_node SET data = set_int32(data, 3, 5000);
Expand All @@ -155,4 +158,3 @@ do_execsql_test 5.2 {
} 0

finish_test

3 changes: 2 additions & 1 deletion src/delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
&& pParse->nested==0)
|| ( (pTab->tabFlags & TF_Shadow)!=0
&& (db->flags & SQLITE_Defensive)!=0
&& db->nVdbeExec==0)
&& db->nVdbeExec==0
&& db->pVtabCtx==0)
){
sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
return 1;
Expand Down
3 changes: 2 additions & 1 deletion src/vacuum.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db, int iDb){
saved_mTrace = db->mTrace;
db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_CountRows);
db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder
| SQLITE_Defensive | SQLITE_CountRows);
db->mTrace = 0;

zDbMain = db->aDb[iDb].zDbSName;
Expand Down
2 changes: 2 additions & 0 deletions test/corrupt2.test
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ do_test corrupt2-2.1 {
forcecopy test.db corrupt.db

sqlite3 db2 corrupt.db
sqlite3_db_config db2 DEFENSIVE 0
execsql "
$::presql
CREATE INDEX a1 ON abc(a);
Expand Down Expand Up @@ -265,6 +266,7 @@ proc corruption_test {args} {
forcedelete corrupt.db-journal

sqlite3 db corrupt.db
sqlite3_db_config db DEFENSIVE 0
db eval $::presql
eval $A(-tclprep)
db eval $A(-sqlprep)
Expand Down
2 changes: 2 additions & 0 deletions test/fts3auto.test
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ proc fts3_make_deferrable {tbl token {nRow 0}} {
# fts3_zero_long_segments TABLE ?LIMIT?
#
proc fts3_zero_long_segments {tbl limit} {
sqlite3_db_config db DEFENSIVE 0
execsql "
UPDATE ${tbl}_segments
SET block = zeroblob(length(block))
WHERE length(block)>$limit
"
sqlite3_db_config db DEFENSIVE 1
return [db changes]
}

Expand Down
1 change: 1 addition & 0 deletions test/fts3corrupt.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set ::testprefix fts3corrupt
# extends past the end of the node on which it resides is correctly identified
# as database corruption.
#
sqlite3_db_config db DEFENSIVE 0
do_execsql_test 1.0 {
CREATE VIRTUAL TABLE t1 USING fts3;
INSERT INTO t1 VALUES('hello');
Expand Down
1 change: 1 addition & 0 deletions test/fts3corrupt2.test
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ lappend data {*}{
"acvmldguld asdvz aqb aeomsyzyu aggylhprbdz asrfkwz auipybpsn agsnszzfb"
}

sqlite3_db_config db DEFENSIVE 0
do_test fts3corrupt2-1.0 {
execsql BEGIN
execsql { CREATE VIRTUAL TABLE t2 USING FTS3(a, b); }
Expand Down
1 change: 1 addition & 0 deletions test/fts3corrupt3.test
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ do_execsql_test 1.0 {
do_execsql_test 1.1 {
SELECT quote(root) from t1_segdir;
} {X'00036F6E6509010200010200010200'}
sqlite3_db_config db DEFENSIVE 0
do_execsql_test 1.2 {
UPDATE t1_segdir SET root = X'00036F6E650EFFFFFFFFFFFFFFFFFFFFFFFF0200';
}
Expand Down
5 changes: 3 additions & 2 deletions test/fts3corrupt4.test
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ do_execsql_test 1.1 {
SELECT quote(root) FROM ft_segdir;
} {X'0005616261636B03010200030266740302020003046E646F6E03030200'}

sqlite3_db_config db DEFENSIVE 0
do_execsql_test 1.2 {
UPDATE ft_segdir SET root = blob(
'0005616261636B03010200 FFFFFFFF0702 66740302020003046E646F6E03030200'
Expand Down Expand Up @@ -83,6 +84,7 @@ do_execsql_test 2.2 {
} {X'00056162633130031F0200'}

db func blob blob
sqlite3_db_config db DEFENSIVE 0
do_execsql_test 2.3.1 {
UPDATE ft_segments SET block =
blob('00056162633130031F0200 FFFFFFFF07FF55 66740302020003046E646F6E03030200')
Expand Down Expand Up @@ -133,6 +135,7 @@ do_execsql_test 3.1 {
} {X'0101056162633132040136030132030136'}

db func blob blob
sqlite3_db_config db DEFENSIVE 0
do_execsql_test 3.2 {
UPDATE ft_segdir
SET root = blob('0101056162633132FFFFFFFF070236030132030136');
Expand All @@ -143,5 +146,3 @@ do_catchsql_test 3.1 {
} {1 {database disk image is malformed}}

finish_test


3 changes: 3 additions & 0 deletions test/fts3cov.test
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ do_test fts3cov-2.1 {
} {03}

# Test the "missing entry" case:
sqlite3_db_config db DEFENSIVE 0
do_test fts3cov-2.2 {
set root [db one {SELECT root FROM t1_segdir}]
read_fts3varint [string range $root 1 end] left_child
Expand All @@ -102,6 +103,7 @@ do_error_test fts3cov-2.3 {
do_test fts3cov-2.4 {
execsql { INSERT INTO t1_segments VALUES($left_child, NULL) }
} {}
sqlite3_db_config db DEFENSIVE 1
do_error_test fts3cov-2.5 {
SELECT * FROM t1 WHERE t1 MATCH 'cloud'
} {SQL logic error}
Expand Down Expand Up @@ -405,6 +407,7 @@ do_execsql_test 15.1 {

# Test a corruption case.
#
sqlite3_db_config db DEFENSIVE 0
do_execsql_test 16.1 {
CREATE VIRTUAL TABLE t16 USING fts4;
INSERT INTO t16 VALUES('theoretical work to examine the relationship');
Expand Down
2 changes: 2 additions & 0 deletions test/fts3d.test
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,15 @@ do_test fts3d-5.0 {
} {{Index already optimal} 1 0}

# Even if we move things around, still does nothing.
sqlite3_db_config db DEFENSIVE 0
do_test fts3d-5.1 {
execsql {
UPDATE t1_segdir SET level = 2 WHERE level = 1 AND idx = 0;
SELECT OPTIMIZE(t1) FROM t1 LIMIT 1;
SELECT level, idx FROM t1_segdir ORDER BY level, idx;
}
} {{Index already optimal} 2 0}
sqlite3_db_config db DEFENSIVE 1


# ALTER TABLE RENAME should work regardless of the database encoding.
Expand Down
Loading

0 comments on commit a929f1f

Please sign in to comment.