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