1# 2009 August 20
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 to verify that ticket #4018 has been
14# fixed.  
15#
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20proc testsql {sql} {
21  set fd [open tf_main.tcl w]
22  puts $fd [subst -nocommands {
23    sqlite3_test_control_pending_byte 0x0010000
24    sqlite3 db test.db
25    set rc [catch { db eval {$sql} } msg]
26    puts -nonewline "[set rc] {[set msg]}"
27    flush stdout
28    exit
29  }]
30  close $fd
31  set fd [open "| [info nameofexec] ./tf_main.tcl" r] 
32  set res [read $fd]
33  close $fd
34  return $res
35}
36
37do_test tkt4018-1.1 {
38  execsql {
39    CREATE TABLE t1(a, b);
40    BEGIN;
41    SELECT * FROM t1;
42  }
43} {}
44
45# The database is locked by connection [db]. Open and close a second
46# connection to test.db 10000 times. If file-descriptors are not being
47# reused, then the process will quickly exceed its maximum number of
48# file descriptors (1024 by default on linux).
49do_test tkt4018-1.2 {
50  for {set i 0} {$i < 10000} {incr i} {
51    sqlite3 db2 test.db
52    db2 close
53  }
54} {}
55
56# Now check that connection [db] is still holding a SHARED lock by
57# having a second process try to write the db.
58do_test tkt4018-1.3 {
59  testsql {INSERT INTO t1 VALUES(3, 4)}
60} {1 {database is locked}}
61
62# Sanity checking. Have [db] release the lock and then retry the
63# INSERT from the previous test case.
64do_test tkt4018-1.4 {
65  db eval COMMIT
66  testsql {INSERT INTO t1 VALUES(3, 4)}
67} {0 {}}
68
69# Check that reusing a file descriptor cannot change a read-only 
70# connection into a read-write connection.
71do_test tkt4018-2.1 {
72  sqlite3 db2 test.db
73  execsql {INSERT INTO t1 VALUES(1, 2)} db2
74} {}
75do_test tkt4018-2.2 {
76  execsql {
77    BEGIN;
78    SELECT * FROM t1 ORDER BY a;
79  }
80} {1 2 3 4}
81do_test tkt4018-2.3 {
82  db2 close
83  sqlite3 db2 test.db -readonly 1
84  execsql COMMIT
85  catchsql {INSERT INTO t1 VALUES(5, 6)} db2
86} {1 {attempt to write a readonly database}}
87db2 close
88
89finish_test
90