1dc2c4915Sdrh# 2009 February 4 2dc2c4915Sdrh# 3dc2c4915Sdrh# The author disclaims copyright to this source code. In place of 4dc2c4915Sdrh# a legal notice, here is a blessing: 5dc2c4915Sdrh# 6dc2c4915Sdrh# May you do good and not evil. 7dc2c4915Sdrh# May you find forgiveness for yourself and forgive others. 8dc2c4915Sdrh# May you share freely, never taking more than you give. 9dc2c4915Sdrh# 10dc2c4915Sdrh#*********************************************************************** 11dc2c4915Sdrh# This file implements regression tests for SQLite library. The 12dc2c4915Sdrh# focus of this file is testing the "backup" and "restore" methods 13dc2c4915Sdrh# of the TCL interface - methods which are based on the 14dc2c4915Sdrh# sqlite3_backup_XXX API. 15dc2c4915Sdrh# 162943c372Sdanielk1977# $Id: backup2.test,v 1.4 2009/04/07 14:14:23 danielk1977 Exp $ 17dc2c4915Sdrh 18dc2c4915Sdrhset testdir [file dirname $argv0] 19dc2c4915Sdrhsource $testdir/tester.tcl 20dc2c4915Sdrh 2110f5a50eSdando_not_use_codec 2210f5a50eSdan 232943c372Sdanielk1977ifcapable !trigger||!view { finish_test ; return } 242943c372Sdanielk1977 25dc2c4915Sdrh# Fill a database with test data. 26dc2c4915Sdrh# 27dc2c4915Sdrhdo_test backup2-1 { 28dc2c4915Sdrh db eval { 29dc2c4915Sdrh CREATE TABLE t1(x); 30dc2c4915Sdrh INSERT INTO t1 VALUES(randstr(8000,8000)); 31dc2c4915Sdrh INSERT INTO t1 VALUES(randstr(8000,8000)); 32dc2c4915Sdrh INSERT INTO t1 VALUES(randstr(8000,8000)); 33dc2c4915Sdrh INSERT INTO t1 VALUES(randstr(8000,8000)); 34dc2c4915Sdrh INSERT INTO t1 VALUES(randstr(8000,8000)); 35dc2c4915Sdrh CREATE VIEW v1 AS SELECT substr(x,10,10) FROM t1; 36dc2c4915Sdrh CREATE TABLE t2(a,b); 37dc2c4915Sdrh INSERT INTO t2 VALUES(1,2); 38dc2c4915Sdrh INSERT INTO t2 VALUES(2,4); 39dc2c4915Sdrh INSERT INTO t2 SELECT a+2, (a+2)*2 FROM t2; 40dc2c4915Sdrh INSERT INTO t2 SELECT a+4, (a+4)*2 FROM t2; 41dc2c4915Sdrh INSERT INTO t2 SELECT a+8, (a+8)*2 FROM t2; 42dc2c4915Sdrh INSERT INTO t2 SELECT a+16, (a+16)*2 FROM t2; 43dc2c4915Sdrh INSERT INTO t2 SELECT a+32, (a+32)*2 FROM t2; 44dc2c4915Sdrh INSERT INTO t2 SELECT a+64, (a+64)*2 FROM t2; 45dc2c4915Sdrh INSERT INTO t2 SELECT a+128, (a+128)*2 FROM t2; 46dc2c4915Sdrh CREATE INDEX t2i1 ON t2(a,b); 47dc2c4915Sdrh CREATE TRIGGER r1 AFTER INSERT ON t2 BEGIN 48dc2c4915Sdrh SELECT 'hello'; 49dc2c4915Sdrh END; 50dc2c4915Sdrh ANALYZE; 51dc2c4915Sdrh PRAGMA integrity_check; 52dc2c4915Sdrh } 53dc2c4915Sdrh} {ok} 54dc2c4915Sdrh 55dc2c4915Sdrh# Remember a check-sum on the database file. 56dc2c4915Sdrh# 57dc2c4915Sdrhunset -nocomplain cksum 58dc2c4915Sdrhset cksum [dbcksum db main] 59dc2c4915Sdrh 60dc2c4915Sdrh# Make a backup of the test data. Verify that the backup copy 61dc2c4915Sdrh# is identical to the original. 62dc2c4915Sdrh# 63dc2c4915Sdrhdo_test backup2-2 { 64fda06befSmistachkin forcedelete bu1.db 65dc2c4915Sdrh db backup bu1.db 66dc2c4915Sdrh sqlite3 db2 bu1.db 67dc2c4915Sdrh dbcksum db2 main 68dc2c4915Sdrh} $cksum 69dc2c4915Sdrh 70dc2c4915Sdrh# Delete the original. Restore from backup. Verify the content is 71dc2c4915Sdrh# unchanged. 72dc2c4915Sdrh# 73dc2c4915Sdrhdo_test backup2-3.1 { 74dc2c4915Sdrh db close 75fda06befSmistachkin forcedelete test.db test.db-journal 76dc2c4915Sdrh sqlite3 db test.db 77dc2c4915Sdrh db2 eval {BEGIN EXCLUSIVE} 78dc2c4915Sdrh set rc [catch {db restore bu1.db} res] 79dc2c4915Sdrh lappend rc $res 80dc2c4915Sdrh db2 eval {ROLLBACK} 81dc2c4915Sdrh set rc 82dc2c4915Sdrh} {1 {restore failed: source database busy}} 83dc2c4915Sdrhdo_test backup2-3.2 { 84dc2c4915Sdrh db close 85fda06befSmistachkin forcedelete test.db test.db-journal 86dc2c4915Sdrh sqlite3 db test.db 87dc2c4915Sdrh db restore bu1.db 88dc2c4915Sdrh dbcksum db main 89dc2c4915Sdrh} $cksum 90dc2c4915Sdrh 91dc2c4915Sdrh# Use alternative databases - other than "main". 92dc2c4915Sdrh# 93dc2c4915Sdrhdo_test backup2-4 { 94dc2c4915Sdrh db restore temp bu1.db 95dc2c4915Sdrh dbcksum db temp 96dc2c4915Sdrh} $cksum 97dc2c4915Sdrhdo_test backup2-5 { 98dc2c4915Sdrh db2 close 99fda06befSmistachkin forcedelete bu1.db bu2.db 100dc2c4915Sdrh db backup temp bu2.db 101dc2c4915Sdrh sqlite3 db2 bu2.db 102dc2c4915Sdrh dbcksum db2 main 103dc2c4915Sdrh} $cksum 104dc2c4915Sdrh 105dc2c4915Sdrh# Try to backup to a readonly file. 106dc2c4915Sdrh# 107dc2c4915Sdrhdo_test backup2-6 { 108dc2c4915Sdrh db2 close 109dc2c4915Sdrh catch {file attributes bu2.db -permissions r--------} 110dc2c4915Sdrh catch {file attributes bu2.db -readonly 1} 111dc2c4915Sdrh set rc [catch {db backup temp bu2.db} res] 112dc2c4915Sdrh lappend rc $res 113dc2c4915Sdrh} {1 {backup failed: attempt to write a readonly database}} 114dc2c4915Sdrh 115dc2c4915Sdrh# Try to backup to something that is not a database file. 116dc2c4915Sdrh# 117dc2c4915Sdrhdo_test backup2-7 { 118dc2c4915Sdrh catch {file attributes bu2.db -readonly 0} 119308ef5a5Sdanielk1977 catch {file attributes bu2.db -permissions rw-------} 120dc2c4915Sdrh set out [open bu2.db w] 121dc2c4915Sdrh puts $out "This is not a valid database file" 122dc2c4915Sdrh close $out 123dc2c4915Sdrh set rc [catch {db backup temp bu2.db} res] 124dc2c4915Sdrh lappend rc $res 125ff4fa772Sdrh} {1 {backup failed: file is not a database}} 126dc2c4915Sdrh 127dc2c4915Sdrh# Try to backup database that does not exist 128dc2c4915Sdrh# 129dc2c4915Sdrhdo_test backup2-8 { 130fda06befSmistachkin forcedelete bu1.db 131dc2c4915Sdrh set rc [catch {db backup aux1 bu1.db} res] 132dc2c4915Sdrh lappend rc $res 133dc2c4915Sdrh} {1 {backup failed: unknown database aux1}} 134dc2c4915Sdrh 135dc2c4915Sdrh# Invalid syntax on the backup method 136dc2c4915Sdrh# 137dc2c4915Sdrhdo_test backup2-9 { 138dc2c4915Sdrh set rc [catch {db backup} res] 139dc2c4915Sdrh lappend rc $res 140dc2c4915Sdrh} {1 {wrong # args: should be "db backup ?DATABASE? FILENAME"}} 141dc2c4915Sdrh 142dc2c4915Sdrh# Try to restore from an unreadable file. 143dc2c4915Sdrh# 144ce14f624Sshaneif {$tcl_platform(platform)=="windows"} { 145983371d9Sdan set msg {cannot open source database: unable to open database file} 146e2b01012Sdrh} elseif {[string match *BSD $tcl_platform(os)]} { 1478b7e93adSdan set msg {} 148983371d9Sdan} else { 149983371d9Sdan set msg {cannot open source database: disk I/O error} 150983371d9Sdan} 151ce14f624Sshanedo_test backup2-10 { 152fda06befSmistachkin forcedelete bu3.db 153ce14f624Sshane file mkdir bu3.db 154ce14f624Sshane set rc [catch {db restore temp bu3.db} res] 155*863fd490Sdan if {[string match *BSD $tcl_platform(os)]} { set res "" } 156*863fd490Sdan list $rc $res 157983371d9Sdan} [list 1 $msg] 158dc2c4915Sdrh 159dc2c4915Sdrh# Try to restore from something that is not a database file. 160dc2c4915Sdrh# 161dc2c4915Sdrhdo_test backup2-11 { 162dc2c4915Sdrh set rc [catch {db restore temp bu2.db} res] 163dc2c4915Sdrh lappend rc $res 164ff4fa772Sdrh} {1 {restore failed: file is not a database}} 165dc2c4915Sdrh 166dc2c4915Sdrh# Try to restore a database that does not exist 167dc2c4915Sdrh# 168dc2c4915Sdrhdo_test backup2-12 { 169dc2c4915Sdrh set rc [catch {db restore aux1 bu2.db} res] 170dc2c4915Sdrh lappend rc $res 171dc2c4915Sdrh} {1 {restore failed: unknown database aux1}} 172dc2c4915Sdrhdo_test backup2-13 { 173fda06befSmistachkin forcedelete bu4.db 174dc2c4915Sdrh set rc [catch {db restore bu4.db} res] 175dc2c4915Sdrh lappend rc $res 176dc2c4915Sdrh} {1 {cannot open source database: unable to open database file}} 177dc2c4915Sdrh 178dc2c4915Sdrh# Invalid syntax on the restore method 179dc2c4915Sdrh# 180dc2c4915Sdrhdo_test backup2-14 { 181dc2c4915Sdrh set rc [catch {db restore} res] 182dc2c4915Sdrh lappend rc $res 183dc2c4915Sdrh} {1 {wrong # args: should be "db restore ?DATABASE? FILENAME"}} 184dc2c4915Sdrh 185fda06befSmistachkinforcedelete bu1.db bu2.db bu3.db bu4.db 186dc2c4915Sdrh 187dc2c4915Sdrhfinish_test 188