1# 2016 March 31
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
6#    May you do good and not evil.
7#    May you find forgiveness for yourself and forgive others.
8#    May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
12# The focus of this file is testing the session module.
13#
14
15if {![info exists testdir]} {
16  set testdir [file join [file dirname [info script]] .. .. test]
17}
18source [file join [file dirname [info script]] session_common.tcl]
19source $testdir/tester.tcl
20ifcapable !session {finish_test; return}
21set testprefix sessionfault2
22
23do_execsql_test 1.0.0 {
24  CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
25  INSERT INTO t1 VALUES(1, 1);
26  INSERT INTO t1 VALUES(2, 2);
27  INSERT INTO t1 VALUES(3, 3);
28
29  CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
30  INSERT INTO t2 VALUES(1, 1);
31  INSERT INTO t2 VALUES(2, 2);
32  INSERT INTO t2 VALUES(3, 3);
33}
34faultsim_save_and_close
35
36faultsim_restore_and_reopen
37do_test 1.0.1 {
38  set ::C [changeset_from_sql {
39    UPDATE t1 SET b=4 WHERE a=3;
40    UPDATE t1 SET b=3 WHERE a=2;
41    UPDATE t1 SET b=2 WHERE a=1;
42    UPDATE t2 SET b=0 WHERE a=1;
43    UPDATE t2 SET b=1 WHERE a=2;
44    UPDATE t2 SET b=2 WHERE a=3;
45  }]
46  set {} {}
47} {}
48
49proc xConflict args { return "OMIT" }
50
51do_faultsim_test 1 -faults oom-p* -prep {
52  faultsim_restore_and_reopen
53} -body {
54  sqlite3changeset_apply db $::C xConflict
55} -test {
56  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
57  faultsim_integrity_check
58
59  catch { db eval ROLLBACK }
60  set res [db eval {
61    SELECT * FROM t1;
62    SELECT * FROM t2;
63  }]
64
65  if {$testrc==0} {
66    if {$res != "1 2 2 3 3 4 1 0 2 1 3 2"} { error "data error" }
67  } else {
68    if {
69         $res != "1 2 2 3 3 4 1 0 2 1 3 2"
70      && $res != "1 1 2 2 3 3 1 1 2 2 3 3"
71    } { error "data error!! $res" }
72  }
73}
74
75#-------------------------------------------------------------------------
76# OOM when applying a changeset for which one of the tables has a name
77# 99 bytes in size. This happens to cause an extra malloc in within the
78# sessions_strm permutation.
79#
80reset_db
81set nm [string repeat t 99]
82do_execsql_test 2.0.0 [string map "%TBL% $nm" {
83  CREATE TABLE %TBL%(a PRIMARY KEY, b UNIQUE);
84}]
85faultsim_save_and_close
86
87faultsim_restore_and_reopen
88do_test 1.0.1 {
89  set ::C [changeset_from_sql [string map "%TBL% $nm" {
90    INSERT INTO %TBL% VALUES(1, 2);
91    INSERT INTO %TBL% VALUES(3, 4);
92  }]]
93  set {} {}
94} {}
95
96proc xConflict args { return "OMIT" }
97do_faultsim_test 2 -faults oom-p* -prep {
98  faultsim_restore_and_reopen
99} -body {
100  sqlite3changeset_apply db $::C xConflict
101} -test {
102  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
103  faultsim_integrity_check
104}
105
106finish_test
107
108