blob: 7f26280e8aa372ae57e79d2005463112be88334e [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#
drhd1d9fc32004-01-07 19:24:48 +000016# $Id: misuse.test,v 1.4 2004/01/07 19:24:48 drh Exp $
drhc22bd472002-05-10 13:14:07 +000017
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# Make sure the test logic works
22#
23do_test misuse-1.1 {
24 db close
drhc977f7f2002-05-21 11:38:11 +000025 catch {file delete -force test2.db}
drh4d908a32002-05-10 14:37:30 +000026 set ::DB [sqlite db test2.db]
drhc22bd472002-05-10 13:14:07 +000027 execsql {
28 CREATE TABLE t1(a,b);
29 INSERT INTO t1 VALUES(1,2);
30 }
31 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
32} {0 {a b 1 2}}
33do_test misuse-1.2 {
34 sqlite_exec_printf $::DB {SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1} {}
35} {1 {no such function: x_coalesce}}
36do_test misuse-1.3 {
37 sqlite_create_function $::DB
38 sqlite_exec_printf $::DB {SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1} {}
39} {0 {xyz 1}}
40
41# Use the x_sqlite_exec() SQL function to simulate the effect of two
42# threads trying to use the same database at the same time.
43#
drhd1d9fc32004-01-07 19:24:48 +000044# It used to be prohibited to invoke sqlite_exec() from within a function,
45# but that has changed. The following tests used to cause errors but now
46# they do not.
47#
drhc22bd472002-05-10 13:14:07 +000048do_test misuse-1.4 {
49 sqlite_exec_printf $::DB {
drhd1d9fc32004-01-07 19:24:48 +000050 SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz;
drhc22bd472002-05-10 13:14:07 +000051 } {}
drhd1d9fc32004-01-07 19:24:48 +000052} {0 {xyz {1 2}}}
drhc22bd472002-05-10 13:14:07 +000053do_test misuse-1.5 {
54 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
drhd1d9fc32004-01-07 19:24:48 +000055} {0 {a b 1 2}}
drhc22bd472002-05-10 13:14:07 +000056do_test misuse-1.6 {
57 catchsql {
58 SELECT * FROM t1
59 }
drhd1d9fc32004-01-07 19:24:48 +000060} {0 {1 2}}
drhc22bd472002-05-10 13:14:07 +000061
62# Attempt to register a new SQL function while an sqlite_exec() is active.
63#
64do_test misuse-2.1 {
65 db close
drh4d908a32002-05-10 14:37:30 +000066 set ::DB [sqlite db test2.db]
drhc22bd472002-05-10 13:14:07 +000067 execsql {
68 SELECT * FROM t1
69 }
70} {1 2}
71do_test misuse-2.2 {
72 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
73} {0 {a b 1 2}}
74do_test misuse-2.3 {
75 set v [catch {
76 db eval {SELECT * FROM t1} {} {
77 sqlite_create_function $::DB
78 }
79 } msg]
80 lappend v $msg
81} {1 {library routine called out of sequence}}
82do_test misuse-2.4 {
83 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
84} {21 {library routine called out of sequence}}
85do_test misuse-2.5 {
86 catchsql {
87 SELECT * FROM t1
88 }
89} {1 {library routine called out of sequence}}
90
91# Attempt to register a new SQL aggregate while an sqlite_exec() is active.
92#
93do_test misuse-3.1 {
94 db close
drh4d908a32002-05-10 14:37:30 +000095 set ::DB [sqlite db test2.db]
drhc22bd472002-05-10 13:14:07 +000096 execsql {
97 SELECT * FROM t1
98 }
99} {1 2}
100do_test misuse-3.2 {
101 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
102} {0 {a b 1 2}}
103do_test misuse-3.3 {
104 set v [catch {
105 db eval {SELECT * FROM t1} {} {
106 sqlite_create_aggregate $::DB
107 }
108 } msg]
109 lappend v $msg
110} {1 {library routine called out of sequence}}
111do_test misuse-3.4 {
112 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
113} {21 {library routine called out of sequence}}
114do_test misuse-3.5 {
115 catchsql {
116 SELECT * FROM t1
117 }
118} {1 {library routine called out of sequence}}
119
120# Attempt to close the database from an sqlite_exec callback.
121#
122do_test misuse-4.1 {
123 db close
drh4d908a32002-05-10 14:37:30 +0000124 set ::DB [sqlite db test2.db]
drhc22bd472002-05-10 13:14:07 +0000125 execsql {
126 SELECT * FROM t1
127 }
128} {1 2}
129do_test misuse-4.2 {
130 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
131} {0 {a b 1 2}}
132do_test misuse-4.3 {
133 set v [catch {
134 db eval {SELECT * FROM t1} {} {
135 sqlite_close $::DB
136 }
137 } msg]
138 lappend v $msg
139} {1 {library routine called out of sequence}}
140do_test misuse-4.4 {
141 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
142} {21 {library routine called out of sequence}}
143do_test misuse-4.5 {
144 catchsql {
145 SELECT * FROM t1
146 }
147} {1 {library routine called out of sequence}}
148
149# Attempt to use a database after it has been closed.
150#
151do_test misuse-5.1 {
152 db close
drh4d908a32002-05-10 14:37:30 +0000153 set ::DB [sqlite db test2.db]
drhc22bd472002-05-10 13:14:07 +0000154 execsql {
155 SELECT * FROM t1
156 }
157} {1 2}
158do_test misuse-5.2 {
159 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
160} {0 {a b 1 2}}
161do_test misuse-5.3 {
162 db close
163 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
164} {21 {library routine called out of sequence}}
165
166finish_test