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
20set testprefix sessionfault2
21
22do_execsql_test 1.0.0 {
23  CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
24  INSERT INTO t1 VALUES(1, 1);
25  INSERT INTO t1 VALUES(2, 2);
26  INSERT INTO t1 VALUES(3, 3);
27
28  CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
29  INSERT INTO t2 VALUES(1, 1);
30  INSERT INTO t2 VALUES(2, 2);
31  INSERT INTO t2 VALUES(3, 3);
32}
33faultsim_save_and_close
34
35faultsim_restore_and_reopen
36do_test 1.0.1 {
37  set ::C [changeset_from_sql {
38    UPDATE t1 SET b=4 WHERE a=3;
39    UPDATE t1 SET b=3 WHERE a=2;
40    UPDATE t1 SET b=2 WHERE a=1;
41    UPDATE t2 SET b=0 WHERE a=1;
42    UPDATE t2 SET b=1 WHERE a=2;
43    UPDATE t2 SET b=2 WHERE a=3;
44  }]
45  set {} {}
46} {}
47
48proc xConflict args { return "OMIT" }
49
50do_faultsim_test 1 -faults oom-p* -prep {
51  faultsim_restore_and_reopen
52} -body {
53  sqlite3changeset_apply db $::C xConflict
54} -test {
55  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
56  faultsim_integrity_check
57
58  catch { db eval ROLLBACK }
59  set res [db eval {
60    SELECT * FROM t1;
61    SELECT * FROM t2;
62  }]
63
64  if {$testrc==0} {
65    if {$res != "1 2 2 3 3 4 1 0 2 1 3 2"} { error "data error" }
66  } else {
67    if {
68         $res != "1 2 2 3 3 4 1 0 2 1 3 2"
69      && $res != "1 1 2 2 3 3 1 1 2 2 3 3"
70    } { error "data error!! $res" }
71  }
72}
73
74#-------------------------------------------------------------------------
75# OOM when applying a changeset for which one of the tables has a name
76# 99 bytes in size. This happens to cause an extra malloc in within the
77# sessions_strm permutation.
78#
79reset_db
80set nm [string repeat t 99]
81do_execsql_test 2.0.0 [string map "%TBL% $nm" {
82  CREATE TABLE %TBL%(a PRIMARY KEY, b UNIQUE);
83}]
84faultsim_save_and_close
85
86faultsim_restore_and_reopen
87do_test 1.0.1 {
88  set ::C [changeset_from_sql [string map "%TBL% $nm" {
89    INSERT INTO %TBL% VALUES(1, 2);
90    INSERT INTO %TBL% VALUES(3, 4);
91  }]]
92  set {} {}
93} {}
94
95proc xConflict args { return "OMIT" }
96do_faultsim_test 2 -faults oom-p* -prep {
97  faultsim_restore_and_reopen
98} -body {
99  sqlite3changeset_apply db $::C xConflict
100} -test {
101  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
102  faultsim_integrity_check
103}
104
105finish_test
106
107