summaryrefslogtreecommitdiffstats
path: root/test/backup_ioerr.test
blob: ca3fd3240d778d1b6fd19b59a66ffcf7a66b9f78 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# 2009 January 30
#
# 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 handling of IO errors by the
# sqlite3_backup_XXX APIs.
#
# $Id: backup_ioerr.test,v 1.3 2009/04/10 18:41:01 danielk1977 Exp $

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

proc data_checksum {db file} { 
  $db one "SELECT md5sum(a, b) FROM ${file}.t1" 
}
proc test_contents {name db1 file1 db2 file2} {
  $db2 eval {select * from sqlite_master}
  $db1 eval {select * from sqlite_master}
  set checksum [data_checksum $db2 $file2]
  uplevel [list do_test $name [list data_checksum $db1 $file1] $checksum]
}

#--------------------------------------------------------------------
# This proc creates a database of approximately 290 pages. Depending
# on whether or not auto-vacuum is configured. Test cases backup_ioerr-1.*
# verify nothing more than this assumption.
#
proc populate_database {db {xtra_large 0}} {
  execsql {
    BEGIN;
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES(1, randstr(1000,1000));
    INSERT INTO t1 SELECT a+ 1, randstr(1000,1000) FROM t1;
    INSERT INTO t1 SELECT a+ 2, randstr(1000,1000) FROM t1;
    INSERT INTO t1 SELECT a+ 4, randstr(1000,1000) FROM t1;
    INSERT INTO t1 SELECT a+ 8, randstr(1000,1000) FROM t1;
    INSERT INTO t1 SELECT a+16, randstr(1000,1000) FROM t1;
    INSERT INTO t1 SELECT a+32, randstr(1000,1000) FROM t1;
    CREATE INDEX i1 ON t1(b);
    COMMIT;
  } $db
  if {$xtra_large} {
    execsql { INSERT INTO t1 SELECT a+64, randstr(1000,1000) FROM t1 } $db
  }
}
do_test backup_ioerr-1.1 {
  populate_database db
  set nPage [expr {[file size test.db] / 1024}]
  expr {$nPage>130 && $nPage<160}
} {1}
do_test backup_ioerr-1.2 {
  expr {[file size test.db] > $sqlite_pending_byte}
} {1}
do_test backup_ioerr-1.3 {
  db close
  forcedelete test.db
} {}

# Turn off IO error simulation.
#
proc clear_ioerr_simulation {} {
  set ::sqlite_io_error_hit 0
  set ::sqlite_io_error_hardhit 0
  set ::sqlite_io_error_pending 0
  set ::sqlite_io_error_persist 0
}

#--------------------------------------------------------------------
# The following procedure runs with SQLite's IO error simulation 
# enabled.
#
#   1) Start with a reasonably sized database. One that includes the
#      pending-byte (locking) page.
#
#   2) Open a backup process. Set the cache-size for the destination
#      database to 10 pages only.
#
#   3) Step the backup process N times to partially backup the database
#      file. If an IO error is reported, then the backup process is
#      concluded with a call to backup_finish().
#
#      If an IO error occurs, verify that:
#
#      * the call to backup_step() returns an SQLITE_IOERR_XXX error code.
#
#      * after the failed call to backup_step() but before the call to
#        backup_finish() the destination database handle error code and 
#        error message remain unchanged.
#
#      * the call to backup_finish() returns an SQLITE_IOERR_XXX error code.
#
#      * following the call to backup_finish(), the destination database
#        handle has been populated with an error code and error message.
#
#   4) Write to the database via the source database connection. Check 
#      that:
#
#      * If an IO error occurs while writing the source database, the
#        write operation should report an IO error. The backup should 
#        proceed as normal.
#
#      * If an IO error occurs while updating the backup, the write 
#        operation should proceed normally. The error should be reported
#        from the next call to backup_step() (in step 5 of this test
#        procedure).
#
#   5) Step the backup process to finish the backup. If an IO error is 
#      reported, then the backup process is concluded with a call to 
#      backup_finish().
#
#      Test that if an IO error occurs, or if one occurred while updating
#      the backup database during step 4, then the conditions listed
#      under step 3 are all true.
#
#   6) Finish the backup process.
#
#   * If the backup succeeds (backup_finish() returns SQLITE_OK), then
#     the contents of the backup database should match that of the
#     source database.
#
#   * If the backup fails (backup_finish() returns other than SQLITE_OK), 
#     then the contents of the backup database should be as they were 
#     before the operation was started.
#
# The following factors are varied:
#
#   * Destination database is initially larger than the source database, OR
#   * Destination database is initially smaller than the source database.
#
#   * IO errors are transient, OR
#   * IO errors are persistent.
#
#   * Destination page-size is smaller than the source.
#   * Destination page-size is the same as the source.
#   * Destination page-size is larger than the source.
#

