Home | History | Annotate | Download | only in test
      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.4 2009/04/07 14:14:23 danielk1977 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   file delete -force 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   file delete -force 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   file delete -force 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   file delete -force 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   file delete -force 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   do_test backup2-10 {
    146     file delete -force bu3.db
    147     file mkdir bu3.db
    148     set rc [catch {db restore temp bu3.db} res]
    149     lappend rc $res
    150   } {1 {cannot open source database: unable to open database file}}
    151 }
    152 if {$tcl_platform(platform)!="windows"} {
    153   do_test backup2-10 {
    154     file delete -force bu3.db
    155     file mkdir bu3.db
    156     set rc [catch {db restore temp bu3.db} res]
    157     lappend rc $res
    158   } {1 {cannot open source database: disk I/O error}}
    159 }
    160 
    161 # Try to restore from something that is not a database file.
    162 #
    163 do_test backup2-11 {
    164   set rc [catch {db restore temp bu2.db} res]
    165   lappend rc $res
    166 } {1 {restore failed: file is encrypted or is not a database}}
    167 
    168 # Try to restore a database that does not exist
    169 #
    170 do_test backup2-12 {
    171   set rc [catch {db restore aux1 bu2.db} res]
    172   lappend rc $res
    173 } {1 {restore failed: unknown database aux1}}
    174 do_test backup2-13 {
    175   file delete -force bu4.db
    176   set rc [catch {db restore bu4.db} res]
    177   lappend rc $res
    178 } {1 {cannot open source database: unable to open database file}}
    179 
    180 # Invalid syntax on the restore method
    181 #
    182 do_test backup2-14 {
    183   set rc [catch {db restore} res]
    184   lappend rc $res
    185 } {1 {wrong # args: should be "db restore ?DATABASE? FILENAME"}}
    186  
    187 file delete -force bu1.db bu2.db bu3.db bu4.db
    188 
    189 finish_test
    190