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