set iTest 1
foreach bPersist {0 1} {
foreach iDestPagesize {512 1024 4096} {
foreach zSetupBak [list "" {populate_database ddb 1}] {

  incr iTest
  set bStop 0
for {set iError 1} {$bStop == 0} {incr iError} {
  # Disable IO error simulation.
  clear_ioerr_simulation

  catch { ddb close }
  catch { sdb close }
  catch { forcedelete test.db }
  catch { forcedelete bak.db }

  # Open the source and destination databases.
  sqlite3 sdb test.db
  sqlite3 ddb bak.db

  # Step 1: Populate the source and destination databases.
  populate_database sdb
  ddb eval "PRAGMA page_size = $iDestPagesize"
  ddb eval "PRAGMA cache_size = 10"
  eval $zSetupBak

  # Step 2: Open the backup process.
  sqlite3_backup B ddb main sdb main

  # Enable IO error simulation.
  set ::sqlite_io_error_pending $iError
  set ::sqlite_io_error_persist $bPersist

  # Step 3: Partially backup the database. If an IO error occurs, check
  # a few things then skip to the next iteration of the loop.
  #
  set rc [B step 100]
  if {$::sqlite_io_error_hardhit} {

    do_test backup_ioerr-$iTest.$iError.1 {
      string match SQLITE_IOERR* $rc
    } {1}
    do_test backup_ioerr-$iTest.$iError.2 {
      list [sqlite3_errcode ddb] [sqlite3_errmsg ddb]
    } {SQLITE_OK {not an error}}

    set rc [B finish]
    do_test backup_ioerr-$iTest.$iError.3 {
      string match SQLITE_IOERR* $rc
    } {1}

    do_test backup_ioerr-$iTest.$iError.4 {
      sqlite3_errmsg ddb
    } {disk I/O error}

    clear_ioerr_simulation
    sqlite3 ddb bak.db
    integrity_check backup_ioerr-$iTest.$iError.5 ddb

    continue
  }

  # No IO error was encountered during step 3. Check that backup_step()
  # returned SQLITE_OK before proceding.
  do_test backup_ioerr-$iTest.$iError.6 {
    expr {$rc eq "SQLITE_OK"}
  } {1}

  # Step 4: Write to the source database.
  set rc [catchsql { UPDATE t1 SET b = randstr(1000,1000) WHERE a < 50 } sdb]

  if {[lindex $rc 0] && $::sqlite_io_error_persist==0} {
    # The IO error occurred while updating the source database. In this
    # case the backup should be able to continue.
    set rc [B step 5000]
    if { $rc != "SQLITE_IOERR_UNLOCK" } {
      do_test backup_ioerr-$iTest.$iError.7 {
        list [B step 5000] [B finish]
      } {SQLITE_DONE SQLITE_OK}

      clear_ioerr_simulation
      test_contents backup_ioerr-$iTest.$iError.8 ddb main sdb main
      integrity_check backup_ioerr-$iTest.$iError.9 ddb
    } else {
      do_test backup_ioerr-$iTest.$iError.10 {
        B finish
      } {SQLITE_IOERR_UNLOCK}
    }

    clear_ioerr_simulation
    sqlite3 ddb bak.db
    integrity_check backup_ioerr-$iTest.$iError.11 ddb

    continue
  }

  # Step 5: Finish the backup operation. If an IO error occurs, check that
  # it is reported correctly and skip to the next iteration of the loop.
  #
  set rc [B step 5000]
  if {$rc != "SQLITE_DONE"} {
    do_test backup_ioerr-$iTest.$iError.12 {
      string match SQLITE_IOERR* $rc
    } {1}
    do_test backup_ioerr-$iTest.$iError.13 {
      list [sqlite3_errcode ddb] [sqlite3_errmsg ddb]
    } {SQLITE_OK {not an error}}

    set rc [B finish]
    do_test backup_ioerr-$iTest.$iError.14 {
      string match SQLITE_IOERR* $rc
    } {1}
    do_test backup_ioerr-$iTest.$iError.15 {
      sqlite3_errmsg ddb
    } {disk I/O error}

    clear_ioerr_simulation
    sqlite3 ddb bak.db
    integrity_check backup_ioerr-$iTest.$iError.16 ddb

    continue
  }

  # The backup was successfully completed.
  #
  do_test backup_ioerr-$iTest.$iError.17 {
    list [set rc] [B finish]
  } {SQLITE_DONE SQLITE_OK}

  clear_ioerr_simulation
  sqlite3 sdb test.db
  sqlite3 ddb bak.db

  test_contents backup_ioerr-$iTest.$iError.18 ddb main sdb main
  integrity_check backup_ioerr-$iTest.$iError.19 ddb

  set bStop [expr $::sqlite_io_error_pending<=0]
}}}}

catch { sdb close }
catch { ddb close }
finish_test