xref: /titanic_44/usr/src/lib/libsqlite/test/ioerr.test (revision 2ec7cc7fc084163eaed884efee9bbd322cc8951b)
1
2#pragma ident	"%Z%%M%	%I%	%E% SMI"
3
4# 2001 October 12
5#
6# The author disclaims copyright to this source code.  In place of
7# a legal notice, here is a blessing:
8#
9#    May you do good and not evil.
10#    May you find forgiveness for yourself and forgive others.
11#    May you share freely, never taking more than you give.
12#
13#***********************************************************************
14# This file implements regression tests for SQLite library.  The
15# focus of this file is testing for correct handling of I/O errors
16# such as writes failing because the disk is full.
17#
18# The tests in this file use special facilities that are only
19# available in the SQLite test fixture.
20#
21# $Id: ioerr.test,v 1.3 2003/04/25 15:37:59 drh Exp $
22
23set testdir [file dirname $argv0]
24source $testdir/tester.tcl
25
26set ::go 1
27for {set n 1} {$go} {incr n} {
28  do_test ioerr-1.$n.1 {
29    set ::sqlite_io_error_pending 0
30    db close
31    catch {file delete -force test.db}
32    catch {file delete -force test.db-journal}
33    sqlite db test.db
34    execsql {SELECT * FROM sqlite_master}
35  } {}
36  do_test ioerr-1.$n.2 [subst {
37    set ::sqlite_io_error_pending $n
38  }] $n
39  do_test ioerr-1.$n.3 {
40    set r [catch {db eval {
41      CREATE TABLE t1(a,b,c);
42      SELECT * FROM sqlite_master;
43      BEGIN TRANSACTION;
44      INSERT INTO t1 VALUES(1,2,3);
45      INSERT INTO t1 VALUES(4,5,6);
46      ROLLBACK;
47      SELECT * FROM t1;
48      BEGIN TRANSACTION;
49      INSERT INTO t1 VALUES(1,2,3);
50      INSERT INTO t1 VALUES(4,5,6);
51      COMMIT;
52      SELECT * FROM t1;
53      DELETE FROM t1 WHERE a<100;
54    }} msg]
55    # if {$r} {puts $msg}
56    set ::go [expr {$::sqlite_io_error_pending<=0}]
57    expr {$::sqlite_io_error_pending>0 || $r!=0}
58  } {1}
59}
60set ::sqlite_io_error_pending 0
61
62proc cksum {{db db}} {
63  set txt [$db eval {SELECT name, type, sql FROM sqlite_master}]\n
64  foreach tbl [$db eval {SELECT name FROM sqlite_master WHERE type='table'}] {
65    append txt [$db eval "SELECT * FROM $tbl"]\n
66  }
67  foreach prag {default_synchronous default_cache_size} {
68    append txt $prag-[$db eval "PRAGMA $prag"]\n
69  }
70  set cksum [string length $txt]-[md5 $txt]
71  # puts $cksum-[file size test.db]
72  return $cksum
73}
74
75set ::go 1
76for {set n 1} {$go} {incr n} {
77  do_test ioerr-2.$n.1 {
78    set ::sqlite_io_error_pending 0
79    db close
80    catch {file delete -force test.db}
81    catch {file delete -force test.db-journal}
82    sqlite db test.db
83    execsql {
84      BEGIN;
85      CREATE TABLE t1(a, b, c);
86      INSERT INTO t1 VALUES(1, randstr(5,50), randstr(5,50));
87      INSERT INTO t1 SELECT a+2, b||'-'||rowid, c||'-'||rowid FROM t1;
88      INSERT INTO t1 SELECT a+4, b||'-'||rowid, c||'-'||rowid FROM t1;
89      INSERT INTO t1 SELECT a+8, b||'-'||rowid, c||'-'||rowid FROM t1;
90      INSERT INTO t1 SELECT a+16, b||'-'||rowid, c||'-'||rowid FROM t1;
91      INSERT INTO t1 SELECT a+32, b||'-'||rowid, c||'-'||rowid FROM t1;
92      INSERT INTO t1 SELECT a+64, b||'-'||rowid, c||'-'||rowid FROM t1;
93      INSERT INTO t1 SELECT a+128, b||'-'||rowid, c||'-'||rowid FROM t1;
94      CREATE TABLE t2 AS SELECT * FROM t1;
95      CREATE TABLE t3 AS SELECT * FROM t1;
96      COMMIT;
97      DROP TABLE t2;
98    }
99    set ::cksum [cksum]
100    execsql {
101      SELECT name FROM sqlite_master WHERE type='table'
102    }
103  } {t1 t3}
104  do_test ioerr-2.$n.2 [subst {
105    set ::sqlite_io_error_pending $n
106  }] $n
107  do_test ioerr-2.$n.3 {
108    set r [catch {db eval {
109      VACUUM;
110    }} msg]
111    # puts "error_pending=$::sqlite_io_error_pending"
112    # if {$r} {puts $msg}
113    set ::go [expr {$::sqlite_io_error_pending<=0}]
114    expr {$::sqlite_io_error_pending>0 || $r!=0}
115    set ::sqlite_io_error_pending 0
116    db close
117    sqlite db test.db
118    cksum
119  } $cksum
120}
121set ::sqlite_io_error_pending 0
122
123finish_test
124