blob: effe8bff029e00195cae854c38a4b115feff7ec6 [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#
drh6c626082004-11-14 21:56:29 +000016# $Id: misuse.test,v 1.8 2004/11/14 21:56:31 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}
drhef4ac8f2004-06-19 00:16:31 +000043 set ::DB [sqlite3 db test2.db]
drhc22bd472002-05-10 13:14:07 +000044 execsql {
45 CREATE TABLE t1(a,b);
46 INSERT INTO t1 VALUES(1,2);
47 }
danielk1977e35ee192004-06-26 09:50:11 +000048 catchsql2 {
49 SELECT * FROM t1
50 }
drhc22bd472002-05-10 13:14:07 +000051} {0 {a b 1 2}}
52do_test misuse-1.2 {
danielk1977e35ee192004-06-26 09:50:11 +000053 catchsql2 {
54 SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1
55 }
drhc22bd472002-05-10 13:14:07 +000056} {1 {no such function: x_coalesce}}
57do_test misuse-1.3 {
danielk1977e35ee192004-06-26 09:50:11 +000058 sqlite3_create_function $::DB
59 catchsql2 {
60 SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1
61 }
drhc22bd472002-05-10 13:14:07 +000062} {0 {xyz 1}}
63
64# Use the x_sqlite_exec() SQL function to simulate the effect of two
65# threads trying to use the same database at the same time.
66#
drhd1d9fc32004-01-07 19:24:48 +000067# It used to be prohibited to invoke sqlite_exec() from within a function,
68# but that has changed. The following tests used to cause errors but now
69# they do not.
70#
drh6c626082004-11-14 21:56:29 +000071ifcapable {utf16} {
72 do_test misuse-1.4 {
73 catchsql2 {
74 SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz;
75 }
76 } {0 {xyz {1 2}}}
77}
drhc22bd472002-05-10 13:14:07 +000078do_test misuse-1.5 {
danielk1977e35ee192004-06-26 09:50:11 +000079 catchsql2 {SELECT * FROM t1}
drhd1d9fc32004-01-07 19:24:48 +000080} {0 {a b 1 2}}
drhc22bd472002-05-10 13:14:07 +000081do_test misuse-1.6 {
82 catchsql {
83 SELECT * FROM t1
84 }
drhd1d9fc32004-01-07 19:24:48 +000085} {0 {1 2}}
drhc22bd472002-05-10 13:14:07 +000086
87# Attempt to register a new SQL function while an sqlite_exec() is active.
88#
89do_test misuse-2.1 {
90 db close
drhef4ac8f2004-06-19 00:16:31 +000091 set ::DB [sqlite3 db test2.db]
drhc22bd472002-05-10 13:14:07 +000092 execsql {
93 SELECT * FROM t1
94 }
95} {1 2}
96do_test misuse-2.2 {
danielk1977e35ee192004-06-26 09:50:11 +000097 catchsql2 {SELECT * FROM t1}
drhc22bd472002-05-10 13:14:07 +000098} {0 {a b 1 2}}
drhc60d0442004-09-30 13:43:13 +000099
100# We used to disallow creating new function from within an exec().
101# But now this is acceptable.
drhc22bd472002-05-10 13:14:07 +0000102do_test misuse-2.3 {
103 set v [catch {
104 db eval {SELECT * FROM t1} {} {
danielk1977e35ee192004-06-26 09:50:11 +0000105 sqlite3_create_function $::DB
drhc22bd472002-05-10 13:14:07 +0000106 }
107 } msg]
108 lappend v $msg
drhc60d0442004-09-30 13:43:13 +0000109} {0 {}}
drhc22bd472002-05-10 13:14:07 +0000110do_test misuse-2.4 {
danielk1977e35ee192004-06-26 09:50:11 +0000111 catchsql2 {SELECT * FROM t1}
drhc60d0442004-09-30 13:43:13 +0000112} {0 {a b 1 2}}
drhc22bd472002-05-10 13:14:07 +0000113do_test misuse-2.5 {
114 catchsql {
115 SELECT * FROM t1
116 }
drhc60d0442004-09-30 13:43:13 +0000117} {0 {1 2}}
drhc22bd472002-05-10 13:14:07 +0000118
119# Attempt to register a new SQL aggregate while an sqlite_exec() is active.
120#
121do_test misuse-3.1 {
122 db close
drhef4ac8f2004-06-19 00:16:31 +0000123 set ::DB [sqlite3 db test2.db]
drhc22bd472002-05-10 13:14:07 +0000124 execsql {
125 SELECT * FROM t1
126 }
127} {1 2}
128do_test misuse-3.2 {
danielk1977e35ee192004-06-26 09:50:11 +0000129 catchsql2 {SELECT * FROM t1}
drhc22bd472002-05-10 13:14:07 +0000130} {0 {a b 1 2}}
drhc60d0442004-09-30 13:43:13 +0000131
132# We used to disallow creating new function from within an exec().
133# But now this is acceptable.
drhc22bd472002-05-10 13:14:07 +0000134do_test misuse-3.3 {
135 set v [catch {
136 db eval {SELECT * FROM t1} {} {
danielk1977e35ee192004-06-26 09:50:11 +0000137 sqlite3_create_aggregate $::DB
drhc22bd472002-05-10 13:14:07 +0000138 }
139 } msg]
140 lappend v $msg
drhc60d0442004-09-30 13:43:13 +0000141} {0 {}}
drhc22bd472002-05-10 13:14:07 +0000142do_test misuse-3.4 {
danielk1977e35ee192004-06-26 09:50:11 +0000143 catchsql2 {SELECT * FROM t1}
drhc60d0442004-09-30 13:43:13 +0000144} {0 {a b 1 2}}
drhc22bd472002-05-10 13:14:07 +0000145do_test misuse-3.5 {
146 catchsql {
147 SELECT * FROM t1
148 }
drhc60d0442004-09-30 13:43:13 +0000149} {0 {1 2}}
drhc22bd472002-05-10 13:14:07 +0000150
151# Attempt to close the database from an sqlite_exec callback.
152#
danielk1977e35ee192004-06-26 09:50:11 +0000153# Update for v3: The db cannot be closed because there are active
154# VMs. The sqlite3_close call would return SQLITE_BUSY.
drhc22bd472002-05-10 13:14:07 +0000155do_test misuse-4.1 {
156 db close
drhef4ac8f2004-06-19 00:16:31 +0000157 set ::DB [sqlite3 db test2.db]
drhc22bd472002-05-10 13:14:07 +0000158 execsql {
159 SELECT * FROM t1
160 }
161} {1 2}
162do_test misuse-4.2 {
danielk1977e35ee192004-06-26 09:50:11 +0000163 catchsql2 {SELECT * FROM t1}
drhc22bd472002-05-10 13:14:07 +0000164} {0 {a b 1 2}}
165do_test misuse-4.3 {
166 set v [catch {
167 db eval {SELECT * FROM t1} {} {
danielk1977e35ee192004-06-26 09:50:11 +0000168 set r [sqlite3_close $::DB]
drhc22bd472002-05-10 13:14:07 +0000169 }
170 } msg]
danielk1977e35ee192004-06-26 09:50:11 +0000171 lappend v $msg $r
172} {0 {} SQLITE_BUSY}
drhc22bd472002-05-10 13:14:07 +0000173do_test misuse-4.4 {
danielk1977e35ee192004-06-26 09:50:11 +0000174 sqlite3_close $::DB
175 catchsql2 {SELECT * FROM t1}
176} {1 {library routine called out of sequence}}
drhc22bd472002-05-10 13:14:07 +0000177do_test misuse-4.5 {
178 catchsql {
179 SELECT * FROM t1
180 }
181} {1 {library routine called out of sequence}}
182
183# Attempt to use a database after it has been closed.
184#
185do_test misuse-5.1 {
186 db close
drhef4ac8f2004-06-19 00:16:31 +0000187 set ::DB [sqlite3 db test2.db]
drhc22bd472002-05-10 13:14:07 +0000188 execsql {
189 SELECT * FROM t1
190 }
191} {1 2}
192do_test misuse-5.2 {
danielk1977e35ee192004-06-26 09:50:11 +0000193 catchsql2 {SELECT * FROM t1}
drhc22bd472002-05-10 13:14:07 +0000194} {0 {a b 1 2}}
195do_test misuse-5.3 {
196 db close
danielk1977e35ee192004-06-26 09:50:11 +0000197 set r [catch {
198 sqlite3_prepare $::DB {SELECT * FROM t1} -1 TAIL
199 } msg]
200 lappend r $msg
201} {1 {(21) library routine called out of sequence}}
drhc22bd472002-05-10 13:14:07 +0000202
203finish_test