blob: 677368f9ff3e890064e5ae16a4a2cea6221d5bcd [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#
danielk1977e35ee192004-06-26 09:50:11 +000016# $Id: misuse.test,v 1.6 2004/06/26 09:50:12 danielk1977 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#
drhc22bd472002-05-10 13:14:07 +000071do_test misuse-1.4 {
danielk1977e35ee192004-06-26 09:50:11 +000072 catchsql2 {
drhd1d9fc32004-01-07 19:24:48 +000073 SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz;
danielk1977e35ee192004-06-26 09:50:11 +000074 }
drhd1d9fc32004-01-07 19:24:48 +000075} {0 {xyz {1 2}}}
drhc22bd472002-05-10 13:14:07 +000076do_test misuse-1.5 {
danielk1977e35ee192004-06-26 09:50:11 +000077 catchsql2 {SELECT * FROM t1}
drhd1d9fc32004-01-07 19:24:48 +000078} {0 {a b 1 2}}
drhc22bd472002-05-10 13:14:07 +000079do_test misuse-1.6 {
80 catchsql {
81 SELECT * FROM t1
82 }
drhd1d9fc32004-01-07 19:24:48 +000083} {0 {1 2}}
drhc22bd472002-05-10 13:14:07 +000084
85# Attempt to register a new SQL function while an sqlite_exec() is active.
86#
87do_test misuse-2.1 {
88 db close
drhef4ac8f2004-06-19 00:16:31 +000089 set ::DB [sqlite3 db test2.db]
drhc22bd472002-05-10 13:14:07 +000090 execsql {
91 SELECT * FROM t1
92 }
93} {1 2}
94do_test misuse-2.2 {
danielk1977e35ee192004-06-26 09:50:11 +000095 catchsql2 {SELECT * FROM t1}
drhc22bd472002-05-10 13:14:07 +000096} {0 {a b 1 2}}
97do_test misuse-2.3 {
98 set v [catch {
99 db eval {SELECT * FROM t1} {} {
danielk1977e35ee192004-06-26 09:50:11 +0000100 sqlite3_create_function $::DB
drhc22bd472002-05-10 13:14:07 +0000101 }
102 } msg]
103 lappend v $msg
104} {1 {library routine called out of sequence}}
105do_test misuse-2.4 {
danielk1977e35ee192004-06-26 09:50:11 +0000106 catchsql2 {SELECT * FROM t1}
107} {1 {library routine called out of sequence}}
drhc22bd472002-05-10 13:14:07 +0000108do_test misuse-2.5 {
109 catchsql {
110 SELECT * FROM t1
111 }
112} {1 {library routine called out of sequence}}
113
114# Attempt to register a new SQL aggregate while an sqlite_exec() is active.
115#
116do_test misuse-3.1 {
117 db close
drhef4ac8f2004-06-19 00:16:31 +0000118 set ::DB [sqlite3 db test2.db]
drhc22bd472002-05-10 13:14:07 +0000119 execsql {
120 SELECT * FROM t1
121 }
122} {1 2}
123do_test misuse-3.2 {
danielk1977e35ee192004-06-26 09:50:11 +0000124 catchsql2 {SELECT * FROM t1}
drhc22bd472002-05-10 13:14:07 +0000125} {0 {a b 1 2}}
126do_test misuse-3.3 {
127 set v [catch {
128 db eval {SELECT * FROM t1} {} {
danielk1977e35ee192004-06-26 09:50:11 +0000129 sqlite3_create_aggregate $::DB
drhc22bd472002-05-10 13:14:07 +0000130 }
131 } msg]
132 lappend v $msg
133} {1 {library routine called out of sequence}}
134do_test misuse-3.4 {
danielk1977e35ee192004-06-26 09:50:11 +0000135 catchsql2 {SELECT * FROM t1}
136} {1 {library routine called out of sequence}}
drhc22bd472002-05-10 13:14:07 +0000137do_test misuse-3.5 {
138 catchsql {
139 SELECT * FROM t1
140 }
141} {1 {library routine called out of sequence}}
142
143# Attempt to close the database from an sqlite_exec callback.
144#
danielk1977e35ee192004-06-26 09:50:11 +0000145# Update for v3: The db cannot be closed because there are active
146# VMs. The sqlite3_close call would return SQLITE_BUSY.
drhc22bd472002-05-10 13:14:07 +0000147do_test misuse-4.1 {
148 db close
drhef4ac8f2004-06-19 00:16:31 +0000149 set ::DB [sqlite3 db test2.db]
drhc22bd472002-05-10 13:14:07 +0000150 execsql {
151 SELECT * FROM t1
152 }
153} {1 2}
154do_test misuse-4.2 {
danielk1977e35ee192004-06-26 09:50:11 +0000155 catchsql2 {SELECT * FROM t1}
drhc22bd472002-05-10 13:14:07 +0000156} {0 {a b 1 2}}
157do_test misuse-4.3 {
158 set v [catch {
159 db eval {SELECT * FROM t1} {} {
danielk1977e35ee192004-06-26 09:50:11 +0000160 set r [sqlite3_close $::DB]
drhc22bd472002-05-10 13:14:07 +0000161 }
162 } msg]
danielk1977e35ee192004-06-26 09:50:11 +0000163 lappend v $msg $r
164} {0 {} SQLITE_BUSY}
drhc22bd472002-05-10 13:14:07 +0000165do_test misuse-4.4 {
danielk1977e35ee192004-06-26 09:50:11 +0000166 sqlite3_close $::DB
167 catchsql2 {SELECT * FROM t1}
168} {1 {library routine called out of sequence}}
drhc22bd472002-05-10 13:14:07 +0000169do_test misuse-4.5 {
170 catchsql {
171 SELECT * FROM t1
172 }
173} {1 {library routine called out of sequence}}
174
175# Attempt to use a database after it has been closed.
176#
177do_test misuse-5.1 {
178 db close
drhef4ac8f2004-06-19 00:16:31 +0000179 set ::DB [sqlite3 db test2.db]
drhc22bd472002-05-10 13:14:07 +0000180 execsql {
181 SELECT * FROM t1
182 }
183} {1 2}
184do_test misuse-5.2 {
danielk1977e35ee192004-06-26 09:50:11 +0000185 catchsql2 {SELECT * FROM t1}
drhc22bd472002-05-10 13:14:07 +0000186} {0 {a b 1 2}}
187do_test misuse-5.3 {
188 db close
danielk1977e35ee192004-06-26 09:50:11 +0000189 set r [catch {
190 sqlite3_prepare $::DB {SELECT * FROM t1} -1 TAIL
191 } msg]
192 lappend r $msg
193} {1 {(21) library routine called out of sequence}}
drhc22bd472002-05-10 13:14:07 +0000194
195finish_test