interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / session / sessionfault3.test

195 lines · 4736 bytes Raw Blame History
1# 2016 October 6
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 sessionfault3
22
23do_execsql_test 1.0 {
24 CREATE TABLE t1(a, b, PRIMARY KEY(a));
25 INSERT INTO t1 VALUES(1, 2);
26 INSERT INTO t1 VALUES(3, 4);
27 INSERT INTO t1 VALUES('five', 'six');
28}
29
30set C1 [changeset_from_sql {
31 INSERT INTO t1 VALUES('seven', 'eight');
32 UPDATE t1 SET b=6 WHERE a='five';
33 DELETE FROM t1 WHERE a=1;
34}]
35
36do_execsql_test 1.1 {
37 ALTER TABLE t1 ADD COLUMN d DEFAULT 123;
38 ALTER TABLE t1 ADD COLUMN e DEFAULT 'string';
39}
40
41set C2 [changeset_from_sql {
42 UPDATE t1 SET e='new value' WHERE a='seven';
43 INSERT INTO t1 VALUES(0, 0, 0, 0);
44}]
45
46do_faultsim_test 1.2 -faults oom* -prep {
47 sqlite3changegroup G
48} -body {
49 G schema db main
50 G add $::C1
51 G add $::C2
52 G output
53 set {} {}
54} -test {
55 catch { G delete }
56 faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
57}
58
59do_faultsim_test 1.3 -faults oom* -prep {
60 sqlite3changegroup G
61} -body {
62 G schema db main
63 G change_begin INSERT t1 0
64 G change_int64 new 0 12345
65 G change_int64 new 1 67890
66 G output
67 set {} {}
68} -test {
69 catch { G delete }
70 faultsim_test_result \
71 {0 {}} \
72 {1 SQLITE_NOMEM} \
73 {1 {SQLITE_NOMEM - no such table: t1}}
74}
75
76#-------------------------------------------------------------------------
77reset_db
78do_execsql_test 2.0 {
79 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
80 INSERT INTO t1 VALUES(1, 'one');
81 INSERT INTO t1 VALUES(2, 'two');
82 ALTER TABLE t1 ADD COLUMN c DEFAULT 'abcdefghijklmnopqrstuvwxyz';
83}
84faultsim_save_and_close
85
86do_faultsim_test 2 -faults oom-t* -prep {
87 faultsim_restore_and_reopen
88 db eval {SELECT * FROM sqlite_schema}
89} -body {
90 sqlite3session S db main
91 S attach *
92 execsql {
93 DELETE FROM t1 WHERE a = 1;
94 }
95} -test {
96 faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
97 catch { S delete }
98}
99
100#-------------------------------------------------------------------------
101reset_db
102do_execsql_test 3.0 {
103 CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
104 INSERT INTO t1 VALUES(1, 'one');
105 INSERT INTO t1 VALUES(2, 'two');
106 INSERT INTO t1 VALUES(3, 'three');
107 INSERT INTO t1 VALUES(4, 'four');
108 INSERT INTO t1 VALUES(5, 'five');
109}
110faultsim_save_and_close
111faultsim_restore_and_reopen
112
113set C [changeset_from_sql {
114 UPDATE t1 SET b=NULL WHERE a IN (1, 3, 5);
115 UPDATE t1 SET b='three' WHERE a=1;
116 UPDATE t1 SET b='five' WHERE a=3;
117 UPDATE t1 SET b='one' WHERE a=5;
118}]
119
120do_execsql_test 3.1 {
121 SELECT * FROM t1
122} {
123 1 three 2 two 3 five 4 four 5 one
124}
125
126proc xConflict {args} {
127 lappend ::conflict_list $args
128 return "OMIT"
129}
130
131do_faultsim_test 3 -faults oom* -prep {
132 faultsim_restore_and_reopen
133 db eval {SELECT * FROM sqlite_schema}
134} -body {
135 sqlite3changeset_apply_v2 db $::C xConflict
136 set {} {}
137} -test {
138 faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
139}
140
141#-------------------------------------------------------------------------
142# Test that an OOM in change_begin() or a change_xxx() function does not
143# leave a partially constructed change behind.
144#
145# Report: https://sqlite.org/bugs/forumpost/875fb2b46f
146#
147reset_db
148do_execsql_test 4.0 {
149 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d);
150}
151
152do_faultsim_test 4 -faults oom* -prep {
153 sqlite3changegroup G
154 G schema db main
155 G change_begin INSERT t1 0
156 G change_finish 1
157} -body {
158 G change_begin UPDATE t1 0
159} -test {
160 faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
161 if {$testrc} { G change_begin UPDATE t1 0 }
162 G change_finish 1
163 G delete
164}
165
166do_faultsim_test 5 -faults oom* -prep {
167 sqlite3changegroup G
168 G schema db main
169 G change_begin INSERT t1 0
170 G change_int64 new 0 1
171 G change_int64 new 1 1
172 G change_null new 2
173 G change_null new 3
174 G change_finish 0
175 G change_begin INSERT t1 0
176 G change_int64 new 0 2
177 G change_null new 2
178 G change_null new 3
179} -body {
180 G change_blob new 1 [binary format x400]
181} -test {
182 faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
183 set rc [catch { G change_finish 0 } msg]
184 G delete
185 if {$testrc} {
186 set expect {1 {SQLITE_ERROR - invalid change: column 1 is undefined}}
187 } else {
188 set expect {0 {}}
189 }
190 if {[list $rc $msg]!=$expect} {
191 error "change_finish returned: [list $rc $msg]"
192 }
193}
194
195finish_test