journal2.test 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # 2010 June 16
  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. Specifically,
  12. # it tests SQLite when using a VFS that claims the SAFE_DELETE property.
  13. #
  14. set testdir [file dirname $argv0]
  15. source $testdir/tester.tcl
  16. source $testdir/lock_common.tcl
  17. source $testdir/malloc_common.tcl
  18. db close
  19. if {[permutation] == "inmemory_journal"} {
  20. finish_test
  21. return
  22. }
  23. set a_string_counter 1
  24. proc a_string {n} {
  25. global a_string_counter
  26. incr a_string_counter
  27. string range [string repeat "${a_string_counter}." $n] 1 $n
  28. }
  29. # Create a [testvfs] and install it as the default VFS. Set the device
  30. # characteristics flags to "SAFE_DELETE".
  31. #
  32. testvfs tvfs -default 1
  33. tvfs devchar {undeletable_when_open powersafe_overwrite}
  34. # Set up a hook so that each time a journal file is opened, closed or
  35. # deleted, the method name ("xOpen", "xClose" or "xDelete") and the final
  36. # segment of the journal file-name (i.e. "test.db-journal") are appended to
  37. # global list variable $::oplog.
  38. #
  39. tvfs filter {xOpen xClose xDelete}
  40. tvfs script journal_op_catcher
  41. proc journal_op_catcher {method filename args} {
  42. # If global variable ::tvfs_error_on_write is defined, then return an
  43. # IO error to every attempt to modify the file-system. Otherwise, return
  44. # SQLITE_OK.
  45. #
  46. if {[info exists ::tvfs_error_on_write]} {
  47. if {[lsearch {xDelete xWrite xTruncate} $method]>=0} {
  48. return SQLITE_IOERR
  49. }
  50. }
  51. # The rest of this command only deals with xOpen(), xClose() and xDelete()
  52. # operations on journal files. If this invocation does not represent such
  53. # an operation, return with no further ado.
  54. #
  55. set f [file tail $filename]
  56. if {[string match *journal $f]==0} return
  57. if {[lsearch {xOpen xDelete xClose} $method]<0} return
  58. # Append a record of this operation to global list variable $::oplog.
  59. #
  60. lappend ::oplog $method $f
  61. # If this is an attempt to delete a journal file for which there exists
  62. # one ore more open handles, return an error. The code in test_vfs.c
  63. # will not invoke the xDelete method of the "real" VFS in this case.
  64. #
  65. if {[info exists ::open_journals($f)]==0} { set ::open_journals($f) 0 }
  66. switch -- $method {
  67. xOpen { incr ::open_journals($f) +1 }
  68. xClose { incr ::open_journals($f) -1 }
  69. xDelete { if {$::open_journals($f)>0} { return SQLITE_IOERR } }
  70. }
  71. return ""
  72. }
  73. do_test journal2-1.1 {
  74. set ::oplog [list]
  75. sqlite3 db test.db
  76. execsql { CREATE TABLE t1(a, b) }
  77. set ::oplog
  78. } {xOpen test.db-journal xClose test.db-journal xDelete test.db-journal}
  79. do_test journal2-1.2 {
  80. set ::oplog [list]
  81. execsql {
  82. PRAGMA journal_mode = truncate;
  83. INSERT INTO t1 VALUES(1, 2);
  84. }
  85. set ::oplog
  86. } {xOpen test.db-journal}
  87. do_test journal2-1.3 {
  88. set ::oplog [list]
  89. execsql { INSERT INTO t1 VALUES(3, 4) }
  90. set ::oplog
  91. } {}
  92. do_test journal2-1.4 { execsql { SELECT * FROM t1 } } {1 2 3 4}
  93. # Add a second connection. This connection attempts to commit data in
  94. # journal_mode=DELETE mode. When it tries to delete the journal file,
  95. # the VFS layer returns an IO error.
  96. #
  97. do_test journal2-1.5 {
  98. set ::oplog [list]
  99. sqlite3 db2 test.db
  100. execsql { PRAGMA journal_mode = delete } db2
  101. catchsql { INSERT INTO t1 VALUES(5, 6) } db2
  102. } {1 {disk I/O error}}
  103. do_test journal2-1.6 { file exists test.db-journal } 1
  104. do_test journal2-1.7 { execsql { SELECT * FROM t1 } } {1 2 3 4}
  105. do_test journal2-1.8 {
  106. execsql { PRAGMA journal_mode = truncate } db2
  107. execsql { INSERT INTO t1 VALUES(5, 6) } db2
  108. } {}
  109. do_test journal2-1.9 { execsql { SELECT * FROM t1 } } {1 2 3 4 5 6}
  110. # Grow the database until it is reasonably large.
  111. #
  112. do_test journal2-1.10 {
  113. db2 close
  114. db func a_string a_string
  115. execsql {
  116. CREATE TABLE t2(a UNIQUE, b UNIQUE);
  117. INSERT INTO t2 VALUES(a_string(200), a_string(300));
  118. INSERT INTO t2 SELECT a_string(200), a_string(300) FROM t2; -- 2
  119. INSERT INTO t2 SELECT a_string(200), a_string(300) FROM t2; -- 4
  120. INSERT INTO t2 SELECT a_string(200), a_string(300) FROM t2; -- 8
  121. INSERT INTO t2 SELECT a_string(200), a_string(300) FROM t2; -- 16
  122. INSERT INTO t2 SELECT a_string(200), a_string(300) FROM t2; -- 32
  123. INSERT INTO t2 SELECT a_string(200), a_string(300) FROM t2; -- 64
  124. }
  125. file size test.db-journal
  126. } {0}
  127. do_test journal2-1.11 {
  128. set sz [expr [file size test.db] / 1024]
  129. expr {$sz>120 && $sz<200}
  130. } 1
  131. # Using new connection [db2] (with journal_mode=DELETE), write a lot of
  132. # data to the database. So that many pages within the database file are
  133. # modified before the transaction is committed.
  134. #
  135. # Then, enable simulated IO errors in all calls to xDelete, xWrite
  136. # and xTruncate before committing the transaction and closing the
  137. # database file. From the point of view of other file-system users, it
  138. # appears as if the process hosting [db2] unexpectedly exited.
  139. #
  140. do_test journal2-1.12 {
  141. sqlite3 db2 test.db
  142. execsql {
  143. PRAGMA cache_size = 10;
  144. BEGIN;
  145. INSERT INTO t2 SELECT randomblob(200), randomblob(300) FROM t2; -- 128
  146. } db2
  147. } {}
  148. do_test journal2-1.13 {
  149. tvfs filter {xOpen xClose xDelete xWrite xTruncate}
  150. set ::tvfs_error_on_write 1
  151. catchsql { COMMIT } db2
  152. } {1 {disk I/O error}}
  153. db2 close
  154. unset ::tvfs_error_on_write
  155. forcecopy test.db testX.db
  156. do_test journal2-1.14 { file exists test.db-journal } 1
  157. do_test journal2-1.15 {
  158. execsql {
  159. SELECT count(*) FROM t2;
  160. PRAGMA integrity_check;
  161. }
  162. } {64 ok}
  163. # This block checks that in the test case above, connection [db2] really
  164. # did begin writing to the database file before it hit IO errors. If
  165. # this is true, then the copy of the database file made before [db]
  166. # rolled back the hot journal should fail the integrity-check.
  167. #
  168. do_test journal2-1.16 {
  169. set sz [expr [file size testX.db] / 1024]
  170. expr {$sz>240 && $sz<400}
  171. } 1
  172. do_test journal2-1.17 {
  173. expr {[catchsql { PRAGMA integrity_check } db] == "0 ok"}
  174. } {1}
  175. do_test journal2-1.20 {
  176. sqlite3 db2 testX.db
  177. expr {[catchsql { PRAGMA integrity_check } db2] == "0 ok"}
  178. } {0}
  179. do_test journal2-1.21 {
  180. db2 close
  181. } {}
  182. db close
  183. #-------------------------------------------------------------------------
  184. # Test that it is possible to switch from journal_mode=truncate to
  185. # journal_mode=WAL on a SAFE_DELETE file-system. SQLite should close and
  186. # delete the journal file when committing the transaction that switches
  187. # the system to WAL mode.
  188. #
  189. ifcapable wal {
  190. do_test journal2-2.1 {
  191. faultsim_delete_and_reopen
  192. set ::oplog [list]
  193. execsql { PRAGMA journal_mode = persist }
  194. set ::oplog
  195. } {}
  196. do_test journal2-2.2 {
  197. execsql {
  198. CREATE TABLE t1(x);
  199. INSERT INTO t1 VALUES(3.14159);
  200. }
  201. set ::oplog
  202. } {xOpen test.db-journal}
  203. do_test journal2-2.3 {
  204. expr {[file size test.db-journal] > 512}
  205. } {1}
  206. do_test journal2-2.4 {
  207. set ::oplog [list]
  208. execsql { PRAGMA journal_mode = WAL }
  209. set ::oplog
  210. } {xClose test.db-journal xDelete test.db-journal}
  211. db close
  212. }
  213. tvfs delete
  214. finish_test