1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/misuse.test Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,207 @@
1.4 +# 2002 May 10
1.5 +#
1.6 +# The author disclaims copyright to this source code. In place of
1.7 +# a legal notice, here is a blessing:
1.8 +#
1.9 +# May you do good and not evil.
1.10 +# May you find forgiveness for yourself and forgive others.
1.11 +# May you share freely, never taking more than you give.
1.12 +#
1.13 +#***********************************************************************
1.14 +# This file implements regression tests for SQLite library.
1.15 +#
1.16 +# This file implements tests for the SQLITE_MISUSE detection logic.
1.17 +# This test file leaks memory and file descriptors.
1.18 +#
1.19 +# $Id: misuse.test,v 1.11 2006/01/03 00:33:50 drh Exp $
1.20 +
1.21 +set testdir [file dirname $argv0]
1.22 +source $testdir/tester.tcl
1.23 +
1.24 +proc catchsql2 {sql} {
1.25 + set r [
1.26 + catch {
1.27 + set res [list]
1.28 + db eval $sql data {
1.29 + if { $res==[list] } {
1.30 + foreach f $data(*) {lappend res $f}
1.31 + }
1.32 + foreach f $data(*) {lappend res $data($f)}
1.33 + }
1.34 + set res
1.35 + } msg
1.36 + ]
1.37 + lappend r $msg
1.38 +}
1.39 +
1.40 +
1.41 +# Make sure the test logic works
1.42 +#
1.43 +do_test misuse-1.1 {
1.44 + db close
1.45 + catch {file delete -force test2.db}
1.46 + catch {file delete -force test2.db-journal}
1.47 + sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
1.48 + execsql {
1.49 + CREATE TABLE t1(a,b);
1.50 + INSERT INTO t1 VALUES(1,2);
1.51 + }
1.52 + catchsql2 {
1.53 + SELECT * FROM t1
1.54 + }
1.55 +} {0 {a b 1 2}}
1.56 +do_test misuse-1.2 {
1.57 + catchsql2 {
1.58 + SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1
1.59 + }
1.60 +} {1 {no such function: x_coalesce}}
1.61 +do_test misuse-1.3 {
1.62 + sqlite3_create_function $::DB
1.63 + catchsql2 {
1.64 + SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1
1.65 + }
1.66 +} {0 {xyz 1}}
1.67 +
1.68 +# Use the x_sqlite_exec() SQL function to simulate the effect of two
1.69 +# threads trying to use the same database at the same time.
1.70 +#
1.71 +# It used to be prohibited to invoke sqlite_exec() from within a function,
1.72 +# but that has changed. The following tests used to cause errors but now
1.73 +# they do not.
1.74 +#
1.75 +ifcapable {utf16} {
1.76 + do_test misuse-1.4 {
1.77 + catchsql2 {
1.78 + SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz;
1.79 + }
1.80 + } {0 {xyz {1 2}}}
1.81 +}
1.82 +do_test misuse-1.5 {
1.83 + catchsql2 {SELECT * FROM t1}
1.84 +} {0 {a b 1 2}}
1.85 +do_test misuse-1.6 {
1.86 + catchsql {
1.87 + SELECT * FROM t1
1.88 + }
1.89 +} {0 {1 2}}
1.90 +
1.91 +# Attempt to register a new SQL function while an sqlite_exec() is active.
1.92 +#
1.93 +do_test misuse-2.1 {
1.94 + db close
1.95 + sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
1.96 + execsql {
1.97 + SELECT * FROM t1
1.98 + }
1.99 +} {1 2}
1.100 +do_test misuse-2.2 {
1.101 + catchsql2 {SELECT * FROM t1}
1.102 +} {0 {a b 1 2}}
1.103 +
1.104 +# We used to disallow creating new function from within an exec().
1.105 +# But now this is acceptable.
1.106 +do_test misuse-2.3 {
1.107 + set v [catch {
1.108 + db eval {SELECT * FROM t1} {} {
1.109 + sqlite3_create_function $::DB
1.110 + }
1.111 + } msg]
1.112 + lappend v $msg
1.113 +} {0 {}}
1.114 +do_test misuse-2.4 {
1.115 + catchsql2 {SELECT * FROM t1}
1.116 +} {0 {a b 1 2}}
1.117 +do_test misuse-2.5 {
1.118 + catchsql {
1.119 + SELECT * FROM t1
1.120 + }
1.121 +} {0 {1 2}}
1.122 +
1.123 +# Attempt to register a new SQL aggregate while an sqlite_exec() is active.
1.124 +#
1.125 +do_test misuse-3.1 {
1.126 + db close
1.127 + sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
1.128 + execsql {
1.129 + SELECT * FROM t1
1.130 + }
1.131 +} {1 2}
1.132 +do_test misuse-3.2 {
1.133 + catchsql2 {SELECT * FROM t1}
1.134 +} {0 {a b 1 2}}
1.135 +
1.136 +# We used to disallow creating new function from within an exec().
1.137 +# But now this is acceptable.
1.138 +do_test misuse-3.3 {
1.139 + set v [catch {
1.140 + db eval {SELECT * FROM t1} {} {
1.141 + sqlite3_create_aggregate $::DB
1.142 + }
1.143 + } msg]
1.144 + lappend v $msg
1.145 +} {0 {}}
1.146 +do_test misuse-3.4 {
1.147 + catchsql2 {SELECT * FROM t1}
1.148 +} {0 {a b 1 2}}
1.149 +do_test misuse-3.5 {
1.150 + catchsql {
1.151 + SELECT * FROM t1
1.152 + }
1.153 +} {0 {1 2}}
1.154 +
1.155 +# Attempt to close the database from an sqlite_exec callback.
1.156 +#
1.157 +# Update for v3: The db cannot be closed because there are active
1.158 +# VMs. The sqlite3_close call would return SQLITE_BUSY.
1.159 +do_test misuse-4.1 {
1.160 + db close
1.161 + sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
1.162 + execsql {
1.163 + SELECT * FROM t1
1.164 + }
1.165 +} {1 2}
1.166 +do_test misuse-4.2 {
1.167 + catchsql2 {SELECT * FROM t1}
1.168 +} {0 {a b 1 2}}
1.169 +do_test misuse-4.3 {
1.170 + set v [catch {
1.171 + db eval {SELECT * FROM t1} {} {
1.172 + set r [sqlite3_close $::DB]
1.173 + }
1.174 + } msg]
1.175 + lappend v $msg $r
1.176 +} {0 {} SQLITE_BUSY}
1.177 +do_test misuse-4.4 {
1.178 + # Flush the TCL statement cache here, otherwise the sqlite3_close() will
1.179 + # fail because there are still un-finalized() VDBEs.
1.180 + db cache flush
1.181 + sqlite3_close $::DB
1.182 + catchsql2 {SELECT * FROM t1}
1.183 +} {1 {library routine called out of sequence}}
1.184 +do_test misuse-4.5 {
1.185 + catchsql {
1.186 + SELECT * FROM t1
1.187 + }
1.188 +} {1 {library routine called out of sequence}}
1.189 +
1.190 +# Attempt to use a database after it has been closed.
1.191 +#
1.192 +do_test misuse-5.1 {
1.193 + db close
1.194 + sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
1.195 + execsql {
1.196 + SELECT * FROM t1
1.197 + }
1.198 +} {1 2}
1.199 +do_test misuse-5.2 {
1.200 + catchsql2 {SELECT * FROM t1}
1.201 +} {0 {a b 1 2}}
1.202 +do_test misuse-5.3 {
1.203 + db close
1.204 + set r [catch {
1.205 + sqlite3_prepare $::DB {SELECT * FROM t1} -1 TAIL
1.206 + } msg]
1.207 + lappend r $msg
1.208 +} {1 {(21) library routine called out of sequence}}
1.209 +
1.210 +finish_test