filefmt.test 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # 2007 April 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 verify database file format.
  14. #
  15. # $Id: filefmt.test,v 1.3 2009/06/18 11:34:43 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Do not use a codec for tests in this file, as the database file is
  19. # manipulated directly using tcl scripts (using the [hexio_write] command).
  20. #
  21. do_not_use_codec
  22. db close
  23. forcedelete test.db test.db-journal
  24. # Database begins with valid 16-byte header string.
  25. #
  26. do_test filefmt-1.1 {
  27. sqlite3 db test.db
  28. db eval {CREATE TABLE t1(x)}
  29. db close
  30. hexio_read test.db 0 16
  31. } {53514C69746520666F726D6174203300}
  32. # If the 16-byte header is changed, the file will not open
  33. #
  34. do_test filefmt-1.2 {
  35. hexio_write test.db 0 54
  36. set x [catch {sqlite3 db test.db} err]
  37. lappend x $err
  38. } {0 {}}
  39. do_test filefmt-1.3 {
  40. catchsql {
  41. SELECT count(*) FROM sqlite_master
  42. }
  43. } {1 {file is encrypted or is not a database}}
  44. do_test filefmt-1.4 {
  45. db close
  46. hexio_write test.db 0 53
  47. sqlite3 db test.db
  48. catchsql {
  49. SELECT count(*) FROM sqlite_master
  50. }
  51. } {0 1}
  52. # The page-size is stored at offset 16
  53. #
  54. ifcapable pager_pragmas {
  55. foreach pagesize {512 1024 2048 4096 8192 16384 32768} {
  56. if {[info exists SQLITE_MAX_PAGE_SIZE]
  57. && $pagesize>$SQLITE_MAX_PAGE_SIZE} continue
  58. do_test filefmt-1.5.$pagesize.1 {
  59. db close
  60. forcedelete test.db
  61. sqlite3 db test.db
  62. db eval "PRAGMA auto_vacuum=OFF"
  63. db eval "PRAGMA page_size=$pagesize"
  64. db eval {CREATE TABLE t1(x)}
  65. file size test.db
  66. } [expr $pagesize*2]
  67. do_test filefmt-1.5.$pagesize.2 {
  68. hexio_get_int [hexio_read test.db 16 2]
  69. } $pagesize
  70. }
  71. }
  72. # The page-size must be a power of 2
  73. #
  74. do_test filefmt-1.6 {
  75. db close
  76. hexio_write test.db 16 [hexio_render_int16 1025]
  77. sqlite3 db test.db
  78. catchsql {
  79. SELECT count(*) FROM sqlite_master
  80. }
  81. } {1 {file is encrypted or is not a database}}
  82. # The page-size must be at least 512 bytes
  83. #
  84. do_test filefmt-1.7 {
  85. db close
  86. hexio_write test.db 16 [hexio_render_int16 256]
  87. sqlite3 db test.db
  88. catchsql {
  89. SELECT count(*) FROM sqlite_master
  90. }
  91. } {1 {file is encrypted or is not a database}}
  92. # Usable space per page (page-size minus unused space per page)
  93. # must be at least 480 bytes
  94. #
  95. ifcapable pager_pragmas {
  96. do_test filefmt-1.8 {
  97. db close
  98. forcedelete test.db
  99. sqlite3 db test.db
  100. db eval {PRAGMA page_size=512; CREATE TABLE t1(x)}
  101. db close
  102. hexio_write test.db 20 21
  103. sqlite3 db test.db
  104. catchsql {
  105. SELECT count(*) FROM sqlite_master
  106. }
  107. } {1 {file is encrypted or is not a database}}
  108. }
  109. #-------------------------------------------------------------------------
  110. # The following block of tests - filefmt-2.* - test that versions 3.7.0
  111. # and later can read and write databases that have been modified or created
  112. # by 3.6.23.1 and earlier. The difference difference is that 3.7.0 stores
  113. # the size of the database in the database file header, whereas 3.6.23.1
  114. # always derives this from the size of the file.
  115. #
  116. db close
  117. forcedelete test.db
  118. set a_string_counter 1
  119. proc a_string {n} {
  120. incr ::a_string_counter
  121. string range [string repeat "${::a_string_counter}." $n] 1 $n
  122. }
  123. sqlite3 db test.db
  124. db func a_string a_string
  125. do_execsql_test filefmt-2.1.1 {
  126. PRAGMA page_size = 1024;
  127. PRAGMA auto_vacuum = 0;
  128. CREATE TABLE t1(a);
  129. CREATE INDEX i1 ON t1(a);
  130. INSERT INTO t1 VALUES(a_string(3000));
  131. CREATE TABLE t2(a);
  132. INSERT INTO t2 VALUES(1);
  133. } {}
  134. do_test filefmt-2.1.2 {
  135. hexio_read test.db 28 4
  136. } {00000009}
  137. do_test filefmt-2.1.3 {
  138. sql36231 { INSERT INTO t1 VALUES(a_string(3000)) }
  139. } {}
  140. do_execsql_test filefmt-2.1.4 { INSERT INTO t2 VALUES(2) } {}
  141. integrity_check filefmt-2.1.5
  142. do_test filefmt-2.1.6 { hexio_read test.db 28 4 } {00000010}
  143. db close
  144. forcedelete test.db
  145. sqlite3 db test.db
  146. db func a_string a_string
  147. do_execsql_test filefmt-2.2.1 {
  148. PRAGMA page_size = 1024;
  149. PRAGMA auto_vacuum = 0;
  150. CREATE TABLE t1(a);
  151. CREATE INDEX i1 ON t1(a);
  152. INSERT INTO t1 VALUES(a_string(3000));
  153. CREATE TABLE t2(a);
  154. INSERT INTO t2 VALUES(1);
  155. } {}
  156. do_test filefmt-2.2.2 {
  157. hexio_read test.db 28 4
  158. } {00000009}
  159. do_test filefmt-2.2.3 {
  160. sql36231 { INSERT INTO t1 VALUES(a_string(3000)) }
  161. } {}
  162. do_execsql_test filefmt-2.2.4 {
  163. PRAGMA integrity_check;
  164. BEGIN;
  165. INSERT INTO t2 VALUES(2);
  166. SAVEPOINT a;
  167. INSERT INTO t2 VALUES(3);
  168. ROLLBACK TO a;
  169. } {ok}
  170. integrity_check filefmt-2.2.5
  171. do_execsql_test filefmt-2.2.6 { COMMIT } {}
  172. db close
  173. sqlite3 db test.db
  174. integrity_check filefmt-2.2.7
  175. #--------------------------------------------------------------------------
  176. # Check that ticket 89b8c9ac54 is fixed. Before the fix, the SELECT
  177. # statement would return SQLITE_CORRUPT. The database file was not actually
  178. # corrupted, but SQLite was reporting that it was.
  179. #
  180. db close
  181. forcedelete test.db
  182. sqlite3 db test.db
  183. do_execsql_test filefmt-3.1 {
  184. PRAGMA auto_vacuum = 1;
  185. CREATE TABLE t1(a, b);
  186. } {}
  187. do_test filefmt-3.2 {
  188. sql36231 { DROP TABLE t1 }
  189. } {}
  190. do_execsql_test filefmt-3.3 {
  191. SELECT * FROM sqlite_master;
  192. PRAGMA integrity_check;
  193. } {ok}
  194. reset_db
  195. do_execsql_test filefmt-4.1 {
  196. PRAGMA auto_vacuum = 1;
  197. CREATE TABLE t1(x, y);
  198. CREATE TABLE t2(x, y);
  199. INSERT INTO t1 VALUES(randomblob(100), randomblob(100));
  200. INSERT INTO t1 VALUES(randomblob(100), randomblob(100));
  201. INSERT INTO t1 VALUES(randomblob(100), randomblob(100));
  202. INSERT INTO t1 VALUES(randomblob(100), randomblob(100));
  203. INSERT INTO t1 VALUES(randomblob(100), randomblob(100));
  204. INSERT INTO t1 VALUES(randomblob(100), randomblob(100));
  205. INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM t1;
  206. INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM t1;
  207. INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM t1;
  208. INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM t1;
  209. }
  210. do_test filefmt-4.2 {
  211. sql36231 { INSERT INTO t2 SELECT * FROM t1 }
  212. } {}
  213. do_test filefmt-4.3 {
  214. forcedelete bak.db
  215. db backup bak.db
  216. } {}
  217. do_test filefmt-4.4 {
  218. sqlite3 db2 bak.db
  219. db2 eval { PRAGMA integrity_check }
  220. } {ok}
  221. db2 close
  222. finish_test