162e603a9Sdrh# 2014-05-07 262e603a9Sdrh# 362e603a9Sdrh# The author disclaims copyright to this source code. In place of 462e603a9Sdrh# a legal notice, here is a blessing: 562e603a9Sdrh# 662e603a9Sdrh# May you do good and not evil. 762e603a9Sdrh# May you find forgiveness for yourself and forgive others. 862e603a9Sdrh# May you share freely, never taking more than you give. 962e603a9Sdrh# 1062e603a9Sdrh#*********************************************************************** 1162e603a9Sdrh# 1262e603a9Sdrh# This file implements regression tests for SQLite library. The 1362e603a9Sdrh# focus of this file is testing the nolock=1 and immutable=1 query 1462e603a9Sdrh# parameters and the SQLITE_IOCAP_IMMUTABLE device characteristic. 1562e603a9Sdrh# 1662e603a9Sdrh 1762e603a9Sdrhset testdir [file dirname $argv0] 1862e603a9Sdrhsource $testdir/tester.tcl 1962e603a9Sdrh 2062e603a9Sdrhunset -nocomplain tvfs_calls 2162e603a9Sdrhproc tvfs_reset {} { 2262e603a9Sdrh global tvfs_calls 2362e603a9Sdrh array set tvfs_calls {xLock 0 xUnlock 0 xCheckReservedLock 0 xAccess 0} 2462e603a9Sdrh} 2562e603a9Sdrhproc tvfs_callback {op args} { 2662e603a9Sdrh global tvfs_calls 2762e603a9Sdrh incr tvfs_calls($op) 2862e603a9Sdrh return SQLITE_OK 2962e603a9Sdrh} 3062e603a9Sdrhtvfs_reset 3162e603a9Sdrh 3262e603a9Sdrhtestvfs tvfs 3362e603a9Sdrhtvfs script tvfs_callback 3462e603a9Sdrhtvfs filter {xLock xUnlock xCheckReservedLock xAccess} 3562e603a9Sdrh 3662e603a9Sdrh############################################################################ 3762e603a9Sdrh# Verify that the nolock=1 query parameter for URI filenames disables all 3862e603a9Sdrh# calls to xLock and xUnlock for rollback databases. 3962e603a9Sdrh# 4062e603a9Sdrhdo_test nolock-1.0 { 4162e603a9Sdrh db close 4262e603a9Sdrh forcedelete test.db 4362e603a9Sdrh tvfs_reset 4462e603a9Sdrh sqlite db test.db -vfs tvfs 4562e603a9Sdrh db eval {CREATE TABLE t1(a,b,c); INSERT INTO t1 VALUES(1,2,3);} 4662e603a9Sdrh list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \ 4762e603a9Sdrh xCheckReservedLock $::tvfs_calls(xCheckReservedLock) 4862e603a9Sdrh} {xLock 7 xUnlock 5 xCheckReservedLock 0} 4962e603a9Sdrh 5062e603a9Sdrhdo_test nolock-1.1 { 5162e603a9Sdrh db close 5262e603a9Sdrh forcedelete test.db 5362e603a9Sdrh tvfs_reset 5462e603a9Sdrh sqlite db file:test.db?nolock=0 -vfs tvfs -uri 1 5562e603a9Sdrh db eval {CREATE TABLE t1(a,b,c); INSERT INTO t1 VALUES(1,2,3);} 5662e603a9Sdrh list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \ 5762e603a9Sdrh xCheckReservedLock $::tvfs_calls(xCheckReservedLock) 5862e603a9Sdrh} {xLock 7 xUnlock 5 xCheckReservedLock 0} 5962e603a9Sdrh 6062e603a9Sdrhdo_test nolock-1.2 { 6162e603a9Sdrh db close 6262e603a9Sdrh forcedelete test.db 6362e603a9Sdrh tvfs_reset 6462e603a9Sdrh sqlite db file:test.db?nolock=1 -vfs tvfs -uri 1 6562e603a9Sdrh db eval {CREATE TABLE t1(a,b,c); INSERT INTO t1 VALUES(1,2,3);} 6662e603a9Sdrh list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \ 6762e603a9Sdrh xCheckReservedLock $::tvfs_calls(xCheckReservedLock) 6862e603a9Sdrh} {xLock 0 xUnlock 0 xCheckReservedLock 0} 6962e603a9Sdrh 706451c2b0Sdrhdo_test nolock-1.3 { 716451c2b0Sdrh db close 726451c2b0Sdrh tvfs_reset 736451c2b0Sdrh sqlite db file:test.db?nolock=0 -vfs tvfs -uri 1 -readonly 1 746451c2b0Sdrh db eval {SELECT * FROM t1} 756451c2b0Sdrh list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \ 766451c2b0Sdrh xCheckReservedLock $::tvfs_calls(xCheckReservedLock) 776451c2b0Sdrh} {xLock 2 xUnlock 2 xCheckReservedLock 0} 786451c2b0Sdrh 796451c2b0Sdrhdo_test nolock-1.4 { 806451c2b0Sdrh db close 816451c2b0Sdrh tvfs_reset 826451c2b0Sdrh sqlite db file:test.db?nolock=1 -vfs tvfs -uri 1 -readonly 1 836451c2b0Sdrh db eval {SELECT * FROM t1} 846451c2b0Sdrh list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \ 856451c2b0Sdrh xCheckReservedLock $::tvfs_calls(xCheckReservedLock) 866451c2b0Sdrh} {xLock 0 xUnlock 0 xCheckReservedLock 0} 876451c2b0Sdrh 8862e603a9Sdrh############################################################################# 8962e603a9Sdrh# Verify that immutable=1 disables both locking and xAccess calls to the 9062e603a9Sdrh# journal files. 9162e603a9Sdrh# 9262e603a9Sdrhdo_test nolock-2.0 { 9362e603a9Sdrh db close 9462e603a9Sdrh forcedelete test.db 9562e603a9Sdrh # begin by creating a test database 9662e603a9Sdrh sqlite3 db test.db 9762e603a9Sdrh db eval { 9862e603a9Sdrh CREATE TABLE t1(a,b); 9962e603a9Sdrh INSERT INTO t1 VALUES('hello','world'); 10062e603a9Sdrh CREATE TABLE t2(x,y); 10162e603a9Sdrh INSERT INTO t2 VALUES(12345,67890); 10262e603a9Sdrh SELECT * FROM t1, t2; 10362e603a9Sdrh } 10462e603a9Sdrh} {hello world 12345 67890} 10562e603a9Sdrhdo_test nolock-2.1 { 10662e603a9Sdrh tvfs_reset 10762e603a9Sdrh sqlite3 db2 test.db -vfs tvfs 10862e603a9Sdrh db2 eval {SELECT * FROM t1, t2} 10962e603a9Sdrh} {hello world 12345 67890} 11062e603a9Sdrhdo_test nolock-2.2 { 11162e603a9Sdrh list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \ 11262e603a9Sdrh xCheckReservedLock $::tvfs_calls(xCheckReservedLock) \ 11362e603a9Sdrh xAccess $::tvfs_calls(xAccess) 11462e603a9Sdrh} {xLock 2 xUnlock 2 xCheckReservedLock 0 xAccess 4} 11562e603a9Sdrh 11662e603a9Sdrh 11762e603a9Sdrhdo_test nolock-2.11 { 11862e603a9Sdrh db2 close 11962e603a9Sdrh tvfs_reset 12062e603a9Sdrh sqlite3 db2 file:test.db?immutable=0 -vfs tvfs -uri 1 12162e603a9Sdrh db2 eval {SELECT * FROM t1, t2} 12262e603a9Sdrh} {hello world 12345 67890} 12362e603a9Sdrhdo_test nolock-2.12 { 12462e603a9Sdrh list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \ 12562e603a9Sdrh xCheckReservedLock $::tvfs_calls(xCheckReservedLock) \ 12662e603a9Sdrh xAccess $::tvfs_calls(xAccess) 12762e603a9Sdrh} {xLock 2 xUnlock 2 xCheckReservedLock 0 xAccess 4} 12862e603a9Sdrh 12962e603a9Sdrh 13062e603a9Sdrhdo_test nolock-2.21 { 13162e603a9Sdrh db2 close 13262e603a9Sdrh tvfs_reset 13362e603a9Sdrh sqlite3 db2 file:test.db?immutable=1 -vfs tvfs -uri 1 13462e603a9Sdrh db2 eval {SELECT * FROM t1, t2} 13562e603a9Sdrh} {hello world 12345 67890} 13662e603a9Sdrhdo_test nolock-2.22 { 13762e603a9Sdrh list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \ 13862e603a9Sdrh xCheckReservedLock $::tvfs_calls(xCheckReservedLock) \ 13962e603a9Sdrh xAccess $::tvfs_calls(xAccess) 14062e603a9Sdrh} {xLock 0 xUnlock 0 xCheckReservedLock 0 xAccess 0} 14162e603a9Sdrh 1426451c2b0Sdrhdo_test nolock-2.31 { 1436451c2b0Sdrh db2 close 1446451c2b0Sdrh tvfs_reset 1456451c2b0Sdrh sqlite3 db2 file:test.db?immutable=1 -vfs tvfs -uri 1 -readonly 1 1466451c2b0Sdrh db2 eval {SELECT * FROM t1, t2} 1476451c2b0Sdrh} {hello world 12345 67890} 1486451c2b0Sdrhdo_test nolock-2.32 { 1496451c2b0Sdrh list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \ 1506451c2b0Sdrh xCheckReservedLock $::tvfs_calls(xCheckReservedLock) \ 1516451c2b0Sdrh xAccess $::tvfs_calls(xAccess) 1526451c2b0Sdrh} {xLock 0 xUnlock 0 xCheckReservedLock 0 xAccess 0} 1536451c2b0Sdrh 15462e603a9Sdrh############################################################################ 15562e603a9Sdrh# Verify that the SQLITE_IOCAP_IMMUTABLE flag works 15662e603a9Sdrh# 15762e603a9Sdrhdo_test nolock-3.1 { 15862e603a9Sdrh db2 close 15962e603a9Sdrh tvfs devchar immutable 16062e603a9Sdrh tvfs_reset 16162e603a9Sdrh sqlite3 db2 test.db -vfs tvfs 16262e603a9Sdrh db2 eval {SELECT * FROM t1, t2} 16362e603a9Sdrh} {hello world 12345 67890} 16462e603a9Sdrhdo_test nolock-3.2 { 16562e603a9Sdrh list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \ 16662e603a9Sdrh xCheckReservedLock $::tvfs_calls(xCheckReservedLock) \ 16762e603a9Sdrh xAccess $::tvfs_calls(xAccess) 16862e603a9Sdrh} {xLock 0 xUnlock 0 xCheckReservedLock 0 xAccess 0} 16962e603a9Sdrh 1706451c2b0Sdrhdo_test nolock-3.11 { 1716451c2b0Sdrh db2 close 1726451c2b0Sdrh tvfs_reset 1736451c2b0Sdrh sqlite3 db2 test.db -vfs tvfs -readonly 1 1746451c2b0Sdrh db2 eval {SELECT * FROM t1, t2} 1756451c2b0Sdrh} {hello world 12345 67890} 1766451c2b0Sdrhdo_test nolock-3.12 { 1776451c2b0Sdrh list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \ 1786451c2b0Sdrh xCheckReservedLock $::tvfs_calls(xCheckReservedLock) \ 1796451c2b0Sdrh xAccess $::tvfs_calls(xAccess) 1806451c2b0Sdrh} {xLock 0 xUnlock 0 xCheckReservedLock 0 xAccess 0} 1816451c2b0Sdrh 18262e603a9Sdrhdb2 close 18362e603a9Sdrhdb close 18462e603a9Sdrhtvfs delete 185ffbb02a3Sdrh 186*4567beefSdrhif {[permutation]!="inmemory_journal"} { 187*4567beefSdrh # 2016-03-11: Make sure all works when transitioning to WAL mode 188*4567beefSdrh # under nolock. 189ffbb02a3Sdrh # 190ffbb02a3Sdrh do_test nolock-4.1 { 191ffbb02a3Sdrh forcedelete test.db 192ffbb02a3Sdrh sqlite3 db file:test.db?nolock=1 -uri 1 193ffbb02a3Sdrh db eval { 194ffbb02a3Sdrh PRAGMA journal_mode=WAL; 195ffbb02a3Sdrh CREATE TABLE t1(x); 196ffbb02a3Sdrh INSERT INTO t1 VALUES('youngling'); 197ffbb02a3Sdrh SELECT * FROM t1; 198ffbb02a3Sdrh } 199ffbb02a3Sdrh } {delete youngling} 200ffbb02a3Sdrh db close 201ffbb02a3Sdrh 202ffbb02a3Sdrh do_test nolock-4.2 { 203ffbb02a3Sdrh forcedelete test.db 204ffbb02a3Sdrh sqlite3 db test.db 205ffbb02a3Sdrh db eval { 206ffbb02a3Sdrh PRAGMA journal_mode=WAL; 207ffbb02a3Sdrh CREATE TABLE t1(x); 208ffbb02a3Sdrh INSERT INTO t1 VALUES('catbird'); 209ffbb02a3Sdrh SELECT * FROM t1; 210ffbb02a3Sdrh } 211ffbb02a3Sdrh } {wal catbird} 212ffbb02a3Sdrh do_test nolock-4.3 { 213ffbb02a3Sdrh db close 214ffbb02a3Sdrh sqlite3 db file:test.db?nolock=1 -uri 1 215ffbb02a3Sdrh set rc [catch {db eval {SELECT * FROM t1}} msg] 216ffbb02a3Sdrh lappend rc $msg 217ffbb02a3Sdrh } {1 {unable to open database file}} 218*4567beefSdrh} 219ffbb02a3Sdrh 22062e603a9Sdrhfinish_test 221