blob: 720dbb9abd173ec93e04b4460305802ded3ceb13 [file] [log] [blame]
drhdb83f822007-05-11 00:20:08 +00001# 2007 May 10
danielk1977def0fec2007-05-10 15:37:52 +00002#
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
drhdb83f822007-05-11 00:20:08 +000012# focus of this file is generating semi-random strings of SQL
danielk19771e4eaeb2007-05-14 14:04:59 +000013# (a.k.a. "fuzz") and sending it into the parser to try to
14# generate errors.
danielk1977def0fec2007-05-10 15:37:52 +000015#
danielk19771e4eaeb2007-05-14 14:04:59 +000016# The tests in this file are really about testing fuzzily generated
17# SQL parse-trees. The majority of the fuzzily generated SQL is
18# valid as far as the parser is concerned.
19#
20# The most complicated trees are for SELECT statements.
21#
drh813f31e2009-01-06 00:08:02 +000022# $Id: fuzz.test,v 1.17 2009/01/06 00:08:02 drh Exp $
danielk1977def0fec2007-05-10 15:37:52 +000023
24set testdir [file dirname $argv0]
25source $testdir/tester.tcl
26
danielk1977fa2bb6d2007-05-14 15:49:43 +000027set ::REPEATS 5000
danielk19775453b8d2007-05-30 08:18:04 +000028
29# If running quick.test, don't do so many iterations.
danielk19771ee506d2007-05-15 07:14:32 +000030if {[info exists ::ISQUICK]} {
danielk197715d79822007-05-15 07:00:34 +000031 if {$::ISQUICK} { set ::REPEATS 20 }
32}
danielk19771f7c83e2007-05-11 16:58:03 +000033
danielk1977c9cf9012007-05-30 10:36:47 +000034source $testdir/fuzz_common.tcl
drh66cd1822009-01-05 19:36:30 +000035expr srand(0)
danielk1977bcfc4bc2007-05-11 10:10:33 +000036
danielk1977f75232f2007-05-10 17:32:48 +000037#----------------------------------------------------------------
38# These tests caused errors that were first caught by the tests
39# in this file. They are still here.
danielk1977def0fec2007-05-10 15:37:52 +000040do_test fuzz-1.1 {
41 execsql {
42 SELECT 'abc' LIKE X'ABCD';
43 }
44} {0}
45do_test fuzz-1.2 {
46 execsql {
47 SELECT 'abc' LIKE zeroblob(10);
48 }
49} {0}
50do_test fuzz-1.3 {
51 execsql {
52 SELECT zeroblob(10) LIKE 'abc';
53 }
54} {0}
55do_test fuzz-1.4 {
56 execsql {
57 SELECT (- -21) % NOT (456 LIKE zeroblob(10));
58 }
59} {0}
danielk1977f75232f2007-05-10 17:32:48 +000060do_test fuzz-1.5 {
61 execsql {
62 SELECT (SELECT (
63 SELECT (SELECT -2147483648) FROM (SELECT 1) ORDER BY 1
64 ))
danielk1977def0fec2007-05-10 15:37:52 +000065 }
danielk1977f75232f2007-05-10 17:32:48 +000066} {-2147483648}
67do_test fuzz-1.6 {
68 execsql {
69 SELECT 'abc', zeroblob(1) FROM (SELECT 1) ORDER BY 1
70 }
71} [execsql {SELECT 'abc', zeroblob(1)}]
72
73do_test fuzz-1.7 {
74 execsql {
danielk1977d908f5a2007-05-11 07:08:28 +000075 SELECT ( SELECT zeroblob(1000) FROM (
76 SELECT * FROM (SELECT 'first') ORDER BY NOT 'in')
danielk1977639f45f2007-05-10 17:38:57 +000077 )
danielk1977f75232f2007-05-10 17:32:48 +000078 }
danielk1977d908f5a2007-05-11 07:08:28 +000079} [execsql {SELECT zeroblob(1000)}]
danielk1977def0fec2007-05-10 15:37:52 +000080
danielk1977bcfc4bc2007-05-11 10:10:33 +000081do_test fuzz-1.8 {
danielk19771f7c83e2007-05-11 16:58:03 +000082 # Problems with opcode OP_ToText (did not account for MEM_Zero).
83 # Also MemExpandBlob() was marking expanded blobs as nul-terminated.
84 # They are not.
danielk1977bcfc4bc2007-05-11 10:10:33 +000085 execsql {
86 SELECT CAST(zeroblob(1000) AS text);
87 }
88} {{}}
89
danielk19771f7c83e2007-05-11 16:58:03 +000090do_test fuzz-1.9 {
91 # This was causing a NULL pointer dereference of Expr.pList.
92 execsql {
93 SELECT 1 FROM (SELECT * FROM sqlite_master WHERE random())
94 }
95} {}
96
97do_test fuzz-1.10 {
98 # Bug in calculation of Parse.ckOffset causing an assert()
99 # to fail. Probably harmless.
100 execsql {
101 SELECT coalesce(1, substr( 1, 2, length('in' IN (SELECT 1))))
102 }
103} {1}
104
danielk19771e4eaeb2007-05-14 14:04:59 +0000105do_test fuzz-1.11 {
106 # The literals (A, B, C, D) are not important, they are just used
107 # to make the EXPLAIN output easier to read.
108 #
109 # The problem here is that the EXISTS(...) expression leaves an
110 # extra value on the VDBE stack. This is confusing the parent and
111 # leads to an assert() failure when OP_Insert encounters an integer
112 # when it expects a record blob.
113 #
114 # Update: Any query with (LIMIT 0) was leaking stack.
115 #
116 execsql {
117 SELECT 'A' FROM (SELECT 'B') ORDER BY EXISTS (
118 SELECT 'C' FROM (SELECT 'D' LIMIT 0)
119 )
120 }
121} {A}
122
danielk1977fa2bb6d2007-05-14 15:49:43 +0000123do_test fuzz-1.12.1 {
124 # Create a table with a single row.
125 execsql {
126 CREATE TABLE abc(b);
127 INSERT INTO abc VALUES('ABCDE');
128 }
129
130 # The following query was crashing. The later subquery (in the FROM)
131 # clause was flattened into the parent, but the code was not repairng
132 # the "b" reference in the other sub-query. When the query was executed,
133 # that "b" refered to a non-existant vdbe table-cursor.
134 #
135 execsql {
136 SELECT 1 IN ( SELECT b UNION SELECT 1 ) FROM (SELECT b FROM abc);
137 }
138} {1}
139do_test fuzz-1.12.2 {
140 # Clean up after the previous query.
141 execsql {
142 DROP TABLE abc;
143 }
144} {}
145
danielk1977a670b222007-05-14 16:50:48 +0000146
147do_test fuzz-1.13 {
148 # The problem here was that when there were more expressions in
149 # the ORDER BY list than the result-set list. The temporary b-tree
150 # used for sorting was being misconfigured in this case.
151 #
152 execsql {
153 SELECT 'abcd' UNION SELECT 'efgh' ORDER BY 1 ASC, 1 ASC;
154 }
155} {abcd efgh}
156
danielk197715d79822007-05-15 07:00:34 +0000157do_test fuzz-1.14.1 {
158 execsql {
159 CREATE TABLE abc(a, b, c);
160 INSERT INTO abc VALUES(123, 456, 789);
161 }
162
163 # The [a] reference in the sub-select was causing a problem. Because
164 # the internal walkSelectExpr() function was not considering compound
165 # SELECT operators.
166 execsql {
167 SELECT 1 FROM abc
168 GROUP BY c HAVING EXISTS (SELECT a UNION SELECT 123);
169 }
170} {1}
171do_test fuzz-1.14.2 {
172 execsql {
173 DROP TABLE abc;
174 }
175} {}
176
drh66cd1822009-01-05 19:36:30 +0000177# Making sure previously discovered errors have been fixed.
178#
179do_test fuzz-1.15 {
180 execsql {
181 SELECT hex(CAST(zeroblob(1000) AS integer))
182 }
183} {30}
184
185do_test fuzz-1.16.1 {
186 execsql {
187 CREATE TABLE abc(a, b, c);
188 CREATE TABLE def(a, b, c);
189 CREATE TABLE ghi(a, b, c);
190 }
191} {}
192do_test fuzz-1.16.2 {
drh66cd1822009-01-05 19:36:30 +0000193 catchsql {
194 SELECT DISTINCT EXISTS(
195 SELECT 1
196 FROM (
197 SELECT C FROM (SELECT 1)
198 )
199 WHERE (SELECT c)
200 )
201 FROM abc
202 }
203} {0 {}}
204do_test fuzz-1.16.3 {
205 catchsql {
206 SELECT DISTINCT substr(-456 ISNULL,zeroblob(1000), EXISTS(
207 SELECT DISTINCT EXISTS(
208 SELECT DISTINCT b FROM abc
209 ORDER BY EXISTS (
210 SELECT DISTINCT 2147483647 UNION ALL SELECT -2147483648
211 ) ASC
212 )
213 FROM (
214 SELECT c, c FROM (
215 SELECT 456, 'injection' ORDER BY 56.1 ASC, -56.1 DESC
216 )
217 )
218 GROUP BY (SELECT ALL (SELECT DISTINCT 'hardware'))
219 HAVING (
220 SELECT DISTINCT c
221 FROM (
222 SELECT ALL -2147483648, 'experiments'
223 ORDER BY -56.1 ASC, -56.1 DESC
224 )
225 GROUP BY (SELECT DISTINCT 456) IN
226 (SELECT DISTINCT 'injection') NOT IN (SELECT ALL -456)
227 HAVING EXISTS (
228 SELECT ALL 'injection'
229 )
230 )
231 UNION ALL
232 SELECT a IN (
233 SELECT -2147483647
234 UNION ALL
235 SELECT ALL 'injection'
236 )
237 FROM sqlite_master
238 ) -- end EXISTS
239 ) /* end SUBSTR() */, c NOTNULL ISNULL
240 FROM abc
241 ORDER BY CAST(-56.1 AS blob) ASC
242 }
243} {0 {}}
244do_test fuzz-1.16.4 {
245 execsql {
246 DROP TABLE abc; DROP TABLE def; DROP TABLE ghi;
247 }
248} {}
249
drhde58ddb2009-01-05 22:30:38 +0000250do_test fuzz-1.17 {
251 catchsql {
252 SELECT 'hardware', 56.1 NOTNULL, random()&0
253 FROM (
254 SELECT ALL lower(~ EXISTS (
255 SELECT 1 NOT IN (SELECT ALL 1)
256 )), CAST(456 AS integer), -2147483647
257 FROM (
258 SELECT DISTINCT -456, CAST(1 AS integer) ISNULL
259 FROM (SELECT ALL 2147483647, typeof(2147483649))
260 )
261 )
262 GROUP BY CAST(CAST('experiments' AS blob) AS blob)
263 HAVING random()
264 }
265} {0 {hardware 1 0}}
266
drh813f31e2009-01-06 00:08:02 +0000267do_test fuzz-1.18 {
268breakpoint
269 catchsql {
270 SELECT -2147483649 << upper('fault' NOT IN (
271 SELECT ALL (
272 SELECT ALL -1
273 ORDER BY -2147483649
274 LIMIT (
275 SELECT ALL (
276 SELECT 0 EXCEPT SELECT DISTINCT 'experiments' ORDER BY 1 ASC
277 )
278 )
279 OFFSET EXISTS (
280 SELECT ALL
281 (SELECT ALL -2147483648) NOT IN (
282 SELECT ALL 123456789.1234567899
283 ) IN (SELECT 2147483649)
284 FROM sqlite_master
285 ) NOT IN (SELECT ALL 'The')
286 )
287 ))
288 }
289} {0 -4294967298}
290
danielk1977f75232f2007-05-10 17:32:48 +0000291#----------------------------------------------------------------
292# Test some fuzzily generated expressions.
293#
danielk1977bcfc4bc2007-05-11 10:10:33 +0000294do_fuzzy_test fuzz-2 -template { SELECT [Expr] }
danielk1977f75232f2007-05-10 17:32:48 +0000295
296do_test fuzz-3.1 {
297 execsql {
298 CREATE TABLE abc(a, b, c);
danielk1977bcfc4bc2007-05-11 10:10:33 +0000299 CREATE TABLE def(a, b, c);
300 CREATE TABLE ghi(a, b, c);
danielk1977f75232f2007-05-10 17:32:48 +0000301 }
302} {}
danielk1977bcfc4bc2007-05-11 10:10:33 +0000303set ::TableList [list abc def ghi]
danielk1977f75232f2007-05-10 17:32:48 +0000304
305#----------------------------------------------------------------
306# Test some fuzzily generated SELECT statements.
307#
danielk1977bcfc4bc2007-05-11 10:10:33 +0000308do_fuzzy_test fuzz-3.2 -template {[Select]}
danielk1977f75232f2007-05-10 17:32:48 +0000309
danielk1977bcfc4bc2007-05-11 10:10:33 +0000310#----------------------------------------------------------------
311# Insert a small amount of data into the database and then run
312# some more generated SELECT statements.
313#
314do_test fuzz-4.1 {
315 execsql {
316 INSERT INTO abc VALUES(1, 2, 3);
317 INSERT INTO abc VALUES(4, 5, 6);
318 INSERT INTO abc VALUES(7, 8, 9);
319 INSERT INTO def VALUES(1, 2, 3);
320 INSERT INTO def VALUES(4, 5, 6);
321 INSERT INTO def VALUES(7, 8, 9);
322 INSERT INTO ghi VALUES(1, 2, 3);
323 INSERT INTO ghi VALUES(4, 5, 6);
324 INSERT INTO ghi VALUES(7, 8, 9);
325 CREATE INDEX abc_i ON abc(a, b, c);
326 CREATE INDEX def_i ON def(c, a, b);
327 CREATE INDEX ghi_i ON ghi(b, c, a);
328 }
329} {}
330do_fuzzy_test fuzz-4.2 -template {[Select]}
331
332#----------------------------------------------------------------
333# Test some fuzzy INSERT statements:
334#
335do_test fuzz-5.1 {execsql BEGIN} {}
336do_fuzzy_test fuzz-5.2 -template {[Insert]} -errorlist table
337integrity_check fuzz-5.2.integrity
338do_test fuzz-5.3 {execsql COMMIT} {}
339integrity_check fuzz-5.4.integrity
340
danielk19771f7c83e2007-05-11 16:58:03 +0000341#----------------------------------------------------------------
danielk19771e4eaeb2007-05-14 14:04:59 +0000342# Now that there is data in the database, run some more SELECT
danielk19771f7c83e2007-05-11 16:58:03 +0000343# statements
344#
345set ::ColumnList [list a b c]
346set E {{no such col} {ambiguous column name}}
347do_fuzzy_test fuzz-6.1 -template {[Select]} -errorlist $E
348
349#----------------------------------------------------------------
350# Run some SELECTs, INSERTs, UPDATEs and DELETEs in a transaction.
351#
352set E {{no such col} {ambiguous column name} {table}}
353do_test fuzz-7.1 {execsql BEGIN} {}
354do_fuzzy_test fuzz-7.2 -template {[Statement]} -errorlist $E
355integrity_check fuzz-7.3.integrity
356do_test fuzz-7.4 {execsql COMMIT} {}
357integrity_check fuzz-7.5.integrity
danielk1977bcfc4bc2007-05-11 10:10:33 +0000358
danielk19771e4eaeb2007-05-14 14:04:59 +0000359#----------------------------------------------------------------
danielk197715d79822007-05-15 07:00:34 +0000360# Many CREATE and DROP TABLE statements:
danielk19771e4eaeb2007-05-14 14:04:59 +0000361#
danielk19771ee506d2007-05-15 07:14:32 +0000362set E [list table duplicate {no such col} {ambiguous column name} {use DROP}]
danielk197715d79822007-05-15 07:00:34 +0000363do_fuzzy_test fuzz-8.1 -template {[CreateOrDropTableOrView]} -errorlist $E
danielk19771e4eaeb2007-05-14 14:04:59 +0000364
danielk1977bcfc4bc2007-05-11 10:10:33 +0000365close $::log
danielk1977def0fec2007-05-10 15:37:52 +0000366finish_test