1# 2014 October 30 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 13set testdir [file dirname $argv0] 14source $testdir/tester.tcl 15set testprefix e_blobwrite 16 17#-------------------------------------------------------------------------- 18# EVIDENCE-OF: R-62898-22698 This function is used to write data into an 19# open BLOB handle from a caller-supplied buffer. N bytes of data are 20# copied from the buffer Z into the open BLOB, starting at offset 21# iOffset. 22# 23set dots [string repeat . 40] 24do_execsql_test 1.0 { 25 CREATE TABLE t1(a INTEGER PRIMARY KEY, t TEXT); 26 INSERT INTO t1 VALUES(-1, $dots); 27 INSERT INTO t1 VALUES(-2, $dots); 28 INSERT INTO t1 VALUES(-3, $dots); 29 INSERT INTO t1 VALUES(-4, $dots); 30 INSERT INTO t1 VALUES(-5, $dots); 31 INSERT INTO t1 VALUES(-6, $dots); 32} 33 34proc blob_write_test {tn id iOffset blob nData final} { 35 sqlite3_blob_open db main t1 t $id 1 B 36 37 # EVIDENCE-OF: R-45864-01884 On success, sqlite3_blob_write() returns 38 # SQLITE_OK. Otherwise, an error code or an extended error code is 39 # returned. 40 # 41 # This block tests the SQLITE_OK case in the requirement above (the 42 # Tcl sqlite3_blob_write() wrapper uses an empty string in place of 43 # "SQLITE_OK"). The error cases are tested by the "blob_write_error_test" 44 # tests below. 45 # 46 set res [sqlite3_blob_write $B $iOffset $blob $nData] 47 uplevel [list do_test $tn.1 [list set {} $res] {}] 48 49 sqlite3_blob_close $B 50 uplevel [list do_execsql_test $tn.3 "SELECT t FROM t1 WHERE a=$id" $final] 51} 52 53set blob "0123456789012345678901234567890123456789" 54blob_write_test 1.1 -1 0 $blob 10 { 0123456789.............................. } 55blob_write_test 1.2 -2 8 $blob 10 { ........0123456789...................... } 56blob_write_test 1.3 -3 8 $blob 1 { ........0............................... } 57blob_write_test 1.4 -4 18 $blob 22 { ..................0123456789012345678901 } 58blob_write_test 1.5 -5 18 $blob 0 { ........................................ } 59blob_write_test 1.6 -6 0 $blob 40 { 0123456789012345678901234567890123456789 } 60 61 62proc blob_write_error_test {tn B iOffset blob nData errcode errmsg} { 63 64 # In cases where the underlying sqlite3_blob_write() function returns 65 # SQLITE_OK, the Tcl wrapper returns an empty string. If the underlying 66 # function returns an error, the Tcl wrapper throws an exception with 67 # the error code as the Tcl exception message. 68 # 69 if {$errcode=="SQLITE_OK"} { 70 set ret "" 71 set isError 0 72 } else { 73 set ret $errcode 74 set isError 1 75 } 76 77 set cmd [list sqlite3_blob_write $B $iOffset $blob $nData] 78 uplevel [list do_test $tn.1 [subst -nocommands { 79 list [catch {$cmd} msg] [set msg] 80 }] [list $isError $ret]] 81 82 # EVIDENCE-OF: R-34782-18311 Unless SQLITE_MISUSE is returned, this 83 # function sets the database connection error code and message 84 # accessible via sqlite3_errcode() and sqlite3_errmsg() and related 85 # functions. 86 # 87 if {$errcode == "SQLITE_MISUSE"} { error "test proc misuse!" } 88 uplevel [list do_test $tn.2 [list sqlite3_errcode db] $errcode] 89 uplevel [list do_test $tn.3 [list sqlite3_errmsg db] $errmsg] 90} 91 92do_execsql_test 2.0 { 93 CREATE TABLE t2(a TEXT, b INTEGER PRIMARY KEY); 94 INSERT INTO t2 VALUES($dots, 43); 95 INSERT INTO t2 VALUES($dots, 44); 96 INSERT INTO t2 VALUES($dots, 45); 97} 98 99# EVIDENCE-OF: R-63341-57517 If the BLOB handle passed as the first 100# argument was not opened for writing (the flags parameter to 101# sqlite3_blob_open() was zero), this function returns SQLITE_READONLY. 102# 103sqlite3_blob_open db main t2 a 43 0 B 104blob_write_error_test 2.1 $B 0 $blob 10 \ 105 SQLITE_READONLY {attempt to write a readonly database} 106sqlite3_blob_close $B 107 108# EVIDENCE-OF: R-29804-27366 If offset iOffset is less than N bytes from 109# the end of the BLOB, SQLITE_ERROR is returned and no data is written. 110# 111sqlite3_blob_open db main t2 a 44 3 B 112blob_write_error_test 2.2.1 $B 31 $blob 10 \ 113 SQLITE_ERROR {SQL logic error} 114 115# Make a successful write to the blob handle. This shows that the 116# sqlite3_errcode() and sqlite3_errmsg() values are set even if the 117# blob_write() call succeeds (see requirement in the [blob_write_error_test] 118# proc). 119blob_write_error_test 2.2.1 $B 30 $blob 10 SQLITE_OK {not an error} 120 121# EVIDENCE-OF: R-58570-38916 If N or iOffset are less than zero 122# SQLITE_ERROR is returned and no data is written. 123# 124blob_write_error_test 2.2.2 $B 31 $blob -1 \ 125 SQLITE_ERROR {SQL logic error} 126blob_write_error_test 2.2.3 $B 20 $blob 10 SQLITE_OK {not an error} 127blob_write_error_test 2.2.4 $B -1 $blob 10 \ 128 SQLITE_ERROR {SQL logic error} 129sqlite3_blob_close $B 130 131# EVIDENCE-OF: R-20958-54138 An attempt to write to an expired BLOB 132# handle fails with an error code of SQLITE_ABORT. 133# 134do_test 2.3 { 135 sqlite3_blob_open db main t2 a 43 0 B 136 execsql { DELETE FROM t2 WHERE b=43 } 137} {} 138blob_write_error_test 2.3.1 $B 5 $blob 5 \ 139 SQLITE_ABORT {query aborted} 140do_test 2.3.2 { 141 execsql { SELECT 1, 2, 3 } 142 sqlite3_errcode db 143} {SQLITE_OK} 144blob_write_error_test 2.3.3 $B 5 $blob 5 \ 145 SQLITE_ABORT {query aborted} 146sqlite3_blob_close $B 147 148# EVIDENCE-OF: R-08382-59936 Writes to the BLOB that occurred before the 149# BLOB handle expired are not rolled back by the expiration of the 150# handle, though of course those changes might have been overwritten by 151# the statement that expired the BLOB handle or by other independent 152# statements. 153# 154# 3.1.*: not rolled back, 155# 3.2.*: overwritten. 156# 157do_execsql_test 3.0 { 158 CREATE TABLE t3(i INTEGER PRIMARY KEY, j TEXT, k TEXT); 159 INSERT INTO t3 VALUES(1, $dots, $dots); 160 INSERT INTO t3 VALUES(2, $dots, $dots); 161 SELECT * FROM t3 WHERE i=1; 162} { 163 1 164 ........................................ 165 ........................................ 166} 167sqlite3_blob_open db main t3 j 1 1 B 168blob_write_error_test 3.1.1 $B 5 $blob 10 SQLITE_OK {not an error} 169do_execsql_test 3.1.2 { 170 UPDATE t3 SET k = 'xyz' WHERE i=1; 171 SELECT * FROM t3 WHERE i=1; 172} { 173 1 .....0123456789......................... xyz 174} 175blob_write_error_test 3.1.3 $B 15 $blob 10 \ 176 SQLITE_ABORT {query aborted} 177sqlite3_blob_close $B 178do_execsql_test 3.1.4 { 179 SELECT * FROM t3 WHERE i=1; 180} { 181 1 .....0123456789......................... xyz 182} 183 184sqlite3_blob_open db main t3 j 2 1 B 185blob_write_error_test 3.2.1 $B 5 $blob 10 SQLITE_OK {not an error} 186do_execsql_test 3.2.2 { 187 UPDATE t3 SET j = 'xyz' WHERE i=2; 188 SELECT * FROM t3 WHERE i=2; 189} { 190 2 xyz ........................................ 191} 192blob_write_error_test 3.2.3 $B 15 $blob 10 \ 193 SQLITE_ABORT {query aborted} 194sqlite3_blob_close $B 195do_execsql_test 3.2.4 { 196 SELECT * FROM t3 WHERE i=2; 197} { 198 2 xyz ........................................ 199} 200 201 202 203finish_test 204