summaryrefslogtreecommitdiffstats
path: root/ext/session/sessionfault2.test
blob: dd00eaa1c83c2f14a288c4bd5030197322dd6272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# 2016 March 31
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# The focus of this file is testing the session module.
#

if {![info exists testdir]} {
  set testdir [file join [file dirname [info script]] .. .. test]
} 
source [file join [file dirname [info script]] session_common.tcl]
source $testdir/tester.tcl
ifcapable !session {finish_test; return}
set testprefix sessionfault2

if 1 {

do_execsql_test 1.0.0 {
  CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
  INSERT INTO t1 VALUES(1, 1);
  INSERT INTO t1 VALUES(2, 2);
  INSERT INTO t1 VALUES(3, 3);

  CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
  INSERT INTO t2 VALUES(1, 1);
  INSERT INTO t2 VALUES(2, 2);
  INSERT INTO t2 VALUES(3, 3);
}
faultsim_save_and_close

faultsim_restore_and_reopen
do_test 1.0.1 {
  set ::C [changeset_from_sql {
    UPDATE t1 SET b=4 WHERE a=3;
    UPDATE t1 SET b=3 WHERE a=2;
    UPDATE t1 SET b=2 WHERE a=1;
    UPDATE t2 SET b=0 WHERE a=1;
    UPDATE t2 SET b=1 WHERE a=2;
    UPDATE t2 SET b=2 WHERE a=3;
  }]
  set {} {}
} {}

proc xConflict args { return "OMIT" }

do_faultsim_test 1 -faults oom-p* -prep {
  faultsim_restore_and_reopen
} -body {
  sqlite3changeset_apply db $::C xConflict
} -test {
  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
  faultsim_integrity_check

  catch { db eval ROLLBACK }
  set res [db eval {
    SELECT * FROM t1;
    SELECT * FROM t2;
  }]

  if {$testrc==0} {
    if {$res != "1 2 2 3 3 4 1 0 2 1 3 2"} { error "data error" }
  } else {
    if {
         $res != "1 2 2 3 3 4 1 0 2 1 3 2"
      && $res != "1 1 2 2 3 3 1 1 2 2 3 3"
    } { error "data error!! $res" }
  }
}

#-------------------------------------------------------------------------
# OOM when applying a changeset for which one of the tables has a name
# 99 bytes in size. This happens to cause an extra malloc in within the
# sessions_strm permutation.
#
reset_db
set nm [string repeat t 99]
do_execsql_test 2.0.0 [string map "%TBL% $nm" {
  CREATE TABLE %TBL%(a PRIMARY KEY, b UNIQUE);
}]
faultsim_save_and_close

faultsim_restore_and_reopen
do_test 1.0.1 {
  set ::C [changeset_from_sql [string map "%TBL% $nm" {
    INSERT INTO %TBL% VALUES(1, 2);
    INSERT INTO %TBL% VALUES(3, 4);
  }]]
  set {} {}
} {}

proc xConflict args { return "OMIT" }
do_faultsim_test 2 -faults oom-p* -prep {
  faultsim_restore_and_reopen
} -body {
  sqlite3changeset_apply db $::C xConflict
} -test {
  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
  faultsim_integrity_check
}

#-------------------------------------------------------------------------
# OOM when collecting and apply a changeset that uses sqlite_stat1.
#
reset_db
forcedelete test.db2
sqlite3 db2 test.db2
do_common_sql {
  CREATE TABLE t1(a PRIMARY KEY, b UNIQUE, c);
  CREATE INDEX i1 ON t1(c);
  INSERT INTO t1 VALUES(1, 2, 3);
  INSERT INTO t1 VALUES(4, 5, 6);
  INSERT INTO t1 VALUES(7, 8, 9);
  CREATE TABLE t2(a, b, c);
  INSERT INTO t2 VALUES(1, 2, 3);
  INSERT INTO t2 VALUES(4, 5, 6);
  INSERT INTO t2 VALUES(7, 8, 9);
  ANALYZE;
}
faultsim_save_and_close
db2 close

do_faultsim_test 1.1 -faults oom-* -prep {
  catch {db2 close}
  catch {db close}
  faultsim_restore_and_reopen
  sqlite3 db2 test.db2
} -body {
  do_then_apply_sql {
    INSERT INTO sqlite_stat1 VALUES('x', 'y', 45);
    UPDATE sqlite_stat1 SET stat = 123 WHERE tbl='t1' AND idx='i1';
    UPDATE sqlite_stat1 SET stat = 456 WHERE tbl='t2';
  }
} -test {
  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
  faultsim_integrity_check
  if {$testrc==0} { compare_db db db2 }
}

#-------------------------------------------------------------------------
# OOM when collecting and using a rebase changeset.
#
reset_db
do_execsql_test 2.0 {
  CREATE TABLE t3(a, b, c, PRIMARY KEY(b, c));
  CREATE TABLE t4(x PRIMARY KEY, y, z);

  INSERT INTO t3 VALUES(1, 2, 3);
  INSERT INTO t3 VALUES(4, 2, 5);
  INSERT INTO t3 VALUES(7, 2, 9);

  INSERT INTO t4 VALUES('a', 'b', 'c');
  INSERT INTO t4 VALUES('d', 'e', 'f');
  INSERT INTO t4 VALUES('g', 'h', 'i');
}
faultsim_save_and_close
db2 close

proc xConflict {ret args} { return $ret }

do_test 2.1 {
  faultsim_restore_and_reopen
  set C1 [changeset_from_sql {
    INSERT INTO t3 VALUES(10, 11, 12);
    UPDATE t4 SET y='j' WHERE x='g';
    DELETE FROM t4 WHERE x='a';
  }]

  faultsim_restore_and_reopen
  set C2 [changeset_from_sql {
    INSERT INTO t3 VALUES(1000, 11, 12);
    DELETE FROM t4 WHERE x='g';
  }]

  faultsim_restore_and_reopen
  sqlite3changeset_apply db $C1 [list xConflict OMIT]
  faultsim_save_and_close
} {}

do_faultsim_test 2.2 -faults oom* -prep {
  catch {db2 close}
  catch {db close}
  faultsim_restore_and_reopen
  sqlite3 db2 test.db2
} -body {
  set rebase [sqlite3changeset_apply_v2 db $::C2 [list xConflict OMIT]]
  set {} {}
} -test {
  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
}
do_faultsim_test 2.3 -faults oom* -prep {
  catch {db2 close}
  catch {db close}
  faultsim_restore_and_reopen
  sqlite3 db2 test.db2
} -body {
  set rebase [sqlite3changeset_apply_v2 db $::C2 [list xConflict REPLACE]]
  set {} {}
} -test {
  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
}
do_faultsim_test 2.4 -faults oom* -prep {
  catch {db2 close}
  catch {db close}
  faultsim_restore_and_reopen
  set ::rebase [sqlite3changeset_apply_v2 db $::C2 [list xConflict REPLACE]]
} -body {
  sqlite3rebaser_create R
  R configure $::rebase
  R rebase $::C1
  set {} {}
} -test {
  catch { R delete } 
  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
}
do_faultsim_test 2.5 -faults oom* -prep {
  catch {db2 close}
  catch {db close}
  faultsim_restore_and_reopen
  set ::rebase [sqlite3changeset_apply_v2 db $::C2 [list xConflict OMIT]]
} -body {
  sqlite3rebaser_create R
  R configure $::rebase
  R rebase $::C1
  set {} {}
} -test {
  catch { R delete } 
  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
}

}

