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 | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame^] | 16 | # $Id: misuse.test,v 1.4 2004/01/07 19:24:48 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 | |
| 21 | # Make sure the test logic works |
| 22 | # |
| 23 | do_test misuse-1.1 { |
| 24 | db close |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 25 | catch {file delete -force test2.db} |
drh | 4d908a3 | 2002-05-10 14:37:30 +0000 | [diff] [blame] | 26 | set ::DB [sqlite db test2.db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 27 | 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}} |
| 33 | do_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}} |
| 36 | do_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 | # |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame^] | 44 | # 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 | # |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 48 | do_test misuse-1.4 { |
| 49 | sqlite_exec_printf $::DB { |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame^] | 50 | SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 51 | } {} |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame^] | 52 | } {0 {xyz {1 2}}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 53 | do_test misuse-1.5 { |
| 54 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame^] | 55 | } {0 {a b 1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 56 | do_test misuse-1.6 { |
| 57 | catchsql { |
| 58 | SELECT * FROM t1 |
| 59 | } |
drh | d1d9fc3 | 2004-01-07 19:24:48 +0000 | [diff] [blame^] | 60 | } {0 {1 2}} |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 61 | |
| 62 | # Attempt to register a new SQL function while an sqlite_exec() is active. |
| 63 | # |
| 64 | do_test misuse-2.1 { |
| 65 | db close |
drh | 4d908a3 | 2002-05-10 14:37:30 +0000 | [diff] [blame] | 66 | set ::DB [sqlite db test2.db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 67 | execsql { |
| 68 | SELECT * FROM t1 |
| 69 | } |
| 70 | } {1 2} |
| 71 | do_test misuse-2.2 { |
| 72 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 73 | } {0 {a b 1 2}} |
| 74 | do_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}} |
| 82 | do_test misuse-2.4 { |
| 83 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 84 | } {21 {library routine called out of sequence}} |
| 85 | do_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 | # |
| 93 | do_test misuse-3.1 { |
| 94 | db close |
drh | 4d908a3 | 2002-05-10 14:37:30 +0000 | [diff] [blame] | 95 | set ::DB [sqlite db test2.db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 96 | execsql { |
| 97 | SELECT * FROM t1 |
| 98 | } |
| 99 | } {1 2} |
| 100 | do_test misuse-3.2 { |
| 101 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 102 | } {0 {a b 1 2}} |
| 103 | do_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}} |
| 111 | do_test misuse-3.4 { |
| 112 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 113 | } {21 {library routine called out of sequence}} |
| 114 | do_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 | # |
| 122 | do_test misuse-4.1 { |
| 123 | db close |
drh | 4d908a3 | 2002-05-10 14:37:30 +0000 | [diff] [blame] | 124 | set ::DB [sqlite db test2.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-4.2 { |
| 130 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 131 | } {0 {a b 1 2}} |
| 132 | do_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}} |
| 140 | do_test misuse-4.4 { |
| 141 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 142 | } {21 {library routine called out of sequence}} |
| 143 | do_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 | # |
| 151 | do_test misuse-5.1 { |
| 152 | db close |
drh | 4d908a3 | 2002-05-10 14:37:30 +0000 | [diff] [blame] | 153 | set ::DB [sqlite db test2.db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 154 | execsql { |
| 155 | SELECT * FROM t1 |
| 156 | } |
| 157 | } {1 2} |
| 158 | do_test misuse-5.2 { |
| 159 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 160 | } {0 {a b 1 2}} |
| 161 | do_test misuse-5.3 { |
| 162 | db close |
| 163 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 164 | } {21 {library routine called out of sequence}} |
| 165 | |
| 166 | finish_test |