1
0

corruptE.test 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # 2010 February 18
  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 specifcally
  15. # focuses on rowid order corruption.
  16. #
  17. # $Id: corruptE.test,v 1.14 2009/07/11 06:55:34 danielk1977 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. # Do not run the tests in this file if ENABLE_OVERSIZE_CELL_CHECK is on.
  25. #
  26. ifcapable oversize_cell_check {
  27. finish_test
  28. return
  29. }
  30. # Construct a compact, dense database for testing.
  31. #
  32. do_test corruptE-1.1 {
  33. execsql {
  34. PRAGMA auto_vacuum = 0;
  35. PRAGMA legacy_file_format=1;
  36. BEGIN;
  37. CREATE TABLE t1(x,y);
  38. INSERT INTO t1 VALUES(1,1);
  39. INSERT OR IGNORE INTO t1 SELECT x*2,y FROM t1;
  40. INSERT OR IGNORE INTO t1 SELECT x*3,y FROM t1;
  41. INSERT OR IGNORE INTO t1 SELECT x*5,y FROM t1;
  42. INSERT OR IGNORE INTO t1 SELECT x*7,y FROM t1;
  43. INSERT OR IGNORE INTO t1 SELECT x*11,y FROM t1;
  44. INSERT OR IGNORE INTO t1 SELECT x*13,y FROM t1;
  45. INSERT OR IGNORE INTO t1 SELECT x*17,y FROM t1;
  46. INSERT OR IGNORE INTO t1 SELECT x*19,y FROM t1;
  47. CREATE INDEX t1i1 ON t1(x);
  48. CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0 ORDER BY rowid;
  49. COMMIT;
  50. }
  51. } {}
  52. ifcapable {integrityck} {
  53. integrity_check corruptE-1.2
  54. }
  55. # Setup for the tests. Make a backup copy of the good database in test.bu.
  56. #
  57. db close
  58. forcecopy test.db test.bu
  59. sqlite3 db test.db
  60. set fsize [file size test.db]
  61. do_test corruptE-2.1 {
  62. db close
  63. forcecopy test.bu test.db
  64. # insert corrupt byte(s)
  65. hexio_write test.db 2041 [format %02x 0x2e]
  66. sqlite3 db test.db
  67. set res [ catchsql {PRAGMA integrity_check} ]
  68. set ans [lindex $res 1]
  69. list [regexp {out of order.*previous was} $ans] \
  70. [regexp {out of order.*max larger than parent max} $ans]
  71. } {1 1}
  72. do_test corruptE-2.2 {
  73. db close
  74. forcecopy test.bu test.db
  75. # insert corrupt byte(s)
  76. hexio_write test.db 2047 [format %02x 0x84]
  77. sqlite3 db test.db
  78. set res [ catchsql {PRAGMA integrity_check} ]
  79. set ans [lindex $res 1]
  80. list [regexp {out of order.*previous was} $ans] \
  81. [regexp {out of order.*min less than parent min} $ans]
  82. } {1 1}
  83. do_test corruptE-2.3 {
  84. db close
  85. forcecopy test.bu test.db
  86. # insert corrupt byte(s)
  87. hexio_write test.db 7420 [format %02x 0xa8]
  88. hexio_write test.db 10459 [format %02x 0x8d]
  89. sqlite3 db test.db
  90. set res [ catchsql {PRAGMA integrity_check} ]
  91. set ans [lindex $res 1]
  92. list [regexp {out of order.*max larger than parent min} $ans]
  93. } {1}
  94. do_test corruptE-2.4 {
  95. db close
  96. forcecopy test.bu test.db
  97. # insert corrupt byte(s)
  98. hexio_write test.db 10233 [format %02x 0xd0]
  99. sqlite3 db test.db
  100. set res [ catchsql {PRAGMA integrity_check} ]
  101. set ans [lindex $res 1]
  102. list [regexp {out of order.*min less than parent max} $ans]
  103. } {1}
  104. set tests [list {10233 0xd0} \
  105. {941 0x42} \
  106. {1028 0x53} \
  107. {2041 0xd0} \
  108. {2042 0x1f} \
  109. {2047 0xaa} \
  110. {2263 0x29} \
  111. {2274 0x75} \
  112. {3267 0xf2} \
  113. {4104 0x2c} \
  114. {5113 0x36} \
  115. {10233 0x84} \
  116. {10234 0x74} \
  117. {10239 0x41} \
  118. {10453 0x11} \
  119. {11273 0x28} \
  120. {11455 0x11} \
  121. {11461 0xe6} \
  122. {12281 0x99} \
  123. {12296 0x9e} \
  124. {12297 0xd7} \
  125. {13303 0x53} ]
  126. set tc 1
  127. foreach test $tests {
  128. do_test corruptE-3.$tc {
  129. db close
  130. forcecopy test.bu test.db
  131. # insert corrupt byte(s)
  132. hexio_write test.db [lindex $test 0] [format %02x [lindex $test 1]]
  133. sqlite3 db test.db
  134. set res [ catchsql {PRAGMA integrity_check} ]
  135. set ans [lindex $res 1]
  136. list [regexp {out of order} $ans]
  137. } {1}
  138. incr tc 1
  139. }
  140. finish_test