blob: 3734aa01c2256f3a4f12bcaaa362d663d407cbd6 [file] [log] [blame]
drhc22bd472002-05-10 13:14:07 +00001# 2002 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.
12#
13# This file implements tests for the SQLITE_MISUSE detection logic.
14# This test file leaks memory and file descriptors.
15#
drhdddca282006-01-03 00:33:50 +000016# $Id: misuse.test,v 1.11 2006/01/03 00:33:50 drh Exp $
drhc22bd472002-05-10 13:14:07 +000017
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
danielk1977e35ee192004-06-26 09:50:11 +000021proc catchsql2 {sql} {
22 set r [
23 catch {
24 set res [list]
25 db eval $sql data {
26 if { $res==[list] } {
27 foreach f $data(*) {lappend res $f}
28 }
29 foreach f $data(*) {lappend res $data($f)}
30 }
31 set res
32 } msg
33 ]
34 lappend r $msg
35}
36
37
drhc22bd472002-05-10 13:14:07 +000038# Make sure the test logic works
39#
40do_test misuse-1.1 {
41 db close
drhc977f7f2002-05-21 11:38:11 +000042 catch {file delete -force test2.db}
danielk19774e17d142005-01-16 09:06:33 +000043 catch {file delete -force test2.db-journal}
drhdddca282006-01-03 00:33:50 +000044 sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
drhc22bd472002-05-10 13:14:07 +000045 execsql {
46 CREATE TABLE t1(a,b);
47 INSERT INTO t1 VALUES(1,2);
48 }
danielk1977e35ee192004-06-26 09:50:11 +000049 catchsql2 {
50 SELECT * FROM t1
51 }
drhc22bd472002-05-10 13:14:07 +000052} {0 {a b 1 2}}
53do_test misuse-1.2 {
danielk1977e35ee192004-06-26 09:50:11 +000054 catchsql2 {
55 SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1
56 }
drhc22bd472002-05-10 13:14:07 +000057} {1 {no such function: x_coalesce}}
58do_test misuse-1.3 {
danielk1977e35ee192004-06-26 09:50:11 +000059 sqlite3_create_function $::DB
60 catchsql2 {
61 SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1
62 }
drhc22bd472002-05-10 13:14:07 +000063} {0 {xyz 1}}
64
65# Use the x_sqlite_exec() SQL function to simulate the effect of two
66# threads trying to use the same database at the same time.
67#
drhd1d9fc32004-01-07 19:24:48 +000068# It used to be prohibited to invoke sqlite_exec() from within a function,
69# but that has changed. The following tests used to cause errors but now
70# they do not.
71#
drh6c626082004-11-14 21:56:29 +000072ifcapable {utf16} {
73 do_test misuse-1.4 {
74 catchsql2 {
75 SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz;
76 }
77 } {0 {xyz {1 2}}}
78}
drhc22bd472002-05-10 13:14:07 +000079do_test misuse-1.5 {
danielk1977e35ee192004-06-26 09:50:11 +000080 catchsql2 {SELECT * FROM t1}
drhd1d9fc32004-01-07 19:24:48 +000081} {0 {a b 1 2}}
drhc22bd472002-05-10 13:14:07 +000082do_test misuse-1.6 {
83 catchsql {
84 SELECT * FROM t1
85 }
drhd1d9fc32004-01-07 19:24:48 +000086} {0 {1 2}}
drhc22bd472002-05-10 13:14:07 +000087
88# Attempt to register a new SQL function while an sqlite_exec() is active.
89#
90do_test misuse-2.1 {
91 db close
drhdddca282006-01-03 00:33:50 +000092 sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
drhc22bd472002-05-10 13:14:07 +000093 execsql {
94 SELECT * FROM t1
95 }
96} {1 2}
97do_test misuse-2.2 {
danielk1977e35ee192004-06-26 09:50:11 +000098 catchsql2 {SELECT * FROM t1}
drhc22bd472002-05-10 13:14:07 +000099} {0 {a b 1 2}}
drhc60d0442004-09-30 13:43:13 +0000100
101# We used to disallow creating new function from within an exec().
102# But now this is acceptable.
drhc22bd472002-05-10 13:14:07 +0000103do_test misuse-2.3 {
104 set v [catch {
105 db eval {SELECT * FROM t1} {} {
danielk1977e35ee192004-06-26 09:50:11 +0000106 sqlite3_create_function $::DB
drhc22bd472002-05-10 13:14:07 +0000107 }
108 } msg]
109 lappend v $msg
drhc60d0442004-09-30 13:43:13 +0000110} {0 {}}
drhc22bd472002-05-10 13:14:07 +0000111do_test misuse-2.4 {
danielk1977e35ee192004-06-26 09:50:11 +0000112 catchsql2 {SELECT * FROM t1}
drhc60d0442004-09-30 13:43:13 +0000113} {0 {a b 1 2}}
drhc22bd472002-05-10 13:14:07 +0000114do_test misuse-2.5 {
115 catchsql {
116 SELECT * FROM t1
117 }
drhc60d0442004-09-30 13:43:13 +0000118} {0 {1 2}}
drhc22bd472002-05-10 13:14:07 +0000119
120# Attempt to register a new SQL aggregate while an sqlite_exec() is active.
121#
122do_test misuse-3.1 {
123 db close
drhdddca282006-01-03 00:33:50 +0000124 sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
drhc22bd472002-05-10 13:14:07 +0000125 execsql {
126 SELECT * FROM t1
127 }
128} {1 2}
129do_test misuse-3.2 {
danielk1977e35ee192004-06-26 09:50:11 +0000130 catchsql2 {SELECT * FROM t1}
drhc22bd472002-05-10 13:14:07 +0000131} {0 {a b 1 2}}
drhc60d0442004-09-30 13:43:13 +0000132
133# We used to disallow creating new function from within an exec().
134# But now this is acceptable.
drhc22bd472002-05-10 13:14:07 +0000135do_test misuse-3.3 {
136 set v [catch {
137 db eval {SELECT * FROM t1} {} {
danielk1977e35ee192004-06-26 09:50:11 +0000138 sqlite3_create_aggregate $::DB
drhc22bd472002-05-10 13:14:07 +0000139 }
140 } msg]
141 lappend v $msg
drhc60d0442004-09-30 13:43:13 +0000142} {0 {}}
drhc22bd472002-05-10 13:14:07 +0000143do_test misuse-3.4 {
danielk1977e35ee192004-06-26 09:50:11 +0000144 catchsql2 {SELECT * FROM t1}
drhc60d0442004-09-30 13:43:13 +0000145} {0 {a b 1 2}}
drhc22bd472002-05-10 13:14:07 +0000146do_test misuse-3.5 {
147 catchsql {
148 SELECT * FROM t1
149 }
drhc60d0442004-09-30 13:43:13 +0000150} {0 {1 2}}
drhc22bd472002-05-10 13:14:07 +0000151
152# Attempt to close the database from an sqlite_exec callback.
153#
danielk1977e35ee192004-06-26 09:50:11 +0000154# Update for v3: The db cannot be closed because there are active
155# VMs. The sqlite3_close call would return SQLITE_BUSY.
drhc22bd472002-05-10 13:14:07 +0000156do_test misuse-4.1 {
157 db close
drhdddca282006-01-03 00:33:50 +0000158 sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
drhc22bd472002-05-10 13:14:07 +0000159 execsql {
160 SELECT * FROM t1
161 }
162} {1 2}
163do_test misuse-4.2 {
danielk1977e35ee192004-06-26 09:50:11 +0000164 catchsql2 {SELECT * FROM t1}
drhc22bd472002-05-10 13:14:07 +0000165} {0 {a b 1 2}}
166do_test misuse-4.3 {
167 set v [catch {
168 db eval {SELECT * FROM t1} {} {
danielk1977e35ee192004-06-26 09:50:11 +0000169 set r [sqlite3_close $::DB]
drhc22bd472002-05-10 13:14:07 +0000170 }
171 } msg]
danielk1977e35ee192004-06-26 09:50:11 +0000172 lappend v $msg $r
173} {0 {} SQLITE_BUSY}
drhc22bd472002-05-10 13:14:07 +0000174do_test misuse-4.4 {
danielk1977a21c6b62005-01-24 10:25:59 +0000175 # Flush the TCL statement cache here, otherwise the sqlite3_close() will
176 # fail because there are still un-finalized() VDBEs.
177 db cache flush
danielk1977e35ee192004-06-26 09:50:11 +0000178 sqlite3_close $::DB
179 catchsql2 {SELECT * FROM t1}
180} {1 {library routine called out of sequence}}
drhc22bd472002-05-10 13:14:07 +0000181do_test misuse-4.5 {
182 catchsql {
183 SELECT * FROM t1
184 }
185} {1 {library routine called out of sequence}}
186
187# Attempt to use a database after it has been closed.
188#
189do_test misuse-5.1 {
190 db close
drhdddca282006-01-03 00:33:50 +0000191 sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
drhc22bd472002-05-10 13:14:07 +0000192 execsql {
193 SELECT * FROM t1
194 }
195} {1 2}
196do_test misuse-5.2 {
danielk1977e35ee192004-06-26 09:50:11 +0000197 catchsql2 {SELECT * FROM t1}
drhc22bd472002-05-10 13:14:07 +0000198} {0 {a b 1 2}}
199do_test misuse-5.3 {
200 db close
danielk1977e35ee192004-06-26 09:50:11 +0000201 set r [catch {
202 sqlite3_prepare $::DB {SELECT * FROM t1} -1 TAIL
203 } msg]
204 lappend r $msg
205} {1 {(21) library routine called out of sequence}}
drhc22bd472002-05-10 13:14:07 +0000206
207finish_test