1*1da57d55SToomas Soome#
27c478bd9Sstevel@tonic-gate# 2002 May 10
37c478bd9Sstevel@tonic-gate#
47c478bd9Sstevel@tonic-gate# The author disclaims copyright to this source code.  In place of
57c478bd9Sstevel@tonic-gate# a legal notice, here is a blessing:
67c478bd9Sstevel@tonic-gate#
77c478bd9Sstevel@tonic-gate#    May you do good and not evil.
87c478bd9Sstevel@tonic-gate#    May you find forgiveness for yourself and forgive others.
97c478bd9Sstevel@tonic-gate#    May you share freely, never taking more than you give.
107c478bd9Sstevel@tonic-gate#
117c478bd9Sstevel@tonic-gate#***********************************************************************
127c478bd9Sstevel@tonic-gate# This file implements regression tests for SQLite library.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# This file implements tests for the SQLITE_MISUSE detection logic.
157c478bd9Sstevel@tonic-gate# This test file leaks memory and file descriptors.
167c478bd9Sstevel@tonic-gate#
177c478bd9Sstevel@tonic-gate# $Id: misuse.test,v 1.4 2004/01/07 19:24:48 drh Exp $
187c478bd9Sstevel@tonic-gate
197c478bd9Sstevel@tonic-gateset testdir [file dirname $argv0]
207c478bd9Sstevel@tonic-gatesource $testdir/tester.tcl
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate# Make sure the test logic works
237c478bd9Sstevel@tonic-gate#
247c478bd9Sstevel@tonic-gatedo_test misuse-1.1 {
257c478bd9Sstevel@tonic-gate  db close
267c478bd9Sstevel@tonic-gate  catch {file delete -force test2.db}
277c478bd9Sstevel@tonic-gate  set ::DB [sqlite db test2.db]
287c478bd9Sstevel@tonic-gate  execsql {
297c478bd9Sstevel@tonic-gate    CREATE TABLE t1(a,b);
307c478bd9Sstevel@tonic-gate    INSERT INTO t1 VALUES(1,2);
317c478bd9Sstevel@tonic-gate  }
327c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
337c478bd9Sstevel@tonic-gate} {0 {a b 1 2}}
347c478bd9Sstevel@tonic-gatedo_test misuse-1.2 {
357c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::DB {SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1} {}
367c478bd9Sstevel@tonic-gate} {1 {no such function: x_coalesce}}
377c478bd9Sstevel@tonic-gatedo_test misuse-1.3 {
387c478bd9Sstevel@tonic-gate  sqlite_create_function $::DB
397c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::DB {SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1} {}
407c478bd9Sstevel@tonic-gate} {0 {xyz 1}}
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate# Use the x_sqlite_exec() SQL function to simulate the effect of two
437c478bd9Sstevel@tonic-gate# threads trying to use the same database at the same time.
447c478bd9Sstevel@tonic-gate#
457c478bd9Sstevel@tonic-gate# It used to be prohibited to invoke sqlite_exec() from within a function,
467c478bd9Sstevel@tonic-gate# but that has changed.  The following tests used to cause errors but now
477c478bd9Sstevel@tonic-gate# they do not.
487c478bd9Sstevel@tonic-gate#
497c478bd9Sstevel@tonic-gatedo_test misuse-1.4 {
507c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::DB {
517c478bd9Sstevel@tonic-gate     SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz;
527c478bd9Sstevel@tonic-gate  } {}
537c478bd9Sstevel@tonic-gate} {0 {xyz {1 2}}}
547c478bd9Sstevel@tonic-gatedo_test misuse-1.5 {
557c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
567c478bd9Sstevel@tonic-gate} {0 {a b 1 2}}
577c478bd9Sstevel@tonic-gatedo_test misuse-1.6 {
587c478bd9Sstevel@tonic-gate  catchsql {
597c478bd9Sstevel@tonic-gate    SELECT * FROM t1
607c478bd9Sstevel@tonic-gate  }
617c478bd9Sstevel@tonic-gate} {0 {1 2}}
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate# Attempt to register a new SQL function while an sqlite_exec() is active.
647c478bd9Sstevel@tonic-gate#
657c478bd9Sstevel@tonic-gatedo_test misuse-2.1 {
667c478bd9Sstevel@tonic-gate  db close
677c478bd9Sstevel@tonic-gate  set ::DB [sqlite db test2.db]
687c478bd9Sstevel@tonic-gate  execsql {
697c478bd9Sstevel@tonic-gate    SELECT * FROM t1
707c478bd9Sstevel@tonic-gate  }
717c478bd9Sstevel@tonic-gate} {1 2}
727c478bd9Sstevel@tonic-gatedo_test misuse-2.2 {
737c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
747c478bd9Sstevel@tonic-gate} {0 {a b 1 2}}
757c478bd9Sstevel@tonic-gatedo_test misuse-2.3 {
767c478bd9Sstevel@tonic-gate  set v [catch {
777c478bd9Sstevel@tonic-gate    db eval {SELECT * FROM t1} {} {
787c478bd9Sstevel@tonic-gate      sqlite_create_function $::DB
797c478bd9Sstevel@tonic-gate    }
807c478bd9Sstevel@tonic-gate  } msg]
817c478bd9Sstevel@tonic-gate  lappend v $msg
827c478bd9Sstevel@tonic-gate} {1 {library routine called out of sequence}}
837c478bd9Sstevel@tonic-gatedo_test misuse-2.4 {
847c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
857c478bd9Sstevel@tonic-gate} {21 {library routine called out of sequence}}
867c478bd9Sstevel@tonic-gatedo_test misuse-2.5 {
877c478bd9Sstevel@tonic-gate  catchsql {
887c478bd9Sstevel@tonic-gate    SELECT * FROM t1
897c478bd9Sstevel@tonic-gate  }
907c478bd9Sstevel@tonic-gate} {1 {library routine called out of sequence}}
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate# Attempt to register a new SQL aggregate while an sqlite_exec() is active.
937c478bd9Sstevel@tonic-gate#
947c478bd9Sstevel@tonic-gatedo_test misuse-3.1 {
957c478bd9Sstevel@tonic-gate  db close
967c478bd9Sstevel@tonic-gate  set ::DB [sqlite db test2.db]
977c478bd9Sstevel@tonic-gate  execsql {
987c478bd9Sstevel@tonic-gate    SELECT * FROM t1
997c478bd9Sstevel@tonic-gate  }
1007c478bd9Sstevel@tonic-gate} {1 2}
1017c478bd9Sstevel@tonic-gatedo_test misuse-3.2 {
1027c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
1037c478bd9Sstevel@tonic-gate} {0 {a b 1 2}}
1047c478bd9Sstevel@tonic-gatedo_test misuse-3.3 {
1057c478bd9Sstevel@tonic-gate  set v [catch {
1067c478bd9Sstevel@tonic-gate    db eval {SELECT * FROM t1} {} {
1077c478bd9Sstevel@tonic-gate      sqlite_create_aggregate $::DB
1087c478bd9Sstevel@tonic-gate    }
1097c478bd9Sstevel@tonic-gate  } msg]
1107c478bd9Sstevel@tonic-gate  lappend v $msg
1117c478bd9Sstevel@tonic-gate} {1 {library routine called out of sequence}}
1127c478bd9Sstevel@tonic-gatedo_test misuse-3.4 {
1137c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
1147c478bd9Sstevel@tonic-gate} {21 {library routine called out of sequence}}
1157c478bd9Sstevel@tonic-gatedo_test misuse-3.5 {
1167c478bd9Sstevel@tonic-gate  catchsql {
1177c478bd9Sstevel@tonic-gate    SELECT * FROM t1
1187c478bd9Sstevel@tonic-gate  }
1197c478bd9Sstevel@tonic-gate} {1 {library routine called out of sequence}}
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate# Attempt to close the database from an sqlite_exec callback.
1227c478bd9Sstevel@tonic-gate#
1237c478bd9Sstevel@tonic-gatedo_test misuse-4.1 {
1247c478bd9Sstevel@tonic-gate  db close
1257c478bd9Sstevel@tonic-gate  set ::DB [sqlite db test2.db]
1267c478bd9Sstevel@tonic-gate  execsql {
1277c478bd9Sstevel@tonic-gate    SELECT * FROM t1
1287c478bd9Sstevel@tonic-gate  }
1297c478bd9Sstevel@tonic-gate} {1 2}
1307c478bd9Sstevel@tonic-gatedo_test misuse-4.2 {
1317c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
1327c478bd9Sstevel@tonic-gate} {0 {a b 1 2}}
1337c478bd9Sstevel@tonic-gatedo_test misuse-4.3 {
1347c478bd9Sstevel@tonic-gate  set v [catch {
1357c478bd9Sstevel@tonic-gate    db eval {SELECT * FROM t1} {} {
1367c478bd9Sstevel@tonic-gate      sqlite_close $::DB
1377c478bd9Sstevel@tonic-gate    }
1387c478bd9Sstevel@tonic-gate  } msg]
1397c478bd9Sstevel@tonic-gate  lappend v $msg
1407c478bd9Sstevel@tonic-gate} {1 {library routine called out of sequence}}
1417c478bd9Sstevel@tonic-gatedo_test misuse-4.4 {
1427c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
1437c478bd9Sstevel@tonic-gate} {21 {library routine called out of sequence}}
1447c478bd9Sstevel@tonic-gatedo_test misuse-4.5 {
1457c478bd9Sstevel@tonic-gate  catchsql {
1467c478bd9Sstevel@tonic-gate    SELECT * FROM t1
1477c478bd9Sstevel@tonic-gate  }
1487c478bd9Sstevel@tonic-gate} {1 {library routine called out of sequence}}
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate# Attempt to use a database after it has been closed.
1517c478bd9Sstevel@tonic-gate#
1527c478bd9Sstevel@tonic-gatedo_test misuse-5.1 {
1537c478bd9Sstevel@tonic-gate  db close
1547c478bd9Sstevel@tonic-gate  set ::DB [sqlite db test2.db]
1557c478bd9Sstevel@tonic-gate  execsql {
1567c478bd9Sstevel@tonic-gate    SELECT * FROM t1
1577c478bd9Sstevel@tonic-gate  }
1587c478bd9Sstevel@tonic-gate} {1 2}
1597c478bd9Sstevel@tonic-gatedo_test misuse-5.2 {
1607c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
1617c478bd9Sstevel@tonic-gate} {0 {a b 1 2}}
1627c478bd9Sstevel@tonic-gatedo_test misuse-5.3 {
1637c478bd9Sstevel@tonic-gate  db close
1647c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::DB {SELECT * FROM t1} {}
1657c478bd9Sstevel@tonic-gate} {21 {library routine called out of sequence}}
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gatefinish_test
168