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