reset_db
do_execsql_test 3.0 {
  CREATE TABLE t1(x PRIMARY KEY, y, z);
  INSERT INTO t1 VALUES(3, 1, 4);
  INSERT INTO t1 VALUES(1, 5, 9);
}
faultsim_save_and_close

proc xConflict {ret args} { return $ret }

do_test 3.1 {
  faultsim_restore_and_reopen

  execsql { BEGIN; UPDATE t1 SET z=11; }
  set C1 [changeset_from_sql {
    UPDATE t1 SET z=10 WHERE x=1;
  }]
  execsql { ROLLBACK }

  execsql { BEGIN; UPDATE t1 SET z=11; }
  set C2 [changeset_from_sql {
    UPDATE t1 SET z=55 WHERE x=1;
  }]
  execsql { ROLLBACK }

  set ::rebase1 [sqlite3changeset_apply_v2 db $::C1 [list xConflict OMIT]]
  set ::rebase2 [sqlite3changeset_apply_v2 db $::C2 [list xConflict OMIT]]
  set {} {}
  execsql { SELECT * FROM t1 }
} {3 1 4 1 5 9}


do_faultsim_test 3.2 -faults oom* -prep {
  faultsim_restore_and_reopen
} -body {
  sqlite3rebaser_create R
  R configure $::rebase1
  R configure $::rebase2
  set {} {}
} -test {
  catch { R delete } 
  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
}


finish_test