AryaWu/sqlite
0
1# 2008 July 92#3# The author disclaims copyright to this source code. In place of4# 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# This file implements tests to make sure SQLite does not crash or14# segfault if it sees a corrupt database file. It specifically focuses15# on corrupt pointer map pages.16#17# $Id: corrupt8.test,v 1.2 2008/07/11 03:34:10 drh Exp $18 19set testdir [file dirname $argv0]20source $testdir/tester.tcl21 22# Do not use a codec for tests in this file, as the database file is23# manipulated directly using tcl scripts (using the [hexio_write] command).24#25do_not_use_codec26 27# These tests deal with corrupt database files28#29database_may_be_corrupt30 31# We must have the page_size pragma for these tests to work.32#33ifcapable !pager_pragmas||!autovacuum {34 finish_test35 return36}37 38# Create a database to work with.39#40do_test corrupt8-1.1 {41 execsql {42 PRAGMA auto_vacuum=1;43 PRAGMA page_size=1024;44 CREATE TABLE t1(x);45 INSERT INTO t1(x) VALUES(1);46 INSERT INTO t1(x) VALUES(2);47 INSERT INTO t1(x) SELECT x+2 FROM t1;48 INSERT INTO t1(x) SELECT x+4 FROM t1;49 INSERT INTO t1(x) SELECT x+8 FROM t1;50 INSERT INTO t1(x) SELECT x+16 FROM t1;51 INSERT INTO t1(x) SELECT x+32 FROM t1;52 INSERT INTO t1(x) SELECT x+64 FROM t1;53 INSERT INTO t1(x) SELECT x+128 FROM t1;54 INSERT INTO t1(x) SELECT x+256 FROM t1;55 CREATE TABLE t2(a,b);56 INSERT INTO t2 SELECT x, x*x FROM t1;57 }58 expr {[file size test.db]>1024*12}59} {1}60integrity_check corrupt8-1.261 62# Loop through each ptrmap entry. Corrupt the entry and make sure the63# corruption is detected by the integrity_check.64#65for {set i 1024} {$i<2048} {incr i 5} {66 set oldval [hexio_read test.db $i 1]67 if {$oldval==0} break68 hexio_write test.db $i 0069 do_test corrupt8-2.$i.0 {70 db close71 sqlite3 db test.db72 set x [db eval {PRAGMA integrity_check}]73 expr {$x!="ok"}74 } {1}75 for {set k 1} {$k<=5} {incr k} {76 if {$k==$oldval} continue77 hexio_write test.db $i 0$k78 do_test corrupt8-2.$i.$k {79 db close80 sqlite3 db test.db81 set x [db eval {PRAGMA integrity_check}]82 expr {$x!="ok"}83 } {1}84 }85 hexio_write test.db $i 0686 do_test corrupt8-2.$i.6 {87 db close88 sqlite3 db test.db89 set x [db eval {PRAGMA integrity_check}]90 expr {$x!="ok"}91 } {1}92 hexio_write test.db $i $oldval93 if {$oldval>2} {94 set i2 [expr {$i+1+$i%4}]95 set oldval [hexio_read test.db $i2 1]96 hexio_write test.db $i2 [format %02x [expr {($oldval+1)&0xff}]]97 do_test corrupt8-2.$i.7 {98 db close99 sqlite3 db test.db100 set x [db eval {PRAGMA integrity_check}]101 expr {$x!="ok"}102 } {1}103 hexio_write test.db $i2 $oldval104 }105}106 107 108finish_test109 