xref: /sqlite-3.40.0/test/misuse.test (revision c22bd47d)
1# 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#
16# $Id: misuse.test,v 1.1 2002/05/10 13:14:08 drh Exp $
17
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
25  set ::DB [sqlite db test.db]
26  execsql {
27    CREATE TABLE t1(a,b);
28    INSERT INTO t1 VALUES(1,2);
29  }
30  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
31} {0 {a b 1 2}}
32do_test misuse-1.2 {
33  sqlite_exec_printf $::DB {SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1} {}
34} {1 {no such function: x_coalesce}}
35do_test misuse-1.3 {
36  sqlite_create_function $::DB
37  sqlite_exec_printf $::DB {SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1} {}
38} {0 {xyz 1}}
39
40# Use the x_sqlite_exec() SQL function to simulate the effect of two
41# threads trying to use the same database at the same time.
42#
43do_test misuse-1.4 {
44  sqlite_exec_printf $::DB {
45     SELECT x_sqlite_exec('SELECT * FROM t1');
46  } {}
47} {21 {library routine called out of sequence}}
48do_test misuse-1.5 {
49  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
50} {21 {library routine called out of sequence}}
51do_test misuse-1.6 {
52  catchsql {
53    SELECT * FROM t1
54  }
55} {1 {library routine called out of sequence}}
56
57# Attempt to register a new SQL function while an sqlite_exec() is active.
58#
59do_test misuse-2.1 {
60  db close
61  set ::DB [sqlite db test.db]
62  execsql {
63    SELECT * FROM t1
64  }
65} {1 2}
66do_test misuse-2.2 {
67  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
68} {0 {a b 1 2}}
69do_test misuse-2.3 {
70  set v [catch {
71    db eval {SELECT * FROM t1} {} {
72      sqlite_create_function $::DB
73    }
74  } msg]
75  lappend v $msg
76} {1 {library routine called out of sequence}}
77do_test misuse-2.4 {
78  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
79} {21 {library routine called out of sequence}}
80do_test misuse-2.5 {
81  catchsql {
82    SELECT * FROM t1
83  }
84} {1 {library routine called out of sequence}}
85
86# Attempt to register a new SQL aggregate while an sqlite_exec() is active.
87#
88do_test misuse-3.1 {
89  db close
90  set ::DB [sqlite db test.db]
91  execsql {
92    SELECT * FROM t1
93  }
94} {1 2}
95do_test misuse-3.2 {
96  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
97} {0 {a b 1 2}}
98do_test misuse-3.3 {
99  set v [catch {
100    db eval {SELECT * FROM t1} {} {
101      sqlite_create_aggregate $::DB
102    }
103  } msg]
104  lappend v $msg
105} {1 {library routine called out of sequence}}
106do_test misuse-3.4 {
107  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
108} {21 {library routine called out of sequence}}
109do_test misuse-3.5 {
110  catchsql {
111    SELECT * FROM t1
112  }
113} {1 {library routine called out of sequence}}
114
115# Attempt to close the database from an sqlite_exec callback.
116#
117do_test misuse-4.1 {
118  db close
119  set ::DB [sqlite db test.db]
120  execsql {
121    SELECT * FROM t1
122  }
123} {1 2}
124do_test misuse-4.2 {
125  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
126} {0 {a b 1 2}}
127do_test misuse-4.3 {
128  set v [catch {
129    db eval {SELECT * FROM t1} {} {
130      sqlite_close $::DB
131    }
132  } msg]
133  lappend v $msg
134} {1 {library routine called out of sequence}}
135do_test misuse-4.4 {
136  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
137} {21 {library routine called out of sequence}}
138do_test misuse-4.5 {
139  catchsql {
140    SELECT * FROM t1
141  }
142} {1 {library routine called out of sequence}}
143
144# Attempt to use a database after it has been closed.
145#
146do_test misuse-5.1 {
147  db close
148  set ::DB [sqlite db test.db]
149  execsql {
150    SELECT * FROM t1
151  }
152} {1 2}
153do_test misuse-5.2 {
154  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
155} {0 {a b 1 2}}
156do_test misuse-5.3 {
157  db close
158  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
159} {21 {library routine called out of sequence}}
160
161finish_test
162