blob: 04a668ed9fb3afc1c861892fa7da9f8f44838c2b [file] [log] [blame]
drhfdf972a2007-05-02 13:30:27 +00001# 2007 May 02
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# 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.
15#
danielk197728c66302007-09-01 11:04:26 +000016# $Id: zeroblob.test,v 1.7 2007/09/01 11:04:28 danielk1977 Exp $
drhfdf972a2007-05-02 13:30:27 +000017
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
danielk1977f12737d2007-05-17 06:44:28 +000021ifcapable !incrblob {
22 finish_test
23 return
24}
25
drhae7e1512007-05-02 16:51:59 +000026# 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.
drhfdf972a2007-05-02 13:30:27 +000029#
30do_test zeroblob-1.1 {
31 execsql {
32 CREATE TABLE t1(a,b,c,d);
drhfdf972a2007-05-02 13:30:27 +000033 }
drhae7e1512007-05-02 16:51:59 +000034 set ::sqlite3_max_blobsize 0
35 execsql {
36 INSERT INTO t1 VALUES(2,3,4,zeroblob(10000));
37 }
38 set ::sqlite3_max_blobsize
39} {10}
drhfdf972a2007-05-02 13:30:27 +000040do_test zeroblob-1.2 {
41 execsql {
42 SELECT length(d) FROM t1
43 }
44} {10000}
drhae7e1512007-05-02 16:51:59 +000045
46# If a non-NULL column follows the zeroblob, then the content of
47# the zeroblob must be instantiated.
48#
drhfdf972a2007-05-02 13:30:27 +000049do_test zeroblob-1.3 {
drhae7e1512007-05-02 16:51:59 +000050 set ::sqlite3_max_blobsize 0
drhfdf972a2007-05-02 13:30:27 +000051 execsql {
drhae7e1512007-05-02 16:51:59 +000052 INSERT INTO t1 VALUES(3,4,zeroblob(10000),5);
drhfdf972a2007-05-02 13:30:27 +000053 }
drhae7e1512007-05-02 16:51:59 +000054 set ::sqlite3_max_blobsize
55} {10010}
drhfdf972a2007-05-02 13:30:27 +000056do_test zeroblob-1.4 {
57 execsql {
58 SELECT length(c), length(d) FROM t1
59 }
60} {1 10000 10000 1}
drhae7e1512007-05-02 16:51:59 +000061
62# Multiple zeroblobs can appear at the end of record. No instantiation
63# of the blob content occurs on the stack.
64#
drhfdf972a2007-05-02 13:30:27 +000065do_test zeroblob-1.5 {
drhae7e1512007-05-02 16:51:59 +000066 set ::sqlite3_max_blobsize 0
drhfdf972a2007-05-02 13:30:27 +000067 execsql {
drhae7e1512007-05-02 16:51:59 +000068 INSERT INTO t1 VALUES(4,5,zeroblob(10000),zeroblob(10000));
drhfdf972a2007-05-02 13:30:27 +000069 }
drhae7e1512007-05-02 16:51:59 +000070 set ::sqlite3_max_blobsize
71} {11}
drhfdf972a2007-05-02 13:30:27 +000072do_test zeroblob-1.6 {
73 execsql {
74 SELECT length(c), length(d) FROM t1
75 }
76} {1 10000 10000 1 10000 10000}
77
drhae7e1512007-05-02 16:51:59 +000078# NULLs can follow the zeroblob() or be intermixed with zeroblobs and
79# no instantiation of the zeroblobs occurs on the stack.
80#
81do_test zeroblob-1.7 {
82 set ::sqlite3_max_blobsize 0
83 execsql {
84 INSERT INTO t1 VALUES(5,zeroblob(10000),NULL,zeroblob(10000));
85 }
86 set ::sqlite3_max_blobsize
87} {10}
88do_test zeroblob-1.8 {
89 execsql {
90 SELECT length(b), length(d) FROM t1 WHERE a=5
91 }
92} {10000 10000}
93
94# Comparisons against zeroblobs work.
95#
96do_test zeroblob-2.1 {
97 execsql {
98 SELECT a FROM t1 WHERE b=zeroblob(10000)
99 }
100} {5}
101
102# Comparisons against zeroblobs work even when indexed.
103#
104do_test zeroblob-2.2 {
105 execsql {
106 CREATE INDEX i1_1 ON t1(b);
107 SELECT a FROM t1 WHERE b=zeroblob(10000);
108 }
109} {5}
110
111# DISTINCT works for zeroblobs
112#
113do_test zeroblob-3.1 {
114 execsql {
115 SELECT count(DISTINCT a) FROM (
116 SELECT x'00000000000000000000' AS a
117 UNION ALL
118 SELECT zeroblob(10) AS a
119 )
120 }
121} {1}
drh50027d12007-05-23 06:31:38 +0000122
123# Concatentation works with zeroblob
124#
125do_test zeroblob-4.1 {
126 execsql {
127 SELECT hex(zeroblob(2) || x'61')
128 }
129} {000061}
danielk19776b28f052007-05-30 06:19:32 +0000130
131# Check various CAST(...) operations on zeroblob.
132#
133do_test zeroblob-5.1 {
134 execsql {
135 SELECT CAST (zeroblob(100) AS REAL);
136 }
137} {0.0}
138do_test zeroblob-5.2 {
139 execsql {
140 SELECT CAST (zeroblob(100) AS INTEGER);
141 }
142} {0}
143do_test zeroblob-5.3 {
144 execsql {
145 SELECT CAST (zeroblob(100) AS TEXT);
146 }
147} {{}}
148do_test zeroblob-5.4 {
149 execsql {
150 SELECT CAST(zeroblob(100) AS BLOB);
151 }
152} [execsql {SELECT zeroblob(100)}]
drhae7e1512007-05-02 16:51:59 +0000153
154
drh98640a32007-06-07 19:08:32 +0000155# Check for malicious use of zeroblob. Make sure nothing crashes.
156#
157do_test zeroblob-6.1.1 {
158 execsql {select zeroblob(-1)}
159} {{}}
160do_test zeroblob-6.1.2 {
161 execsql {select zeroblob(-10)}
162} {{}}
163do_test zeroblob-6.1.3 {
164 execsql {select zeroblob(-100)}
165} {{}}
166do_test zeroblob-6.2 {
167 execsql {select length(zeroblob(-1))}
168} {0}
169do_test zeroblob-6.3 {
170 execsql {select zeroblob(-1)|1}
171} {1}
172do_test zeroblob-6.4 {
173 catchsql {select length(zeroblob(2147483648))}
174} {1 {string or blob too big}}
175do_test zeroblob-6.5 {
176 catchsql {select zeroblob(2147483648)}
177} {1 {string or blob too big}}
178
danielk197728c66302007-09-01 11:04:26 +0000179# Test bind_zeroblob()
180#
181do_test zeroblob-7.1 {
182 set ::STMT [sqlite3_prepare $::DB "SELECT length(?)" -1 DUMMY]
183 sqlite3_bind_zeroblob $::STMT 1 450
184 sqlite3_step $::STMT
185} {SQLITE_ROW}
186do_test zeroblob-7.2 {
187 sqlite3_column_int $::STMT 0
188} {450}
189do_test zeroblob-7.3 {
190 sqlite3_finalize $::STMT
191} {SQLITE_OK}
192
drhfdf972a2007-05-02 13:30:27 +0000193finish_test