sl@0: # 2002 May 10 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. sl@0: # sl@0: # This file implements tests for the SQLITE_MISUSE detection logic. sl@0: # This test file leaks memory and file descriptors. sl@0: # sl@0: # $Id: misuse.test,v 1.11 2006/01/03 00:33:50 drh Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: proc catchsql2 {sql} { sl@0: set r [ sl@0: catch { sl@0: set res [list] sl@0: db eval $sql data { sl@0: if { $res==[list] } { sl@0: foreach f $data(*) {lappend res $f} sl@0: } sl@0: foreach f $data(*) {lappend res $data($f)} sl@0: } sl@0: set res sl@0: } msg sl@0: ] sl@0: lappend r $msg sl@0: } sl@0: sl@0: sl@0: # Make sure the test logic works sl@0: # sl@0: do_test misuse-1.1 { sl@0: db close sl@0: catch {file delete -force test2.db} sl@0: catch {file delete -force test2.db-journal} sl@0: sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db] sl@0: execsql { sl@0: CREATE TABLE t1(a,b); sl@0: INSERT INTO t1 VALUES(1,2); sl@0: } sl@0: catchsql2 { sl@0: SELECT * FROM t1 sl@0: } sl@0: } {0 {a b 1 2}} sl@0: do_test misuse-1.2 { sl@0: catchsql2 { sl@0: SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1 sl@0: } sl@0: } {1 {no such function: x_coalesce}} sl@0: do_test misuse-1.3 { sl@0: sqlite3_create_function $::DB sl@0: catchsql2 { sl@0: SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1 sl@0: } sl@0: } {0 {xyz 1}} sl@0: sl@0: # Use the x_sqlite_exec() SQL function to simulate the effect of two sl@0: # threads trying to use the same database at the same time. sl@0: # sl@0: # It used to be prohibited to invoke sqlite_exec() from within a function, sl@0: # but that has changed. The following tests used to cause errors but now sl@0: # they do not. sl@0: # sl@0: ifcapable {utf16} { sl@0: do_test misuse-1.4 { sl@0: catchsql2 { sl@0: SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz; sl@0: } sl@0: } {0 {xyz {1 2}}} sl@0: } sl@0: do_test misuse-1.5 { sl@0: catchsql2 {SELECT * FROM t1} sl@0: } {0 {a b 1 2}} sl@0: do_test misuse-1.6 { sl@0: catchsql { sl@0: SELECT * FROM t1 sl@0: } sl@0: } {0 {1 2}} sl@0: sl@0: # Attempt to register a new SQL function while an sqlite_exec() is active. sl@0: # sl@0: do_test misuse-2.1 { sl@0: db close sl@0: sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db] sl@0: execsql { sl@0: SELECT * FROM t1 sl@0: } sl@0: } {1 2} sl@0: do_test misuse-2.2 { sl@0: catchsql2 {SELECT * FROM t1} sl@0: } {0 {a b 1 2}} sl@0: sl@0: # We used to disallow creating new function from within an exec(). sl@0: # But now this is acceptable. sl@0: do_test misuse-2.3 { sl@0: set v [catch { sl@0: db eval {SELECT * FROM t1} {} { sl@0: sqlite3_create_function $::DB sl@0: } sl@0: } msg] sl@0: lappend v $msg sl@0: } {0 {}} sl@0: do_test misuse-2.4 { sl@0: catchsql2 {SELECT * FROM t1} sl@0: } {0 {a b 1 2}} sl@0: do_test misuse-2.5 { sl@0: catchsql { sl@0: SELECT * FROM t1 sl@0: } sl@0: } {0 {1 2}} sl@0: sl@0: # Attempt to register a new SQL aggregate while an sqlite_exec() is active. sl@0: # sl@0: do_test misuse-3.1 { sl@0: db close sl@0: sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db] sl@0: execsql { sl@0: SELECT * FROM t1 sl@0: } sl@0: } {1 2} sl@0: do_test misuse-3.2 { sl@0: catchsql2 {SELECT * FROM t1} sl@0: } {0 {a b 1 2}} sl@0: sl@0: # We used to disallow creating new function from within an exec(). sl@0: # But now this is acceptable. sl@0: do_test misuse-3.3 { sl@0: set v [catch { sl@0: db eval {SELECT * FROM t1} {} { sl@0: sqlite3_create_aggregate $::DB sl@0: } sl@0: } msg] sl@0: lappend v $msg sl@0: } {0 {}} sl@0: do_test misuse-3.4 { sl@0: catchsql2 {SELECT * FROM t1} sl@0: } {0 {a b 1 2}} sl@0: do_test misuse-3.5 { sl@0: catchsql { sl@0: SELECT * FROM t1 sl@0: } sl@0: } {0 {1 2}} sl@0: sl@0: # Attempt to close the database from an sqlite_exec callback. sl@0: # sl@0: # Update for v3: The db cannot be closed because there are active sl@0: # VMs. The sqlite3_close call would return SQLITE_BUSY. sl@0: do_test misuse-4.1 { sl@0: db close sl@0: sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db] sl@0: execsql { sl@0: SELECT * FROM t1 sl@0: } sl@0: } {1 2} sl@0: do_test misuse-4.2 { sl@0: catchsql2 {SELECT * FROM t1} sl@0: } {0 {a b 1 2}} sl@0: do_test misuse-4.3 { sl@0: set v [catch { sl@0: db eval {SELECT * FROM t1} {} { sl@0: set r [sqlite3_close $::DB] sl@0: } sl@0: } msg] sl@0: lappend v $msg $r sl@0: } {0 {} SQLITE_BUSY} sl@0: do_test misuse-4.4 { sl@0: # Flush the TCL statement cache here, otherwise the sqlite3_close() will sl@0: # fail because there are still un-finalized() VDBEs. sl@0: db cache flush sl@0: sqlite3_close $::DB sl@0: catchsql2 {SELECT * FROM t1} sl@0: } {1 {library routine called out of sequence}} sl@0: do_test misuse-4.5 { sl@0: catchsql { sl@0: SELECT * FROM t1 sl@0: } sl@0: } {1 {library routine called out of sequence}} sl@0: sl@0: # Attempt to use a database after it has been closed. sl@0: # sl@0: do_test misuse-5.1 { sl@0: db close sl@0: sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db] sl@0: execsql { sl@0: SELECT * FROM t1 sl@0: } sl@0: } {1 2} sl@0: do_test misuse-5.2 { sl@0: catchsql2 {SELECT * FROM t1} sl@0: } {0 {a b 1 2}} sl@0: do_test misuse-5.3 { sl@0: db close sl@0: set r [catch { sl@0: sqlite3_prepare $::DB {SELECT * FROM t1} -1 TAIL sl@0: } msg] sl@0: lappend r $msg sl@0: } {1 {(21) library routine called out of sequence}} sl@0: sl@0: finish_test