sl@0
|
1 |
# 2007 May 02
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
4 |
# a legal notice, here is a blessing:
|
sl@0
|
5 |
#
|
sl@0
|
6 |
# May you do good and not evil.
|
sl@0
|
7 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
8 |
# May you share freely, never taking more than you give.
|
sl@0
|
9 |
#
|
sl@0
|
10 |
#***********************************************************************
|
sl@0
|
11 |
# This file implements regression tests for SQLite library. The
|
sl@0
|
12 |
# focus of this file is testing of the zero-filled blob functionality
|
sl@0
|
13 |
# including the sqlite3_bind_zeroblob(), sqlite3_result_zeroblob(),
|
sl@0
|
14 |
# and the built-in zeroblob() SQL function.
|
sl@0
|
15 |
#
|
sl@0
|
16 |
# $Id: zeroblob.test,v 1.13 2008/06/13 18:24:28 drh Exp $
|
sl@0
|
17 |
|
sl@0
|
18 |
set testdir [file dirname $argv0]
|
sl@0
|
19 |
source $testdir/tester.tcl
|
sl@0
|
20 |
|
sl@0
|
21 |
ifcapable !incrblob {
|
sl@0
|
22 |
finish_test
|
sl@0
|
23 |
return
|
sl@0
|
24 |
}
|
sl@0
|
25 |
|
sl@0
|
26 |
# When zeroblob() is used for the last field of a column, then the
|
sl@0
|
27 |
# content of the zeroblob is never instantiated on the VDBE stack.
|
sl@0
|
28 |
# But it does get inserted into the database correctly.
|
sl@0
|
29 |
#
|
sl@0
|
30 |
db eval {PRAGMA cache_size=10}
|
sl@0
|
31 |
sqlite3_memory_highwater 1
|
sl@0
|
32 |
unset -nocomplain memused
|
sl@0
|
33 |
set memused [sqlite3_memory_used]
|
sl@0
|
34 |
do_test zeroblob-1.1 {
|
sl@0
|
35 |
execsql {
|
sl@0
|
36 |
CREATE TABLE t1(a,b,c,d);
|
sl@0
|
37 |
}
|
sl@0
|
38 |
set ::sqlite3_max_blobsize 0
|
sl@0
|
39 |
execsql {
|
sl@0
|
40 |
INSERT INTO t1 VALUES(2,3,4,zeroblob(1000000));
|
sl@0
|
41 |
}
|
sl@0
|
42 |
set ::sqlite3_max_blobsize
|
sl@0
|
43 |
} {10}
|
sl@0
|
44 |
do_test zeroblob-1.1.1 {
|
sl@0
|
45 |
expr {[sqlite3_memory_highwater]<$::memused+25000}
|
sl@0
|
46 |
} {1}
|
sl@0
|
47 |
do_test zeroblob-1.2 {
|
sl@0
|
48 |
execsql {
|
sl@0
|
49 |
SELECT length(d) FROM t1
|
sl@0
|
50 |
}
|
sl@0
|
51 |
} {1000000}
|
sl@0
|
52 |
|
sl@0
|
53 |
# If a non-NULL column follows the zeroblob, then the content of
|
sl@0
|
54 |
# the zeroblob must be instantiated.
|
sl@0
|
55 |
#
|
sl@0
|
56 |
do_test zeroblob-1.3 {
|
sl@0
|
57 |
set ::sqlite3_max_blobsize 0
|
sl@0
|
58 |
execsql {
|
sl@0
|
59 |
INSERT INTO t1 VALUES(3,4,zeroblob(10000),5);
|
sl@0
|
60 |
}
|
sl@0
|
61 |
set ::sqlite3_max_blobsize
|
sl@0
|
62 |
} {10010}
|
sl@0
|
63 |
do_test zeroblob-1.4 {
|
sl@0
|
64 |
execsql {
|
sl@0
|
65 |
SELECT length(c), length(d) FROM t1
|
sl@0
|
66 |
}
|
sl@0
|
67 |
} {1 1000000 10000 1}
|
sl@0
|
68 |
|
sl@0
|
69 |
# Multiple zeroblobs can appear at the end of record. No instantiation
|
sl@0
|
70 |
# of the blob content occurs on the stack.
|
sl@0
|
71 |
#
|
sl@0
|
72 |
do_test zeroblob-1.5 {
|
sl@0
|
73 |
set ::sqlite3_max_blobsize 0
|
sl@0
|
74 |
execsql {
|
sl@0
|
75 |
INSERT INTO t1 VALUES(4,5,zeroblob(10000),zeroblob(10000));
|
sl@0
|
76 |
}
|
sl@0
|
77 |
set ::sqlite3_max_blobsize
|
sl@0
|
78 |
} {11}
|
sl@0
|
79 |
do_test zeroblob-1.6 {
|
sl@0
|
80 |
execsql {
|
sl@0
|
81 |
SELECT length(c), length(d) FROM t1
|
sl@0
|
82 |
}
|
sl@0
|
83 |
} {1 1000000 10000 1 10000 10000}
|
sl@0
|
84 |
|
sl@0
|
85 |
# NULLs can follow the zeroblob() or be intermixed with zeroblobs and
|
sl@0
|
86 |
# no instantiation of the zeroblobs occurs on the stack.
|
sl@0
|
87 |
#
|
sl@0
|
88 |
do_test zeroblob-1.7 {
|
sl@0
|
89 |
set ::sqlite3_max_blobsize 0
|
sl@0
|
90 |
execsql {
|
sl@0
|
91 |
INSERT INTO t1 VALUES(5,zeroblob(10000),NULL,zeroblob(10000));
|
sl@0
|
92 |
}
|
sl@0
|
93 |
set ::sqlite3_max_blobsize
|
sl@0
|
94 |
} {10}
|
sl@0
|
95 |
do_test zeroblob-1.8 {
|
sl@0
|
96 |
execsql {
|
sl@0
|
97 |
SELECT length(b), length(d) FROM t1 WHERE a=5
|
sl@0
|
98 |
}
|
sl@0
|
99 |
} {10000 10000}
|
sl@0
|
100 |
|
sl@0
|
101 |
# Comparisons against zeroblobs work.
|
sl@0
|
102 |
#
|
sl@0
|
103 |
do_test zeroblob-2.1 {
|
sl@0
|
104 |
execsql {
|
sl@0
|
105 |
SELECT a FROM t1 WHERE b=zeroblob(10000)
|
sl@0
|
106 |
}
|
sl@0
|
107 |
} {5}
|
sl@0
|
108 |
|
sl@0
|
109 |
# Comparisons against zeroblobs work even when indexed.
|
sl@0
|
110 |
#
|
sl@0
|
111 |
do_test zeroblob-2.2 {
|
sl@0
|
112 |
execsql {
|
sl@0
|
113 |
CREATE INDEX i1_1 ON t1(b);
|
sl@0
|
114 |
SELECT a FROM t1 WHERE b=zeroblob(10000);
|
sl@0
|
115 |
}
|
sl@0
|
116 |
} {5}
|
sl@0
|
117 |
|
sl@0
|
118 |
# DISTINCT works for zeroblobs
|
sl@0
|
119 |
#
|
sl@0
|
120 |
ifcapable bloblit&&subquery&&compound {
|
sl@0
|
121 |
do_test zeroblob-3.1 {
|
sl@0
|
122 |
execsql {
|
sl@0
|
123 |
SELECT count(DISTINCT a) FROM (
|
sl@0
|
124 |
SELECT x'00000000000000000000' AS a
|
sl@0
|
125 |
UNION ALL
|
sl@0
|
126 |
SELECT zeroblob(10) AS a
|
sl@0
|
127 |
)
|
sl@0
|
128 |
}
|
sl@0
|
129 |
} {1}
|
sl@0
|
130 |
}
|
sl@0
|
131 |
|
sl@0
|
132 |
# Concatentation works with zeroblob
|
sl@0
|
133 |
#
|
sl@0
|
134 |
ifcapable bloblit {
|
sl@0
|
135 |
do_test zeroblob-4.1 {
|
sl@0
|
136 |
execsql {
|
sl@0
|
137 |
SELECT hex(zeroblob(2) || x'61')
|
sl@0
|
138 |
}
|
sl@0
|
139 |
} {000061}
|
sl@0
|
140 |
}
|
sl@0
|
141 |
|
sl@0
|
142 |
# Check various CAST(...) operations on zeroblob.
|
sl@0
|
143 |
#
|
sl@0
|
144 |
do_test zeroblob-5.1 {
|
sl@0
|
145 |
execsql {
|
sl@0
|
146 |
SELECT CAST (zeroblob(100) AS REAL);
|
sl@0
|
147 |
}
|
sl@0
|
148 |
} {0.0}
|
sl@0
|
149 |
do_test zeroblob-5.2 {
|
sl@0
|
150 |
execsql {
|
sl@0
|
151 |
SELECT CAST (zeroblob(100) AS INTEGER);
|
sl@0
|
152 |
}
|
sl@0
|
153 |
} {0}
|
sl@0
|
154 |
do_test zeroblob-5.3 {
|
sl@0
|
155 |
execsql {
|
sl@0
|
156 |
SELECT CAST (zeroblob(100) AS TEXT);
|
sl@0
|
157 |
}
|
sl@0
|
158 |
} {{}}
|
sl@0
|
159 |
do_test zeroblob-5.4 {
|
sl@0
|
160 |
execsql {
|
sl@0
|
161 |
SELECT CAST(zeroblob(100) AS BLOB);
|
sl@0
|
162 |
}
|
sl@0
|
163 |
} [execsql {SELECT zeroblob(100)}]
|
sl@0
|
164 |
|
sl@0
|
165 |
|
sl@0
|
166 |
# Check for malicious use of zeroblob. Make sure nothing crashes.
|
sl@0
|
167 |
#
|
sl@0
|
168 |
do_test zeroblob-6.1.1 {
|
sl@0
|
169 |
execsql {select zeroblob(-1)}
|
sl@0
|
170 |
} {{}}
|
sl@0
|
171 |
do_test zeroblob-6.1.2 {
|
sl@0
|
172 |
execsql {select zeroblob(-10)}
|
sl@0
|
173 |
} {{}}
|
sl@0
|
174 |
do_test zeroblob-6.1.3 {
|
sl@0
|
175 |
execsql {select zeroblob(-100)}
|
sl@0
|
176 |
} {{}}
|
sl@0
|
177 |
do_test zeroblob-6.2 {
|
sl@0
|
178 |
execsql {select length(zeroblob(-1))}
|
sl@0
|
179 |
} {0}
|
sl@0
|
180 |
do_test zeroblob-6.3 {
|
sl@0
|
181 |
execsql {select zeroblob(-1)|1}
|
sl@0
|
182 |
} {1}
|
sl@0
|
183 |
do_test zeroblob-6.4 {
|
sl@0
|
184 |
catchsql {select length(zeroblob(2147483648))}
|
sl@0
|
185 |
} {1 {string or blob too big}}
|
sl@0
|
186 |
do_test zeroblob-6.5 {
|
sl@0
|
187 |
catchsql {select zeroblob(2147483648)}
|
sl@0
|
188 |
} {1 {string or blob too big}}
|
sl@0
|
189 |
do_test zeroblob-6.6 {
|
sl@0
|
190 |
execsql {select hex(zeroblob(-1))}
|
sl@0
|
191 |
} {{}}
|
sl@0
|
192 |
do_test zeroblob-6.7 {
|
sl@0
|
193 |
execsql {select typeof(zeroblob(-1))}
|
sl@0
|
194 |
} {blob}
|
sl@0
|
195 |
|
sl@0
|
196 |
# Test bind_zeroblob()
|
sl@0
|
197 |
#
|
sl@0
|
198 |
sqlite3_memory_highwater 1
|
sl@0
|
199 |
unset -nocomplain memused
|
sl@0
|
200 |
set memused [sqlite3_memory_used]
|
sl@0
|
201 |
do_test zeroblob-7.1 {
|
sl@0
|
202 |
set ::STMT [sqlite3_prepare $::DB "SELECT length(?)" -1 DUMMY]
|
sl@0
|
203 |
set ::sqlite3_max_blobsize 0
|
sl@0
|
204 |
sqlite3_bind_zeroblob $::STMT 1 450000
|
sl@0
|
205 |
sqlite3_step $::STMT
|
sl@0
|
206 |
} {SQLITE_ROW}
|
sl@0
|
207 |
do_test zeroblob-7.2 {
|
sl@0
|
208 |
sqlite3_column_int $::STMT 0
|
sl@0
|
209 |
} {450000}
|
sl@0
|
210 |
do_test zeroblob-7.3 {
|
sl@0
|
211 |
sqlite3_finalize $::STMT
|
sl@0
|
212 |
} {SQLITE_OK}
|
sl@0
|
213 |
do_test zeroblob-7.4 {
|
sl@0
|
214 |
set ::sqlite3_max_blobsize
|
sl@0
|
215 |
} {0}
|
sl@0
|
216 |
do_test zeroblob-7.5 {
|
sl@0
|
217 |
expr {[sqlite3_memory_highwater]<$::memused+10000}
|
sl@0
|
218 |
} {1}
|
sl@0
|
219 |
|
sl@0
|
220 |
# Test that MakeRecord can handle a value with some real content
|
sl@0
|
221 |
# and a zero-blob tail.
|
sl@0
|
222 |
#
|
sl@0
|
223 |
do_test zeroblob-8.1 {
|
sl@0
|
224 |
llength [execsql {
|
sl@0
|
225 |
SELECT 'hello' AS a, zeroblob(10) as b from t1 ORDER BY a, b;
|
sl@0
|
226 |
}]
|
sl@0
|
227 |
} {8}
|
sl@0
|
228 |
|
sl@0
|
229 |
|
sl@0
|
230 |
finish_test
|