3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is the "memdb" VFS
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
20 ifcapable !deserialize {
25 # Create a MEMDB and populate it with some dummy data.
26 # Then extract the database into the $::db1 variable.
27 # Verify that the size of $::db1 is the same as the size of
32 unset -nocomplain pgsz
36 INSERT INTO t1 VALUES(1,2);
38 set ::pgsz [db one {PRAGMA page_size}]
39 set ::sz1 [expr {$::pgsz*[db one {PRAGMA page_count}]}]
40 set ::db1 [db serialize]
41 expr {[string length $::db1]==$::sz1}
43 set fd [open db1.db wb]
44 puts -nonewline $fd $db1
47 # Create a new MEMDB and initialize it to the content of $::db1
48 # Verify that the content is the same.
57 # What happens when we try to VACUUM a MEMDB database?
60 PRAGMA auto_vacuum = off;
64 CREATE TABLE t2(x, y);
65 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)
66 INSERT INTO t2(x, y) SELECT x, randomblob(1000) FROM c;
76 catch {db deserialize -unknown 1 $db1} msg
78 } {unknown option: -unknown}
80 db deserialize -readonly 1 $db1
81 db eval {SELECT * FROM t1}
84 catchsql {INSERT INTO t1 VALUES(3,4);}
85 } {1 {attempt to write a readonly database}}
89 db deserialize -maxsize 32768 $db1
90 db eval {SELECT * FROM t1}
93 db eval {INSERT INTO t1 VALUES(3,4); SELECT * FROM t1}
96 catchsql {INSERT INTO t1 VALUES(5,randomblob(100000))}
97 } {1 {database or disk is full}}
100 # Build a largish on-disk database and serialize it. Verify that the
101 # serialization works.
106 do_execsql_test 200 {
107 CREATE TABLE t3(x, y);
108 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<400)
109 INSERT INTO t3(x, y) SELECT x, randomblob(1000) FROM c;
112 set fd [open test.db rb]
113 unset -nocomplain direct
114 set direct [read $fd]
117 string length [db serialize]
118 } [string length $direct]
120 db eval {ATTACH ':memory:' AS aux1}
121 db deserialize aux1 $::direct
123 SELECT x, y FROM main.t3 EXCEPT SELECT x, y FROM aux1.t3;
126 unset -nocomplain direct
128 # Do the same with a :memory: database.
132 do_execsql_test 300 {
133 CREATE TABLE t3(x, y);
134 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<400)
135 INSERT INTO t3(x, y) SELECT x, randomblob(1000) FROM c;
139 db eval {ATTACH ':memory:' AS aux1}
140 db deserialize aux1 [db serialize main]
142 SELECT x, y FROM main.t3 EXCEPT SELECT x, y FROM aux1.t3;
146 # Deserialize an empty database
151 do_execsql_test 400 {
152 PRAGMA integrity_check;
154 do_execsql_test 410 {
155 CREATE TABLE t4(a,b);
156 INSERT INTO t4 VALUES('hello','world!');
157 PRAGMA integrity_check;
160 do_execsql_test 420 {
161 PRAGMA journal_mode=TRUNCATE;
162 PRAGMA journal_mode=OFF;
163 PRAGMA journal_mode=DELETE;
164 PRAGMA journal_mode=WAL;
165 PRAGMA journal_mode=PERSIST;
166 PRAGMA journal_mode=MEMORY;
167 PRAGMA journal_mode=OFF;
168 PRAGMA journal_mode=DELETE;
169 } {truncate off delete delete persist memory off delete}
171 # Deserialize something that is not a database.
176 set rc [catch {db deserialize not-a-database} msg]
179 do_catchsql_test 510 {
180 PRAGMA integrity_check;
181 } {1 {file is not a database}}
183 # Abuse the serialize and deserialize commands. Make sure errors are caught.
186 set rc [catch {db deserialize} msg]
188 } {1 {wrong # args: should be "db deserialize ?DATABASE? VALUE"}}
190 set rc [catch {db deserialize a b c} msg]
192 } {1 {unknown option: a}}
194 set rc [catch {db serialize a b} msg]
196 } {1 {wrong # args: should be "db serialize ?DATABASE?"}}
198 # 2021-07-19 https://sqlite.org/forum/forumpost/e1cbb5f450b98aa6
199 # The TEMP database cannot participate in serialization or
205 CREATE TEMP TABLE t0(a);
207 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000)
208 INSERT INTO t1(x) SELECT random() FROM c;
210 set rc [catch {db deserialize temp [db serialize main]} err]
214 #-------------------------------------------------------------------------
217 do_execsql_test 700 {
218 CREATE TABLE t1(a, b);
219 PRAGMA schema_version = 0;
222 set ser [db serialize main]
225 db deserialize main $ser
227 CREATE VIRTUAL TABLE t1 USING rtree(id, a, b, c, d);
229 } {1 {table t1 already exists}}
233 #-------------------------------------------------------------------------
234 # dbsqlfuzz 0a13dfb474d4f2f11a48a2ea57075c96fb456dd7
236 if {[wal_is_capable]} {
238 do_execsql_test 800 {
239 PRAGMA auto_vacuum = 0;
240 PRAGMA page_size = 8192;
241 PRAGMA journal_mode = wal;
242 CREATE TABLE t1(x, y);
243 INSERT INTO t1 VALUES(1, 2);
244 CREATE TABLE t2(x, y);
248 set fd [open test.db]
249 fconfigure $fd -translation binary -encoding binary
250 set data [read $fd [expr 20*1024]]
255 do_execsql_test 810 {
256 PRAGMA locking_mode = exclusive;
260 do_execsql_test 820 {
261 INSERT INTO t1 VALUES(3, 4);
265 do_catchsql_test 830 {
266 PRAGMA wal_checkpoint;
267 } {1 {database disk image is malformed}}