xref: /sqlite-3.40.0/test/tempfault.test (revision 6572c16a)
167330a12Sdan# 2016 April 11
267330a12Sdan#
367330a12Sdan# The author disclaims copyright to this source code.  In place of
467330a12Sdan# a legal notice, here is a blessing:
567330a12Sdan#
667330a12Sdan#    May you do good and not evil.
767330a12Sdan#    May you find forgiveness for yourself and forgive others.
867330a12Sdan#    May you share freely, never taking more than you give.
967330a12Sdan#
1067330a12Sdan#***********************************************************************
1167330a12Sdan#
1267330a12Sdan# This file contains tests for fault-injection when SQLite is used with
1367330a12Sdan# a temp file database.
1467330a12Sdan#
1567330a12Sdan
1667330a12Sdanset testdir [file dirname $argv0]
1767330a12Sdansource $testdir/tester.tcl
1867330a12Sdansource $testdir/malloc_common.tcl
1967330a12Sdanset testprefix tempfault
2067330a12Sdan
21*6572c16aSdan# sqlite3_memdebug_vfs_oom_test 0
2267330a12Sdan
23*6572c16aSdando_faultsim_test 1 -faults * -prep {
2467330a12Sdan  sqlite3 db ""
2567330a12Sdan  db eval {
2667330a12Sdan    PRAGMA page_size = 1024;
2767330a12Sdan    CREATE TABLE t1(a, b);
2867330a12Sdan    INSERT INTO t1 VALUES(1, 2);
2967330a12Sdan    INSERT INTO t1 VALUES(3, 4);
3067330a12Sdan  }
3167330a12Sdan} -body {
3267330a12Sdan  execsql { INSERT INTO t1 VALUES(5, 6) }
3367330a12Sdan} -test {
3467330a12Sdan  faultsim_test_result {0 {}}
3567330a12Sdan  set rc [catch { execsql { SELECT * FROM t1 } } msg]
3667330a12Sdan  if {$rc==0 && $msg != "1 2 3 4 5 6" && $msg != "1 2 3 4"} {
3767330a12Sdan    error "data mismatch 1: $msg"
3867330a12Sdan  }
3967330a12Sdan  if {$testrc==0 && $msg != "1 2 3 4 5 6"} {
4067330a12Sdan    error "data mismatch 2: $msg"
4167330a12Sdan  }
4267330a12Sdan  faultsim_integrity_check
4367330a12Sdan}
4467330a12Sdan
45*6572c16aSdando_faultsim_test 2 -faults * -prep {
4667330a12Sdan  sqlite3 db ""
4767330a12Sdan  db eval {
4867330a12Sdan    PRAGMA page_size = 1024;
4967330a12Sdan    PRAGMA cache_size = 10;
5067330a12Sdan    CREATE TABLE t1(a, b);
5167330a12Sdan    CREATE INDEX i1 ON t1(b, a);
52*6572c16aSdan    WITH x(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<100)
5367330a12Sdan    INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x;
5467330a12Sdan  }
5567330a12Sdan} -body {
5667330a12Sdan  execsql { UPDATE t1 SET a = randomblob(99) }
5767330a12Sdan} -test {
5867330a12Sdan  faultsim_test_result {0 {}}
5967330a12Sdan  faultsim_integrity_check db
6067330a12Sdan}
6167330a12Sdan
62*6572c16aSdancatch { db close }
63*6572c16aSdando_faultsim_test 2.1 -faults * -prep {
64*6572c16aSdan  if {[info commands db]==""} {
65*6572c16aSdan    sqlite3 db ""
66*6572c16aSdan    execsql {
67*6572c16aSdan      PRAGMA page_size = 1024;
68*6572c16aSdan      PRAGMA cache_size = 10;
69*6572c16aSdan      CREATE TABLE t1(a, b);
70*6572c16aSdan      CREATE INDEX i1 ON t1(b, a);
71*6572c16aSdan      WITH x(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<100)
72*6572c16aSdan          INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x;
73*6572c16aSdan    }
74*6572c16aSdan  }
75*6572c16aSdan} -body {
76*6572c16aSdan  execsql { UPDATE t1 SET a = randomblob(99) }
77*6572c16aSdan} -test {
78*6572c16aSdan  faultsim_test_result {0 {}}
79*6572c16aSdan  faultsim_integrity_check db
80*6572c16aSdan}
81*6572c16aSdan
82*6572c16aSdando_faultsim_test 3 -faults * -prep {
8367330a12Sdan  sqlite3 db ""
8467330a12Sdan  db eval {
8567330a12Sdan    PRAGMA page_size = 1024;
8667330a12Sdan    PRAGMA cache_size = 10;
8767330a12Sdan    CREATE TABLE t1(a, b);
8867330a12Sdan    CREATE INDEX i1 ON t1(b, a);
8967330a12Sdan    WITH x(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<50)
9067330a12Sdan    INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x;
9167330a12Sdan  }
9267330a12Sdan} -body {
9367330a12Sdan  execsql {
9467330a12Sdan    BEGIN;
9567330a12Sdan      UPDATE t1 SET a = randomblob(99);
9667330a12Sdan      SAVEPOINT abc;
9767330a12Sdan        UPDATE t1 SET a = randomblob(98) WHERE (rowid%10)==0;
9867330a12Sdan      ROLLBACK TO abc;
9967330a12Sdan        UPDATE t1 SET a = randomblob(97) WHERE (rowid%5)==0;
10067330a12Sdan      ROLLBACK TO abc;
10167330a12Sdan    COMMIT;
10267330a12Sdan  }
10367330a12Sdan} -test {
10467330a12Sdan  faultsim_test_result {0 {}}
10567330a12Sdan  faultsim_integrity_check db
10667330a12Sdan}
10767330a12Sdan
10867330a12Sdando_faultsim_test 4 -faults * -prep {
10967330a12Sdan  sqlite3 db ""
11067330a12Sdan  db eval {
11167330a12Sdan    PRAGMA page_size = 1024;
11267330a12Sdan    PRAGMA cache_size = 10;
11367330a12Sdan    CREATE TABLE t1(a, b);
11467330a12Sdan    CREATE INDEX i1 ON t1(b, a);
11567330a12Sdan    WITH x(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<50)
11667330a12Sdan    INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x;
11767330a12Sdan  }
11867330a12Sdan} -body {
11967330a12Sdan  execsql {
12067330a12Sdan    BEGIN;
12167330a12Sdan      UPDATE t1 SET a = randomblob(99);
12267330a12Sdan      SAVEPOINT abc;
12367330a12Sdan        UPDATE t1 SET a = randomblob(98) WHERE (rowid%10)==0;
12467330a12Sdan      ROLLBACK TO abc;
12567330a12Sdan        UPDATE t1 SET a = randomblob(97) WHERE (rowid%5)==0;
12667330a12Sdan      ROLLBACK TO abc;
12767330a12Sdan    COMMIT;
12867330a12Sdan  }
12967330a12Sdan} -test {
13067330a12Sdan  faultsim_test_result {0 {}}
13167330a12Sdan}
13267330a12Sdan
13367330a12Sdansqlite3_memdebug_vfs_oom_test 1
13467330a12Sdanfinish_test
135