corrupt8.test 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # 2008 July 9
  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 specifically focuses
  15. # on corrupt pointer map pages.
  16. #
  17. # $Id: corrupt8.test,v 1.2 2008/07/11 03:34:10 drh Exp $
  18. set testdir [file dirname $argv0]
  19. source $testdir/tester.tcl
  20. # Do not use a codec for tests in this file, as the database file is
  21. # manipulated directly using tcl scripts (using the [hexio_write] command).
  22. #
  23. do_not_use_codec
  24. # We must have the page_size pragma for these tests to work.
  25. #
  26. ifcapable !pager_pragmas||!autovacuum {
  27. finish_test
  28. return
  29. }
  30. # Create a database to work with.
  31. #
  32. do_test corrupt8-1.1 {
  33. execsql {
  34. PRAGMA auto_vacuum=1;
  35. PRAGMA page_size=1024;
  36. CREATE TABLE t1(x);
  37. INSERT INTO t1(x) VALUES(1);
  38. INSERT INTO t1(x) VALUES(2);
  39. INSERT INTO t1(x) SELECT x+2 FROM t1;
  40. INSERT INTO t1(x) SELECT x+4 FROM t1;
  41. INSERT INTO t1(x) SELECT x+8 FROM t1;
  42. INSERT INTO t1(x) SELECT x+16 FROM t1;
  43. INSERT INTO t1(x) SELECT x+32 FROM t1;
  44. INSERT INTO t1(x) SELECT x+64 FROM t1;
  45. INSERT INTO t1(x) SELECT x+128 FROM t1;
  46. INSERT INTO t1(x) SELECT x+256 FROM t1;
  47. CREATE TABLE t2(a,b);
  48. INSERT INTO t2 SELECT x, x*x FROM t1;
  49. }
  50. expr {[file size test.db]>1024*12}
  51. } {1}
  52. integrity_check corrupt8-1.2
  53. # Loop through each ptrmap entry. Corrupt the entry and make sure the
  54. # corruption is detected by the integrity_check.
  55. #
  56. for {set i 1024} {$i<2048} {incr i 5} {
  57. set oldval [hexio_read test.db $i 1]
  58. if {$oldval==0} break
  59. hexio_write test.db $i 00
  60. do_test corrupt8-2.$i.0 {
  61. db close
  62. sqlite3 db test.db
  63. set x [db eval {PRAGMA integrity_check}]
  64. expr {$x!="ok"}
  65. } {1}
  66. for {set k 1} {$k<=5} {incr k} {
  67. if {$k==$oldval} continue
  68. hexio_write test.db $i 0$k
  69. do_test corrupt8-2.$i.$k {
  70. db close
  71. sqlite3 db test.db
  72. set x [db eval {PRAGMA integrity_check}]
  73. expr {$x!="ok"}
  74. } {1}
  75. }
  76. hexio_write test.db $i 06
  77. do_test corrupt8-2.$i.6 {
  78. db close
  79. sqlite3 db test.db
  80. set x [db eval {PRAGMA integrity_check}]
  81. expr {$x!="ok"}
  82. } {1}
  83. hexio_write test.db $i $oldval
  84. if {$oldval>2} {
  85. set i2 [expr {$i+1+$i%4}]
  86. set oldval [hexio_read test.db $i2 1]
  87. hexio_write test.db $i2 [format %02x [expr {($oldval+1)&0xff}]]
  88. do_test corrupt8-2.$i.7 {
  89. db close
  90. sqlite3 db test.db
  91. set x [db eval {PRAGMA integrity_check}]
  92. expr {$x!="ok"}
  93. } {1}
  94. hexio_write test.db $i2 $oldval
  95. }
  96. }
  97. finish_test