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: #
16: # $Id: backup2.test,v 1.1.1.1 2012/02/21 17:04:16 misho Exp $
17:
18: set testdir [file dirname $argv0]
19: source $testdir/tester.tcl
20:
21: do_not_use_codec
22:
23: ifcapable !trigger||!view { finish_test ; return }
24:
25: # Fill a database with test data.
26: #
27: do_test backup2-1 {
28: db eval {
29: CREATE TABLE t1(x);
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;
36: CREATE TABLE t2(a,b);
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
48: SELECT 'hello';
49: END;
50: ANALYZE;
51: PRAGMA integrity_check;
52: }
53: } {ok}
54:
55: # Remember a check-sum on the database file.
56: #
57: unset -nocomplain cksum
58: set cksum [dbcksum db main]
59:
60: # Make a backup of the test data. Verify that the backup copy
61: # is identical to the original.
62: #
63: do_test backup2-2 {
64: forcedelete bu1.db
65: db backup bu1.db
66: sqlite3 db2 bu1.db
67: dbcksum db2 main
68: } $cksum
69:
70: # Delete the original. Restore from backup. Verify the content is
71: # unchanged.
72: #
73: do_test backup2-3.1 {
74: db close
75: forcedelete test.db test.db-journal
76: sqlite3 db test.db
77: db2 eval {BEGIN EXCLUSIVE}
78: set rc [catch {db restore bu1.db} res]
79: lappend rc $res
80: db2 eval {ROLLBACK}
81: set rc
82: } {1 {restore failed: source database busy}}
83: do_test backup2-3.2 {
84: db close
85: forcedelete test.db test.db-journal
86: sqlite3 db test.db
87: db restore bu1.db
88: dbcksum db main
89: } $cksum
90:
91: # Use alternative databases - other than "main".
92: #
93: do_test backup2-4 {
94: db restore temp bu1.db
95: dbcksum db temp
96: } $cksum
97: do_test backup2-5 {
98: db2 close
99: forcedelete bu1.db bu2.db
100: db backup temp bu2.db
101: sqlite3 db2 bu2.db
102: dbcksum db2 main
103: } $cksum
104:
105: # Try to backup to a readonly file.
106: #
107: do_test backup2-6 {
108: db2 close
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]
112: lappend rc $res
113: } {1 {backup failed: attempt to write a readonly database}}
114:
115: # Try to backup to something that is not a database file.
116: #
117: do_test backup2-7 {
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"
122: close $out
123: set rc [catch {db backup temp bu2.db} res]
124: lappend rc $res
125: } {1 {backup failed: file is encrypted or is not a database}}
126:
127: # Try to backup database that does not exist
128: #
129: do_test backup2-8 {
130: forcedelete bu1.db
131: set rc [catch {db backup aux1 bu1.db} res]
132: lappend rc $res
133: } {1 {backup failed: unknown database aux1}}
134:
135: # Invalid syntax on the backup method
136: #
137: do_test backup2-9 {
138: set rc [catch {db backup} res]
139: lappend rc $res
140: } {1 {wrong # args: should be "db backup ?DATABASE? FILENAME"}}
141:
142: # Try to restore from an unreadable file.
143: #
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}
148: } else {
149: set msg {cannot open source database: disk I/O error}
150: }
151: do_test backup2-10 {
152: forcedelete bu3.db
153: file mkdir bu3.db
154: set rc [catch {db restore temp bu3.db} res]
155: lappend rc $res
156: } [list 1 $msg]
157:
158: # Try to restore from something that is not a database file.
159: #
160: do_test backup2-11 {
161: set rc [catch {db restore temp bu2.db} res]
162: lappend rc $res
163: } {1 {restore failed: file is encrypted or is not a database}}
164:
165: # Try to restore a database that does not exist
166: #
167: do_test backup2-12 {
168: set rc [catch {db restore aux1 bu2.db} res]
169: lappend rc $res
170: } {1 {restore failed: unknown database aux1}}
171: do_test backup2-13 {
172: forcedelete bu4.db
173: set rc [catch {db restore bu4.db} res]
174: lappend rc $res
175: } {1 {cannot open source database: unable to open database file}}
176:
177: # Invalid syntax on the restore method
178: #
179: do_test backup2-14 {
180: set rc [catch {db restore} res]
181: lappend rc $res
182: } {1 {wrong # args: should be "db restore ?DATABASE? FILENAME"}}
183:
184: forcedelete bu1.db bu2.db bu3.db bu4.db
185:
186: finish_test
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>