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 | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame^] | 16 | # $Id: misuse.test,v 1.3 2002/05/21 11:38:12 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 | # |
| 44 | do_test misuse-1.4 { |
| 45 | sqlite_exec_printf $::DB { |
| 46 | SELECT x_sqlite_exec('SELECT * FROM t1'); |
| 47 | } {} |
| 48 | } {21 {library routine called out of sequence}} |
| 49 | do_test misuse-1.5 { |
| 50 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 51 | } {21 {library routine called out of sequence}} |
| 52 | do_test misuse-1.6 { |
| 53 | catchsql { |
| 54 | SELECT * FROM t1 |
| 55 | } |
| 56 | } {1 {library routine called out of sequence}} |
| 57 | |
| 58 | # Attempt to register a new SQL function while an sqlite_exec() is active. |
| 59 | # |
| 60 | do_test misuse-2.1 { |
| 61 | db close |
drh | 4d908a3 | 2002-05-10 14:37:30 +0000 | [diff] [blame] | 62 | set ::DB [sqlite db test2.db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 63 | execsql { |
| 64 | SELECT * FROM t1 |
| 65 | } |
| 66 | } {1 2} |
| 67 | do_test misuse-2.2 { |
| 68 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 69 | } {0 {a b 1 2}} |
| 70 | do_test misuse-2.3 { |
| 71 | set v [catch { |
| 72 | db eval {SELECT * FROM t1} {} { |
| 73 | sqlite_create_function $::DB |
| 74 | } |
| 75 | } msg] |
| 76 | lappend v $msg |
| 77 | } {1 {library routine called out of sequence}} |
| 78 | do_test misuse-2.4 { |
| 79 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 80 | } {21 {library routine called out of sequence}} |
| 81 | do_test misuse-2.5 { |
| 82 | catchsql { |
| 83 | SELECT * FROM t1 |
| 84 | } |
| 85 | } {1 {library routine called out of sequence}} |
| 86 | |
| 87 | # Attempt to register a new SQL aggregate while an sqlite_exec() is active. |
| 88 | # |
| 89 | do_test misuse-3.1 { |
| 90 | db close |
drh | 4d908a3 | 2002-05-10 14:37:30 +0000 | [diff] [blame] | 91 | set ::DB [sqlite db test2.db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 92 | execsql { |
| 93 | SELECT * FROM t1 |
| 94 | } |
| 95 | } {1 2} |
| 96 | do_test misuse-3.2 { |
| 97 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 98 | } {0 {a b 1 2}} |
| 99 | do_test misuse-3.3 { |
| 100 | set v [catch { |
| 101 | db eval {SELECT * FROM t1} {} { |
| 102 | sqlite_create_aggregate $::DB |
| 103 | } |
| 104 | } msg] |
| 105 | lappend v $msg |
| 106 | } {1 {library routine called out of sequence}} |
| 107 | do_test misuse-3.4 { |
| 108 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 109 | } {21 {library routine called out of sequence}} |
| 110 | do_test misuse-3.5 { |
| 111 | catchsql { |
| 112 | SELECT * FROM t1 |
| 113 | } |
| 114 | } {1 {library routine called out of sequence}} |
| 115 | |
| 116 | # Attempt to close the database from an sqlite_exec callback. |
| 117 | # |
| 118 | do_test misuse-4.1 { |
| 119 | db close |
drh | 4d908a3 | 2002-05-10 14:37:30 +0000 | [diff] [blame] | 120 | set ::DB [sqlite db test2.db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 121 | execsql { |
| 122 | SELECT * FROM t1 |
| 123 | } |
| 124 | } {1 2} |
| 125 | do_test misuse-4.2 { |
| 126 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 127 | } {0 {a b 1 2}} |
| 128 | do_test misuse-4.3 { |
| 129 | set v [catch { |
| 130 | db eval {SELECT * FROM t1} {} { |
| 131 | sqlite_close $::DB |
| 132 | } |
| 133 | } msg] |
| 134 | lappend v $msg |
| 135 | } {1 {library routine called out of sequence}} |
| 136 | do_test misuse-4.4 { |
| 137 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 138 | } {21 {library routine called out of sequence}} |
| 139 | do_test misuse-4.5 { |
| 140 | catchsql { |
| 141 | SELECT * FROM t1 |
| 142 | } |
| 143 | } {1 {library routine called out of sequence}} |
| 144 | |
| 145 | # Attempt to use a database after it has been closed. |
| 146 | # |
| 147 | do_test misuse-5.1 { |
| 148 | db close |
drh | 4d908a3 | 2002-05-10 14:37:30 +0000 | [diff] [blame] | 149 | set ::DB [sqlite db test2.db] |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 150 | execsql { |
| 151 | SELECT * FROM t1 |
| 152 | } |
| 153 | } {1 2} |
| 154 | do_test misuse-5.2 { |
| 155 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 156 | } {0 {a b 1 2}} |
| 157 | do_test misuse-5.3 { |
| 158 | db close |
| 159 | sqlite_exec_printf $::DB {SELECT * FROM t1} {} |
| 160 | } {21 {library routine called out of sequence}} |
| 161 | |
| 162 | finish_test |