corruptC.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. # 2004 August 30
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # 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 or
  14. # segfault if it sees a corrupt database file. It creates a base
  15. # data base file, then tests that single byte corruptions in
  16. # increasingly larger quantities are handled gracefully.
  17. #
  18. # $Id: corruptC.test,v 1.14 2009/07/11 06:55:34 danielk1977 Exp $
  19. catch {forcedelete test.db test.db-journal test.bu}
  20. set testdir [file dirname $argv0]
  21. source $testdir/tester.tcl
  22. # Do not use a codec for tests in this file, as the database file is
  23. # manipulated directly using tcl scripts (using the [hexio_write] command).
  24. #
  25. do_not_use_codec
  26. # Construct a compact, dense database for testing.
  27. #
  28. do_test corruptC-1.1 {
  29. execsql {
  30. PRAGMA auto_vacuum = 0;
  31. PRAGMA legacy_file_format=1;
  32. BEGIN;
  33. CREATE TABLE t1(x,y);
  34. INSERT INTO t1 VALUES(1,1);
  35. INSERT OR IGNORE INTO t1 SELECT x*2,y FROM t1;
  36. INSERT OR IGNORE INTO t1 SELECT x*3,y FROM t1;
  37. INSERT OR IGNORE INTO t1 SELECT x*5,y FROM t1;
  38. INSERT OR IGNORE INTO t1 SELECT x*7,y FROM t1;
  39. INSERT OR IGNORE INTO t1 SELECT x*11,y FROM t1;
  40. INSERT OR IGNORE INTO t1 SELECT x*13,y FROM t1;
  41. CREATE INDEX t1i1 ON t1(x);
  42. CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0;
  43. COMMIT;
  44. }
  45. } {}
  46. ifcapable {integrityck} {
  47. integrity_check corruptC-1.2
  48. }
  49. # Generate random integer
  50. #
  51. proc random {range} {
  52. return [expr {round(rand()*$range)}]
  53. }
  54. # Setup for the tests. Make a backup copy of the good database in test.bu.
  55. #
  56. db close
  57. forcecopy test.db test.bu
  58. sqlite3 db test.db
  59. set fsize [file size test.db]
  60. # Set a quasi-random random seed.
  61. if {[info exists ::G(issoak)]} {
  62. # If we are doing SOAK tests, we want a different
  63. # random seed for each run. Ideally we would like
  64. # to use [clock clicks] or something like that here.
  65. set qseed [file mtime test.db]
  66. } else {
  67. # If we are not doing soak tests,
  68. # make it repeatable.
  69. set qseed 0
  70. }
  71. expr srand($qseed)
  72. #
  73. # First test some specific corruption tests found from earlier runs
  74. # with specific seeds.
  75. #
  76. # test that a corrupt content offset size is handled (seed 5577)
  77. do_test corruptC-2.1 {
  78. db close
  79. forcecopy test.bu test.db
  80. # insert corrupt byte(s)
  81. hexio_write test.db 2053 [format %02x 0x04]
  82. sqlite3 db test.db
  83. catchsql {PRAGMA integrity_check}
  84. } {1 {database disk image is malformed}}
  85. # test that a corrupt content offset size is handled (seed 5649)
  86. do_test corruptC-2.2 {
  87. db close
  88. forcecopy test.bu test.db
  89. # insert corrupt byte(s)
  90. hexio_write test.db 27 [format %02x 0x08]
  91. hexio_write test.db 233 [format %02x 0x6a]
  92. hexio_write test.db 328 [format %02x 0x67]
  93. hexio_write test.db 750 [format %02x 0x1f]
  94. hexio_write test.db 1132 [format %02x 0x52]
  95. hexio_write test.db 1133 [format %02x 0x84]
  96. hexio_write test.db 1220 [format %02x 0x01]
  97. hexio_write test.db 3688 [format %02x 0xc1]
  98. hexio_write test.db 3714 [format %02x 0x58]
  99. hexio_write test.db 3746 [format %02x 0x9a]
  100. sqlite3 db test.db
  101. catchsql {UPDATE t1 SET y=1}
  102. } {1 {database disk image is malformed}}
  103. # test that a corrupt free cell size is handled (seed 13329)
  104. do_test corruptC-2.3 {
  105. db close
  106. forcecopy test.bu test.db
  107. # insert corrupt byte(s)
  108. hexio_write test.db 1094 [format %02x 0x76]
  109. sqlite3 db test.db
  110. catchsql {UPDATE t1 SET y=1}
  111. } {1 {database disk image is malformed}}
  112. # test that a corrupt free cell size is handled (seed 169571)
  113. do_test corruptC-2.4 {
  114. db close
  115. forcecopy test.bu test.db
  116. # insert corrupt byte(s)
  117. hexio_write test.db 3119 [format %02x 0xdf]
  118. sqlite3 db test.db
  119. catchsql {UPDATE t2 SET y='abcdef-uvwxyz'}
  120. } {1 {database disk image is malformed}}
  121. # test that a corrupt free cell size is handled (seed 169571)
  122. do_test corruptC-2.5 {
  123. db close
  124. forcecopy test.bu test.db
  125. # insert corrupt byte(s)
  126. hexio_write test.db 3119 [format %02x 0xdf]
  127. hexio_write test.db 4073 [format %02x 0xbf]
  128. sqlite3 db test.db
  129. catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;}
  130. catchsql {PRAGMA integrity_check}
  131. } {0 {{*** in database main ***
  132. Page 4: btreeInitPage() returns error code 11}}}
  133. # {0 {{*** in database main ***
  134. # Corruption detected in cell 710 on page 4
  135. # Multiple uses for byte 661 of page 4
  136. # Fragmented space is 249 byte reported as 21 on page 4}}}
  137. # test that a corrupt free cell size is handled (seed 169595)
  138. do_test corruptC-2.6 {
  139. db close
  140. forcecopy test.bu test.db
  141. # insert corrupt byte(s)
  142. hexio_write test.db 619 [format %02x 0xe2]
  143. hexio_write test.db 3150 [format %02x 0xa8]
  144. sqlite3 db test.db
  145. catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;}
  146. } {1 {database disk image is malformed}}
  147. # corruption (seed 178692)
  148. do_test corruptC-2.7 {
  149. db close
  150. forcecopy test.bu test.db
  151. # insert corrupt byte(s)
  152. hexio_write test.db 3074 [format %02x 0xa0]
  153. sqlite3 db test.db
  154. catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;}
  155. } {1 {database disk image is malformed}}
  156. # corruption (seed 179069)
  157. do_test corruptC-2.8 {
  158. db close
  159. forcecopy test.bu test.db
  160. # insert corrupt byte(s)
  161. hexio_write test.db 1393 [format %02x 0x7d]
  162. hexio_write test.db 84 [format %02x 0x19]
  163. hexio_write test.db 3287 [format %02x 0x3b]
  164. hexio_write test.db 2564 [format %02x 0xed]
  165. hexio_write test.db 2139 [format %02x 0x55]
  166. sqlite3 db test.db
  167. catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;}
  168. } {1 {database disk image is malformed}}
  169. # corruption (seed 170434)
  170. do_test corruptC-2.9 {
  171. db close
  172. forcecopy test.bu test.db
  173. # insert corrupt byte(s)
  174. hexio_write test.db 2095 [format %02x 0xd6]
  175. sqlite3 db test.db
  176. catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;}
  177. } {1 {database disk image is malformed}}
  178. # corruption (seed 186504)
  179. do_test corruptC-2.10 {
  180. db close
  181. forcecopy test.bu test.db
  182. # insert corrupt byte(s)
  183. hexio_write test.db 3130 [format %02x 0x02]
  184. sqlite3 db test.db
  185. catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;}
  186. } {1 {database disk image is malformed}}
  187. # corruption (seed 1589)
  188. do_test corruptC-2.11 {
  189. db close
  190. forcecopy test.bu test.db
  191. # insert corrupt byte(s)
  192. hexio_write test.db 55 [format %02x 0xa7]
  193. sqlite3 db test.db
  194. catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0; ROLLBACK;}
  195. } {1 {database disk image is malformed}}
  196. # corruption (seed 14166)
  197. do_test corruptC-2.12 {
  198. db close
  199. forcecopy test.bu test.db
  200. # insert corrupt byte(s)
  201. hexio_write test.db 974 [format %02x 0x2e]
  202. sqlite3 db test.db
  203. catchsql {SELECT count(*) FROM sqlite_master;}
  204. } {1 {malformed database schema (t1i1) - corrupt database}}
  205. # corruption (seed 218803)
  206. do_test corruptC-2.13 {
  207. db close
  208. forcecopy test.bu test.db
  209. # insert corrupt byte(s)
  210. hexio_write test.db 102 [format %02x 0x12]
  211. sqlite3 db test.db
  212. catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0; ROLLBACK;}
  213. } {1 {database disk image is malformed}}
  214. do_test corruptC-2.14 {
  215. db close
  216. forcecopy test.bu test.db
  217. sqlite3 db test.db
  218. set blob [string repeat abcdefghij 10000]
  219. execsql { INSERT INTO t1 VALUES (1, $blob) }
  220. sqlite3 db test.db
  221. set filesize [file size test.db]
  222. hexio_write test.db [expr $filesize-2048] 00000001
  223. catchsql {DELETE FROM t1 WHERE rowid = (SELECT max(rowid) FROM t1)}
  224. } {1 {database disk image is malformed}}
  225. # At one point this particular corrupt database was causing a buffer
  226. # overread. Which caused a crash in a run of all.test once.
  227. #
  228. do_test corruptC-2.15 {
  229. db close
  230. forcecopy test.bu test.db
  231. hexio_write test.db 986 b9
  232. sqlite3 db test.db
  233. catchsql {SELECT count(*) FROM sqlite_master;}
  234. } {1 {malformed database schema (t1i1) - no such table: main.t1}}
  235. #
  236. # Now test for a series of quasi-random seeds.
  237. # We loop over the entire file size and touch
  238. # each byte at least once.
  239. for {set tn 0} {$tn<$fsize} {incr tn 1} {
  240. # setup for test
  241. db close
  242. forcecopy test.bu test.db
  243. sqlite3 db test.db
  244. # Seek to a random location in the file, and write a random single byte
  245. # value. Then do various operations on the file to make sure that
  246. # the database engine can handle the corruption gracefully.
  247. #
  248. set last 0
  249. for {set i 1} {$i<=512 && !$last} {incr i 1} {
  250. db close
  251. if {$i==1} {
  252. # on the first corrupt value, use location $tn
  253. # this ensures that we touch each location in the
  254. # file at least once.
  255. set roffset $tn
  256. } else {
  257. # insert random byte at random location
  258. set roffset [random $fsize]
  259. }
  260. set rbyte [format %02x [random 255]]
  261. # You can uncomment the following to have it trace
  262. # exactly how it's corrupting the file. This is
  263. # useful for generating the "seed specific" tests
  264. # above.
  265. # set rline "$roffset $rbyte"
  266. # puts stdout $rline
  267. hexio_write test.db $roffset $rbyte
  268. sqlite3 db test.db
  269. # do a few random operations to make sure that if
  270. # they error, they error gracefully instead of crashing.
  271. do_test corruptC-3.$tn.($qseed).$i.1 {
  272. catchsql {SELECT count(*) FROM sqlite_master}
  273. set x {}
  274. } {}
  275. do_test corruptC-3.$tn.($qseed).$i.2 {
  276. catchsql {SELECT count(*) FROM t1}
  277. set x {}
  278. } {}
  279. do_test corruptC-3.$tn.($qseed).$i.3 {
  280. catchsql {SELECT count(*) FROM t1 WHERE x>13}
  281. set x {}
  282. } {}
  283. do_test corruptC-3.$tn.($qseed).$i.4 {
  284. catchsql {SELECT count(*) FROM t2}
  285. set x {}
  286. } {}
  287. do_test corruptC-3.$tn.($qseed).$i.5 {
  288. catchsql {SELECT count(*) FROM t2 WHERE x<13}
  289. set x {}
  290. } {}
  291. do_test corruptC-3.$tn.($qseed).$i.6 {
  292. catchsql {BEGIN; UPDATE t1 SET y=1; ROLLBACK;}
  293. set x {}
  294. } {}
  295. do_test corruptC-3.$tn.($qseed).$i.7 {
  296. catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;}
  297. set x {}
  298. } {}
  299. do_test corruptC-3.$tn.($qseed).$i.8 {
  300. catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;}
  301. set x {}
  302. } {}
  303. do_test corruptC-3.$tn.($qseed).$i.9 {
  304. catchsql {BEGIN; DELETE FROM t2 WHERE x<13; ROLLBACK;}
  305. set x {}
  306. } {}
  307. do_test corruptC-3.$tn.($qseed).$i.10 {
  308. catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0; ROLLBACK;}
  309. set x {}
  310. } {}
  311. # check the integrity of the database.
  312. # once the corruption is detected, we can stop.
  313. ifcapable {integrityck} {
  314. set res [ catchsql {PRAGMA integrity_check} ]
  315. set ans [lindex $res 1]
  316. if { [ string compare $ans "ok" ] != 0 } {
  317. set last -1
  318. }
  319. }
  320. # if we are not capable of doing an integrity check,
  321. # stop after corrupting 5 bytes.
  322. ifcapable {!integrityck} {
  323. if { $i > 5 } {
  324. set last -1
  325. }
  326. }
  327. # Check that no page references were leaked.
  328. # TBD: need to figure out why this doesn't work
  329. # work with ROLLBACKs...
  330. if {0} {
  331. do_test corruptC-3.$tn.($qseed).$i.11 {
  332. set bt [btree_from_db db]
  333. db_enter db
  334. array set stats [btree_pager_stats $bt]
  335. db_leave db
  336. set stats(ref)
  337. } {0}
  338. }
  339. }
  340. # end for i
  341. }
  342. # end for tn
  343. finish_test