xref: /sqlite-3.40.0/test/e_blobwrite.test (revision a32536b4)
1923c4b35Sdan# 2014 October 30
2923c4b35Sdan#
3923c4b35Sdan# The author disclaims copyright to this source code.  In place of
4923c4b35Sdan# a legal notice, here is a blessing:
5923c4b35Sdan#
6923c4b35Sdan#    May you do good and not evil.
7923c4b35Sdan#    May you find forgiveness for yourself and forgive others.
8923c4b35Sdan#    May you share freely, never taking more than you give.
9923c4b35Sdan#
10923c4b35Sdan#***********************************************************************
11923c4b35Sdan#
12923c4b35Sdan
13923c4b35Sdanset testdir [file dirname $argv0]
14923c4b35Sdansource $testdir/tester.tcl
15923c4b35Sdanset testprefix e_blobwrite
16923c4b35Sdan
17*a32536b4Sdanifcapable !incrblob {
18*a32536b4Sdan  finish_test
19*a32536b4Sdan  return
20*a32536b4Sdan}
21*a32536b4Sdan
22923c4b35Sdan#--------------------------------------------------------------------------
23923c4b35Sdan# EVIDENCE-OF: R-62898-22698 This function is used to write data into an
24923c4b35Sdan# open BLOB handle from a caller-supplied buffer. N bytes of data are
25923c4b35Sdan# copied from the buffer Z into the open BLOB, starting at offset
26923c4b35Sdan# iOffset.
27923c4b35Sdan#
28923c4b35Sdanset dots [string repeat . 40]
29923c4b35Sdando_execsql_test 1.0 {
30923c4b35Sdan  CREATE TABLE t1(a INTEGER PRIMARY KEY, t TEXT);
31923c4b35Sdan  INSERT INTO t1 VALUES(-1, $dots);
32923c4b35Sdan  INSERT INTO t1 VALUES(-2, $dots);
33923c4b35Sdan  INSERT INTO t1 VALUES(-3, $dots);
34923c4b35Sdan  INSERT INTO t1 VALUES(-4, $dots);
35923c4b35Sdan  INSERT INTO t1 VALUES(-5, $dots);
36923c4b35Sdan  INSERT INTO t1 VALUES(-6, $dots);
37923c4b35Sdan}
38923c4b35Sdan
39923c4b35Sdanproc blob_write_test {tn id iOffset blob nData final} {
40923c4b35Sdan  sqlite3_blob_open db main t1 t $id 1 B
41923c4b35Sdan
42923c4b35Sdan  # EVIDENCE-OF: R-45864-01884 On success, sqlite3_blob_write() returns
43923c4b35Sdan  # SQLITE_OK. Otherwise, an error code or an extended error code is
44923c4b35Sdan  # returned.
45923c4b35Sdan  #
46923c4b35Sdan  #   This block tests the SQLITE_OK case in the requirement above (the
47923c4b35Sdan  #   Tcl sqlite3_blob_write() wrapper uses an empty string in place of
48923c4b35Sdan  #   "SQLITE_OK"). The error cases are tested by the "blob_write_error_test"
49923c4b35Sdan  #   tests below.
50923c4b35Sdan  #
51923c4b35Sdan  set res [sqlite3_blob_write $B $iOffset $blob $nData]
52923c4b35Sdan  uplevel [list do_test $tn.1 [list set {} $res] {}]
53923c4b35Sdan
54923c4b35Sdan  sqlite3_blob_close $B
55923c4b35Sdan  uplevel [list do_execsql_test $tn.3 "SELECT t FROM t1 WHERE a=$id" $final]
56923c4b35Sdan}
57923c4b35Sdan
58923c4b35Sdanset blob "0123456789012345678901234567890123456789"
59923c4b35Sdanblob_write_test 1.1 -1 0 $blob 10  { 0123456789.............................. }
60923c4b35Sdanblob_write_test 1.2 -2 8 $blob 10  { ........0123456789...................... }
61923c4b35Sdanblob_write_test 1.3 -3 8 $blob 1   { ........0............................... }
62923c4b35Sdanblob_write_test 1.4 -4 18 $blob 22 { ..................0123456789012345678901 }
63923c4b35Sdanblob_write_test 1.5 -5 18 $blob 0  { ........................................ }
64923c4b35Sdanblob_write_test 1.6 -6 0 $blob 40  { 0123456789012345678901234567890123456789 }
65923c4b35Sdan
66923c4b35Sdan
67923c4b35Sdanproc blob_write_error_test {tn B iOffset blob nData errcode errmsg} {
68923c4b35Sdan
69923c4b35Sdan  # In cases where the underlying sqlite3_blob_write() function returns
70923c4b35Sdan  # SQLITE_OK, the Tcl wrapper returns an empty string. If the underlying
71923c4b35Sdan  # function returns an error, the Tcl wrapper throws an exception with
72923c4b35Sdan  # the error code as the Tcl exception message.
73923c4b35Sdan  #
74923c4b35Sdan  if {$errcode=="SQLITE_OK"} {
75923c4b35Sdan    set ret ""
76923c4b35Sdan    set isError 0
77923c4b35Sdan  } else {
78923c4b35Sdan    set ret $errcode
79923c4b35Sdan    set isError 1
80923c4b35Sdan  }
81923c4b35Sdan
82923c4b35Sdan  set cmd [list sqlite3_blob_write $B $iOffset $blob $nData]
83923c4b35Sdan  uplevel [list do_test $tn.1 [subst -nocommands {
84923c4b35Sdan    list [catch {$cmd} msg] [set msg]
85923c4b35Sdan  }] [list $isError $ret]]
86923c4b35Sdan
87923c4b35Sdan  # EVIDENCE-OF: R-34782-18311 Unless SQLITE_MISUSE is returned, this
88923c4b35Sdan  # function sets the database connection error code and message
89923c4b35Sdan  # accessible via sqlite3_errcode() and sqlite3_errmsg() and related
90923c4b35Sdan  # functions.
91923c4b35Sdan  #
92923c4b35Sdan  if {$errcode == "SQLITE_MISUSE"} { error "test proc misuse!" }
93923c4b35Sdan  uplevel [list do_test $tn.2 [list sqlite3_errcode db] $errcode]
94923c4b35Sdan  uplevel [list do_test $tn.3 [list sqlite3_errmsg db] $errmsg]
95923c4b35Sdan}
96923c4b35Sdan
97923c4b35Sdando_execsql_test 2.0 {
98923c4b35Sdan  CREATE TABLE t2(a TEXT, b INTEGER PRIMARY KEY);
99923c4b35Sdan  INSERT INTO t2 VALUES($dots, 43);
100923c4b35Sdan  INSERT INTO t2 VALUES($dots, 44);
101923c4b35Sdan  INSERT INTO t2 VALUES($dots, 45);
102923c4b35Sdan}
103923c4b35Sdan
104923c4b35Sdan# EVIDENCE-OF: R-63341-57517 If the BLOB handle passed as the first
105923c4b35Sdan# argument was not opened for writing (the flags parameter to
106923c4b35Sdan# sqlite3_blob_open() was zero), this function returns SQLITE_READONLY.
107923c4b35Sdan#
108923c4b35Sdansqlite3_blob_open db main t2 a 43 0 B
109923c4b35Sdanblob_write_error_test 2.1 $B 0 $blob 10   \
110923c4b35Sdan    SQLITE_READONLY {attempt to write a readonly database}
111923c4b35Sdansqlite3_blob_close $B
112923c4b35Sdan
113923c4b35Sdan# EVIDENCE-OF: R-29804-27366 If offset iOffset is less than N bytes from
114923c4b35Sdan# the end of the BLOB, SQLITE_ERROR is returned and no data is written.
115923c4b35Sdan#
116923c4b35Sdansqlite3_blob_open db main t2 a 44 3 B
117923c4b35Sdanblob_write_error_test 2.2.1 $B 31 $blob 10   \
118a690ff36Sdrh    SQLITE_ERROR {SQL logic error}
119923c4b35Sdan
120923c4b35Sdan# Make a successful write to the blob handle. This shows that the
121923c4b35Sdan# sqlite3_errcode() and sqlite3_errmsg() values are set even if the
122923c4b35Sdan# blob_write() call succeeds (see requirement in the [blob_write_error_test]
123923c4b35Sdan# proc).
124923c4b35Sdanblob_write_error_test 2.2.1 $B 30 $blob 10 SQLITE_OK {not an error}
125923c4b35Sdan
126923c4b35Sdan# EVIDENCE-OF: R-58570-38916 If N or iOffset are less than zero
127923c4b35Sdan# SQLITE_ERROR is returned and no data is written.
128923c4b35Sdan#
129923c4b35Sdanblob_write_error_test 2.2.2 $B 31 $blob -1   \
130a690ff36Sdrh    SQLITE_ERROR {SQL logic error}
131923c4b35Sdanblob_write_error_test 2.2.3 $B 20 $blob 10 SQLITE_OK {not an error}
132923c4b35Sdanblob_write_error_test 2.2.4 $B -1 $blob 10   \
133a690ff36Sdrh    SQLITE_ERROR {SQL logic error}
134923c4b35Sdansqlite3_blob_close $B
135923c4b35Sdan
136923c4b35Sdan# EVIDENCE-OF: R-20958-54138 An attempt to write to an expired BLOB
137923c4b35Sdan# handle fails with an error code of SQLITE_ABORT.
138923c4b35Sdan#
139923c4b35Sdando_test 2.3 {
140923c4b35Sdan  sqlite3_blob_open db main t2 a 43 0 B
141923c4b35Sdan  execsql { DELETE FROM t2 WHERE b=43 }
142923c4b35Sdan} {}
143923c4b35Sdanblob_write_error_test 2.3.1 $B 5 $blob 5 \
144ff4fa772Sdrh    SQLITE_ABORT {query aborted}
145923c4b35Sdando_test 2.3.2 {
146923c4b35Sdan  execsql { SELECT 1, 2, 3 }
147923c4b35Sdan  sqlite3_errcode db
148923c4b35Sdan} {SQLITE_OK}
149923c4b35Sdanblob_write_error_test 2.3.3 $B 5 $blob 5 \
150ff4fa772Sdrh    SQLITE_ABORT {query aborted}
151923c4b35Sdansqlite3_blob_close $B
152923c4b35Sdan
153923c4b35Sdan# EVIDENCE-OF: R-08382-59936 Writes to the BLOB that occurred before the
154923c4b35Sdan# BLOB handle expired are not rolled back by the expiration of the
155923c4b35Sdan# handle, though of course those changes might have been overwritten by
156923c4b35Sdan# the statement that expired the BLOB handle or by other independent
157923c4b35Sdan# statements.
158923c4b35Sdan#
159923c4b35Sdan#   3.1.*: not rolled back,
160923c4b35Sdan#   3.2.*: overwritten.
161923c4b35Sdan#
162923c4b35Sdando_execsql_test 3.0 {
163923c4b35Sdan  CREATE TABLE t3(i INTEGER PRIMARY KEY, j TEXT, k TEXT);
164923c4b35Sdan  INSERT INTO t3 VALUES(1, $dots, $dots);
165923c4b35Sdan  INSERT INTO t3 VALUES(2, $dots, $dots);
166923c4b35Sdan  SELECT * FROM t3 WHERE i=1;
167923c4b35Sdan} {
168923c4b35Sdan  1
169923c4b35Sdan  ........................................
170923c4b35Sdan  ........................................
171923c4b35Sdan}
172923c4b35Sdansqlite3_blob_open db main t3 j 1 1 B
173923c4b35Sdanblob_write_error_test 3.1.1 $B 5 $blob 10 SQLITE_OK {not an error}
174923c4b35Sdando_execsql_test 3.1.2 {
175923c4b35Sdan  UPDATE t3 SET k = 'xyz' WHERE i=1;
176923c4b35Sdan  SELECT * FROM t3 WHERE i=1;
177923c4b35Sdan} {
178923c4b35Sdan  1 .....0123456789......................... xyz
179923c4b35Sdan}
180923c4b35Sdanblob_write_error_test 3.1.3 $B 15 $blob 10 \
181ff4fa772Sdrh    SQLITE_ABORT {query aborted}
182923c4b35Sdansqlite3_blob_close $B
183923c4b35Sdando_execsql_test 3.1.4 {
184923c4b35Sdan  SELECT * FROM t3 WHERE i=1;
185923c4b35Sdan} {
186923c4b35Sdan  1 .....0123456789......................... xyz
187923c4b35Sdan}
188923c4b35Sdan
189923c4b35Sdansqlite3_blob_open db main t3 j 2 1 B
190923c4b35Sdanblob_write_error_test 3.2.1 $B 5 $blob 10 SQLITE_OK {not an error}
191923c4b35Sdando_execsql_test 3.2.2 {
192923c4b35Sdan  UPDATE t3 SET j = 'xyz' WHERE i=2;
193923c4b35Sdan  SELECT * FROM t3 WHERE i=2;
194923c4b35Sdan} {
195923c4b35Sdan  2 xyz ........................................
196923c4b35Sdan}
197923c4b35Sdanblob_write_error_test 3.2.3 $B 15 $blob 10 \
198ff4fa772Sdrh    SQLITE_ABORT {query aborted}
199923c4b35Sdansqlite3_blob_close $B
200923c4b35Sdando_execsql_test 3.2.4 {
201923c4b35Sdan  SELECT * FROM t3 WHERE i=2;
202923c4b35Sdan} {
203923c4b35Sdan  2 xyz ........................................
204923c4b35Sdan}
205923c4b35Sdan
206923c4b35Sdan
207923c4b35Sdan
208923c4b35Sdanfinish_test
209