1*4520Snw141292 2*4520Snw141292#pragma ident "%Z%%M% %I% %E% SMI" 3*4520Snw141292 4*4520Snw141292# 2002 May 10 5*4520Snw141292# 6*4520Snw141292# The author disclaims copyright to this source code. In place of 7*4520Snw141292# a legal notice, here is a blessing: 8*4520Snw141292# 9*4520Snw141292# May you do good and not evil. 10*4520Snw141292# May you find forgiveness for yourself and forgive others. 11*4520Snw141292# May you share freely, never taking more than you give. 12*4520Snw141292# 13*4520Snw141292#*********************************************************************** 14*4520Snw141292# This file implements regression tests for SQLite library. 15*4520Snw141292# 16*4520Snw141292# This file implements tests for the SQLITE_MISUSE detection logic. 17*4520Snw141292# This test file leaks memory and file descriptors. 18*4520Snw141292# 19*4520Snw141292# $Id: misuse.test,v 1.4 2004/01/07 19:24:48 drh Exp $ 20*4520Snw141292 21*4520Snw141292set testdir [file dirname $argv0] 22*4520Snw141292source $testdir/tester.tcl 23*4520Snw141292 24*4520Snw141292# Make sure the test logic works 25*4520Snw141292# 26*4520Snw141292do_test misuse-1.1 { 27*4520Snw141292 db close 28*4520Snw141292 catch {file delete -force test2.db} 29*4520Snw141292 set ::DB [sqlite db test2.db] 30*4520Snw141292 execsql { 31*4520Snw141292 CREATE TABLE t1(a,b); 32*4520Snw141292 INSERT INTO t1 VALUES(1,2); 33*4520Snw141292 } 34*4520Snw141292 sqlite_exec_printf $::DB {SELECT * FROM t1} {} 35*4520Snw141292} {0 {a b 1 2}} 36*4520Snw141292do_test misuse-1.2 { 37*4520Snw141292 sqlite_exec_printf $::DB {SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1} {} 38*4520Snw141292} {1 {no such function: x_coalesce}} 39*4520Snw141292do_test misuse-1.3 { 40*4520Snw141292 sqlite_create_function $::DB 41*4520Snw141292 sqlite_exec_printf $::DB {SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1} {} 42*4520Snw141292} {0 {xyz 1}} 43*4520Snw141292 44*4520Snw141292# Use the x_sqlite_exec() SQL function to simulate the effect of two 45*4520Snw141292# threads trying to use the same database at the same time. 46*4520Snw141292# 47*4520Snw141292# It used to be prohibited to invoke sqlite_exec() from within a function, 48*4520Snw141292# but that has changed. The following tests used to cause errors but now 49*4520Snw141292# they do not. 50*4520Snw141292# 51*4520Snw141292do_test misuse-1.4 { 52*4520Snw141292 sqlite_exec_printf $::DB { 53*4520Snw141292 SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz; 54*4520Snw141292 } {} 55*4520Snw141292} {0 {xyz {1 2}}} 56*4520Snw141292do_test misuse-1.5 { 57*4520Snw141292 sqlite_exec_printf $::DB {SELECT * FROM t1} {} 58*4520Snw141292} {0 {a b 1 2}} 59*4520Snw141292do_test misuse-1.6 { 60*4520Snw141292 catchsql { 61*4520Snw141292 SELECT * FROM t1 62*4520Snw141292 } 63*4520Snw141292} {0 {1 2}} 64*4520Snw141292 65*4520Snw141292# Attempt to register a new SQL function while an sqlite_exec() is active. 66*4520Snw141292# 67*4520Snw141292do_test misuse-2.1 { 68*4520Snw141292 db close 69*4520Snw141292 set ::DB [sqlite db test2.db] 70*4520Snw141292 execsql { 71*4520Snw141292 SELECT * FROM t1 72*4520Snw141292 } 73*4520Snw141292} {1 2} 74*4520Snw141292do_test misuse-2.2 { 75*4520Snw141292 sqlite_exec_printf $::DB {SELECT * FROM t1} {} 76*4520Snw141292} {0 {a b 1 2}} 77*4520Snw141292do_test misuse-2.3 { 78*4520Snw141292 set v [catch { 79*4520Snw141292 db eval {SELECT * FROM t1} {} { 80*4520Snw141292 sqlite_create_function $::DB 81*4520Snw141292 } 82*4520Snw141292 } msg] 83*4520Snw141292 lappend v $msg 84*4520Snw141292} {1 {library routine called out of sequence}} 85*4520Snw141292do_test misuse-2.4 { 86*4520Snw141292 sqlite_exec_printf $::DB {SELECT * FROM t1} {} 87*4520Snw141292} {21 {library routine called out of sequence}} 88*4520Snw141292do_test misuse-2.5 { 89*4520Snw141292 catchsql { 90*4520Snw141292 SELECT * FROM t1 91*4520Snw141292 } 92*4520Snw141292} {1 {library routine called out of sequence}} 93*4520Snw141292 94*4520Snw141292# Attempt to register a new SQL aggregate while an sqlite_exec() is active. 95*4520Snw141292# 96*4520Snw141292do_test misuse-3.1 { 97*4520Snw141292 db close 98*4520Snw141292 set ::DB [sqlite db test2.db] 99*4520Snw141292 execsql { 100*4520Snw141292 SELECT * FROM t1 101*4520Snw141292 } 102*4520Snw141292} {1 2} 103*4520Snw141292do_test misuse-3.2 { 104*4520Snw141292 sqlite_exec_printf $::DB {SELECT * FROM t1} {} 105*4520Snw141292} {0 {a b 1 2}} 106*4520Snw141292do_test misuse-3.3 { 107*4520Snw141292 set v [catch { 108*4520Snw141292 db eval {SELECT * FROM t1} {} { 109*4520Snw141292 sqlite_create_aggregate $::DB 110*4520Snw141292 } 111*4520Snw141292 } msg] 112*4520Snw141292 lappend v $msg 113*4520Snw141292} {1 {library routine called out of sequence}} 114*4520Snw141292do_test misuse-3.4 { 115*4520Snw141292 sqlite_exec_printf $::DB {SELECT * FROM t1} {} 116*4520Snw141292} {21 {library routine called out of sequence}} 117*4520Snw141292do_test misuse-3.5 { 118*4520Snw141292 catchsql { 119*4520Snw141292 SELECT * FROM t1 120*4520Snw141292 } 121*4520Snw141292} {1 {library routine called out of sequence}} 122*4520Snw141292 123*4520Snw141292# Attempt to close the database from an sqlite_exec callback. 124*4520Snw141292# 125*4520Snw141292do_test misuse-4.1 { 126*4520Snw141292 db close 127*4520Snw141292 set ::DB [sqlite db test2.db] 128*4520Snw141292 execsql { 129*4520Snw141292 SELECT * FROM t1 130*4520Snw141292 } 131*4520Snw141292} {1 2} 132*4520Snw141292do_test misuse-4.2 { 133*4520Snw141292 sqlite_exec_printf $::DB {SELECT * FROM t1} {} 134*4520Snw141292} {0 {a b 1 2}} 135*4520Snw141292do_test misuse-4.3 { 136*4520Snw141292 set v [catch { 137*4520Snw141292 db eval {SELECT * FROM t1} {} { 138*4520Snw141292 sqlite_close $::DB 139*4520Snw141292 } 140*4520Snw141292 } msg] 141*4520Snw141292 lappend v $msg 142*4520Snw141292} {1 {library routine called out of sequence}} 143*4520Snw141292do_test misuse-4.4 { 144*4520Snw141292 sqlite_exec_printf $::DB {SELECT * FROM t1} {} 145*4520Snw141292} {21 {library routine called out of sequence}} 146*4520Snw141292do_test misuse-4.5 { 147*4520Snw141292 catchsql { 148*4520Snw141292 SELECT * FROM t1 149*4520Snw141292 } 150*4520Snw141292} {1 {library routine called out of sequence}} 151*4520Snw141292 152*4520Snw141292# Attempt to use a database after it has been closed. 153*4520Snw141292# 154*4520Snw141292do_test misuse-5.1 { 155*4520Snw141292 db close 156*4520Snw141292 set ::DB [sqlite db test2.db] 157*4520Snw141292 execsql { 158*4520Snw141292 SELECT * FROM t1 159*4520Snw141292 } 160*4520Snw141292} {1 2} 161*4520Snw141292do_test misuse-5.2 { 162*4520Snw141292 sqlite_exec_printf $::DB {SELECT * FROM t1} {} 163*4520Snw141292} {0 {a b 1 2}} 164*4520Snw141292do_test misuse-5.3 { 165*4520Snw141292 db close 166*4520Snw141292 sqlite_exec_printf $::DB {SELECT * FROM t1} {} 167*4520Snw141292} {21 {library routine called out of sequence}} 168*4520Snw141292 169*4520Snw141292finish_test 170