drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1 | # 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 | # | ||||
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame^] | 16 | # $Id: misuse.test,v 1.7 2004/09/30 13:43:14 drh Exp $ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 17 | |
18 | set testdir [file dirname $argv0] | ||||
19 | source $testdir/tester.tcl | ||||
20 | |||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 21 | proc 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 | |||||
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 38 | # Make sure the test logic works |
39 | # | ||||
40 | do_test misuse-1.1 { | ||||
41 | db close | ||||
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 42 | catch {file delete -force test2.db} |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 43 | set ::DB [sqlite3 db test2.db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 44 | execsql { |
45 | CREATE TABLE t1(a,b); | ||||
46 | INSERT INTO t1 VALUES(1,2); | ||||
47 | } | ||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 48 | catchsql2 { |
49 | SELECT * FROM t1 | ||||
50 | } | ||||
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 51 | } {0 {a b 1 2}} |
52 | do_test misuse-1.2 { | ||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 53 | catchsql2 { |
54 | SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1 | ||||
55 | } | ||||
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 56 | } {1 {no such function: x_coalesce}} |
57 | do_test misuse-1.3 { | ||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 58 | sqlite3_create_function $::DB |
59 | catchsql2 { | ||||
60 | SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1 | ||||
61 | } | ||||
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 62 | } {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 | # | ||||
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 67 | # 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 | # | ||||
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 71 | do_test misuse-1.4 { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 72 | catchsql2 { |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 73 | SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz; |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 74 | } |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 75 | } {0 {xyz {1 2}}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 76 | do_test misuse-1.5 { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 77 | catchsql2 {SELECT * FROM t1} |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 78 | } {0 {a b 1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 79 | do_test misuse-1.6 { |
80 | catchsql { | ||||
81 | SELECT * FROM t1 | ||||
82 | } | ||||
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 83 | } {0 {1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 84 | |
85 | # Attempt to register a new SQL function while an sqlite_exec() is active. | ||||
86 | # | ||||
87 | do_test misuse-2.1 { | ||||
88 | db close | ||||
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 89 | set ::DB [sqlite3 db test2.db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 90 | execsql { |
91 | SELECT * FROM t1 | ||||
92 | } | ||||
93 | } {1 2} | ||||
94 | do_test misuse-2.2 { | ||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 95 | catchsql2 {SELECT * FROM t1} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 96 | } {0 {a b 1 2}} |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame^] | 97 | |
98 | # We used to disallow creating new function from within an exec(). | ||||
99 | # But now this is acceptable. | ||||
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 100 | do_test misuse-2.3 { |
101 | set v [catch { | ||||
102 | db eval {SELECT * FROM t1} {} { | ||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 103 | sqlite3_create_function $::DB |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 104 | } |
105 | } msg] | ||||
106 | lappend v $msg | ||||
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame^] | 107 | } {0 {}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 108 | do_test misuse-2.4 { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 109 | catchsql2 {SELECT * FROM t1} |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame^] | 110 | } {0 {a b 1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 111 | do_test misuse-2.5 { |
112 | catchsql { | ||||
113 | SELECT * FROM t1 | ||||
114 | } | ||||
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame^] | 115 | } {0 {1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 116 | |
117 | # Attempt to register a new SQL aggregate while an sqlite_exec() is active. | ||||
118 | # | ||||
119 | do_test misuse-3.1 { | ||||
120 | db close | ||||
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 121 | set ::DB [sqlite3 db test2.db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 122 | execsql { |
123 | SELECT * FROM t1 | ||||
124 | } | ||||
125 | } {1 2} | ||||
126 | do_test misuse-3.2 { | ||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 127 | catchsql2 {SELECT * FROM t1} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 128 | } {0 {a b 1 2}} |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame^] | 129 | |
130 | # We used to disallow creating new function from within an exec(). | ||||
131 | # But now this is acceptable. | ||||
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 132 | do_test misuse-3.3 { |
133 | set v [catch { | ||||
134 | db eval {SELECT * FROM t1} {} { | ||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 135 | sqlite3_create_aggregate $::DB |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 136 | } |
137 | } msg] | ||||
138 | lappend v $msg | ||||
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame^] | 139 | } {0 {}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 140 | do_test misuse-3.4 { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 141 | catchsql2 {SELECT * FROM t1} |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame^] | 142 | } {0 {a b 1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 143 | do_test misuse-3.5 { |
144 | catchsql { | ||||
145 | SELECT * FROM t1 | ||||
146 | } | ||||
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame^] | 147 | } {0 {1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 148 | |
149 | # Attempt to close the database from an sqlite_exec callback. | ||||
150 | # | ||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 151 | # Update for v3: The db cannot be closed because there are active |
152 | # VMs. The sqlite3_close call would return SQLITE_BUSY. | ||||
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 153 | do_test misuse-4.1 { |
154 | db close | ||||
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 155 | set ::DB [sqlite3 db test2.db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 156 | execsql { |
157 | SELECT * FROM t1 | ||||
158 | } | ||||
159 | } {1 2} | ||||
160 | do_test misuse-4.2 { | ||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 161 | catchsql2 {SELECT * FROM t1} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 162 | } {0 {a b 1 2}} |
163 | do_test misuse-4.3 { | ||||
164 | set v [catch { | ||||
165 | db eval {SELECT * FROM t1} {} { | ||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 166 | set r [sqlite3_close $::DB] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 167 | } |
168 | } msg] | ||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 169 | lappend v $msg $r |
170 | } {0 {} SQLITE_BUSY} | ||||
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 171 | do_test misuse-4.4 { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 172 | sqlite3_close $::DB |
173 | catchsql2 {SELECT * FROM t1} | ||||
174 | } {1 {library routine called out of sequence}} | ||||
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 175 | do_test misuse-4.5 { |
176 | catchsql { | ||||
177 | SELECT * FROM t1 | ||||
178 | } | ||||
179 | } {1 {library routine called out of sequence}} | ||||
180 | |||||
181 | # Attempt to use a database after it has been closed. | ||||
182 | # | ||||
183 | do_test misuse-5.1 { | ||||
184 | db close | ||||
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 185 | set ::DB [sqlite3 db test2.db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 186 | execsql { |
187 | SELECT * FROM t1 | ||||
188 | } | ||||
189 | } {1 2} | ||||
190 | do_test misuse-5.2 { | ||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 191 | catchsql2 {SELECT * FROM t1} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 192 | } {0 {a b 1 2}} |
193 | do_test misuse-5.3 { | ||||
194 | db close | ||||
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 195 | set r [catch { |
196 | sqlite3_prepare $::DB {SELECT * FROM t1} -1 TAIL | ||||
197 | } msg] | ||||
198 | lappend r $msg | ||||
199 | } {1 {(21) library routine called out of sequence}} | ||||
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 200 | |
201 | finish_test |