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 generating semi-random strings of SQL
13 # (a.k.a. "fuzz") and sending it into the parser to try to
16 # 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.
20 # The most complicated trees are for SELECT statements.
22 # $Id: fuzz.test,v 1.19 2009/04/28 11:10:39 danielk1977 Exp $
24 set testdir [file dirname $argv0]
25 source $testdir/tester.tcl
29 # If running quick.test, don't do so many iterations.
30 if {[info exists ::G(isquick)]} {
31 if {$::G(isquick)} { set ::REPEATS 20 }
34 source $testdir/fuzz_common.tcl
37 #----------------------------------------------------------------
38 # These tests caused errors that were first caught by the tests
39 # in this file. They are still here.
42 SELECT 'abc' LIKE X'ABCD';
47 SELECT 'abc' LIKE zeroblob(10);
52 SELECT zeroblob(10) LIKE 'abc';
57 SELECT (- -21) % NOT (456 LIKE zeroblob(10));
63 SELECT (SELECT -2147483648) FROM (SELECT 1) ORDER BY 1
69 SELECT 'abc', zeroblob(1) FROM (SELECT 1) ORDER BY 1
71 } [execsql {SELECT 'abc', zeroblob(1)}]
75 SELECT ( SELECT zeroblob(1000) FROM (
76 SELECT * FROM (SELECT 'first') ORDER BY NOT 'in')
79 } [execsql {SELECT zeroblob(1000)}]
82 # Problems with opcode OP_ToText (did not account for MEM_Zero).
83 # Also MemExpandBlob() was marking expanded blobs as nul-terminated.
86 SELECT CAST(zeroblob(1000) AS text);
91 # This was causing a NULL pointer dereference of Expr.pList.
93 SELECT 1 FROM (SELECT * FROM sqlite_master WHERE random())
98 # Bug in calculation of Parse.ckOffset causing an assert()
99 # to fail. Probably harmless.
101 SELECT coalesce(1, substr( 1, 2, length('in' IN (SELECT 1))))
106 # The literals (A, B, C, D) are not important, they are just used
107 # to make the EXPLAIN output easier to read.
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.
114 # Update: Any query with (LIMIT 0) was leaking stack.
117 SELECT 'A' FROM (SELECT 'B') ORDER BY EXISTS (
118 SELECT 'C' FROM (SELECT 'D' LIMIT 0)
123 do_test fuzz-1.12.1 {
124 # Create a table with a single row.
127 INSERT INTO abc VALUES('ABCDE');
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.
136 SELECT 1 IN ( SELECT b UNION SELECT 1 ) FROM (SELECT b FROM abc);
139 do_test fuzz-1.12.2 {
140 # Clean up after the previous query.
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.
153 SELECT 'abcd' UNION SELECT 'efgh' ORDER BY 1 ASC, 1 ASC;
157 do_test fuzz-1.14.1 {
159 CREATE TABLE abc(a, b, c);
160 INSERT INTO abc VALUES(123, 456, 789);
163 # The [a] reference in the sub-select was causing a problem. Because
164 # the internal walkSelectExpr() function was not considering compound
168 GROUP BY c HAVING EXISTS (SELECT a UNION SELECT 123);
171 do_test fuzz-1.14.2 {
177 # Making sure previously discovered errors have been fixed.
181 SELECT hex(CAST(zeroblob(1000) AS integer))
185 do_test fuzz-1.16.1 {
187 CREATE TABLE abc(a, b, c);
188 CREATE TABLE def(a, b, c);
189 CREATE TABLE ghi(a, b, c);
192 do_test fuzz-1.16.2 {
194 SELECT DISTINCT EXISTS(
197 SELECT C FROM (SELECT 1)
204 do_test fuzz-1.16.3 {
206 SELECT DISTINCT substr(-456 ISNULL,zeroblob(1000), EXISTS(
207 SELECT DISTINCT EXISTS(
208 SELECT DISTINCT b FROM abc
210 SELECT DISTINCT 2147483647 UNION ALL SELECT -2147483648
215 SELECT 456, 'injection' ORDER BY 56.1 ASC, -56.1 DESC
218 GROUP BY (SELECT ALL (SELECT DISTINCT 'hardware'))
222 SELECT ALL -2147483648, 'experiments'
223 ORDER BY -56.1 ASC, -56.1 DESC
225 GROUP BY (SELECT DISTINCT 456) IN
226 (SELECT DISTINCT 'injection') NOT IN (SELECT ALL -456)
228 SELECT ALL 'injection'
235 SELECT ALL 'injection'
239 ) /* end SUBSTR() */, c NOTNULL ISNULL
241 ORDER BY CAST(-56.1 AS blob) ASC
244 do_test fuzz-1.16.4 {
246 DROP TABLE abc; DROP TABLE def; DROP TABLE ghi;
252 SELECT 'hardware', 56.1 NOTNULL, random()&0
254 SELECT ALL lower(~ EXISTS (
255 SELECT 1 NOT IN (SELECT ALL 1)
256 )), CAST(456 AS integer), -2147483647
258 SELECT DISTINCT -456, CAST(1 AS integer) ISNULL
259 FROM (SELECT ALL 2147483647, typeof(2147483649))
262 GROUP BY CAST(CAST('experiments' AS blob) AS blob)
269 SELECT -2147483649 << upper('fault' NOT IN (
275 SELECT 0 EXCEPT SELECT DISTINCT 'experiments' ORDER BY 1 ASC
280 (SELECT ALL -2147483648) NOT IN (
281 SELECT ALL 123456789.1234567899
282 ) IN (SELECT 2147483649)
284 ) NOT IN (SELECT ALL 'The')
290 # At one point the following INSERT statement caused an assert() to fail.
293 execsql { CREATE TABLE t1(a) }
295 INSERT INTO t1 VALUES(
296 CASE WHEN NULL THEN NULL ELSE ( SELECT 0 ORDER BY 456 ) END
299 } {1 {1st ORDER BY term out of range - should be between 1 and 1}}
301 execsql { DROP TABLE t1 }
304 #----------------------------------------------------------------
305 # Test some fuzzily generated expressions.
307 do_fuzzy_test fuzz-2 -template { SELECT [Expr] }
311 CREATE TABLE abc(a, b, c);
312 CREATE TABLE def(a, b, c);
313 CREATE TABLE ghi(a, b, c);
316 set ::TableList [list abc def ghi]
318 #----------------------------------------------------------------
319 # Test some fuzzily generated SELECT statements.
321 do_fuzzy_test fuzz-3.2 -template {[Select]}
323 #----------------------------------------------------------------
324 # Insert a small amount of data into the database and then run
325 # some more generated SELECT statements.
329 INSERT INTO abc VALUES(1, 2, 3);
330 INSERT INTO abc VALUES(4, 5, 6);
331 INSERT INTO abc VALUES(7, 8, 9);
332 INSERT INTO def VALUES(1, 2, 3);
333 INSERT INTO def VALUES(4, 5, 6);
334 INSERT INTO def VALUES(7, 8, 9);
335 INSERT INTO ghi VALUES(1, 2, 3);
336 INSERT INTO ghi VALUES(4, 5, 6);
337 INSERT INTO ghi VALUES(7, 8, 9);
338 CREATE INDEX abc_i ON abc(a, b, c);
339 CREATE INDEX def_i ON def(c, a, b);
340 CREATE INDEX ghi_i ON ghi(b, c, a);
343 do_fuzzy_test fuzz-4.2 -template {[Select]}
345 #----------------------------------------------------------------
346 # Test some fuzzy INSERT statements:
348 do_test fuzz-5.1 {execsql BEGIN} {}
349 do_fuzzy_test fuzz-5.2 -template {[Insert]} -errorlist table
350 integrity_check fuzz-5.2.integrity
351 do_test fuzz-5.3 {execsql COMMIT} {}
352 integrity_check fuzz-5.4.integrity
354 #----------------------------------------------------------------
355 # Now that there is data in the database, run some more SELECT
358 set ::ColumnList [list a b c]
359 set E {{no such col} {ambiguous column name}}
360 do_fuzzy_test fuzz-6.1 -template {[Select]} -errorlist $E
362 #----------------------------------------------------------------
363 # Run some SELECTs, INSERTs, UPDATEs and DELETEs in a transaction.
365 set E {{no such col} {ambiguous column name} {table}}
366 do_test fuzz-7.1 {execsql BEGIN} {}
367 do_fuzzy_test fuzz-7.2 -template {[Statement]} -errorlist $E
368 integrity_check fuzz-7.3.integrity
369 do_test fuzz-7.4 {execsql COMMIT} {}
370 integrity_check fuzz-7.5.integrity
372 #----------------------------------------------------------------
373 # Many CREATE and DROP TABLE statements:
375 set E [list table duplicate {no such col} {ambiguous column name} {use DROP}]
376 do_fuzzy_test fuzz-8.1 -template {[CreateOrDropTableOrView]} -errorlist $E