3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
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.
16 # $Id: backup2.test,v 1.4 2009/04/07 14:14:23 danielk1977 Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
23 ifcapable !trigger||!view { finish_test ; return }
25 # Fill a database with test data.
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 INSERT INTO t1 VALUES(randstr(8000,8000));
34 INSERT INTO t1 VALUES(randstr(8000,8000));
35 CREATE VIEW v1 AS SELECT substr(x,10,10) FROM t1;
37 INSERT INTO t2 VALUES(1,2);
38 INSERT INTO t2 VALUES(2,4);
39 INSERT INTO t2 SELECT a+2, (a+2)*2 FROM t2;
40 INSERT INTO t2 SELECT a+4, (a+4)*2 FROM t2;
41 INSERT INTO t2 SELECT a+8, (a+8)*2 FROM t2;
42 INSERT INTO t2 SELECT a+16, (a+16)*2 FROM t2;
43 INSERT INTO t2 SELECT a+32, (a+32)*2 FROM t2;
44 INSERT INTO t2 SELECT a+64, (a+64)*2 FROM t2;
45 INSERT INTO t2 SELECT a+128, (a+128)*2 FROM t2;
46 CREATE INDEX t2i1 ON t2(a,b);
47 CREATE TRIGGER r1 AFTER INSERT ON t2 BEGIN
51 PRAGMA integrity_check;
55 # Remember a check-sum on the database file.
57 unset -nocomplain cksum
58 set cksum [dbcksum db main]
60 # Make a backup of the test data. Verify that the backup copy
61 # is identical to the original.
70 # Delete the original. Restore from backup. Verify the content is
75 forcedelete test.db test.db-journal
77 db2 eval {BEGIN EXCLUSIVE}
78 set rc [catch {db restore bu1.db} res]
82 } {1 {restore failed: source database busy}}
85 forcedelete test.db test.db-journal
91 # Use alternative databases - other than "main".
94 db restore temp bu1.db
99 forcedelete bu1.db bu2.db
100 db backup temp bu2.db
105 # Try to backup to a readonly file.
109 catch {file attributes bu2.db -permissions r--------}
110 catch {file attributes bu2.db -readonly 1}
111 set rc [catch {db backup temp bu2.db} res]
113 } {1 {backup failed: attempt to write a readonly database}}
115 # Try to backup to something that is not a database file.
118 catch {file attributes bu2.db -readonly 0}
119 catch {file attributes bu2.db -permissions rw-------}
120 set out [open bu2.db w]
121 puts $out "This is not a valid database file"
123 set rc [catch {db backup temp bu2.db} res]
125 } {1 {backup failed: file is encrypted or is not a database}}
127 # Try to backup database that does not exist
131 set rc [catch {db backup aux1 bu1.db} res]
133 } {1 {backup failed: unknown database aux1}}
135 # Invalid syntax on the backup method
138 set rc [catch {db backup} res]
140 } {1 {wrong # args: should be "db backup ?DATABASE? FILENAME"}}
142 # Try to restore from an unreadable file.
144 if {$tcl_platform(platform)=="windows"} {
145 set msg {cannot open source database: unable to open database file}
146 } elseif {$tcl_platform(os)=="OpenBSD"} {
147 set msg {restore failed: file is encrypted or is not a database}
149 set msg {cannot open source database: disk I/O error}
154 set rc [catch {db restore temp bu3.db} res]
158 # Try to restore from something that is not a database file.
161 set rc [catch {db restore temp bu2.db} res]
163 } {1 {restore failed: file is encrypted or is not a database}}
165 # Try to restore a database that does not exist
168 set rc [catch {db restore aux1 bu2.db} res]
170 } {1 {restore failed: unknown database aux1}}
173 set rc [catch {db restore bu4.db} res]
175 } {1 {cannot open source database: unable to open database file}}
177 # Invalid syntax on the restore method
180 set rc [catch {db restore} res]
182 } {1 {wrong # args: should be "db restore ?DATABASE? FILENAME"}}
184 forcedelete bu1.db bu2.db bu3.db bu4.db