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# 16# $Id: misuse.test,v 1.4 2004/01/07 19:24:48 drh Exp $ 17 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 25 catch {file delete -force test2.db} 26 set ::DB [sqlite db test2.db] 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}} 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# 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# 48do_test misuse-1.4 { 49 sqlite_exec_printf $::DB { 50 SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz; 51 } {} 52} {0 {xyz {1 2}}} 53do_test misuse-1.5 { 54 sqlite_exec_printf $::DB {SELECT * FROM t1} {} 55} {0 {a b 1 2}} 56do_test misuse-1.6 { 57 catchsql { 58 SELECT * FROM t1 59 } 60} {0 {1 2}} 61 62# Attempt to register a new SQL function while an sqlite_exec() is active. 63# 64do_test misuse-2.1 { 65 db close 66 set ::DB [sqlite db test2.db] 67 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 95 set ::DB [sqlite db test2.db] 96 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 124 set ::DB [sqlite db test2.db] 125 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 153 set ::DB [sqlite db test2.db] 154 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 167