xref: /sqlite-3.40.0/test/rdonly.test (revision 8dd7a6a9)
1309169a1Sdrh# 2007 April 24
2309169a1Sdrh#
3309169a1Sdrh# The author disclaims copyright to this source code.  In place of
4309169a1Sdrh# a legal notice, here is a blessing:
5309169a1Sdrh#
6309169a1Sdrh#    May you do good and not evil.
7309169a1Sdrh#    May you find forgiveness for yourself and forgive others.
8309169a1Sdrh#    May you share freely, never taking more than you give.
9309169a1Sdrh#
10309169a1Sdrh#***********************************************************************
11309169a1Sdrh# This file implements regression tests for SQLite library.
12309169a1Sdrh#
13309169a1Sdrh# This file implements tests to make sure SQLite treats a database
14309169a1Sdrh# as readonly if its write version is set to  high.
15309169a1Sdrh#
163aa4b67fSdanielk1977# $Id: rdonly.test,v 1.2 2008/07/08 10:19:58 danielk1977 Exp $
17309169a1Sdrh
18309169a1Sdrhset testdir [file dirname $argv0]
19309169a1Sdrhsource $testdir/tester.tcl
20309169a1Sdrh
2168928b6cSdan# Do not use a codec for tests in this file, as the database file is
2268928b6cSdan# manipulated directly using tcl scripts (using the [hexio_write] command).
2368928b6cSdan#
2468928b6cSdando_not_use_codec
25309169a1Sdrh
26309169a1Sdrh# Create a database.
27309169a1Sdrh#
28309169a1Sdrhdo_test rdonly-1.1 {
29309169a1Sdrh  execsql {
30309169a1Sdrh    CREATE TABLE t1(x);
31309169a1Sdrh    INSERT INTO t1 VALUES(1);
32309169a1Sdrh    SELECT * FROM t1;
33309169a1Sdrh  }
34309169a1Sdrh} {1}
35*8dd7a6a9Sdrh
36*8dd7a6a9Sdrh# EVIDENCE-OF: R-29639-16887 The sqlite3_db_readonly(D,N) interface
37*8dd7a6a9Sdrh# returns 1 if the database N of connection D is read-only, 0 if it is
38*8dd7a6a9Sdrh# read/write, or -1 if N is not the name of a database on connection D.
39*8dd7a6a9Sdrh#
40781597feSdrhdo_test rdonly-1.1.1 {
41781597feSdrh  sqlite3_db_readonly db main
42781597feSdrh} {0}
43309169a1Sdrh
4428e5386fSdan# Changes the write version from 1 to 3.  Verify that the database
45309169a1Sdrh# can be read but not written.
46309169a1Sdrh#
47309169a1Sdrhdo_test rdonly-1.2 {
48309169a1Sdrh  db close
49309169a1Sdrh  hexio_get_int [hexio_read test.db 18 1]
50309169a1Sdrh} 1
51309169a1Sdrhdo_test rdonly-1.3 {
5228e5386fSdan  hexio_write test.db 18 03
53309169a1Sdrh  sqlite3 db test.db
54309169a1Sdrh  execsql {
55309169a1Sdrh    SELECT * FROM t1;
56309169a1Sdrh  }
57309169a1Sdrh} {1}
58781597feSdrhdo_test rdonly-1.3.1 {
59781597feSdrh  sqlite3_db_readonly db main
60781597feSdrh} {1}
61309169a1Sdrhdo_test rdonly-1.4 {
62309169a1Sdrh  catchsql {
63309169a1Sdrh    INSERT INTO t1 VALUES(2)
64309169a1Sdrh  }
65309169a1Sdrh} {1 {attempt to write a readonly database}}
66309169a1Sdrh
67309169a1Sdrh# Change the write version back to 1.  Verify that the database
68309169a1Sdrh# is read-write again.
69309169a1Sdrh#
70309169a1Sdrhdo_test rdonly-1.5 {
71309169a1Sdrh  db close
72309169a1Sdrh  hexio_write test.db 18 01
73309169a1Sdrh  sqlite3 db test.db
74309169a1Sdrh  catchsql {
75309169a1Sdrh    INSERT INTO t1 VALUES(2);
76309169a1Sdrh    SELECT * FROM t1;
77309169a1Sdrh  }
78309169a1Sdrh} {0 {1 2}}
79309169a1Sdrh
803aa4b67fSdanielk1977# Now, after connection [db] has loaded the database schema, modify the
813aa4b67fSdanielk1977# write-version of the file (and the change-counter, so that the
823aa4b67fSdanielk1977# write-version is reloaded). This way, SQLite does not discover that
833aa4b67fSdanielk1977# the database is read-only until after it is locked.
843aa4b67fSdanielk1977#
855cf53537Sdanset ro_version 02
865cf53537Sdanifcapable wal { set ro_version 03 }
873aa4b67fSdanielk1977do_test rdonly-1.6 {
885cf53537Sdan  hexio_write test.db 18 $ro_version     ; # write-version
893aa4b67fSdanielk1977  hexio_write test.db 24 11223344        ; # change-counter
903aa4b67fSdanielk1977  catchsql {
913aa4b67fSdanielk1977    INSERT INTO t1 VALUES(2);
923aa4b67fSdanielk1977  }
933aa4b67fSdanielk1977} {1 {attempt to write a readonly database}}
943aa4b67fSdanielk1977
95309169a1Sdrhfinish_test
96