3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing of the zero-filled blob functionality
13 # including the sqlite3_bind_zeroblob(), sqlite3_result_zeroblob(),
14 # and the built-in zeroblob() SQL function.
16 # $Id: zeroblob.test,v 1.14 2009/07/14 02:33:02 drh Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
20 set testprefix zeroblob
22 # ifcapable !incrblob { finish_test return }
24 test_set_config_pagecache 0 0
26 # When zeroblob() is used for the last field of a column, then the
27 # content of the zeroblob is never instantiated on the VDBE stack.
28 # But it does get inserted into the database correctly.
30 db eval {PRAGMA cache_size=10}
31 sqlite3_memory_highwater 1
32 unset -nocomplain memused
33 set memused [sqlite3_memory_used]
34 do_test zeroblob-1.1 {
36 CREATE TABLE t1(a,b,c,d);
38 set ::sqlite3_max_blobsize 0
40 INSERT INTO t1 VALUES(2,3,4,zeroblob(1000000));
45 do_test zeroblob-1.1.1 {
46 set ::sqlite3_max_blobsize
48 do_test zeroblob-1.1.2 {
49 expr {[sqlite3_memory_highwater]<$::memused+35000}
53 do_test zeroblob-1.2 {
55 SELECT length(d) FROM t1
59 # If a non-NULL column follows the zeroblob, then the content of
60 # the zeroblob must be instantiated.
62 do_test zeroblob-1.3 {
63 set ::sqlite3_max_blobsize 0
65 INSERT INTO t1 VALUES(3,4,zeroblob(10000),5);
67 set ::sqlite3_max_blobsize
69 do_test zeroblob-1.4 {
71 SELECT length(c), length(d) FROM t1
75 # Multiple zeroblobs can appear at the end of record. No instantiation
76 # of the blob content occurs on the stack.
78 do_test zeroblob-1.5 {
79 set ::sqlite3_max_blobsize 0
81 INSERT INTO t1 VALUES(4,5,zeroblob(10000),zeroblob(10000));
85 do_test zeroblob-1.5.1 {
86 set ::sqlite3_max_blobsize
89 do_test zeroblob-1.6 {
91 SELECT length(c), length(d) FROM t1
93 } {1 1000000 10000 1 10000 10000}
95 # NULLs can follow the zeroblob() or be intermixed with zeroblobs and
96 # no instantiation of the zeroblobs occurs on the stack.
98 do_test zeroblob-1.7 {
99 set ::sqlite3_max_blobsize 0
101 INSERT INTO t1 VALUES(5,zeroblob(10000),NULL,zeroblob(10000));
105 do_test zeroblob-1.7.1 {
106 set ::sqlite3_max_blobsize
109 do_test zeroblob-1.8 {
111 SELECT length(b), length(d) FROM t1 WHERE a=5
115 # Comparisons against zeroblobs work.
117 do_test zeroblob-2.1 {
119 SELECT a FROM t1 WHERE b=zeroblob(10000)
123 # Comparisons against zeroblobs work even when indexed.
125 do_test zeroblob-2.2 {
127 CREATE INDEX i1_1 ON t1(b);
128 SELECT a FROM t1 WHERE b=zeroblob(10000);
132 # DISTINCT works for zeroblobs
134 ifcapable bloblit&&subquery&&compound {
135 do_test zeroblob-3.1 {
137 SELECT count(DISTINCT a) FROM (
138 SELECT x'00000000000000000000' AS a
140 SELECT zeroblob(10) AS a
146 # Concatentation works with zeroblob
149 do_test zeroblob-4.1 {
151 SELECT hex(zeroblob(2) || x'61')
156 # Check various CAST(...) operations on zeroblob.
158 do_test zeroblob-5.1 {
160 SELECT CAST (zeroblob(100) AS REAL);
163 do_test zeroblob-5.2 {
165 SELECT CAST (zeroblob(100) AS INTEGER);
168 do_test zeroblob-5.3 {
170 SELECT CAST (zeroblob(100) AS TEXT);
173 do_test zeroblob-5.4 {
175 SELECT CAST(zeroblob(100) AS BLOB);
177 } [execsql {SELECT zeroblob(100)}]
180 # Check for malicious use of zeroblob. Make sure nothing crashes.
182 do_test zeroblob-6.1.1 {
183 execsql {select zeroblob(-1)}
185 do_test zeroblob-6.1.2 {
186 execsql {select zeroblob(-10)}
188 do_test zeroblob-6.1.3 {
189 execsql {select zeroblob(-100)}
191 do_test zeroblob-6.2 {
192 execsql {select length(zeroblob(-1))}
194 do_test zeroblob-6.3 {
195 execsql {select zeroblob(-1)|1}
197 do_test zeroblob-6.4 {
198 catchsql {select length(zeroblob(2147483648))}
199 } {1 {string or blob too big}}
200 do_test zeroblob-6.5 {
201 catchsql {select zeroblob(2147483648)}
202 } {1 {string or blob too big}}
203 do_test zeroblob-6.6 {
204 execsql {select hex(zeroblob(-1))}
206 do_test zeroblob-6.7 {
207 execsql {select typeof(zeroblob(-1))}
210 # Test bind_zeroblob()
212 sqlite3_memory_highwater 1
213 unset -nocomplain memused
214 set memused [sqlite3_memory_used]
215 do_test zeroblob-7.1 {
216 set ::STMT [sqlite3_prepare $::DB "SELECT length(?)" -1 DUMMY]
217 set ::sqlite3_max_blobsize 0
218 sqlite3_bind_zeroblob $::STMT 1 450000
221 do_test zeroblob-7.2 {
222 sqlite3_column_int $::STMT 0
224 do_test zeroblob-7.3 {
225 sqlite3_finalize $::STMT
228 do_test zeroblob-7.4 {
229 set ::sqlite3_max_blobsize
231 do_test zeroblob-7.5 {
232 expr {[sqlite3_memory_highwater]<$::memused+10000}
236 # Test that MakeRecord can handle a value with some real content
237 # and a zero-blob tail.
239 do_test zeroblob-8.1 {
241 SELECT 'hello' AS a, zeroblob(10) as b from t1 ORDER BY a, b;
247 # zeroblobs on either size of an IN operator
249 do_test zeroblob-9.1 {
250 db eval {SELECT x'0000' IN (x'000000')}
252 do_test zeroblob-9.2 {
253 db eval {SELECT x'0000' IN (x'0000')}
255 do_test zeroblob-9.3 {
256 db eval {SELECT zeroblob(2) IN (x'000000')}
258 do_test zeroblob-9.4 {
259 db eval {SELECT zeroblob(2) IN (x'0000')}
261 do_test zeroblob-9.5 {
262 db eval {SELECT x'0000' IN (zeroblob(3))}
264 do_test zeroblob-9.6 {
265 db eval {SELECT x'0000' IN (zeroblob(2))}
267 do_test zeroblob-9.7 {
268 db eval {SELECT zeroblob(2) IN (zeroblob(3))}
270 do_test zeroblob-9.8 {
271 db eval {SELECT zeroblob(2) IN (zeroblob(2))}
274 # Oversized zeroblob records
276 do_test zeroblob-10.1 {
278 CREATE TABLE t10(a,b,c);
280 catchsql {INSERT INTO t10 VALUES(zeroblob(1e9),zeroblob(1e9),zeroblob(1e9))}
281 } {1 {string or blob too big}}
283 #-------------------------------------------------------------------------
284 # Test the zeroblob() function on its own with negative or oversized
287 do_execsql_test 11.0 {
288 SELECT length(zeroblob(-1444444444444444));
290 do_catchsql_test 11.1 {
291 SELECT zeroblob(5000 * 1024 * 1024);
292 } {1 {string or blob too big}}
293 do_catchsql_test 11.2 {
294 SELECT quote(zeroblob(5000 * 1024 * 1024));
295 } {1 {string or blob too big}}
296 do_catchsql_test 11.3 {
297 SELECT quote(zeroblob(-1444444444444444));
299 do_catchsql_test 11.4 {
300 SELECT quote(test_zeroblob(-1));
303 #-------------------------------------------------------------------------
304 # Test the sqlite3_bind_zeroblob64() API.
306 proc bind_and_run {stmt nZero} {
307 sqlite3_bind_zeroblob64 $stmt 1 $nZero
309 set ret [sqlite3_column_int $stmt 0]
313 set stmt [sqlite3_prepare db "SELECT length(?)" -1 dummy]
315 do_test 12.1 { bind_and_run $stmt 40 } 40
316 do_test 12.2 { bind_and_run $stmt 0 } 0
317 do_test 12.3 { bind_and_run $stmt 1000 } 1000
320 list [catch { bind_and_run $stmt [expr 5000 * 1024 * 1024] } msg] $msg
324 set ret [sqlite3_column_int $stmt 0]
329 sqlite3_finalize $stmt
331 # 2019-01-25 https://sqlite.org/src/tktview/bb4bdb9f7f654b0bb9f34cfbac
332 # Zeroblob truncated by an index on expression
334 do_execsql_test 13.100 {
335 DROP TABLE IF EXISTS t1;
336 CREATE TABLE t1(a,b,c);
337 CREATE INDEX t1bbc ON t1(b, b+c);
338 INSERT INTO t1(a,b,c) VALUES(1,zeroblob(8),3);
339 SELECT a, quote(b), length(b), c FROM t1;
340 } {1 X'0000000000000000' 8 3}
342 test_restore_config_pagecache