drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 1 | # 2009 February 4 |
| 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. The |
| 12 | # focus of this file is testing the "backup" and "restore" methods |
| 13 | # of the TCL interface - methods which are based on the |
| 14 | # sqlite3_backup_XXX API. |
| 15 | # |
danielk1977 | 2943c37 | 2009-04-07 14:14:22 +0000 | [diff] [blame^] | 16 | # $Id: backup2.test,v 1.4 2009/04/07 14:14:23 danielk1977 Exp $ |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
| 20 | |
danielk1977 | 2943c37 | 2009-04-07 14:14:22 +0000 | [diff] [blame^] | 21 | ifcapable !trigger||!view { finish_test ; return } |
| 22 | |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 23 | # Fill a database with test data. |
| 24 | # |
| 25 | do_test backup2-1 { |
| 26 | db eval { |
| 27 | CREATE TABLE t1(x); |
| 28 | INSERT INTO t1 VALUES(randstr(8000,8000)); |
| 29 | INSERT INTO t1 VALUES(randstr(8000,8000)); |
| 30 | INSERT INTO t1 VALUES(randstr(8000,8000)); |
| 31 | INSERT INTO t1 VALUES(randstr(8000,8000)); |
| 32 | INSERT INTO t1 VALUES(randstr(8000,8000)); |
| 33 | CREATE VIEW v1 AS SELECT substr(x,10,10) FROM t1; |
| 34 | CREATE TABLE t2(a,b); |
| 35 | INSERT INTO t2 VALUES(1,2); |
| 36 | INSERT INTO t2 VALUES(2,4); |
| 37 | INSERT INTO t2 SELECT a+2, (a+2)*2 FROM t2; |
| 38 | INSERT INTO t2 SELECT a+4, (a+4)*2 FROM t2; |
| 39 | INSERT INTO t2 SELECT a+8, (a+8)*2 FROM t2; |
| 40 | INSERT INTO t2 SELECT a+16, (a+16)*2 FROM t2; |
| 41 | INSERT INTO t2 SELECT a+32, (a+32)*2 FROM t2; |
| 42 | INSERT INTO t2 SELECT a+64, (a+64)*2 FROM t2; |
| 43 | INSERT INTO t2 SELECT a+128, (a+128)*2 FROM t2; |
| 44 | CREATE INDEX t2i1 ON t2(a,b); |
| 45 | CREATE TRIGGER r1 AFTER INSERT ON t2 BEGIN |
| 46 | SELECT 'hello'; |
| 47 | END; |
| 48 | ANALYZE; |
| 49 | PRAGMA integrity_check; |
| 50 | } |
| 51 | } {ok} |
| 52 | |
| 53 | # Remember a check-sum on the database file. |
| 54 | # |
| 55 | unset -nocomplain cksum |
| 56 | set cksum [dbcksum db main] |
| 57 | |
| 58 | # Make a backup of the test data. Verify that the backup copy |
| 59 | # is identical to the original. |
| 60 | # |
| 61 | do_test backup2-2 { |
| 62 | file delete -force bu1.db |
| 63 | db backup bu1.db |
| 64 | sqlite3 db2 bu1.db |
| 65 | dbcksum db2 main |
| 66 | } $cksum |
| 67 | |
| 68 | # Delete the original. Restore from backup. Verify the content is |
| 69 | # unchanged. |
| 70 | # |
| 71 | do_test backup2-3.1 { |
| 72 | db close |
| 73 | file delete -force test.db test.db-journal |
| 74 | sqlite3 db test.db |
| 75 | db2 eval {BEGIN EXCLUSIVE} |
| 76 | set rc [catch {db restore bu1.db} res] |
| 77 | lappend rc $res |
| 78 | db2 eval {ROLLBACK} |
| 79 | set rc |
| 80 | } {1 {restore failed: source database busy}} |
| 81 | do_test backup2-3.2 { |
| 82 | db close |
| 83 | file delete -force test.db test.db-journal |
| 84 | sqlite3 db test.db |
| 85 | db restore bu1.db |
| 86 | dbcksum db main |
| 87 | } $cksum |
| 88 | |
| 89 | # Use alternative databases - other than "main". |
| 90 | # |
| 91 | do_test backup2-4 { |
| 92 | db restore temp bu1.db |
| 93 | dbcksum db temp |
| 94 | } $cksum |
| 95 | do_test backup2-5 { |
| 96 | db2 close |
| 97 | file delete -force bu1.db bu2.db |
| 98 | db backup temp bu2.db |
| 99 | sqlite3 db2 bu2.db |
| 100 | dbcksum db2 main |
| 101 | } $cksum |
| 102 | |
| 103 | # Try to backup to a readonly file. |
| 104 | # |
| 105 | do_test backup2-6 { |
| 106 | db2 close |
| 107 | catch {file attributes bu2.db -permissions r--------} |
| 108 | catch {file attributes bu2.db -readonly 1} |
| 109 | set rc [catch {db backup temp bu2.db} res] |
| 110 | lappend rc $res |
| 111 | } {1 {backup failed: attempt to write a readonly database}} |
| 112 | |
| 113 | # Try to backup to something that is not a database file. |
| 114 | # |
| 115 | do_test backup2-7 { |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 116 | catch {file attributes bu2.db -readonly 0} |
danielk1977 | 308ef5a | 2009-03-18 13:55:29 +0000 | [diff] [blame] | 117 | catch {file attributes bu2.db -permissions rw-------} |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 118 | set out [open bu2.db w] |
| 119 | puts $out "This is not a valid database file" |
| 120 | close $out |
| 121 | set rc [catch {db backup temp bu2.db} res] |
| 122 | lappend rc $res |
| 123 | } {1 {backup failed: file is encrypted or is not a database}} |
| 124 | |
| 125 | # Try to backup database that does not exist |
| 126 | # |
| 127 | do_test backup2-8 { |
| 128 | file delete -force bu1.db |
| 129 | set rc [catch {db backup aux1 bu1.db} res] |
| 130 | lappend rc $res |
| 131 | } {1 {backup failed: unknown database aux1}} |
| 132 | |
| 133 | # Invalid syntax on the backup method |
| 134 | # |
| 135 | do_test backup2-9 { |
| 136 | set rc [catch {db backup} res] |
| 137 | lappend rc $res |
| 138 | } {1 {wrong # args: should be "db backup ?DATABASE? FILENAME"}} |
| 139 | |
| 140 | # Try to restore from an unreadable file. |
| 141 | # |
shane | ce14f62 | 2009-02-11 16:06:18 +0000 | [diff] [blame] | 142 | if {$tcl_platform(platform)=="windows"} { |
| 143 | do_test backup2-10 { |
| 144 | file delete -force bu3.db |
| 145 | file mkdir bu3.db |
| 146 | set rc [catch {db restore temp bu3.db} res] |
| 147 | lappend rc $res |
| 148 | } {1 {cannot open source database: unable to open database file}} |
| 149 | } |
| 150 | if {$tcl_platform(platform)!="windows"} { |
| 151 | do_test backup2-10 { |
| 152 | file delete -force bu3.db |
| 153 | file mkdir bu3.db |
| 154 | set rc [catch {db restore temp bu3.db} res] |
| 155 | lappend rc $res |
| 156 | } {1 {cannot open source database: disk I/O error}} |
| 157 | } |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 158 | |
| 159 | # Try to restore from something that is not a database file. |
| 160 | # |
| 161 | do_test backup2-11 { |
| 162 | set rc [catch {db restore temp bu2.db} res] |
| 163 | lappend rc $res |
| 164 | } {1 {restore failed: file is encrypted or is not a database}} |
| 165 | |
| 166 | # Try to restore a database that does not exist |
| 167 | # |
| 168 | do_test backup2-12 { |
| 169 | set rc [catch {db restore aux1 bu2.db} res] |
| 170 | lappend rc $res |
| 171 | } {1 {restore failed: unknown database aux1}} |
| 172 | do_test backup2-13 { |
| 173 | file delete -force bu4.db |
| 174 | set rc [catch {db restore bu4.db} res] |
| 175 | lappend rc $res |
| 176 | } {1 {cannot open source database: unable to open database file}} |
| 177 | |
| 178 | # Invalid syntax on the restore method |
| 179 | # |
| 180 | do_test backup2-14 { |
| 181 | set rc [catch {db restore} res] |
| 182 | lappend rc $res |
| 183 | } {1 {wrong # args: should be "db restore ?DATABASE? FILENAME"}} |
| 184 | |
| 185 | file delete -force bu1.db bu2.db bu3.db bu4.db |
| 186 | |
| 187 | finish_test |