CoolFace
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
corrupt3.test122 linesDownload Raw Back to test
1# 2007 April 62#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.15#16# $Id: corrupt3.test,v 1.2 2007/04/06 21:42:22 drh Exp $17 18set testdir [file dirname $argv0]19source $testdir/tester.tcl20 21# This module uses hard-coded offsets which do not work if the reserved_bytes22# value is nonzero.23if {[nonzero_reserved_bytes]} {finish_test; return;}24 25# These tests deal with corrupt database files26#27database_may_be_corrupt28 29# We must have the page_size pragma for these tests to work.30#31ifcapable !pager_pragmas||direct_read {32  finish_test33  return34}35 36# Create a database with an overflow page.37#38do_test corrupt3-1.1 {39  set bigstring [string repeat 0123456789 200]40  execsql {41    PRAGMA auto_vacuum=OFF;42    PRAGMA page_size=1024;43    CREATE TABLE t1(x);44    INSERT INTO t1 VALUES($bigstring);45  }46  file size test.db47} [expr {1024*3}]48 49# Verify that the file format is as we expect.  The page size50# should be 1024 bytes.  The only record should have a single51# overflow page.  The overflow page is page 3.  The pointer to52# the overflow page is on the last 4 bytes of page 2.53#54do_test corrupt3-1.2 {55  hexio_get_int [hexio_read test.db 16 2]56} 1024   ;# The page size is 102457do_test corrupt3-1.3 {58  hexio_get_int [hexio_read test.db 20 1]59} 0      ;# Unused bytes per page is 060do_test corrupt3-1.4 {61  hexio_get_int [hexio_read test.db 2044 4]62} 3      ;# Overflow page is 363do_test corrupt3-1.5 {64  hexio_get_int [hexio_read test.db 2048 4]65} 0      ;# First chained overflow is 066 67integrity_check corrupt3-1.668 69# Make the overflow chain loop back on itself.   See if the70# corruption is detected.71#72do_test corrupt3-1.7 {73  db close74  hexio_write test.db 2048 [hexio_render_int32 3]75  sqlite3 db test.db76  catchsql {77    SELECT x FROM t178  }79} [list 0 $bigstring]80do_test corrupt3-1.8 {81  catchsql {82    PRAGMA integrity_check83  }84} {0 {{*** in database main ***85Tree 2 page 2 cell 0: 2nd reference to page 3}}}86 87# Change the pointer for the first page of the overflow88# change to be a non-existant page.89#90do_test corrupt3-1.9 {91  db close92  hexio_write test.db 2044 [hexio_render_int32 4]93  sqlite3 db test.db94  catchsql {95    SELECT substr(x,1,10) FROM t196  }97} [list 1 {database disk image is malformed}]98do_test corrupt3-1.10 {99  catchsql {100    PRAGMA integrity_check101  }102} {0 {{*** in database main ***103Tree 2 page 2 cell 0: invalid page number 4104Page 3: never used}}}105do_test corrupt3-1.11 {106  db close107  hexio_write test.db 2044 [hexio_render_int32 0]108  sqlite3 db test.db109  catchsql {110    SELECT substr(x,1,10) FROM t1111  }112} [list 1 {database disk image is malformed}]113do_test corrupt3-1.12 {114  catchsql {115    PRAGMA integrity_check116  }117} {0 {{*** in database main ***118Tree 2 page 2 cell 0: overflow list length is 0 but should be 1119Page 3: never used}}}120 121finish_test122