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