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