interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / rbu / rbu11.test

223 lines · 5402 bytes Raw Blame History
1# 2015 February 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#
12
13source [file join [file dirname [info script]] rbu_common.tcl]
14if_no_rbu_support { finish_test ; return }
15set ::testprefix rbu11
16
17
18#--------------------------------------------------------------------
19# Test that the xAccess() method of an rbu vfs handles queries other
20# than SQLITE_ACCESS_EXISTS correctly. The test code below causes
21# SQLite to call xAccess(SQLITE_ACCESS_READWRITE) on the directory
22# path argument passed to "PRAGMA temp_store_directory".
23#
24do_test 1.1 {
25 sqlite3rbu_create_vfs -default rbu ""
26 reset_db
27 catchsql { PRAGMA temp_store_directory = '/no/such/directory' }
28} {1 {not a writable directory}}
29
30do_test 1.2 {
31 catchsql " PRAGMA temp_store_directory = '[pwd]' "
32} {0 {}}
33
34do_test 1.3 {
35 catchsql " PRAGMA temp_store_directory = '' "
36} {0 {}}
37
38do_test 1.4 {
39 db close
40 sqlite3rbu_destroy_vfs rbu
41} {}
42
43#--------------------------------------------------------------------
44# Try to trick rbu into operating on a database opened in wal mode.
45#
46reset_db
47do_execsql_test 2.1 {
48 CREATE TABLE t1(a PRIMARY KEY, b, c);
49 INSERT INTO t1 VALUES(1, 2, 3);
50 PRAGMA journal_mode = 'wal';
51 CREATE TABLE t2(d PRIMARY KEY, e, f);
52} {wal}
53
54do_test 2.2 {
55 db_save
56 db close
57
58 forcedelete rbu.db
59 sqlite3 dbo rbu.db
60 dbo eval {
61 CREATE TABLE data_t1(a, b, c, rbu_control);
62 INSERT INTO data_t1 VALUES(4, 5, 6, 0);
63 INSERT INTO data_t1 VALUES(7, 8, 9, 0);
64 }
65 dbo close
66
67 db_restore
68 hexio_write test.db 18 0101
69 file exists test.db-wal
70} {1}
71
72do_test 2.3 {
73 sqlite3rbu rbu test.db rbu.db
74 rbu step
75} {SQLITE_ERROR}
76
77do_test 2.4 {
78 list [catch {rbu close} msg] $msg
79} {1 {SQLITE_ERROR - cannot update wal mode database}}
80
81#--------------------------------------------------------------------
82# Test a constraint violation message with an unusual table name.
83# Specifically, one for which the first character is a codepoint
84# smaller than 30 (character '0').
85#
86reset_db
87do_execsql_test 3.1 {
88 CREATE TABLE "(t1)"(a PRIMARY KEY, b, c);
89 INSERT INTO "(t1)" VALUES(1, 2, 3);
90 INSERT INTO "(t1)" VALUES(4, 5, 6);
91}
92db close
93
94do_test 3.2 {
95 forcedelete rbu.db
96 sqlite3 dbo rbu.db
97 dbo eval {
98 CREATE TABLE "data_(t1)"(a, b, c, rbu_control);
99 INSERT INTO "data_(t1)" VALUES(4, 8, 9, 0);
100 }
101 dbo close
102
103 sqlite3rbu rbu test.db rbu.db
104 rbu step
105 rbu step
106} {SQLITE_CONSTRAINT}
107
108do_test 3.3 {
109 list [catch {rbu close} msg] $msg
110} {1 {SQLITE_CONSTRAINT - UNIQUE constraint failed: (t1).a}}
111
112#--------------------------------------------------------------------
113# Check that once an RBU update has been applied, attempting to apply
114# it a second time is a no-op (as the state stored in the RBU database is
115# "all steps completed").
116#
117reset_db
118do_execsql_test 4.1 {
119 CREATE TABLE "(t1)"(a, b, c, PRIMARY KEY(c, b, a));
120 INSERT INTO "(t1)" VALUES(1, 2, 3);
121 INSERT INTO "(t1)" VALUES(4, 5, 6);
122}
123db close
124
125do_test 4.2 {
126 forcedelete rbu.db
127 sqlite3 dbo rbu.db
128 dbo eval {
129 CREATE TABLE "data_(t1)"(a, b, c, rbu_control);
130 INSERT INTO "data_(t1)" VALUES(7, 8, 9, 0);
131 INSERT INTO "data_(t1)" VALUES(1, 2, 3, 1);
132 }
133 dbo close
134
135 sqlite3rbu rbu test.db rbu.db
136 while {[rbu step]=="SQLITE_OK"} { }
137 rbu close
138} {SQLITE_DONE}
139
140do_test 4.3 {
141 sqlite3rbu rbu test.db rbu.db
142 rbu step
143} {SQLITE_DONE}
144
145do_test 4.4 {
146 rbu close
147} {SQLITE_DONE}
148
149do_test 4.5.1 {
150 sqlite3 dbo rbu.db
151 dbo eval { INSERT INTO rbu_state VALUES(100, 100) }
152 dbo close
153 sqlite3rbu rbu test.db rbu.db
154 rbu step
155} {SQLITE_CORRUPT}
156do_test 4.5.2 {
157 list [catch {rbu close} msg] $msg
158} {1 SQLITE_CORRUPT}
159do_test 4.5.3 {
160 sqlite3 dbo rbu.db
161 dbo eval { DELETE FROM rbu_state WHERE k = 100 }
162 dbo close
163} {}
164
165# Also, check that an invalid state value in the rbu_state table is
166# detected and reported as corruption.
167do_test 4.6.1 {
168 sqlite3 dbo rbu.db
169 dbo eval { UPDATE rbu_state SET v = v*-1 WHERE k = 1 }
170 dbo close
171 sqlite3rbu rbu test.db rbu.db
172 rbu step
173} {SQLITE_CORRUPT}
174do_test 4.6.2 {
175 list [catch {rbu close} msg] $msg
176} {1 SQLITE_CORRUPT}
177do_test 4.6.3 {
178 sqlite3 dbo rbu.db
179 dbo eval { UPDATE rbu_state SET v = v*-1 WHERE k = 1 }
180 dbo close
181} {}
182
183do_test 4.7.1 {
184 sqlite3 dbo rbu.db
185 dbo eval { UPDATE rbu_state SET v = 1 WHERE k = 1 }
186 dbo eval { UPDATE rbu_state SET v = 'nosuchtable' WHERE k = 2 }
187 dbo close
188 sqlite3rbu rbu test.db rbu.db
189 rbu step
190} {SQLITE_ERROR}
191do_test 4.7.2 {
192 list [catch {rbu close} msg] $msg
193} {1 {SQLITE_ERROR - rbu_state mismatch error}}
194
195#-------------------------------------------------------------------------
196# https://sqlite.org/forum/info/6d0a31e22a435877
197#
198reset_db
199forcedelete rbu.db
200
201do_execsql_test 5.0 {
202 CREATE TABLE t1(a BLOB);
203 INSERT INTO t1 VALUES(x'41');
204}
205
206sqlite3 dbRbu rbu.db
207dbRbu eval {
208 CREATE TABLE data_t1(a, rbu_control, rbu_rowid);
209 INSERT INTO data_t1 VALUES(X'310a313a5a337e7e7e7e7e40302c303b','f',1);
210}
211dbRbu close
212
213do_test 5.1 {
214 sqlite3rbu rbu test.db rbu.db
215 rbu step
216} {SQLITE_ERROR}
217
218do_test 5.2 {
219 list [catch {rbu close} msg] $msg
220} {1 {SQLITE_ERROR - corrupt fossil delta}}
221
222
223finish_test