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 | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 16 | # $Id: misuse.test,v 1.11 2006/01/03 00:33:50 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 |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 42 | catch {forcedelete test2.db} |
| 43 | catch {forcedelete test2.db-journal} |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 44 | sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 45 | execsql { |
| 46 | CREATE TABLE t1(a,b); |
| 47 | INSERT INTO t1 VALUES(1,2); |
| 48 | } |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 49 | catchsql2 { |
| 50 | SELECT * FROM t1 |
| 51 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 52 | } {0 {a b 1 2}} |
| 53 | do_test misuse-1.2 { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 54 | catchsql2 { |
| 55 | SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1 |
| 56 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 57 | } {1 {no such function: x_coalesce}} |
| 58 | do_test misuse-1.3 { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 59 | sqlite3_create_function $::DB |
| 60 | catchsql2 { |
| 61 | SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1 |
| 62 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 63 | } {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 | # |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 68 | # 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 | # |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 72 | ifcapable {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 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 79 | do_test misuse-1.5 { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 80 | catchsql2 {SELECT * FROM t1} |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 81 | } {0 {a b 1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 82 | do_test misuse-1.6 { |
| 83 | catchsql { |
| 84 | SELECT * FROM t1 |
| 85 | } |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame] | 86 | } {0 {1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 87 | |
| 88 | # Attempt to register a new SQL function while an sqlite_exec() is active. |
| 89 | # |
| 90 | do_test misuse-2.1 { |
| 91 | db close |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 92 | sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 93 | execsql { |
| 94 | SELECT * FROM t1 |
| 95 | } |
| 96 | } {1 2} |
| 97 | do_test misuse-2.2 { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 98 | catchsql2 {SELECT * FROM t1} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 99 | } {0 {a b 1 2}} |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 100 | |
| 101 | # We used to disallow creating new function from within an exec(). |
| 102 | # But now this is acceptable. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 103 | do_test misuse-2.3 { |
| 104 | set v [catch { |
| 105 | db eval {SELECT * FROM t1} {} { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 106 | sqlite3_create_function $::DB |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 107 | } |
| 108 | } msg] |
| 109 | lappend v $msg |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 110 | } {0 {}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 111 | do_test misuse-2.4 { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 112 | catchsql2 {SELECT * FROM t1} |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 113 | } {0 {a b 1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 114 | do_test misuse-2.5 { |
| 115 | catchsql { |
| 116 | SELECT * FROM t1 |
| 117 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 118 | } {0 {1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 119 | |
| 120 | # Attempt to register a new SQL aggregate while an sqlite_exec() is active. |
| 121 | # |
| 122 | do_test misuse-3.1 { |
| 123 | db close |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 124 | sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 125 | execsql { |
| 126 | SELECT * FROM t1 |
| 127 | } |
| 128 | } {1 2} |
| 129 | do_test misuse-3.2 { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 130 | catchsql2 {SELECT * FROM t1} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 131 | } {0 {a b 1 2}} |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 132 | |
| 133 | # We used to disallow creating new function from within an exec(). |
| 134 | # But now this is acceptable. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 135 | do_test misuse-3.3 { |
| 136 | set v [catch { |
| 137 | db eval {SELECT * FROM t1} {} { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 138 | sqlite3_create_aggregate $::DB |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 139 | } |
| 140 | } msg] |
| 141 | lappend v $msg |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 142 | } {0 {}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 143 | do_test misuse-3.4 { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 144 | catchsql2 {SELECT * FROM t1} |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 145 | } {0 {a b 1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 146 | do_test misuse-3.5 { |
| 147 | catchsql { |
| 148 | SELECT * FROM t1 |
| 149 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 150 | } {0 {1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 151 | |
| 152 | # Attempt to close the database from an sqlite_exec callback. |
| 153 | # |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 154 | # Update for v3: The db cannot be closed because there are active |
| 155 | # VMs. The sqlite3_close call would return SQLITE_BUSY. |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 156 | do_test misuse-4.1 { |
| 157 | db close |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 158 | sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 159 | execsql { |
| 160 | SELECT * FROM t1 |
| 161 | } |
| 162 | } {1 2} |
| 163 | do_test misuse-4.2 { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 164 | catchsql2 {SELECT * FROM t1} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 165 | } {0 {a b 1 2}} |
| 166 | do_test misuse-4.3 { |
| 167 | set v [catch { |
| 168 | db eval {SELECT * FROM t1} {} { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 169 | set r [sqlite3_close $::DB] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 170 | } |
| 171 | } msg] |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 172 | lappend v $msg $r |
| 173 | } {0 {} SQLITE_BUSY} |
dan | afcf9bd | 2014-01-23 14:44:08 +0000 | [diff] [blame] | 174 | |
| 175 | if {[clang_sanitize_address]==0} { |
| 176 | do_test misuse-4.4 { |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 177 | # Flush the TCL statement cache here, otherwise the sqlite3_close() will |
| 178 | # fail because there are still un-finalized() VDBEs. |
dan | afcf9bd | 2014-01-23 14:44:08 +0000 | [diff] [blame] | 179 | db cache flush |
| 180 | sqlite3_close $::DB |
| 181 | catchsql2 {SELECT * FROM t1} |
drh | ff4fa77 | 2017-07-10 12:07:53 +0000 | [diff] [blame] | 182 | } {1 {bad parameter or other API misuse}} |
dan | afcf9bd | 2014-01-23 14:44:08 +0000 | [diff] [blame] | 183 | do_test misuse-4.5 { |
| 184 | catchsql { |
| 185 | SELECT * FROM t1 |
| 186 | } |
drh | ff4fa77 | 2017-07-10 12:07:53 +0000 | [diff] [blame] | 187 | } {1 {bad parameter or other API misuse}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 188 | |
dan | afcf9bd | 2014-01-23 14:44:08 +0000 | [diff] [blame] | 189 | # Attempt to use a database after it has been closed. |
| 190 | # |
| 191 | do_test misuse-5.1 { |
| 192 | db close |
| 193 | sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db] |
| 194 | execsql { |
| 195 | SELECT * FROM t1 |
| 196 | } |
| 197 | } {1 2} |
| 198 | do_test misuse-5.2 { |
| 199 | catchsql2 {SELECT * FROM t1} |
| 200 | } {0 {a b 1 2}} |
| 201 | do_test misuse-5.3 { |
| 202 | db close |
| 203 | set r [catch { |
| 204 | sqlite3_prepare $::DB {SELECT * FROM t1} -1 TAIL |
| 205 | } msg] |
| 206 | lappend r $msg |
drh | ff4fa77 | 2017-07-10 12:07:53 +0000 | [diff] [blame] | 207 | } {1 {(21) bad parameter or other API misuse}} |
dan | afcf9bd | 2014-01-23 14:44:08 +0000 | [diff] [blame] | 208 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 209 | |
| 210 | finish_test |