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