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
27 test_set_config_pagecache 0 0
29 # When zeroblob() is used for the last field of a column, then the
30 # content of the zeroblob is never instantiated on the VDBE stack.
31 # But it does get inserted into the database correctly.
33 db eval {PRAGMA cache_size=10}
34 sqlite3_memory_highwater 1
35 unset -nocomplain memused
36 set memused [sqlite3_memory_used]
37 do_test zeroblob-1.1 {
39 CREATE TABLE t1(a,b,c,d);
41 set ::sqlite3_max_blobsize 0
43 INSERT INTO t1 VALUES(2,3,4,zeroblob(1000000));
45 set ::sqlite3_max_blobsize
48 do_test zeroblob-1.1.1 {
49 expr {[sqlite3_memory_highwater]<$::memused+35000}
51 do_test zeroblob-1.2 {
53 SELECT length(d) FROM t1
57 # If a non-NULL column follows the zeroblob, then the content of
58 # the zeroblob must be instantiated.
60 do_test zeroblob-1.3 {
61 set ::sqlite3_max_blobsize 0
63 INSERT INTO t1 VALUES(3,4,zeroblob(10000),5);
65 set ::sqlite3_max_blobsize
67 do_test zeroblob-1.4 {
69 SELECT length(c), length(d) FROM t1
73 # Multiple zeroblobs can appear at the end of record. No instantiation
74 # of the blob content occurs on the stack.
76 do_test zeroblob-1.5 {
77 set ::sqlite3_max_blobsize 0
79 INSERT INTO t1 VALUES(4,5,zeroblob(10000),zeroblob(10000));
81 set ::sqlite3_max_blobsize
83 do_test zeroblob-1.6 {
85 SELECT length(c), length(d) FROM t1
87 } {1 1000000 10000 1 10000 10000}
89 # NULLs can follow the zeroblob() or be intermixed with zeroblobs and
90 # no instantiation of the zeroblobs occurs on the stack.
92 do_test zeroblob-1.7 {
93 set ::sqlite3_max_blobsize 0
95 INSERT INTO t1 VALUES(5,zeroblob(10000),NULL,zeroblob(10000));
97 set ::sqlite3_max_blobsize
99 do_test zeroblob-1.8 {
101 SELECT length(b), length(d) FROM t1 WHERE a=5
105 # Comparisons against zeroblobs work.
107 do_test zeroblob-2.1 {
109 SELECT a FROM t1 WHERE b=zeroblob(10000)
113 # Comparisons against zeroblobs work even when indexed.
115 do_test zeroblob-2.2 {
117 CREATE INDEX i1_1 ON t1(b);
118 SELECT a FROM t1 WHERE b=zeroblob(10000);
122 # DISTINCT works for zeroblobs
124 ifcapable bloblit&&subquery&&compound {
125 do_test zeroblob-3.1 {
127 SELECT count(DISTINCT a) FROM (
128 SELECT x'00000000000000000000' AS a
130 SELECT zeroblob(10) AS a
136 # Concatentation works with zeroblob
139 do_test zeroblob-4.1 {
141 SELECT hex(zeroblob(2) || x'61')
146 # Check various CAST(...) operations on zeroblob.
148 do_test zeroblob-5.1 {
150 SELECT CAST (zeroblob(100) AS REAL);
153 do_test zeroblob-5.2 {
155 SELECT CAST (zeroblob(100) AS INTEGER);
158 do_test zeroblob-5.3 {
160 SELECT CAST (zeroblob(100) AS TEXT);
163 do_test zeroblob-5.4 {
165 SELECT CAST(zeroblob(100) AS BLOB);
167 } [execsql {SELECT zeroblob(100)}]
170 # Check for malicious use of zeroblob. Make sure nothing crashes.
172 do_test zeroblob-6.1.1 {
173 execsql {select zeroblob(-1)}
175 do_test zeroblob-6.1.2 {
176 execsql {select zeroblob(-10)}
178 do_test zeroblob-6.1.3 {
179 execsql {select zeroblob(-100)}
181 do_test zeroblob-6.2 {
182 execsql {select length(zeroblob(-1))}
184 do_test zeroblob-6.3 {
185 execsql {select zeroblob(-1)|1}
187 do_test zeroblob-6.4 {
188 catchsql {select length(zeroblob(2147483648))}
189 } {1 {string or blob too big}}
190 do_test zeroblob-6.5 {
191 catchsql {select zeroblob(2147483648)}
192 } {1 {string or blob too big}}
193 do_test zeroblob-6.6 {
194 execsql {select hex(zeroblob(-1))}
196 do_test zeroblob-6.7 {
197 execsql {select typeof(zeroblob(-1))}
200 # Test bind_zeroblob()
202 sqlite3_memory_highwater 1
203 unset -nocomplain memused
204 set memused [sqlite3_memory_used]
205 do_test zeroblob-7.1 {
206 set ::STMT [sqlite3_prepare $::DB "SELECT length(?)" -1 DUMMY]
207 set ::sqlite3_max_blobsize 0
208 sqlite3_bind_zeroblob $::STMT 1 450000
211 do_test zeroblob-7.2 {
212 sqlite3_column_int $::STMT 0
214 do_test zeroblob-7.3 {
215 sqlite3_finalize $::STMT
217 do_test zeroblob-7.4 {
218 set ::sqlite3_max_blobsize
220 do_test zeroblob-7.5 {
221 expr {[sqlite3_memory_highwater]<$::memused+10000}
224 # Test that MakeRecord can handle a value with some real content
225 # and a zero-blob tail.
227 do_test zeroblob-8.1 {
229 SELECT 'hello' AS a, zeroblob(10) as b from t1 ORDER BY a, b;
235 # zeroblobs on either size of an IN operator
237 do_test zeroblob-9.1 {
238 db eval {SELECT x'0000' IN (x'000000')}
240 do_test zeroblob-9.2 {
241 db eval {SELECT x'0000' IN (x'0000')}
243 do_test zeroblob-9.3 {
244 db eval {SELECT zeroblob(2) IN (x'000000')}
246 do_test zeroblob-9.4 {
247 db eval {SELECT zeroblob(2) IN (x'0000')}
249 do_test zeroblob-9.5 {
250 db eval {SELECT x'0000' IN (zeroblob(3))}
252 do_test zeroblob-9.6 {
253 db eval {SELECT x'0000' IN (zeroblob(2))}
255 do_test zeroblob-9.7 {
256 db eval {SELECT zeroblob(2) IN (zeroblob(3))}
258 do_test zeroblob-9.8 {
259 db eval {SELECT zeroblob(2) IN (zeroblob(2))}
262 # Oversized zeroblob records
264 do_test zeroblob-10.1 {
266 CREATE TABLE t10(a,b,c);
268 catchsql {INSERT INTO t10 VALUES(zeroblob(1e9),zeroblob(1e9),zeroblob(1e9))}
269 } {1 {string or blob too big}}
271 #-------------------------------------------------------------------------
272 # Test the zeroblob() function on its own with negative or oversized
275 do_execsql_test 11.0 {
276 SELECT length(zeroblob(-1444444444444444));
278 do_catchsql_test 11.1 {
279 SELECT zeroblob(5000 * 1024 * 1024);
280 } {1 {string or blob too big}}
281 do_catchsql_test 11.2 {
282 SELECT quote(zeroblob(5000 * 1024 * 1024));
283 } {1 {string or blob too big}}
284 do_catchsql_test 11.3 {
285 SELECT quote(zeroblob(-1444444444444444));
287 do_catchsql_test 11.4 {
288 SELECT quote(test_zeroblob(-1));
291 #-------------------------------------------------------------------------
292 # Test the sqlite3_bind_zeroblob64() API.
294 proc bind_and_run {stmt nZero} {
295 sqlite3_bind_zeroblob64 $stmt 1 $nZero
297 set ret [sqlite3_column_int $stmt 0]
301 set stmt [sqlite3_prepare db "SELECT length(?)" -1 dummy]
303 do_test 12.1 { bind_and_run $stmt 40 } 40
304 do_test 12.2 { bind_and_run $stmt 0 } 0
305 do_test 12.3 { bind_and_run $stmt 1000 } 1000
308 list [catch { bind_and_run $stmt [expr 5000 * 1024 * 1024] } msg] $msg
312 set ret [sqlite3_column_int $stmt 0]
317 sqlite3_finalize $stmt
319 # 2019-01-25 https://sqlite.org/src/tktview/bb4bdb9f7f654b0bb9f34cfbac
320 # Zeroblob truncated by an index on expression
322 do_execsql_test 13.100 {
323 DROP TABLE IF EXISTS t1;
324 CREATE TABLE t1(a,b,c);
325 CREATE INDEX t1bbc ON t1(b, b+c);
326 INSERT INTO t1(a,b,c) VALUES(1,zeroblob(8),3);
327 SELECT a, quote(b), length(b), c FROM t1;
328 } {1 X'0000000000000000' 8 3}
330 test_restore_config_pagecache