corruptF.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # 2012 January 12
  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. #
  12. set testdir [file dirname $argv0]
  13. source $testdir/tester.tcl
  14. set testprefix corruptF
  15. # Do not use a codec for tests in this file, as the database file is
  16. # manipulated directly using tcl scripts (using the [hexio_write] command).
  17. #
  18. do_not_use_codec
  19. proc str {i} { format %08d $i }
  20. # Create a 6 page database containing a single table - t1. Table t1
  21. # consists of page 2 (the root page) and pages 5 and 6 (leaf pages).
  22. # Database pages 3 and 4 are on the free list.
  23. #
  24. proc create_test_db {} {
  25. catch { db close }
  26. forcedelete test.db
  27. sqlite3 db test.db
  28. db func str str
  29. execsql {
  30. PRAGMA auto_vacuum = 0;
  31. PRAGMA page_size = 1024;
  32. CREATE TABLE t1(x); /* root page = 2 */
  33. CREATE TABLE t2(x); /* root page = 3 */
  34. CREATE TABLE t3(x); /* root page = 4 */
  35. INSERT INTO t1 VALUES(str(1));
  36. INSERT INTO t1 SELECT str(rowid+1) FROM t1;
  37. INSERT INTO t1 SELECT str(rowid+2) FROM t1;
  38. INSERT INTO t1 SELECT str(rowid+4) FROM t1;
  39. INSERT INTO t1 SELECT str(rowid+8) FROM t1;
  40. INSERT INTO t1 SELECT str(rowid+16) FROM t1;
  41. INSERT INTO t1 SELECT str(rowid+32) FROM t1;
  42. INSERT INTO t1 SELECT str(rowid+64) FROM t1;
  43. DROP TABLE t2;
  44. DROP TABLE t3;
  45. }
  46. db close
  47. }
  48. do_test 1.1 { create_test_db } {}
  49. # Check the db is as we expect. 6 pages in total, with 3 and 4 on the free
  50. # list. Page 3 is the free list trunk and page 4 is a leaf.
  51. #
  52. do_test 1.2 { file size test.db } [expr 6*1024]
  53. do_test 1.3 { hexio_read test.db 32 4 } 00000003
  54. do_test 1.4 { hexio_read test.db [expr 2*1024] 12 } 000000000000000100000004
  55. # Change the free-list entry to page 6 and reopen the db file.
  56. do_test 1.5 {
  57. hexio_write test.db [expr 2*1024 + 8] 00000006
  58. sqlite3 db test.db
  59. } {}
  60. # Now create a new table in the database file. The root of the new table
  61. # is page 6, which is also the right-most leaf page in table t1.
  62. #
  63. do_execsql_test 1.6 {
  64. CREATE TABLE t4(x);
  65. SELECT * FROM sqlite_master;
  66. } {
  67. table t1 t1 2 {CREATE TABLE t1(x)}
  68. table t4 t4 6 {CREATE TABLE t4(x)}
  69. }
  70. # At one point this was causing an assert to fail.
  71. #
  72. # This statement opens a cursor on table t1 and does a full table scan. As
  73. # each row is visited, it is copied into table t4. There is no temporary
  74. # table.
  75. #
  76. # When the t1 cursor reaches page 6 (which is both the right-most leaf of
  77. # t1 and the root of t4), it continues to iterate through the keys within
  78. # it (which at this point are keys that have been inserted into t4). And
  79. # for each row visited, another row is inserted into page 6 - it being the
  80. # root page of t4. Eventually, page 6 becomes full and the height of the
  81. # b-tree for table t4 increased. From the point of view of the t1 cursor,
  82. # this unexpectedly reduces the number of keys on page 6 in the middle of
  83. # its iteration, which causes an assert() to fail.
  84. #
  85. db_save_and_close
  86. if 1 {
  87. for {set i 0} {$i < 128} {incr i} {
  88. db_restore_and_reopen
  89. do_test 1.7.$i {
  90. set res [
  91. catchsql { INSERT INTO t4 SELECT x FROM t1 WHERE rowid>$i }
  92. ]
  93. if {$res == "0 {}" || $res == "1 {database disk image is malformed}"} {
  94. set res ""
  95. }
  96. set res
  97. } {}
  98. }
  99. }
  100. do_test 2.1 { create_test_db } {}
  101. do_test 2.2 { file size test.db } [expr 6*1024]
  102. do_test 2.3 { hexio_read test.db 32 4 } 00000003
  103. do_test 2.4 { hexio_read test.db [expr 2*1024] 12 } 000000000000000100000004
  104. # Change the free-list entry to page 5 and reopen the db file.
  105. do_test 2.5 {
  106. hexio_write test.db [expr 2*1024 + 8] 00000005
  107. sqlite3 db test.db
  108. } {}
  109. # Now create a new table in the database file. The root of the new table
  110. # is page 5, which is also the right-most leaf page in table t1.
  111. #
  112. do_execsql_test 2.6 {
  113. CREATE TABLE t4(x);
  114. SELECT * FROM sqlite_master;
  115. } {
  116. table t1 t1 2 {CREATE TABLE t1(x)}
  117. table t4 t4 5 {CREATE TABLE t4(x)}
  118. }
  119. db_save_and_close
  120. for {set i 127} {$i >= 0} {incr i -1} {
  121. db_restore_and_reopen
  122. do_test 2.7.$i {
  123. set res [
  124. catchsql {
  125. INSERT INTO t4 SELECT x FROM t1 WHERE rowid<$i ORDER BY rowid DESC
  126. }
  127. ]
  128. if {$res == "0 {}" || $res == "1 {database disk image is malformed}"} {
  129. set res ""
  130. }
  131. set res
  132. } {}
  133. }
  134. finish_test