corrupt7.test 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # 2008 June 11
  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 cell offsets in a btree page.
  16. #
  17. # $Id: corrupt7.test,v 1.8 2009/08/10 10:18:08 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. # We must have the page_size pragma for these tests to work.
  25. #
  26. ifcapable !pager_pragmas {
  27. finish_test
  28. return
  29. }
  30. # Create a simple, small database.
  31. #
  32. do_test corrupt7-1.1 {
  33. execsql {
  34. PRAGMA auto_vacuum=OFF;
  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. }
  43. file size test.db
  44. } [expr {1024*2}]
  45. # Verify that the file format is as we expect. The page size
  46. # should be 1024 bytes.
  47. #
  48. do_test corrupt7-1.2 {
  49. hexio_get_int [hexio_read test.db 16 2]
  50. } 1024 ;# The page size is 1024
  51. do_test corrupt7-1.3 {
  52. hexio_get_int [hexio_read test.db 20 1]
  53. } 0 ;# Unused bytes per page is 0
  54. integrity_check corrupt7-1.4
  55. # Deliberately corrupt some of the cell offsets in the btree page
  56. # on page 2 of the database.
  57. #
  58. # The error message is different depending on whether or not the
  59. # SQLITE_ENABLE_OVERSIZE_CELL_CHECK compile-time option is engaged.
  60. #
  61. ifcapable oversize_cell_check {
  62. do_test corrupt7-2.1 {
  63. db close
  64. hexio_write test.db 1062 FF
  65. sqlite3 db test.db
  66. db eval {PRAGMA integrity_check(1)}
  67. } {{*** in database main ***
  68. Page 2: btreeInitPage() returns error code 11}}
  69. do_test corrupt7-2.2 {
  70. db close
  71. hexio_write test.db 1062 04
  72. sqlite3 db test.db
  73. db eval {PRAGMA integrity_check(1)}
  74. } {{*** in database main ***
  75. Page 2: btreeInitPage() returns error code 11}}
  76. } else {
  77. do_test corrupt7-2.1 {
  78. db close
  79. hexio_write test.db 1062 FF
  80. sqlite3 db test.db
  81. db eval {PRAGMA integrity_check(1)}
  82. } {{*** in database main ***
  83. Corruption detected in cell 15 on page 2}}
  84. do_test corrupt7-2.2 {
  85. db close
  86. hexio_write test.db 1062 04
  87. sqlite3 db test.db
  88. db eval {PRAGMA integrity_check(1)}
  89. } {{*** in database main ***
  90. On tree page 2 cell 15: Rowid 0 out of order (previous was 15)}}
  91. }
  92. # The code path that was causing the buffer overrun that this test
  93. # case was checking for was removed.
  94. #
  95. #do_test corrupt7-3.1 {
  96. # execsql {
  97. # DROP TABLE t1;
  98. # CREATE TABLE t1(a, b);
  99. # INSERT INTO t1 VALUES(1, 'one');
  100. # INSERT INTO t1 VALUES(100, 'one hundred');
  101. # INSERT INTO t1 VALUES(100000, 'one hundred thousand');
  102. # CREATE INDEX i1 ON t1(b);
  103. # }
  104. # db close
  105. #
  106. # # Locate the 3rd cell in the index.
  107. # set cell_offset [hexio_get_int [hexio_read test.db [expr 1024*2 + 12] 2]]
  108. # incr cell_offset [expr 1024*2]
  109. # incr cell_offset 1
  110. #
  111. # # This write corrupts the "header-size" field of the database record
  112. # # stored in the index cell. At one point this was causing sqlite to
  113. # # reference invalid memory.
  114. # hexio_write test.db $cell_offset FFFF7F
  115. #
  116. # sqlite3 db test.db
  117. # catchsql {
  118. # SELECT b FROM t1 WHERE b > 'o' AND b < 'p';
  119. # }
  120. #} {1 {database disk image is malformed}}
  121. finish_test