blob: 404c613b2117363a2b8503ec3591f8989b163470 [file] [log] [blame]
drh309169a2007-04-24 17:27:51 +00001# 2007 April 24
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 make sure SQLite treats a database
14# as readonly if its write version is set to high.
15#
danielk19773aa4b672008-07-08 10:19:58 +000016# $Id: rdonly.test,v 1.2 2008/07/08 10:19:58 danielk1977 Exp $
drh309169a2007-04-24 17:27:51 +000017
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
dan68928b62010-06-22 13:46:43 +000021# Do not use a codec for tests in this file, as the database file is
22# manipulated directly using tcl scripts (using the [hexio_write] command).
23#
24do_not_use_codec
drh309169a2007-04-24 17:27:51 +000025
26# Create a database.
27#
28do_test rdonly-1.1 {
29 execsql {
30 CREATE TABLE t1(x);
31 INSERT INTO t1 VALUES(1);
32 SELECT * FROM t1;
33 }
34} {1}
drh8dd7a6a2015-03-06 04:37:26 +000035
36# EVIDENCE-OF: R-29639-16887 The sqlite3_db_readonly(D,N) interface
37# returns 1 if the database N of connection D is read-only, 0 if it is
38# read/write, or -1 if N is not the name of a database on connection D.
39#
drh781597f2014-05-21 08:21:07 +000040do_test rdonly-1.1.1 {
41 sqlite3_db_readonly db main
42} {0}
drh309169a2007-04-24 17:27:51 +000043
dan28e53862010-04-21 06:19:12 +000044# Changes the write version from 1 to 3. Verify that the database
drh309169a2007-04-24 17:27:51 +000045# can be read but not written.
46#
47do_test rdonly-1.2 {
48 db close
49 hexio_get_int [hexio_read test.db 18 1]
50} 1
51do_test rdonly-1.3 {
dan28e53862010-04-21 06:19:12 +000052 hexio_write test.db 18 03
drh309169a2007-04-24 17:27:51 +000053 sqlite3 db test.db
54 execsql {
55 SELECT * FROM t1;
56 }
57} {1}
drh781597f2014-05-21 08:21:07 +000058do_test rdonly-1.3.1 {
59 sqlite3_db_readonly db main
60} {1}
drh309169a2007-04-24 17:27:51 +000061do_test rdonly-1.4 {
62 catchsql {
63 INSERT INTO t1 VALUES(2)
64 }
65} {1 {attempt to write a readonly database}}
66
67# Change the write version back to 1. Verify that the database
68# is read-write again.
69#
70do_test rdonly-1.5 {
71 db close
72 hexio_write test.db 18 01
73 sqlite3 db test.db
74 catchsql {
75 INSERT INTO t1 VALUES(2);
76 SELECT * FROM t1;
77 }
78} {0 {1 2}}
79
danielk19773aa4b672008-07-08 10:19:58 +000080# Now, after connection [db] has loaded the database schema, modify the
81# write-version of the file (and the change-counter, so that the
82# write-version is reloaded). This way, SQLite does not discover that
83# the database is read-only until after it is locked.
84#
dan5cf53532010-05-01 16:40:20 +000085set ro_version 02
86ifcapable wal { set ro_version 03 }
danielk19773aa4b672008-07-08 10:19:58 +000087do_test rdonly-1.6 {
dan5cf53532010-05-01 16:40:20 +000088 hexio_write test.db 18 $ro_version ; # write-version
danielk19773aa4b672008-07-08 10:19:58 +000089 hexio_write test.db 24 11223344 ; # change-counter
90 catchsql {
91 INSERT INTO t1 VALUES(2);
92 }
93} {1 {attempt to write a readonly database}}
94
drh309169a2007-04-24 17:27:51 +000095finish_test