blob: 062b359fdafd100331b9259be55b0374029d3a4c [file] [log] [blame]
drhc22bd472002-05-10 13:14:07 +00001# 2002 May 10
2#
3# The author disclaims copyright to this source code. In place of
4# 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#
drhc977f7f2002-05-21 11:38:11 +000016# $Id: misuse.test,v 1.3 2002/05/21 11:38:12 drh Exp $
drhc22bd472002-05-10 13:14:07 +000017
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# Make sure the test logic works
22#
23do_test misuse-1.1 {
24 db close
drhc977f7f2002-05-21 11:38:11 +000025 catch {file delete -force test2.db}
drh4d908a32002-05-10 14:37:30 +000026 set ::DB [sqlite db test2.db]
drhc22bd472002-05-10 13:14:07 +000027 execsql {
28 CREATE TABLE t1(a,b);
29 INSERT INTO t1 VALUES(1,2);
30 }
31 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
32} {0 {a b 1 2}}
33do_test misuse-1.2 {
34 sqlite_exec_printf $::DB {SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1} {}
35} {1 {no such function: x_coalesce}}
36do_test misuse-1.3 {
37 sqlite_create_function $::DB
38 sqlite_exec_printf $::DB {SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1} {}
39} {0 {xyz 1}}
40
41# Use the x_sqlite_exec() SQL function to simulate the effect of two
42# threads trying to use the same database at the same time.
43#
44do_test misuse-1.4 {
45 sqlite_exec_printf $::DB {
46 SELECT x_sqlite_exec('SELECT * FROM t1');
47 } {}
48} {21 {library routine called out of sequence}}
49do_test misuse-1.5 {
50 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
51} {21 {library routine called out of sequence}}
52do_test misuse-1.6 {
53 catchsql {
54 SELECT * FROM t1
55 }
56} {1 {library routine called out of sequence}}
57
58# Attempt to register a new SQL function while an sqlite_exec() is active.
59#
60do_test misuse-2.1 {
61 db close
drh4d908a32002-05-10 14:37:30 +000062 set ::DB [sqlite db test2.db]
drhc22bd472002-05-10 13:14:07 +000063 execsql {
64 SELECT * FROM t1
65 }
66} {1 2}
67do_test misuse-2.2 {
68 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
69} {0 {a b 1 2}}
70do_test misuse-2.3 {
71 set v [catch {
72 db eval {SELECT * FROM t1} {} {
73 sqlite_create_function $::DB
74 }
75 } msg]
76 lappend v $msg
77} {1 {library routine called out of sequence}}
78do_test misuse-2.4 {
79 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
80} {21 {library routine called out of sequence}}
81do_test misuse-2.5 {
82 catchsql {
83 SELECT * FROM t1
84 }
85} {1 {library routine called out of sequence}}
86
87# Attempt to register a new SQL aggregate while an sqlite_exec() is active.
88#
89do_test misuse-3.1 {
90 db close
drh4d908a32002-05-10 14:37:30 +000091 set ::DB [sqlite db test2.db]
drhc22bd472002-05-10 13:14:07 +000092 execsql {
93 SELECT * FROM t1
94 }
95} {1 2}
96do_test misuse-3.2 {
97 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
98} {0 {a b 1 2}}
99do_test misuse-3.3 {
100 set v [catch {
101 db eval {SELECT * FROM t1} {} {
102 sqlite_create_aggregate $::DB
103 }
104 } msg]
105 lappend v $msg
106} {1 {library routine called out of sequence}}
107do_test misuse-3.4 {
108 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
109} {21 {library routine called out of sequence}}
110do_test misuse-3.5 {
111 catchsql {
112 SELECT * FROM t1
113 }
114} {1 {library routine called out of sequence}}
115
116# Attempt to close the database from an sqlite_exec callback.
117#
118do_test misuse-4.1 {
119 db close
drh4d908a32002-05-10 14:37:30 +0000120 set ::DB [sqlite db test2.db]
drhc22bd472002-05-10 13:14:07 +0000121 execsql {
122 SELECT * FROM t1
123 }
124} {1 2}
125do_test misuse-4.2 {
126 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
127} {0 {a b 1 2}}
128do_test misuse-4.3 {
129 set v [catch {
130 db eval {SELECT * FROM t1} {} {
131 sqlite_close $::DB
132 }
133 } msg]
134 lappend v $msg
135} {1 {library routine called out of sequence}}
136do_test misuse-4.4 {
137 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
138} {21 {library routine called out of sequence}}
139do_test misuse-4.5 {
140 catchsql {
141 SELECT * FROM t1
142 }
143} {1 {library routine called out of sequence}}
144
145# Attempt to use a database after it has been closed.
146#
147do_test misuse-5.1 {
148 db close
drh4d908a32002-05-10 14:37:30 +0000149 set ::DB [sqlite db test2.db]
drhc22bd472002-05-10 13:14:07 +0000150 execsql {
151 SELECT * FROM t1
152 }
153} {1 2}
154do_test misuse-5.2 {
155 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
156} {0 {a b 1 2}}
157do_test misuse-5.3 {
158 db close
159 sqlite_exec_printf $::DB {SELECT * FROM t1} {}
160} {21 {library routine called out of sequence}}
161
162finish_test