corrupt3.test 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # 2007 April 6
  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.
  15. #
  16. # $Id: corrupt3.test,v 1.2 2007/04/06 21:42:22 drh Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. # Do not use a codec for tests in this file, as the database file is
  20. # manipulated directly using tcl scripts (using the [hexio_write] command).
  21. #
  22. do_not_use_codec
  23. # We must have the page_size pragma for these tests to work.
  24. #
  25. ifcapable !pager_pragmas||direct_read {
  26. finish_test
  27. return
  28. }
  29. # Create a database with an overflow page.
  30. #
  31. do_test corrupt3-1.1 {
  32. set bigstring [string repeat 0123456789 200]
  33. execsql {
  34. PRAGMA auto_vacuum=OFF;
  35. PRAGMA page_size=1024;
  36. CREATE TABLE t1(x);
  37. INSERT INTO t1 VALUES($bigstring);
  38. }
  39. file size test.db
  40. } [expr {1024*3}]
  41. # Verify that the file format is as we expect. The page size
  42. # should be 1024 bytes. The only record should have a single
  43. # overflow page. The overflow page is page 3. The pointer to
  44. # the overflow page is on the last 4 bytes of page 2.
  45. #
  46. do_test corrupt3-1.2 {
  47. hexio_get_int [hexio_read test.db 16 2]
  48. } 1024 ;# The page size is 1024
  49. do_test corrupt3-1.3 {
  50. hexio_get_int [hexio_read test.db 20 1]
  51. } 0 ;# Unused bytes per page is 0
  52. do_test corrupt3-1.4 {
  53. hexio_get_int [hexio_read test.db 2044 4]
  54. } 3 ;# Overflow page is 3
  55. do_test corrupt3-1.5 {
  56. hexio_get_int [hexio_read test.db 2048 4]
  57. } 0 ;# First chained overflow is 0
  58. integrity_check corrupt3-1.6
  59. # Make the overflow chain loop back on itself. See if the
  60. # corruption is detected. (Actually, the last pointer in
  61. # an overflow chain is ignored, so this is not an error.)
  62. #
  63. do_test corrupt3-1.7 {
  64. db close
  65. hexio_write test.db 2048 [hexio_render_int32 3]
  66. sqlite3 db test.db
  67. catchsql {
  68. SELECT x FROM t1
  69. }
  70. } [list 0 $bigstring]
  71. integrity_check corrupt3-1.8
  72. # Change the pointer for the first page of the overflow
  73. # change to be a non-existant page.
  74. #
  75. do_test corrupt3-1.9 {
  76. db close
  77. hexio_write test.db 2044 [hexio_render_int32 4]
  78. sqlite3 db test.db
  79. catchsql {
  80. SELECT substr(x,1,10) FROM t1
  81. }
  82. } [list 0 0123456789]
  83. do_test corrupt3-1.10 {
  84. catchsql {
  85. PRAGMA integrity_check
  86. }
  87. } {0 {{*** in database main ***
  88. On tree page 2 cell 0: invalid page number 4
  89. Page 3 is never used}}}
  90. do_test corrupt3-1.11 {
  91. db close
  92. hexio_write test.db 2044 [hexio_render_int32 0]
  93. sqlite3 db test.db
  94. catchsql {
  95. SELECT substr(x,1,10) FROM t1
  96. }
  97. } [list 1 {database disk image is malformed}]
  98. do_test corrupt3-1.12 {
  99. catchsql {
  100. PRAGMA integrity_check
  101. }
  102. } {0 {{*** in database main ***
  103. On tree page 2 cell 0: 1 of 1 pages missing from overflow list starting at 0
  104. Page 3 is never used}}}
  105. finish_test