crash2.test 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # 2001 September 15
  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. # The focus of this file is testing the ability of the database to
  14. # uses its rollback journal to recover intact (no database corruption)
  15. # from a power failure during the middle of a COMMIT. Even more
  16. # specifically, the tests in this file verify this functionality
  17. # for storage mediums with various sector sizes.
  18. #
  19. # $Id: crash2.test,v 1.6 2008/08/25 07:12:29 danielk1977 Exp $
  20. set testdir [file dirname $argv0]
  21. source $testdir/tester.tcl
  22. ifcapable !crashtest {
  23. finish_test
  24. return
  25. }
  26. db close
  27. # This test is designed to check that the crash-test infrastructure
  28. # can create files that do not consist of an integer number of
  29. # simulated disk blocks (i.e. 3KB file using 2KB disk blocks).
  30. #
  31. do_test crash2-1.1 {
  32. crashsql -delay 500 -file test.db -blocksize 2048 {
  33. PRAGMA auto_vacuum=OFF;
  34. PRAGMA page_size=1024;
  35. BEGIN;
  36. CREATE TABLE abc AS SELECT 1 AS a, 2 AS b, 3 AS c;
  37. CREATE TABLE def AS SELECT 1 AS d, 2 AS e, 3 AS f;
  38. COMMIT;
  39. }
  40. file size test.db
  41. } {3072}
  42. for {set ii 0} {$ii < 5} {incr ii} {
  43. # Simple test using the database created above: Create a new
  44. # table so that page 1 and page 4 are modified. Using a
  45. # block-size of 2048 and page-size of 1024, this means
  46. # pages 2 and 3 must also be saved in the journal to avoid
  47. # risking corruption.
  48. #
  49. # The loop is so that this test can be run with a couple
  50. # of different seeds for the random number generator.
  51. #
  52. do_test crash2-1.2.$ii {
  53. crashsql -file test.db -blocksize 2048 [subst {
  54. [string repeat {SELECT random();} $ii]
  55. CREATE TABLE hij(h, i, j);
  56. }]
  57. sqlite3 db test.db
  58. db eval {PRAGMA integrity_check}
  59. } {ok}
  60. }
  61. proc signature {} {
  62. return [db eval {SELECT count(*), md5sum(a), md5sum(b), md5sum(c) FROM abc}]
  63. }
  64. # Test case for crashing during journal sync with simulated
  65. # sector-size values from 1024 to 8192.
  66. #
  67. do_test crash2-2.0 {
  68. execsql BEGIN
  69. for {set n 0} {$n < 1000} {incr n} {
  70. execsql "INSERT INTO abc VALUES($n, [expr 2*$n], [expr 3*$n])"
  71. }
  72. execsql {
  73. INSERT INTO abc SELECT * FROM abc;
  74. INSERT INTO abc SELECT * FROM abc;
  75. INSERT INTO abc SELECT * FROM abc;
  76. INSERT INTO abc SELECT * FROM abc;
  77. INSERT INTO abc SELECT * FROM abc;
  78. }
  79. execsql COMMIT
  80. expr ([file size test.db] / 1024) > 450
  81. } {1}
  82. for {set i 1} {$i < 30} {incr i} {
  83. set sig [signature]
  84. set sector [expr 1024 * 1<<($i%4)]
  85. db close
  86. do_test crash2-2.$i.1 {
  87. crashsql -blocksize $sector -delay [expr $i%5 + 1] -file test.db-journal "
  88. PRAGMA temp_store = memory;
  89. BEGIN;
  90. SELECT random() FROM abc LIMIT $i;
  91. INSERT INTO abc SELECT randstr(10,10), 0, 0 FROM abc WHERE random()%2==0;
  92. DELETE FROM abc WHERE random()%2!=0;
  93. COMMIT;
  94. "
  95. } {1 {child process exited abnormally}}
  96. do_test crash2-2.$i.2 {
  97. sqlite3 db test.db
  98. signature
  99. } $sig
  100. }
  101. # Test case for crashing during database sync with simulated
  102. # sector-size values from 1024 to 8192.
  103. #
  104. for {set i 1} {$i < 10} {incr i} {
  105. set sig [signature]
  106. set sector [expr 1024 * 1<<($i%4)]
  107. db close
  108. do_test crash2-3.$i.1 {
  109. crashsql -blocksize $sector -file test.db "
  110. BEGIN;
  111. SELECT random() FROM abc LIMIT $i;
  112. INSERT INTO abc SELECT randstr(10,10), 0, 0 FROM abc WHERE random()%2==0;
  113. DELETE FROM abc WHERE random()%2!=0;
  114. COMMIT;
  115. "
  116. } {1 {child process exited abnormally}}
  117. do_test crash2-3.$i.2 {
  118. sqlite3 db test.db
  119. signature
  120. } $sig
  121. }
  122. finish_test