summaryrefslogtreecommitdiffstats
path: root/test/backup2.test
blob: 1822e2dbf0072cde4d0b221dc74a270caa2f007b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# 2009 February 4
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this file is testing the "backup" and "restore" methods
# of the TCL interface - methods which are based on the
# sqlite3_backup_XXX API.
#
# $Id: backup2.test,v 1.4 2009/04/07 14:14:23 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

do_not_use_codec

ifcapable !trigger||!view { finish_test ; return }

# Fill a database with test data.
#
do_test backup2-1 {
  db eval {
    CREATE TABLE t1(x);
    INSERT INTO t1 VALUES(randstr(8000,8000));
    INSERT INTO t1 VALUES(randstr(8000,8000));
    INSERT INTO t1 VALUES(randstr(8000,8000));
    INSERT INTO t1 VALUES(randstr(8000,8000));
    INSERT INTO t1 VALUES(randstr(8000,8000));
    CREATE VIEW v1 AS SELECT substr(x,10,10) FROM t1;
    CREATE TABLE t2(a,b);
    INSERT INTO t2 VALUES(1,2);
    INSERT INTO t2 VALUES(2,4);
    INSERT INTO t2 SELECT a+2, (a+2)*2 FROM t2;
    INSERT INTO t2 SELECT a+4, (a+4)*2 FROM t2;
    INSERT INTO t2 SELECT a+8, (a+8)*2 FROM t2;
    INSERT INTO t2 SELECT a+16, (a+16)*2 FROM t2;
    INSERT INTO t2 SELECT a+32, (a+32)*2 FROM t2;
    INSERT INTO t2 SELECT a+64, (a+64)*2 FROM t2;
    INSERT INTO t2 SELECT a+128, (a+128)*2 FROM t2;
    CREATE INDEX t2i1 ON t2(a,b);
    CREATE TRIGGER r1 AFTER INSERT ON t2 BEGIN
      SELECT 'hello';
    END;
    ANALYZE;
    PRAGMA integrity_check;
  }
} {ok}

# Remember a check-sum on the database file.
#
unset -nocomplain cksum
set cksum [dbcksum db main]

# Make a backup of the test data.  Verify that the backup copy
# is identical to the original.
#
do_test backup2-2 {
  forcedelete bu1.db
  db backup bu1.db
  sqlite3 db2 bu1.db
  dbcksum db2 main
} $cksum

# Delete the original.  Restore from backup.  Verify the content is
# unchanged.
#
do_test backup2-3.1 {
  db close
  forcedelete test.db test.db-journal
  sqlite3 db test.db
  db2 eval {BEGIN EXCLUSIVE}
  set rc [catch {db restore bu1.db} res]
  lappend rc $res
  db2 eval {ROLLBACK}
  set rc
} {1 {restore failed: source database busy}}
do_test backup2-3.2 {
  db close
  forcedelete test.db test.db-journal
  sqlite3 db test.db
  db restore bu1.db
  dbcksum db main
} $cksum

# Use alternative databases - other than "main".
#
do_test backup2-4 {
  db restore temp bu1.db
  dbcksum db temp
} $cksum
do_test backup2-5 {
  db2 close
  forcedelete bu1.db bu2.db
  db backup temp bu2.db
  sqlite3 db2 bu2.db
  dbcksum db2 main
} $cksum

# Try to backup to a readonly file.
#
do_test backup2-6 {
  db2 close
  catch {file attributes bu2.db -permissions r--------}
  catch {file attributes bu2.db -readonly 1}
  set rc [catch {db backup temp bu2.db} res]
  lappend rc $res
} {1 {backup failed: attempt to write a readonly database}}

# Try to backup to something that is not a database file.
#
do_test backup2-7 {
  catch {file attributes bu2.db -readonly 0}
  catch {file attributes bu2.db -permissions rw-------}
  set out [open bu2.db w]
  puts $out "This is not a valid database file"
  close $out
  set rc [catch {db backup temp bu2.db} res]
  lappend rc $res
} {1 {backup failed: file is not a database}}

# Try to backup database that does not exist
#
do_test backup2-8 {
  forcedelete bu1.db
  set rc [catch {db backup aux1 bu1.db} res]
  lappend rc $res
} {1 {backup failed: unknown database aux1}}

# Invalid syntax on the backup method
#
do_test backup2-9 {
  set rc [catch {db backup} res]
  lappend rc $res
} {1 {wrong # args: should be "db backup ?DATABASE? FILENAME"}}

# Try to restore from an unreadable file.
#
if {$tcl_platform(platform)=="windows"} {
  set msg {cannot open source database: unable to open database file}
} elseif {[string match *BSD $tcl_platform(os)]} {
  set msg {}
} else {
  set msg {cannot open source database: disk I/O error}
}
do_test backup2-10 {
  forcedelete bu3.db
  file mkdir bu3.db
  set rc [catch {db restore temp bu3.db} res]
  if {[string match *BSD $tcl_platform(os)]} { set res "" }
  list $rc $res
} [list 1 $msg]

# Try to restore from something that is not a database file.
#
do_test backup2-11 {
  set rc [catch {db restore temp bu2.db} res]
  lappend rc $res
} {1 {restore failed: file is not a database}}

# Try to restore a database that does not exist
#
do_test backup2-12 {
  set rc [catch {db restore aux1 bu2.db} res]
  lappend rc $res
} {1 {restore failed: unknown database aux1}}
do_test backup2-13 {
  forcedelete bu4.db
  set rc [catch {db restore bu4.db} res]
  lappend rc $res
} {1 {cannot open source database: unable to open database file}}

# Invalid syntax on the restore method
#
do_test backup2-14 {
  set rc [catch {db restore} res]
  lappend rc $res
} {1 {wrong # args: should be "db restore ?DATABASE? FILENAME"}}
 
forcedelete bu1.db bu2.db bu3.db bu4.db

finish_test