multiplex3.test 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # 2011 December 13
  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. # This file contains tests for error (IO, OOM etc.) handling when using
  13. # the multiplexor extension with 8.3 filenames.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. source $testdir/malloc_common.tcl
  18. set ::testprefix multiplex3
  19. ifcapable !8_3_names {
  20. puts -nonewline "SQLite compiled without SQLITE_ENABLE_8_3_NAMES. "
  21. puts "Skipping tests multiplex3-*."
  22. finish_test
  23. return
  24. }
  25. db close
  26. sqlite3_shutdown
  27. sqlite3_config_uri 1
  28. autoinstall_test_functions
  29. sqlite3_multiplex_initialize "" 1
  30. proc destroy_vfs_stack {} {
  31. generic_unregister stack
  32. sqlite3_multiplex_shutdown
  33. }
  34. proc multiplex_delete_db {} {
  35. forcedelete test.db
  36. for {set i 1} {$i <= 1000} {incr i} {
  37. forcedelete test.[format %03d $i]
  38. }
  39. }
  40. # Procs to save and restore the current muliplexed database.
  41. #
  42. proc multiplex_save_db {} {
  43. foreach f [glob -nocomplain sv_test.*] { forcedelete $f }
  44. foreach f [glob -nocomplain test.*] { forcecopy $f "sv_$f" }
  45. }
  46. proc multiplex_restore_db {} {
  47. foreach f [glob -nocomplain test.*] {forcedelete $f}
  48. foreach f [glob -nocomplain sv_test.*] {forcecopy $f [string range $f 3 end]} }
  49. proc setup_and_save_db {} {
  50. multiplex_delete_db
  51. sqlite3 db file:test.db?8_3_names=1
  52. sqlite3_multiplex_control db main chunk_size [expr 256*1024]
  53. execsql {
  54. CREATE TABLE t1(a PRIMARY KEY, b);
  55. INSERT INTO t1 VALUES(randomblob(15), randomblob(2000));
  56. INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 2
  57. INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 4
  58. INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 8
  59. INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 16
  60. INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 32
  61. INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 64
  62. INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 128
  63. INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 256
  64. INSERT INTO t1 SELECT randomblob(15), randomblob(2000) FROM t1; -- 512
  65. }
  66. set ::cksum1 [execsql {SELECT md5sum(a, b) FROM t1 ORDER BY a}]
  67. db close
  68. multiplex_save_db
  69. }
  70. do_test 1.0 { setup_and_save_db } {}
  71. do_faultsim_test 1 -prep {
  72. multiplex_restore_db
  73. sqlite3 db file:test.db?8_3_names=1
  74. sqlite3_multiplex_control db main chunk_size [expr 256*1024]
  75. } -body {
  76. execsql {
  77. UPDATE t1 SET a=randomblob(12), b=randomblob(1500) WHERE (rowid%32)=0
  78. }
  79. } -test {
  80. faultsim_test_result {0 {}}
  81. if {$testrc!=0} {
  82. set cksum2 [execsql {SELECT md5sum(a, b) FROM t1 ORDER BY a}]
  83. if {$cksum2 != $::cksum1} { error "data mismatch" }
  84. }
  85. }
  86. #-------------------------------------------------------------------------
  87. # The following tests verify that hot-journal rollback works. As follows:
  88. #
  89. # 1. Create a large database.
  90. # 2. Set the pager cache to be very small.
  91. # 3. Open a transaction.
  92. # 4. Run the following 100 times:
  93. # a. Update a row.
  94. # b. Copy all files on disk to a new db location, including the journal.
  95. # c. Verify that the new db can be opened and that the content matches
  96. # the database created in step 1 (proving the journal was rolled
  97. # back).
  98. do_test 2.0 {
  99. setup_and_save_db
  100. multiplex_restore_db
  101. sqlite3 db file:test.db?8_3_names=1
  102. execsql { PRAGMA cache_size = 10 }
  103. execsql { BEGIN }
  104. } {}
  105. for {set iTest 1} {$iTest<=100} {incr iTest} {
  106. do_test 2.$iTest {
  107. execsql {
  108. UPDATE t1 SET a=randomblob(12), b=randomblob(1400) WHERE rowid=5*$iTest
  109. }
  110. foreach f [glob -nocomplain test.*] {forcecopy $f "xx_$f"}
  111. sqlite3 db2 file:xx_test.db?8_3_names=1
  112. execsql {SELECT md5sum(a, b) FROM t1 ORDER BY a} db2
  113. } $::cksum1
  114. db2 close
  115. }
  116. catch { db close }
  117. do_test 3.0 { setup_and_save_db } {}
  118. do_faultsim_test 3 -faults ioerr-trans* -prep {
  119. forcedelete test2.db
  120. set fd [open test2.wal w]
  121. seek $fd 4095
  122. puts -nonewline $fd x
  123. close $fd
  124. multiplex_restore_db
  125. sqlite3 db file:test.db?8_3_names=1
  126. sqlite3 db2 file:test2.db?8_3_names=1
  127. sqlite3_multiplex_control db main chunk_size [expr 256*1024]
  128. sqlite3_multiplex_control db2 main chunk_size [expr 256*1024]
  129. } -body {
  130. sqlite3_backup B db2 main db main
  131. B step 100000
  132. set rc [B finish]
  133. if { [string match SQLITE_IOERR_* $rc] } {error "disk I/O error"}
  134. set rc
  135. } -test {
  136. faultsim_test_result {0 SQLITE_OK}
  137. if {$testrc==0} {
  138. set cksum2 [execsql {SELECT md5sum(a, b) FROM t1 ORDER BY a} db2]
  139. if {$cksum2 != $::cksum1} { error "data mismatch" }
  140. }
  141. catch { B finish }
  142. catch { db close }
  143. catch { db2 close }
  144. }
  145. catch { db close }
  146. sqlite3_multiplex_shutdown
  147. finish_test