blob: b2bc8f8e0d7a5ab790818497e3b025ee6ae63d13 [file] [log] [blame]
dan61c7f592010-10-26 18:42:52 +00001# 2010 October 20
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#
13
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16
dana32536b2021-11-08 19:35:26 +000017ifcapable !incrblob {
18 finish_test
19 return
20}
21
dan61c7f592010-10-26 18:42:52 +000022sqlite3 db test.db
23sqlite3_db_config_lookaside db 0 0 0
24
25do_execsql_test incrblob3-1.1 {
26 CREATE TABLE blobs(k INTEGER PRIMARY KEY, v BLOB);
27 INSERT INTO blobs VALUES(1, zeroblob(100));
28 INSERT INTO blobs VALUES(2, zeroblob(100));
29} {}
30
31# Test the sqlite3_blob_reopen()/read()/write() functions.
32#
33do_test incrblob3-1.2 {
34 set ::blob [db incrblob blobs v 1]
35 puts $::blob "hello world"
36} {}
37
38do_test incrblob3-1.3 {
39 sqlite3_blob_reopen $::blob 2
40 puts $::blob "world hello"
41} {}
42
43do_test incrblob3-1.4 {
44 sqlite3_blob_reopen $::blob 1
45 gets $::blob
46} {hello world}
47
48do_test incrblob3-1.5 {
49 sqlite3_blob_reopen $::blob 2
50 gets $::blob
51} {world hello}
52
53do_test incrblob3-1.6 { close $::blob } {}
54
55# Test some error conditions.
56#
57# incrblob3-2.1: Attempting to reopen a row that does not exist.
58# incrblob3-2.2: Attempting to reopen a row that does not contain a blob
59# or text value.
60#
61do_test incrblob3-2.1.1 {
62 set ::blob [db incrblob blobs v 1]
63 list [catch {sqlite3_blob_reopen $::blob 3} msg] $msg
64} {1 SQLITE_ERROR}
65do_test incrblob3-2.1.2 {
66 list [sqlite3_errcode db] [sqlite3_errmsg db]
67} {SQLITE_ERROR {no such rowid: 3}}
68do_test incrblob3-2.1.3 {
69 list [catch {sqlite3_blob_reopen $::blob 1} msg] $msg
70} {1 SQLITE_ABORT}
71do_test incrblob3-2.1.4 { close $::blob } {}
72
73do_execsql_test incrblob3-2.2.1 {
74 INSERT INTO blobs VALUES(3, 42);
75 INSERT INTO blobs VALUES(4, 54.4);
76 INSERT INTO blobs VALUES(5, NULL);
77}
78foreach {tn rowid type} {
79 1 3 integer
80 2 4 real
81 3 5 null
82} {
83 do_test incrblob3-2.2.$tn.1 {
84 set ::blob [db incrblob blobs v 1]
85 list [catch {sqlite3_blob_reopen $::blob $rowid} msg] $msg
86 } {1 SQLITE_ERROR}
87 do_test incrblob3-2.2.$tn.2 {
88 list [sqlite3_errcode db] [sqlite3_errmsg db]
89 } "SQLITE_ERROR {cannot open value of type $type}"
90
91 do_test incrblob3-2.2.$tn.3 {
92 list [catch {sqlite3_blob_reopen $::blob 1} msg] $msg
93 } {1 SQLITE_ABORT}
94 do_test incrblob3-2.2.$tn.4 {
95 list [catch {sqlite3_blob_read $::blob 0 10} msg] $msg
96 } {1 SQLITE_ABORT}
97 do_test incrblob3-2.2.$tn.5 {
98 list [catch {sqlite3_blob_write $::blob 0 "abcd"} msg] $msg
99 } {1 SQLITE_ABORT}
daneefab752010-12-06 17:11:05 +0000100 do_test incrblob3-2.2.$tn.6 {
101 sqlite3_blob_bytes $::blob
102 } {0}
dan61c7f592010-10-26 18:42:52 +0000103
daneefab752010-12-06 17:11:05 +0000104 do_test incrblob3-2.2.$tn.7 { close $::blob } {}
dan61c7f592010-10-26 18:42:52 +0000105}
106
107# Test that passing NULL to sqlite3_blob_XXX() APIs returns SQLITE_MISUSE.
108#
109# incrblob3-3.1: sqlite3_blob_reopen()
110# incrblob3-3.2: sqlite3_blob_read()
111# incrblob3-3.3: sqlite3_blob_write()
112# incrblob3-3.4: sqlite3_blob_bytes()
113#
114do_test incrblob3-3.1 {
115 list [catch {sqlite3_blob_reopen {} 3} msg] $msg
116} {1 SQLITE_MISUSE}
117
118do_test incrblob3-3.2 {
119 list [catch {sqlite3_blob_read {} 0 10} msg] $msg
120} {1 SQLITE_MISUSE}
121
122do_test incrblob3-3.3 {
123 list [catch {sqlite3_blob_write {} 0 "abcd"} msg] $msg
124} {1 SQLITE_MISUSE}
125
126do_test incrblob3-3.4 { sqlite3_blob_bytes {} } {0}
127
128do_test incrblob3-3.5 { sqlite3_blob_close {} } {}
129
130# Test out-of-range reading and writing
131#
132do_test incrblob3-4.1 {
133 set ::blob [db incrblob blobs v 1]
134 sqlite3_blob_bytes $::blob
135} {100}
136do_test incrblob3-4.2 {
137 list [catch { sqlite3_blob_read $::blob -1 10 } msg] $msg
138} {1 SQLITE_ERROR}
139do_test incrblob3-4.3 {
140 list [catch { sqlite3_blob_read $::blob 0 -10 } msg] $msg
141} {1 SQLITE_ERROR}
142do_test incrblob3-4.4 {
143 list [catch { sqlite3_blob_read $::blob 95 10 } msg] $msg
144} {1 SQLITE_ERROR}
145do_test incrblob3-4.5 {
146 list [catch { sqlite3_blob_write $::blob -1 "abcdefghij" 10 } msg] $msg
147} {1 SQLITE_ERROR}
148do_test incrblob3-4.6 {
149 list [catch { sqlite3_blob_write $::blob 0 "abcdefghij" -10 } msg] $msg
150} {1 SQLITE_ERROR}
151do_test incrblob3-4.7 {
152 list [catch { sqlite3_blob_write $::blob 95 "abcdefghij" } msg] $msg
153} {1 SQLITE_ERROR}
154
155do_test incrblob3-4.8 { close $::blob } {}
156
157# Test that modifying the row a blob handle points to aborts the blob.
158#
159do_test incrblob3-5.1 {
160 set ::blob [db incrblob blobs v 1]
161 sqlite3_blob_bytes $::blob
162} {100}
163do_test incrblob3-5.2 {
164 execsql { UPDATE blobs SET v = '123456789012345678901234567890' WHERE k = 1 }
165 list [catch { sqlite3_blob_read $::blob 0 10 } msg] $msg
166} {1 SQLITE_ABORT}
167
168# Test various errors that can occur in sqlite3_blob_open():
169#
170# 1. Trying to open a virtual table column.
171# 2. Trying to open a view column.
172# 3. Trying to open a column that does not exist.
173# 4. Trying to open a read/write handle on an indexed column.
174# 5. Trying to open a read/write handle on the child key of an FK constraint.
175#
176ifcapable fts3 {
177 do_test incrblob3-6.1 {
178 execsql {
179 CREATE VIRTUAL TABLE ft USING fts3;
180 INSERT INTO ft VALUES('rules to open a column to which');
181 }
182
183 list [catch { db incrblob ft content 1 } msg] $msg
184 } {1 {cannot open virtual table: ft}}
185}
186ifcapable view {
187 do_test incrblob3-6.2 {
188 execsql { CREATE VIEW v1 AS SELECT * FROM blobs }
189 list [catch { db incrblob v1 content 1 } msg] $msg
190 } {1 {cannot open view: v1}}
191}
192
193do_test incrblob3-6.3 {
194 list [catch { db incrblob blobs content 1 } msg] $msg
195} {1 {no such column: "content"}}
196
197do_test incrblob3-6.4.1 {
198 execsql {
199 CREATE TABLE t1(a, b);
200 CREATE INDEX i1 ON t1(b);
201 INSERT INTO t1 VALUES(zeroblob(100), zeroblob(100));
202 }
203 list [catch { db incrblob t1 b 1 } msg] $msg
204} {1 {cannot open indexed column for writing}}
205do_test incrblob3-6.4.2 {
206 set ::blob [db incrblob t1 a 1]
207 close $::blob
208} {}
209do_test incrblob3-6.4.3 {
210 set ::blob [db incrblob -readonly t1 b 1]
211 close $::blob
212} {}
213
214do_test incrblob3-6.5.1 {
215 execsql {
216 CREATE TABLE p1(a PRIMARY KEY);
217 CREATE TABLE c1(a, b REFERENCES p1);
218 PRAGMA foreign_keys = 1;
219 INSERT INTO p1 VALUES(zeroblob(100));
220 INSERT INTO c1 VALUES(zeroblob(100), zeroblob(100));
221 }
222 list [catch { db incrblob c1 b 1 } msg] $msg
223} {1 {cannot open foreign key column for writing}}
224
225do_test incrblob3-6.5.2 {
226 set ::blob [db incrblob c1 a 1]
227 close $::blob
228} {}
229do_test incrblob3-6.5.3 {
230 set ::blob [db incrblob -readonly c1 b 1]
231 close $::blob
232} {}
233do_test incrblob3-6.5.4 {
234 execsql { PRAGMA foreign_keys = 0 }
235 set ::blob [db incrblob c1 b 1]
236 close $::blob
237} {}
238
239
240# Test that sqlite3_blob_open() handles transient and persistent schema
241# errors correctly.
242#
243do_test incrblob3-7.1 {
244 sqlite3 db2 test.db
245 sqlite3_db_config_lookaside db2 0 0 0
246 execsql { CREATE TABLE t2(x) } db2
247 set ::blob [db incrblob blobs v 1]
248 close $::blob
249} {}
250db2 close
251
252testvfs tvfs -default 1
253tvfs filter xAccess
254tvfs script access_method
255
256proc access_method {args} {
257 set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
258 incr schemacookie
259 hexio_write test.db 40 [hexio_render_int32 $schemacookie]
260
261 set dbversion [hexio_get_int [hexio_read test.db 24 4]]
262 incr dbversion
263 hexio_write test.db 24 [hexio_render_int32 $dbversion]
264
265 return ""
266}
267
268do_test incrblob3-7.2 {
269 sqlite3 db test.db
270 sqlite3_db_config_lookaside db 0 0 0
271 list [catch {db incrblob blobs v 1} msg] $msg
272} {1 {database schema has changed}}
273db close
274tvfs delete
275
276finish_test