000001  # 2007 April 24
000002  #
000003  # The author disclaims copyright to this source code.  In place of
000004  # a legal notice, here is a blessing:
000005  #
000006  #    May you do good and not evil.
000007  #    May you find forgiveness for yourself and forgive others.
000008  #    May you share freely, never taking more than you give.
000009  #
000010  #***********************************************************************
000011  # This file implements regression tests for SQLite library.
000012  #
000013  # This file implements tests to make sure SQLite treats a database
000014  # as readonly if its write version is set to  high.
000015  #
000016  # $Id: rdonly.test,v 1.2 2008/07/08 10:19:58 danielk1977 Exp $
000017  
000018  set testdir [file dirname $argv0]
000019  source $testdir/tester.tcl
000020  
000021  # Do not use a codec for tests in this file, as the database file is
000022  # manipulated directly using tcl scripts (using the [hexio_write] command).
000023  #
000024  do_not_use_codec
000025  
000026  # Create a database.
000027  #
000028  do_test rdonly-1.1 {
000029    execsql {
000030      CREATE TABLE t1(x);
000031      INSERT INTO t1 VALUES(1);
000032      SELECT * FROM t1;
000033    }
000034  } {1}
000035  
000036  # EVIDENCE-OF: R-29639-16887 The sqlite3_db_readonly(D,N) interface
000037  # returns 1 if the database N of connection D is read-only, 0 if it is
000038  # read/write, or -1 if N is not the name of a database on connection D.
000039  #
000040  do_test rdonly-1.1.1 {
000041    sqlite3_db_readonly db main
000042  } {0}
000043  
000044  # Changes the write version from 1 to 3.  Verify that the database
000045  # can be read but not written.
000046  #
000047  do_test rdonly-1.2 {
000048    db close
000049    hexio_get_int [hexio_read test.db 18 1]
000050  } 1
000051  do_test rdonly-1.3 {
000052    hexio_write test.db 18 03
000053    sqlite3 db test.db
000054    execsql {
000055      SELECT * FROM t1;
000056    }
000057  } {1}
000058  do_test rdonly-1.3.1 {
000059    sqlite3_db_readonly db main
000060  } {1}
000061  do_test rdonly-1.4 {
000062    catchsql {
000063      INSERT INTO t1 VALUES(2)
000064    }
000065  } {1 {attempt to write a readonly database}}
000066  
000067  # Change the write version back to 1.  Verify that the database
000068  # is read-write again.
000069  #
000070  do_test rdonly-1.5 {
000071    db close
000072    hexio_write test.db 18 01
000073    sqlite3 db test.db
000074    catchsql {
000075      INSERT INTO t1 VALUES(2);
000076      SELECT * FROM t1;
000077    }
000078  } {0 {1 2}}
000079  
000080  # Now, after connection [db] has loaded the database schema, modify the
000081  # write-version of the file (and the change-counter, so that the 
000082  # write-version is reloaded). This way, SQLite does not discover that
000083  # the database is read-only until after it is locked.
000084  #
000085  set ro_version 02
000086  ifcapable wal { set ro_version 03 }
000087  do_test rdonly-1.6 {
000088    hexio_write test.db 18 $ro_version     ; # write-version
000089    hexio_write test.db 24 11223344        ; # change-counter
000090    catchsql {
000091      INSERT INTO t1 VALUES(2);
000092    }
000093  } {1 {attempt to write a readonly database}}
000094  
000095  finish_test