corrupt6.test 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # 2008 May 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. It specifically focuses
  15. # on corrupt SerialTypeLen values.
  16. #
  17. # $Id: corrupt6.test,v 1.2 2008/05/19 15:37:10 shane Exp $
  18. set testdir [file dirname $argv0]
  19. source $testdir/tester.tcl
  20. # Do not use a codec for tests in this file, as the database file is
  21. # manipulated directly using tcl scripts (using the [hexio_write] command).
  22. #
  23. do_not_use_codec
  24. # We must have the page_size pragma for these tests to work.
  25. #
  26. ifcapable !pager_pragmas {
  27. finish_test
  28. return
  29. }
  30. # Create a simple, small database.
  31. #
  32. do_test corrupt6-1.1 {
  33. execsql {
  34. PRAGMA auto_vacuum=OFF;
  35. PRAGMA page_size=1024;
  36. CREATE TABLE t1(x);
  37. INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789');
  38. INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789');
  39. }
  40. file size test.db
  41. } [expr {1024*2}]
  42. # Verify that the file format is as we expect. The page size
  43. # should be 1024 bytes.
  44. #
  45. do_test corrupt6-1.2 {
  46. hexio_get_int [hexio_read test.db 16 2]
  47. } 1024 ;# The page size is 1024
  48. do_test corrupt6-1.3 {
  49. hexio_get_int [hexio_read test.db 20 1]
  50. } 0 ;# Unused bytes per page is 0
  51. integrity_check corrupt6-1.4
  52. # Verify SerialTypeLen for first field of two records as we expect.
  53. # SerialTypeLen = (len*2+12) = 60*2+12 = 132
  54. do_test corrupt6-1.5.1 {
  55. hexio_read test.db 1923 2
  56. } 8103 ;# First text field size is 81 03 == 131
  57. do_test corrupt6-1.5.2 {
  58. hexio_read test.db 1987 2
  59. } 8103 ;# Second text field size is 81 03 == 131
  60. # Verify simple query results as expected.
  61. do_test corrupt6-1.6 {
  62. db close
  63. sqlite3 db test.db
  64. catchsql {
  65. SELECT substr(x,1,8) FROM t1
  66. }
  67. } [list 0 {varint32 varint32} ]
  68. integrity_check corrupt6-1.7
  69. # Adjust value of record 1 / field 1 SerialTypeLen and see if the
  70. # corruption is detected.
  71. # Increase SerialTypeLen by 2.
  72. do_test corrupt6-1.8.1 {
  73. db close
  74. hexio_write test.db 1923 8105
  75. sqlite3 db test.db
  76. catchsql {
  77. SELECT substr(x,1,8) FROM t1
  78. }
  79. } [list 1 {database disk image is malformed}]
  80. # Adjust value of record 1 / field 1 SerialTypeLen and see if the
  81. # corruption is detected.
  82. # Decrease SerialTypeLen by 2.
  83. do_test corrupt6-1.8.2 {
  84. db close
  85. hexio_write test.db 1923 8101
  86. sqlite3 db test.db
  87. catchsql {
  88. SELECT substr(x,1,8) FROM t1
  89. }
  90. } [list 1 {database disk image is malformed}]
  91. # Put value of record 1 / field 1 SerialTypeLen back.
  92. do_test corrupt6-1.8.3 {
  93. db close
  94. hexio_write test.db 1923 8103
  95. sqlite3 db test.db
  96. catchsql {
  97. SELECT substr(x,1,8) FROM t1
  98. }
  99. } [list 0 {varint32 varint32} ]
  100. integrity_check corrupt6-1.8.4
  101. # Adjust value of record 2 / field 1 SerialTypeLen and see if the
  102. # corruption is detected.
  103. # Increase SerialTypeLen by 2.
  104. do_test corrupt6-1.9.1 {
  105. db close
  106. hexio_write test.db 1987 8105
  107. sqlite3 db test.db
  108. catchsql {
  109. SELECT substr(x,1,8) FROM t1
  110. }
  111. } [list 1 {database disk image is malformed}]
  112. # Adjust value of record 2 / field 2 SerialTypeLen and see if the
  113. # corruption is detected.
  114. # Decrease SerialTypeLen by 2.
  115. do_test corrupt6-1.9.2 {
  116. db close
  117. hexio_write test.db 1987 8101
  118. sqlite3 db test.db
  119. catchsql {
  120. SELECT substr(x,1,8) FROM t1
  121. }
  122. } [list 1 {database disk image is malformed}]
  123. # Put value of record 1 / field 2 SerialTypeLen back.
  124. do_test corrupt6-1.9.3 {
  125. db close
  126. hexio_write test.db 1987 8103
  127. sqlite3 db test.db
  128. catchsql {
  129. SELECT substr(x,1,8) FROM t1
  130. }
  131. } [list 0 {varint32 varint32} ]
  132. integrity_check corrupt6-1.9.4
  133. # Adjust value of record 1 / field 1 SerialTypeLen and see if the
  134. # corruption is detected.
  135. # Set SerialTypeLen to FF 7F (2 bytes)
  136. do_test corrupt6-1.10.1 {
  137. db close
  138. hexio_write test.db 1923 FF7F
  139. sqlite3 db test.db
  140. catchsql {
  141. SELECT substr(x,1,8) FROM t1
  142. }
  143. } [list 1 {database disk image is malformed}]
  144. # Adjust value of record 1 / field 1 SerialTypeLen and see if the
  145. # corruption is detected.
  146. # Set SerialTypeLen to FF FF 7F (3 bytes)
  147. do_test corrupt6-1.10.2 {
  148. db close
  149. hexio_write test.db 1923 FFFF7F
  150. sqlite3 db test.db
  151. catchsql {
  152. SELECT substr(x,1,8) FROM t1
  153. }
  154. } [list 1 {database disk image is malformed}]
  155. # Adjust value of record 1 / field 1 SerialTypeLen and see if the
  156. # corruption is detected.
  157. # Set SerialTypeLen to FF FF FF 7F (4 bytes)
  158. do_test corrupt6-1.10.3 {
  159. db close
  160. hexio_write test.db 1923 FFFFFF7F
  161. sqlite3 db test.db
  162. catchsql {
  163. SELECT substr(x,1,8) FROM t1
  164. }
  165. } [list 1 {database disk image is malformed}]
  166. # Adjust value of record 1 / field 1 SerialTypeLen and see if the
  167. # corruption is detected.
  168. # Set SerialTypeLen to FF FF FF FF 7F (5 bytes)
  169. do_test corrupt6-1.10.4 {
  170. db close
  171. hexio_write test.db 1923 FFFFFFFF7F
  172. sqlite3 db test.db
  173. catchsql {
  174. SELECT substr(x,1,8) FROM t1
  175. }
  176. } [list 1 {database disk image is malformed}]
  177. # Adjust value of record 1 / field 1 SerialTypeLen and see if the
  178. # corruption is detected.
  179. # Set SerialTypeLen to FF FF FF FF FF 7F (6 bytes, and overflows).
  180. do_test corrupt6-1.10.5 {
  181. db close
  182. hexio_write test.db 1923 FFFFFFFFFF7F
  183. sqlite3 db test.db
  184. catchsql {
  185. SELECT substr(x,1,8) FROM t1
  186. }
  187. } [list 1 {database disk image is malformed}]
  188. # Adjust value of record 1 / field 1 SerialTypeLen and see if the
  189. # corruption is detected.
  190. # Set SerialTypeLen to FF FF FF FF FF FF 7F (7 bytes, and overflows).
  191. do_test corrupt6-1.10.6 {
  192. db close
  193. hexio_write test.db 1923 FFFFFFFFFFFF7F
  194. sqlite3 db test.db
  195. catchsql {
  196. SELECT substr(x,1,8) FROM t1
  197. }
  198. } [list 1 {database disk image is malformed}]
  199. # Adjust value of record 1 / field 1 SerialTypeLen and see if the
  200. # corruption is detected.
  201. # Set SerialTypeLen to FF FF FF FF FF FF FF 7F (8 bytes, and overflows).
  202. do_test corrupt6-1.10.7 {
  203. db close
  204. hexio_write test.db 1923 FFFFFFFFFFFFFF7F
  205. sqlite3 db test.db
  206. catchsql {
  207. SELECT substr(x,1,8) FROM t1
  208. }
  209. } [list 1 {database disk image is malformed}]
  210. # Adjust value of record 1 / field 1 SerialTypeLen and see if the
  211. # corruption is detected.
  212. # Set SerialTypeLen to FF FF FF FF FF FF FF FF 7F (9 bytes, and overflows).
  213. do_test corrupt6-1.10.8 {
  214. db close
  215. hexio_write test.db 1923 FFFFFFFFFFFFFFFF7F
  216. sqlite3 db test.db
  217. catchsql {
  218. SELECT substr(x,1,8) FROM t1
  219. }
  220. } [list 1 {database disk image is malformed}]
  221. # Adjust value of record 1 / field 1 SerialTypeLen and see if the
  222. # corruption is detected.
  223. # Set SerialTypeLen to FFFF FF FF FF FF FF FF FF 7F (10 bytes, and overflows).
  224. do_test corrupt6-1.10.9 {
  225. db close
  226. hexio_write test.db 1923 FFFFFFFFFFFFFFFFFF7F
  227. sqlite3 db test.db
  228. catchsql {
  229. SELECT substr(x,1,8) FROM t1
  230. }
  231. } [list 1 {database disk image is malformed}]
  232. finish_test