drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1 | # 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 | # |
danielk1977 | 3aa4b67 | 2008-07-08 10:19:58 +0000 | [diff] [blame] | 16 | # $Id: rdonly.test,v 1.2 2008/07/08 10:19:58 danielk1977 Exp $ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
| 20 | |
dan | 68928b6 | 2010-06-22 13:46:43 +0000 | [diff] [blame] | 21 | # 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 | # |
| 24 | do_not_use_codec |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 25 | |
| 26 | # Create a database. |
| 27 | # |
| 28 | do_test rdonly-1.1 { |
| 29 | execsql { |
| 30 | CREATE TABLE t1(x); |
| 31 | INSERT INTO t1 VALUES(1); |
| 32 | SELECT * FROM t1; |
| 33 | } |
| 34 | } {1} |
| 35 | |
dan | 28e5386 | 2010-04-21 06:19:12 +0000 | [diff] [blame] | 36 | # Changes the write version from 1 to 3. Verify that the database |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 37 | # can be read but not written. |
| 38 | # |
| 39 | do_test rdonly-1.2 { |
| 40 | db close |
| 41 | hexio_get_int [hexio_read test.db 18 1] |
| 42 | } 1 |
| 43 | do_test rdonly-1.3 { |
dan | 28e5386 | 2010-04-21 06:19:12 +0000 | [diff] [blame] | 44 | hexio_write test.db 18 03 |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 45 | sqlite3 db test.db |
| 46 | execsql { |
| 47 | SELECT * FROM t1; |
| 48 | } |
| 49 | } {1} |
| 50 | do_test rdonly-1.4 { |
| 51 | catchsql { |
| 52 | INSERT INTO t1 VALUES(2) |
| 53 | } |
| 54 | } {1 {attempt to write a readonly database}} |
| 55 | |
| 56 | # Change the write version back to 1. Verify that the database |
| 57 | # is read-write again. |
| 58 | # |
| 59 | do_test rdonly-1.5 { |
| 60 | db close |
| 61 | hexio_write test.db 18 01 |
| 62 | sqlite3 db test.db |
| 63 | catchsql { |
| 64 | INSERT INTO t1 VALUES(2); |
| 65 | SELECT * FROM t1; |
| 66 | } |
| 67 | } {0 {1 2}} |
| 68 | |
danielk1977 | 3aa4b67 | 2008-07-08 10:19:58 +0000 | [diff] [blame] | 69 | # Now, after connection [db] has loaded the database schema, modify the |
| 70 | # write-version of the file (and the change-counter, so that the |
| 71 | # write-version is reloaded). This way, SQLite does not discover that |
| 72 | # the database is read-only until after it is locked. |
| 73 | # |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 74 | set ro_version 02 |
| 75 | ifcapable wal { set ro_version 03 } |
danielk1977 | 3aa4b67 | 2008-07-08 10:19:58 +0000 | [diff] [blame] | 76 | do_test rdonly-1.6 { |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 77 | hexio_write test.db 18 $ro_version ; # write-version |
danielk1977 | 3aa4b67 | 2008-07-08 10:19:58 +0000 | [diff] [blame] | 78 | hexio_write test.db 24 11223344 ; # change-counter |
| 79 | catchsql { |
| 80 | INSERT INTO t1 VALUES(2); |
| 81 | } |
| 82 | } {1 {attempt to write a readonly database}} |
| 83 | |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 84 | finish_test |