AryaWu/sqlite
0
1# 2009 February 42#3# The author disclaims copyright to this source code. In place of4# 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. The12# focus of this file is testing the "backup" and "restore" methods13# of the TCL interface - methods which are based on the14# sqlite3_backup_XXX API.15#16# $Id: backup2.test,v 1.4 2009/04/07 14:14:23 danielk1977 Exp $17 18set testdir [file dirname $argv0]19source $testdir/tester.tcl20 21do_not_use_codec22 23ifcapable !trigger||!view { finish_test ; return }24 25# Fill a database with test data.26#27do_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 BEGIN48 SELECT 'hello';49 END;50 ANALYZE;51 PRAGMA integrity_check;52 }53} {ok}54 55# Remember a check-sum on the database file.56#57unset -nocomplain cksum58set cksum [dbcksum db main]59 60# Make a backup of the test data. Verify that the backup copy61# is identical to the original.62#63do_test backup2-2 {64 forcedelete bu1.db65 db backup bu1.db66 sqlite3 db2 bu1.db67 dbcksum db2 main68} $cksum69 70# Delete the original. Restore from backup. Verify the content is71# unchanged.72#73do_test backup2-3.1 {74 db close75 forcedelete test.db test.db-journal76 sqlite3 db test.db77 db2 eval {BEGIN EXCLUSIVE}78 set rc [catch {db restore bu1.db} res]79 lappend rc $res80 db2 eval {ROLLBACK}81 set rc82} {1 {restore failed: source database busy}}83do_test backup2-3.2 {84 db close85 forcedelete test.db test.db-journal86 sqlite3 db test.db87 db restore bu1.db88 dbcksum db main89} $cksum90 91# Use alternative databases - other than "main".92#93do_test backup2-4 {94 db restore temp bu1.db95 dbcksum db temp96} $cksum97do_test backup2-5 {98 db2 close99 forcedelete bu1.db bu2.db100 db backup temp bu2.db101 sqlite3 db2 bu2.db102 dbcksum db2 main103} $cksum104 105# Try to backup to a readonly file.106#107do_test backup2-6 {108 db2 close109 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 $res113} {1 {backup failed: attempt to write a readonly database}}114 115# Try to backup to something that is not a database file.116#117do_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 $out123 set rc [catch {db backup temp bu2.db} res]124 lappend rc $res125} {1 {backup failed: file is not a database}}126 127# Try to backup database that does not exist128#129do_test backup2-8 {130 forcedelete bu1.db131 set rc [catch {db backup aux1 bu1.db} res]132 lappend rc $res133} {1 {backup failed: unknown database aux1}}134 135# Invalid syntax on the backup method136#137do_test backup2-9 {138 set rc [catch {db backup} res]139 lappend rc $res140} {1 {wrong # args: should be "db backup ?DATABASE? FILENAME"}}141 142# Try to restore from an unreadable file.143#144if {$::tcl_platform(os) eq "Windows NT"} {145 set msg {cannot open source database: unable to open database file}146} elseif {[string match *BSD $tcl_platform(os)]} {147 set msg {}148} else {149 set msg {cannot open source database: disk I/O error}150}151do_test backup2-10 {152 forcedelete bu3.db153 file mkdir bu3.db154 set rc [catch {db restore temp bu3.db} res]155 if {[string match *BSD $tcl_platform(os)]} { set res "" }156 list $rc $res157} [list 1 $msg]158 159# Try to restore from something that is not a database file.160#161do_test backup2-11 {162 set rc [catch {db restore temp bu2.db} res]163 lappend rc $res164} {1 {restore failed: file is not a database}}165 166# Try to restore a database that does not exist167#168do_test backup2-12 {169 set rc [catch {db restore aux1 bu2.db} res]170 lappend rc $res171} {1 {restore failed: unknown database aux1}}172do_test backup2-13 {173 forcedelete bu4.db174 set rc [catch {db restore bu4.db} res]175 lappend rc $res176} {1 {cannot open source database: unable to open database file}}177 178# Invalid syntax on the restore method179#180do_test backup2-14 {181 set rc [catch {db restore} res]182 lappend rc $res183} {1 {wrong # args: should be "db restore ?DATABASE? FILENAME"}}184 185forcedelete bu1.db bu2.db bu3.db bu4.db186 187finish_test188 