fuzz.test 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. # 2007 May 10
  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 generating semi-random strings of SQL
  13. # (a.k.a. "fuzz") and sending it into the parser to try to
  14. # generate errors.
  15. #
  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.
  19. #
  20. # The most complicated trees are for SELECT statements.
  21. #
  22. # $Id: fuzz.test,v 1.19 2009/04/28 11:10:39 danielk1977 Exp $
  23. set testdir [file dirname $argv0]
  24. source $testdir/tester.tcl
  25. set ::REPEATS 5000
  26. # If running quick.test, don't do so many iterations.
  27. if {[info exists ::G(isquick)]} {
  28. if {$::G(isquick)} { set ::REPEATS 20 }
  29. }
  30. source $testdir/fuzz_common.tcl
  31. expr srand(0)
  32. #----------------------------------------------------------------
  33. # These tests caused errors that were first caught by the tests
  34. # in this file. They are still here.
  35. do_test fuzz-1.1 {
  36. execsql {
  37. SELECT 'abc' LIKE X'ABCD';
  38. }
  39. } {0}
  40. do_test fuzz-1.2 {
  41. execsql {
  42. SELECT 'abc' LIKE zeroblob(10);
  43. }
  44. } {0}
  45. do_test fuzz-1.3 {
  46. execsql {
  47. SELECT zeroblob(10) LIKE 'abc';
  48. }
  49. } {0}
  50. do_test fuzz-1.4 {
  51. execsql {
  52. SELECT (- -21) % NOT (456 LIKE zeroblob(10));
  53. }
  54. } {0}
  55. do_test fuzz-1.5 {
  56. execsql {
  57. SELECT (SELECT (
  58. SELECT (SELECT -2147483648) FROM (SELECT 1) ORDER BY 1
  59. ))
  60. }
  61. } {-2147483648}
  62. do_test fuzz-1.6 {
  63. execsql {
  64. SELECT 'abc', zeroblob(1) FROM (SELECT 1) ORDER BY 1
  65. }
  66. } [execsql {SELECT 'abc', zeroblob(1)}]
  67. do_test fuzz-1.7 {
  68. execsql {
  69. SELECT ( SELECT zeroblob(1000) FROM (
  70. SELECT * FROM (SELECT 'first') ORDER BY NOT 'in')
  71. )
  72. }
  73. } [execsql {SELECT zeroblob(1000)}]
  74. do_test fuzz-1.8 {
  75. # Problems with opcode OP_ToText (did not account for MEM_Zero).
  76. # Also MemExpandBlob() was marking expanded blobs as nul-terminated.
  77. # They are not.
  78. execsql {
  79. SELECT CAST(zeroblob(1000) AS text);
  80. }
  81. } {{}}
  82. do_test fuzz-1.9 {
  83. # This was causing a NULL pointer dereference of Expr.pList.
  84. execsql {
  85. SELECT 1 FROM (SELECT * FROM sqlite_master WHERE random())
  86. }
  87. } {}
  88. do_test fuzz-1.10 {
  89. # Bug in calculation of Parse.ckOffset causing an assert()
  90. # to fail. Probably harmless.
  91. execsql {
  92. SELECT coalesce(1, substr( 1, 2, length('in' IN (SELECT 1))))
  93. }
  94. } {1}
  95. do_test fuzz-1.11 {
  96. # The literals (A, B, C, D) are not important, they are just used
  97. # to make the EXPLAIN output easier to read.
  98. #
  99. # The problem here is that the EXISTS(...) expression leaves an
  100. # extra value on the VDBE stack. This is confusing the parent and
  101. # leads to an assert() failure when OP_Insert encounters an integer
  102. # when it expects a record blob.
  103. #
  104. # Update: Any query with (LIMIT 0) was leaking stack.
  105. #
  106. execsql {
  107. SELECT 'A' FROM (SELECT 'B') ORDER BY EXISTS (
  108. SELECT 'C' FROM (SELECT 'D' LIMIT 0)
  109. )
  110. }
  111. } {A}
  112. do_test fuzz-1.12.1 {
  113. # Create a table with a single row.
  114. execsql {
  115. CREATE TABLE abc(b);
  116. INSERT INTO abc VALUES('ABCDE');
  117. }
  118. # The following query was crashing. The later subquery (in the FROM)
  119. # clause was flattened into the parent, but the code was not repairng
  120. # the "b" reference in the other sub-query. When the query was executed,
  121. # that "b" refered to a non-existant vdbe table-cursor.
  122. #
  123. execsql {
  124. SELECT 1 IN ( SELECT b UNION SELECT 1 ) FROM (SELECT b FROM abc);
  125. }
  126. } {1}
  127. do_test fuzz-1.12.2 {
  128. # Clean up after the previous query.
  129. execsql {
  130. DROP TABLE abc;
  131. }
  132. } {}
  133. do_test fuzz-1.13 {
  134. # The problem here was that when there were more expressions in
  135. # the ORDER BY list than the result-set list. The temporary b-tree
  136. # used for sorting was being misconfigured in this case.
  137. #
  138. execsql {
  139. SELECT 'abcd' UNION SELECT 'efgh' ORDER BY 1 ASC, 1 ASC;
  140. }
  141. } {abcd efgh}
  142. do_test fuzz-1.14.1 {
  143. execsql {
  144. CREATE TABLE abc(a, b, c);
  145. INSERT INTO abc VALUES(123, 456, 789);
  146. }
  147. # The [a] reference in the sub-select was causing a problem. Because
  148. # the internal walkSelectExpr() function was not considering compound
  149. # SELECT operators.
  150. execsql {
  151. SELECT 1 FROM abc
  152. GROUP BY c HAVING EXISTS (SELECT a UNION SELECT 123);
  153. }
  154. } {1}
  155. do_test fuzz-1.14.2 {
  156. execsql {
  157. DROP TABLE abc;
  158. }
  159. } {}
  160. # Making sure previously discovered errors have been fixed.
  161. #
  162. do_test fuzz-1.15 {
  163. execsql {
  164. SELECT hex(CAST(zeroblob(1000) AS integer))
  165. }
  166. } {30}
  167. do_test fuzz-1.16.1 {
  168. execsql {
  169. CREATE TABLE abc(a, b, c);
  170. CREATE TABLE def(a, b, c);
  171. CREATE TABLE ghi(a, b, c);
  172. }
  173. } {}
  174. do_test fuzz-1.16.2 {
  175. catchsql {
  176. SELECT DISTINCT EXISTS(
  177. SELECT 1
  178. FROM (
  179. SELECT C FROM (SELECT 1)
  180. )
  181. WHERE (SELECT c)
  182. )
  183. FROM abc
  184. }
  185. } {0 {}}
  186. do_test fuzz-1.16.3 {
  187. catchsql {
  188. SELECT DISTINCT substr(-456 ISNULL,zeroblob(1000), EXISTS(
  189. SELECT DISTINCT EXISTS(
  190. SELECT DISTINCT b FROM abc
  191. ORDER BY EXISTS (
  192. SELECT DISTINCT 2147483647 UNION ALL SELECT -2147483648
  193. ) ASC
  194. )
  195. FROM (
  196. SELECT c, c FROM (
  197. SELECT 456, 'injection' ORDER BY 56.1 ASC, -56.1 DESC
  198. )
  199. )
  200. GROUP BY (SELECT ALL (SELECT DISTINCT 'hardware'))
  201. HAVING (
  202. SELECT DISTINCT c
  203. FROM (
  204. SELECT ALL -2147483648, 'experiments'
  205. ORDER BY -56.1 ASC, -56.1 DESC
  206. )
  207. GROUP BY (SELECT DISTINCT 456) IN
  208. (SELECT DISTINCT 'injection') NOT IN (SELECT ALL -456)
  209. HAVING EXISTS (
  210. SELECT ALL 'injection'
  211. )
  212. )
  213. UNION ALL
  214. SELECT a IN (
  215. SELECT -2147483647
  216. UNION ALL
  217. SELECT ALL 'injection'
  218. )
  219. FROM sqlite_master
  220. ) -- end EXISTS
  221. ) /* end SUBSTR() */, c NOTNULL ISNULL
  222. FROM abc
  223. ORDER BY CAST(-56.1 AS blob) ASC
  224. }
  225. } {0 {}}
  226. do_test fuzz-1.16.4 {
  227. execsql {
  228. DROP TABLE abc; DROP TABLE def; DROP TABLE ghi;
  229. }
  230. } {}
  231. do_test fuzz-1.17 {
  232. catchsql {
  233. SELECT 'hardware', 56.1 NOTNULL, random()&0
  234. FROM (
  235. SELECT ALL lower(~ EXISTS (
  236. SELECT 1 NOT IN (SELECT ALL 1)
  237. )), CAST(456 AS integer), -2147483647
  238. FROM (
  239. SELECT DISTINCT -456, CAST(1 AS integer) ISNULL
  240. FROM (SELECT ALL 2147483647, typeof(2147483649))
  241. )
  242. )
  243. GROUP BY CAST(CAST('experiments' AS blob) AS blob)
  244. HAVING random()
  245. }
  246. } {0 {hardware 1 0}}
  247. do_test fuzz-1.18 {
  248. catchsql {
  249. SELECT -2147483649 << upper('fault' NOT IN (
  250. SELECT ALL (
  251. SELECT ALL -1
  252. ORDER BY -2147483649
  253. LIMIT (
  254. SELECT ALL (
  255. SELECT 0 EXCEPT SELECT DISTINCT 'experiments' ORDER BY 1 ASC
  256. )
  257. )
  258. OFFSET EXISTS (
  259. SELECT ALL
  260. (SELECT ALL -2147483648) NOT IN (
  261. SELECT ALL 123456789.1234567899
  262. ) IN (SELECT 2147483649)
  263. FROM sqlite_master
  264. ) NOT IN (SELECT ALL 'The')
  265. )
  266. ))
  267. }
  268. } {0 -4294967298}
  269. # At one point the following INSERT statement caused an assert() to fail.
  270. #
  271. do_test fuzz-1.19 {
  272. execsql { CREATE TABLE t1(a) }
  273. catchsql {
  274. INSERT INTO t1 VALUES(
  275. CASE WHEN NULL THEN NULL ELSE ( SELECT 0 ORDER BY 456 ) END
  276. )
  277. }
  278. } {1 {1st ORDER BY term out of range - should be between 1 and 1}}
  279. do_test fuzz-1.20 {
  280. execsql { DROP TABLE t1 }
  281. } {}
  282. #----------------------------------------------------------------
  283. # Test some fuzzily generated expressions.
  284. #
  285. do_fuzzy_test fuzz-2 -template { SELECT [Expr] }
  286. do_test fuzz-3.1 {
  287. execsql {
  288. CREATE TABLE abc(a, b, c);
  289. CREATE TABLE def(a, b, c);
  290. CREATE TABLE ghi(a, b, c);
  291. }
  292. } {}
  293. set ::TableList [list abc def ghi]
  294. #----------------------------------------------------------------
  295. # Test some fuzzily generated SELECT statements.
  296. #
  297. do_fuzzy_test fuzz-3.2 -template {[Select]}
  298. #----------------------------------------------------------------
  299. # Insert a small amount of data into the database and then run
  300. # some more generated SELECT statements.
  301. #
  302. do_test fuzz-4.1 {
  303. execsql {
  304. INSERT INTO abc VALUES(1, 2, 3);
  305. INSERT INTO abc VALUES(4, 5, 6);
  306. INSERT INTO abc VALUES(7, 8, 9);
  307. INSERT INTO def VALUES(1, 2, 3);
  308. INSERT INTO def VALUES(4, 5, 6);
  309. INSERT INTO def VALUES(7, 8, 9);
  310. INSERT INTO ghi VALUES(1, 2, 3);
  311. INSERT INTO ghi VALUES(4, 5, 6);
  312. INSERT INTO ghi VALUES(7, 8, 9);
  313. CREATE INDEX abc_i ON abc(a, b, c);
  314. CREATE INDEX def_i ON def(c, a, b);
  315. CREATE INDEX ghi_i ON ghi(b, c, a);
  316. }
  317. } {}
  318. do_fuzzy_test fuzz-4.2 -template {[Select]}
  319. #----------------------------------------------------------------
  320. # Test some fuzzy INSERT statements:
  321. #
  322. do_test fuzz-5.1 {execsql BEGIN} {}
  323. do_fuzzy_test fuzz-5.2 -template {[Insert]} -errorlist table
  324. integrity_check fuzz-5.2.integrity
  325. do_test fuzz-5.3 {execsql COMMIT} {}
  326. integrity_check fuzz-5.4.integrity
  327. #----------------------------------------------------------------
  328. # Now that there is data in the database, run some more SELECT
  329. # statements
  330. #
  331. set ::ColumnList [list a b c]
  332. set E {{no such col} {ambiguous column name}}
  333. do_fuzzy_test fuzz-6.1 -template {[Select]} -errorlist $E
  334. #----------------------------------------------------------------
  335. # Run some SELECTs, INSERTs, UPDATEs and DELETEs in a transaction.
  336. #
  337. set E {{no such col} {ambiguous column name} {table}}
  338. do_test fuzz-7.1 {execsql BEGIN} {}
  339. do_fuzzy_test fuzz-7.2 -template {[Statement]} -errorlist $E
  340. integrity_check fuzz-7.3.integrity
  341. do_test fuzz-7.4 {execsql COMMIT} {}
  342. integrity_check fuzz-7.5.integrity
  343. #----------------------------------------------------------------
  344. # Many CREATE and DROP TABLE statements:
  345. #
  346. set E [list table duplicate {no such col} {ambiguous column name} {use DROP}]
  347. do_fuzzy_test fuzz-8.1 -template {[CreateOrDropTableOrView]} -errorlist $E
  348. close $::log
  349. finish_test