AryaWu/sqlite
0
1# 2002 May 102#3# The author disclaims copyright to this source code. In place of4# 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.11 2006/01/03 00:33:50 drh Exp $17 18set testdir [file dirname $argv0]19source $testdir/tester.tcl20 21proc catchsql2 {sql} {22 set r [23 catch {24 set res [list]25 db eval $sql data {26 if { $res==[list] } {27 foreach f $data(*) {lappend res $f}28 }29 foreach f $data(*) {lappend res $data($f)}30 }31 set res32 } msg33 ]34 lappend r $msg35}36 37 38# Make sure the test logic works39#40do_test misuse-1.1 {41 db close42 catch {forcedelete test2.db}43 catch {forcedelete test2.db-journal}44 sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]45 execsql {46 CREATE TABLE t1(a,b);47 INSERT INTO t1 VALUES(1,2);48 }49 catchsql2 {50 SELECT * FROM t151 }52} {0 {a b 1 2}}53do_test misuse-1.2 {54 catchsql2 {55 SELECT x_coalesce(NULL,a) AS 'xyz' FROM t156 }57} {1 {no such function: x_coalesce}}58do_test misuse-1.3 {59 sqlite3_create_function $::DB60 catchsql2 {61 SELECT x_coalesce(NULL,a) AS 'xyz' FROM t162 }63} {0 {xyz 1}}64 65# Use the x_sqlite_exec() SQL function to simulate the effect of two66# threads trying to use the same database at the same time.67#68# It used to be prohibited to invoke sqlite_exec() from within a function,69# but that has changed. The following tests used to cause errors but now70# they do not.71#72ifcapable {utf16} {73 do_test misuse-1.4 {74 catchsql2 {75 SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz;76 } 77 } {0 {xyz {1 2}}}78}79do_test misuse-1.5 {80 catchsql2 {SELECT * FROM t1}81} {0 {a b 1 2}}82do_test misuse-1.6 {83 catchsql {84 SELECT * FROM t185 }86} {0 {1 2}}87 88# Attempt to register a new SQL function while an sqlite_exec() is active.89#90do_test misuse-2.1 {91 db close92 sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]93 execsql {94 SELECT * FROM t195 }96} {1 2}97do_test misuse-2.2 {98 catchsql2 {SELECT * FROM t1}99} {0 {a b 1 2}}100 101# We used to disallow creating new function from within an exec().102# But now this is acceptable.103do_test misuse-2.3 {104 set v [catch {105 db eval {SELECT * FROM t1} {} {106 sqlite3_create_function $::DB107 }108 } msg]109 lappend v $msg110} {0 {}}111do_test misuse-2.4 {112 catchsql2 {SELECT * FROM t1}113} {0 {a b 1 2}}114do_test misuse-2.5 {115 catchsql {116 SELECT * FROM t1117 }118} {0 {1 2}}119 120# Attempt to register a new SQL aggregate while an sqlite_exec() is active.121#122do_test misuse-3.1 {123 db close124 sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]125 execsql {126 SELECT * FROM t1127 }128} {1 2}129do_test misuse-3.2 {130 catchsql2 {SELECT * FROM t1}131} {0 {a b 1 2}}132 133# We used to disallow creating new function from within an exec().134# But now this is acceptable.135do_test misuse-3.3 {136 set v [catch {137 db eval {SELECT * FROM t1} {} {138 sqlite3_create_aggregate $::DB139 }140 } msg]141 lappend v $msg142} {0 {}}143do_test misuse-3.4 {144 catchsql2 {SELECT * FROM t1}145} {0 {a b 1 2}}146do_test misuse-3.5 {147 catchsql {148 SELECT * FROM t1149 }150} {0 {1 2}}151 152# Attempt to close the database from an sqlite_exec callback.153#154# Update for v3: The db cannot be closed because there are active155# VMs. The sqlite3_close call would return SQLITE_BUSY.156do_test misuse-4.1 {157 db close158 sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]159 execsql {160 SELECT * FROM t1161 }162} {1 2}163do_test misuse-4.2 {164 catchsql2 {SELECT * FROM t1}165} {0 {a b 1 2}}166do_test misuse-4.3 {167 set v [catch {168 db eval {SELECT * FROM t1} {} {169 set r [sqlite3_close $::DB]170 }171 } msg]172 lappend v $msg $r173} {0 {} SQLITE_BUSY}174 175# All of the following tests can potentially (though rarely)176# lead to segfaults, which is unsettling. So they are disabled177# for now__________________________178# v179if {[clang_sanitize_address]==0 && 0} {180 do_test misuse-4.4 {181 # Flush the TCL statement cache here, otherwise the sqlite3_close() will182 # fail because there are still un-finalized() VDBEs.183 db cache flush184 sqlite3_close $::DB185 catchsql2 {SELECT * FROM t1}186 } {1 {bad parameter or other API misuse}}187 do_test misuse-4.5 {188 catchsql {189 SELECT * FROM t1190 }191 } {1 {bad parameter or other API misuse}}192 193 # Attempt to use a database after it has been closed.194 #195 do_test misuse-5.1 {196 db close197 sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]198 execsql {199 SELECT * FROM t1200 }201 } {1 2}202 do_test misuse-5.2 {203 catchsql2 {SELECT * FROM t1}204 } {0 {a b 1 2}}205 do_test misuse-5.3 {206 db close207 set r [catch {208 sqlite3_prepare $::DB {SELECT * FROM t1} -1 TAIL209 } msg]210 lappend r $msg211 } {1 {(21) bad parameter or other API misuse}}212}213 214#-------------------------------------------------------------------------215reset_db216do_test misuse-6.0 {217 sqlite3_set_errmsg db 1 "an error has occurred"218} {SQLITE_OK}219do_test misuse-6.1 {220 sqlite3_errmsg db221} {an error has occurred}222do_test misuse-6.2 {223 sqlite3_set_errmsg "" 1 "an error has occurred"224} {SQLITE_MISUSE}225 226finish_test227 