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