corrupt9.test 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 corruption in the form of duplicate entries on the freelist.
  16. #
  17. # $Id: corrupt9.test,v 1.3 2009/06/04 02:47:04 shane 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 {
  27. finish_test
  28. return
  29. }
  30. # Return the offset to the first (trunk) page of the freelist. Return
  31. # zero of the freelist is empty.
  32. #
  33. proc freelist_trunk_offset {filename} {
  34. if {[hexio_read $filename 36 4]==0} {return 0}
  35. set pgno [hexio_get_int [hexio_read $filename 32 4]]
  36. return [expr {($pgno-1)*[hexio_get_int [hexio_read $filename 16 2]]}]
  37. }
  38. # This procedure looks at the first trunk page of the freelist and
  39. # corrupts that page by overwriting up to N entries with duplicates
  40. # of the first entry.
  41. #
  42. proc corrupt_freelist {filename N} {
  43. set offset [freelist_trunk_offset $filename]
  44. if {$offset==0} {error "Freelist is empty"}
  45. set cnt [hexio_get_int [hexio_read $filename [expr {$offset+4}] 4]]
  46. set pgno [hexio_read $filename [expr {$offset+8}] 4]
  47. for {set i 12} {$N>0 && $i<8+4*$cnt} {incr i 4; incr N -1} {
  48. hexio_write $filename [expr {$offset+$i}] $pgno
  49. }
  50. }
  51. # Create a database to work with. Make sure there are plenty of
  52. # entries on the freelist.
  53. #
  54. do_test corrupt9-1.1 {
  55. execsql {
  56. PRAGMA auto_vacuum=NONE;
  57. PRAGMA page_size=1024;
  58. CREATE TABLE t1(x);
  59. INSERT INTO t1(x) VALUES(1);
  60. INSERT INTO t1(x) VALUES(2);
  61. INSERT INTO t1(x) SELECT x+2 FROM t1;
  62. INSERT INTO t1(x) SELECT x+4 FROM t1;
  63. INSERT INTO t1(x) SELECT x+8 FROM t1;
  64. INSERT INTO t1(x) SELECT x+16 FROM t1;
  65. INSERT INTO t1(x) SELECT x+32 FROM t1;
  66. INSERT INTO t1(x) SELECT x+64 FROM t1;
  67. INSERT INTO t1(x) SELECT x+128 FROM t1;
  68. INSERT INTO t1(x) SELECT x+256 FROM t1;
  69. CREATE TABLE t2(a,b);
  70. INSERT INTO t2 SELECT x, x*x FROM t1;
  71. CREATE INDEX i1 ON t1(x);
  72. CREATE INDEX i2 ON t2(b,a);
  73. DROP INDEX i2;
  74. }
  75. expr {[file size test.db]>1024*24}
  76. } {1}
  77. integrity_check corrupt9-1.2
  78. # Corrupt the freelist by adding duplicate entries to the freelist.
  79. # Make sure the corruption is detected.
  80. #
  81. db close
  82. forcecopy test.db test.db-template
  83. corrupt_freelist test.db 1
  84. sqlite3 db test.db
  85. do_test corrupt9-2.1 {
  86. set x [db eval {PRAGMA integrity_check}]
  87. expr {$x!="ok"}
  88. } {1}
  89. do_test corrupt9-2.2 {
  90. catchsql {
  91. CREATE INDEX i2 ON t2(b,a);
  92. REINDEX;
  93. }
  94. } {1 {database disk image is malformed}}
  95. db close
  96. forcecopy test.db-template test.db
  97. corrupt_freelist test.db 2
  98. sqlite3 db test.db
  99. do_test corrupt9-3.1 {
  100. set x [db eval {PRAGMA integrity_check}]
  101. expr {$x!="ok"}
  102. } {1}
  103. do_test corrupt9-3.2 {
  104. catchsql {
  105. CREATE INDEX i2 ON t2(b,a);
  106. REINDEX;
  107. }
  108. } {1 {database disk image is malformed}}
  109. db close
  110. forcecopy test.db-template test.db
  111. corrupt_freelist test.db 3
  112. sqlite3 db test.db
  113. do_test corrupt9-4.1 {
  114. set x [db eval {PRAGMA integrity_check}]
  115. expr {$x!="ok"}
  116. } {1}
  117. do_test corrupt9-4.2 {
  118. catchsql {
  119. CREATE INDEX i2 ON t2(b,a);
  120. REINDEX;
  121. }
  122. } {1 {database disk image is malformed}}
  123. finish_test