File: pagerfault.test

package info (click to toggle)
sqlcipher 3.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 70,888 kB
  • sloc: ansic: 195,357; tcl: 14,300; sh: 3,782; yacc: 1,245; makefile: 958; cs: 299; cpp: 128
file content (1551 lines) | stat: -rw-r--r-- 43,369 bytes parent folder | download | duplicates (4)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
# 2010 June 15
#
# 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.
#
#***********************************************************************
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/lock_common.tcl
source $testdir/malloc_common.tcl

if {[permutation] == "inmemory_journal"} {
  finish_test
  return
}

if {$::tcl_platform(platform)=="windows"} {
  finish_test
  return
}

set a_string_counter 1
proc a_string {n} {
  global a_string_counter
  incr a_string_counter
  string range [string repeat "${a_string_counter}." $n] 1 $n
}
db func a_string a_string

#-------------------------------------------------------------------------
# Test fault-injection while rolling back a hot-journal file.
#
do_test pagerfault-1-pre1 {
  execsql {
    PRAGMA journal_mode = DELETE;
    PRAGMA cache_size = 10;
    CREATE TABLE t1(a UNIQUE, b UNIQUE);
    INSERT INTO t1 VALUES(a_string(200), a_string(300));
    INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
    INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
    BEGIN;
      INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
      INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
      INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
      INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-1 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { SELECT count(*) FROM t1 }
} -test {
  faultsim_test_result {0 4} 
  faultsim_integrity_check
  if {[db one { SELECT count(*) FROM t1 }] != 4} {
    error "Database content appears incorrect"
  }
}

#-------------------------------------------------------------------------
# Test fault-injection while rolling back a hot-journal file with a 
# page-size different from the current value stored on page 1 of the
# database file.
#
do_test pagerfault-2-pre1 {
  testvfs tv -default 1
  tv filter xSync
  tv script xSyncCb
  proc xSyncCb {filename args} {
    if {[string match *journal filename]==0} faultsim_save
  }
  faultsim_delete_and_reopen
  execsql {
    PRAGMA page_size = 4096;
    BEGIN;
      CREATE TABLE abc(a, b, c);
      INSERT INTO abc VALUES('o', 't', 't'); 
      INSERT INTO abc VALUES('f', 'f', 's'); 
      INSERT INTO abc SELECT * FROM abc; -- 4
      INSERT INTO abc SELECT * FROM abc; -- 8
      INSERT INTO abc SELECT * FROM abc; -- 16
      INSERT INTO abc SELECT * FROM abc; -- 32
      INSERT INTO abc SELECT * FROM abc; -- 64
      INSERT INTO abc SELECT * FROM abc; -- 128
      INSERT INTO abc SELECT * FROM abc; -- 256
    COMMIT;
    PRAGMA page_size = 1024;
    VACUUM;
  }
  db close
  tv delete
} {}
do_faultsim_test pagerfault-2 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { SELECT * FROM abc }
} -test {
  set answer [split [string repeat "ottffs" 128] ""]
  faultsim_test_result [list 0 $answer]
  faultsim_integrity_check
  set res [db eval { SELECT * FROM abc }]
  if {$res != $answer} { error "Database content appears incorrect ($res)" }
} 

#-------------------------------------------------------------------------
# Test fault-injection while rolling back hot-journals that were created
# as part of a multi-file transaction.
#
do_test pagerfault-3-pre1 {
  testvfs tstvfs -default 1
  tstvfs filter xDelete
  tstvfs script xDeleteCallback

  proc xDeleteCallback {method file args} {
    set file [file tail $file]
    if { [string match *mj* $file] } { faultsim_save }
  }

  faultsim_delete_and_reopen
  db func a_string a_string

  execsql {
    ATTACH 'test.db2' AS aux;
    PRAGMA journal_mode = DELETE;
    PRAGMA main.cache_size = 10;
    PRAGMA aux.cache_size = 10;

    CREATE TABLE t1(a UNIQUE, b UNIQUE);
    CREATE TABLE aux.t2(a UNIQUE, b UNIQUE);
    INSERT INTO t1 VALUES(a_string(200), a_string(300));
    INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
    INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
    INSERT INTO t2 SELECT * FROM t1;

    BEGIN;
      INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
      INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
      INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
      INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
      REPLACE INTO t2 SELECT * FROM t1;
    COMMIT;
  }

  db close
  tstvfs delete
} {}
do_faultsim_test pagerfault-3 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { 
    ATTACH 'test.db2' AS aux;
    SELECT count(*) FROM t2;
    SELECT count(*) FROM t1;
  }
} -test {
  faultsim_test_result {0 {4 4}} {1 {unable to open database: test.db2}}
  faultsim_integrity_check
  catchsql { ATTACH 'test.db2' AS aux }
  if {[db one { SELECT count(*) FROM t1 }] != 4
   || [db one { SELECT count(*) FROM t2 }] != 4
  } {
    error "Database content appears incorrect"
  }
}

#-------------------------------------------------------------------------
# Test fault-injection as part of a vanilla, no-transaction, INSERT
# statement.
#
do_faultsim_test pagerfault-4 -prep {
  faultsim_delete_and_reopen
} -body {
  execsql { 
    CREATE TABLE x(y);
    INSERT INTO x VALUES('z');
    SELECT * FROM x;
  }
} -test {
  faultsim_test_result {0 z}
  faultsim_integrity_check
}

#-------------------------------------------------------------------------
# Test fault-injection as part of a commit when using journal_mode=PERSIST.
# Three different cases:
#
#    pagerfault-5.1: With no journal_size_limit configured.
#    pagerfault-5.2: With a journal_size_limit configured.
#    pagerfault-5.4: Multi-file transaction. One connection has a 
#                    journal_size_limit of 0, the other has no limit.
#
do_test pagerfault-5-pre1 {
  faultsim_delete_and_reopen
  db func a_string a_string
  execsql {
    CREATE TABLE t1(a UNIQUE, b UNIQUE);
    INSERT INTO t1 VALUES(a_string(200), a_string(300));
    INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
    INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-5.1 -prep {
  faultsim_restore_and_reopen
  db func a_string a_string
  execsql { PRAGMA journal_mode = PERSIST }
} -body {
  execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
} -test {
  faultsim_test_result {0 {}}
  faultsim_integrity_check
}
do_faultsim_test pagerfault-5.2 -prep {
  faultsim_restore_and_reopen
  db func a_string a_string
  execsql { 
    PRAGMA journal_mode = PERSIST;
    PRAGMA journal_size_limit = 2048;
  }
} -body {
  execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
} -test {
  faultsim_test_result {0 {}}
  faultsim_integrity_check
}
do_faultsim_test pagerfault-5.3 -faults oom-transient -prep {
  faultsim_restore_and_reopen
  db func a_string a_string
  forcedelete test2.db test2.db-journal test2.db-wal
  execsql { 
    PRAGMA journal_mode = PERSIST;
    ATTACH 'test2.db' AS aux;
    PRAGMA aux.journal_mode = PERSIST;
    PRAGMA aux.journal_size_limit = 0;
  }
} -body {
  execsql {
    BEGIN;
      INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
      CREATE TABLE aux.t2 AS SELECT * FROM t1;
    COMMIT;
  }
} -test {
  faultsim_test_result {0 {}}

  catchsql { COMMIT }
  catchsql { ROLLBACK }

  faultsim_integrity_check
  set res ""
  set rc [catch { set res [db one { PRAGMA aux.integrity_check }] }]
  if {$rc!=0 || $res != "ok"} {error "integrity-check problem:$rc $res"}
}

#-------------------------------------------------------------------------
# Test fault-injection as part of a commit when using 
# journal_mode=TRUNCATE.
#
do_test pagerfault-6-pre1 {
  faultsim_delete_and_reopen
  db func a_string a_string
  execsql {
    CREATE TABLE t1(a UNIQUE, b UNIQUE);
    INSERT INTO t1 VALUES(a_string(200), a_string(300));
  }
  faultsim_save_and_close
} {}

do_faultsim_test pagerfault-6.1 -prep {
  faultsim_restore_and_reopen
  db func a_string a_string
  execsql { PRAGMA journal_mode = TRUNCATE }
} -body {
  execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
  execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
} -test {
  faultsim_test_result {0 {}}
  faultsim_integrity_check
}

# The unix vfs xAccess() method considers a file zero bytes in size to
# "not exist". This proc overrides that behaviour so that a zero length
# file is considered to exist.
#
proc xAccess {method filename op args} {
  if {$op != "SQLITE_ACCESS_EXISTS"} { return "" }
  return [file exists $filename]
}
do_faultsim_test pagerfault-6.2 -faults cantopen-* -prep {
  shmfault filter xAccess
  shmfault script xAccess

  faultsim_restore_and_reopen
  db func a_string a_string
  execsql { PRAGMA journal_mode = TRUNCATE }
} -body {
  execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
  execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
} -test {
  faultsim_test_result {0 {}}
  faultsim_integrity_check
}

# The following was an attempt to get a bitvec malloc to fail. Didn't work.
#
# do_test pagerfault-6-pre1 {
#   faultsim_delete_and_reopen
#   execsql {
#     CREATE TABLE t1(x, y, UNIQUE(x, y));
#     INSERT INTO t1 VALUES(1, randomblob(1501));
#     INSERT INTO t1 VALUES(2, randomblob(1502));
#     INSERT INTO t1 VALUES(3, randomblob(1503));
#     INSERT INTO t1 VALUES(4, randomblob(1504));
#     INSERT INTO t1 
#       SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
#     INSERT INTO t1 
#       SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
#     INSERT INTO t1 
#       SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
#     INSERT INTO t1 
#       SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
#   }
#   faultsim_save_and_close
# } {}
# do_faultsim_test pagerfault-6 -prep {
#   faultsim_restore_and_reopen
# } -body {
#   execsql { 
#     BEGIN;
#       UPDATE t1 SET x=x+4 WHERE x=1;
#       SAVEPOINT one;
#         UPDATE t1 SET x=x+4 WHERE x=2;
#         SAVEPOINT three;
#           UPDATE t1 SET x=x+4 WHERE x=3;
#           SAVEPOINT four;
#             UPDATE t1 SET x=x+4 WHERE x=4;
#         RELEASE three;
#     COMMIT;
#     SELECT DISTINCT x FROM t1;
#   }
# } -test {
#   faultsim_test_result {0 {5 6 7 8}}
#   faultsim_integrity_check
# }
#

# This is designed to provoke a special case in the pager code:
#
# If an error (specifically, a FULL or IOERR error) occurs while writing a
# dirty page to the file-system in order to free up memory, the pager enters
# the "error state". An IO error causes SQLite to roll back the current
# transaction (exiting the error state). A FULL error, however, may only
# rollback the current statement.
#
# This block tests that nothing goes wrong if a FULL error occurs while
# writing a dirty page out to free memory from within a statement that has
# opened a statement transaction.
#
do_test pagerfault-7-pre1 {
  faultsim_delete_and_reopen
  execsql {
    CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
    BEGIN;
      INSERT INTO t2 VALUES(NULL, randomblob(1500));
      INSERT INTO t2 VALUES(NULL, randomblob(1500));
      INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2;    --  4
      INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2;    --  8
      INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2;    -- 16
      INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2;    -- 32
      INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2;    -- 64
    COMMIT;
    CREATE TABLE t1(a PRIMARY KEY, b);
    INSERT INTO t1 SELECT * FROM t2;
    DROP TABLE t2;
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-7 -prep {
  faultsim_restore_and_reopen
  execsql { 
    PRAGMA cache_size = 10;
    BEGIN;
      UPDATE t1 SET b = randomblob(1500);
  }
} -body {
  execsql { UPDATE t1 SET a = 65, b = randomblob(1500) WHERE (a+1)>200 }
  execsql COMMIT
} -test {
  faultsim_test_result {0 {}}
  faultsim_integrity_check
}

do_test pagerfault-8-pre1 {
  faultsim_delete_and_reopen
  execsql {
    PRAGMA auto_vacuum = 1;
    CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
    BEGIN;
      INSERT INTO t1 VALUES(NULL, randomblob(1500));
      INSERT INTO t1 VALUES(NULL, randomblob(1500));
      INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1;    --  4
      INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1;    --  8
      INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1;    -- 16
      INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1;    -- 32
      INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1;    -- 64
    COMMIT;
  }
  faultsim_save_and_close
  set filesize [file size test.db]
  set {} {}
} {}
do_test pagerfault-8-pre2 {
  faultsim_restore_and_reopen
  execsql { DELETE FROM t1 WHERE a>32 }
  expr {[file size test.db] < $filesize}
} {1}
do_faultsim_test pagerfault-8 -prep {
  faultsim_restore_and_reopen
  execsql { 
    BEGIN;
    DELETE FROM t1 WHERE a>32;
  }
} -body {
  execsql COMMIT
} -test {
  faultsim_test_result {0 {}}
  faultsim_integrity_check
}

#-------------------------------------------------------------------------
# This test case is specially designed so that during a savepoint 
# rollback, a new cache entry must be allocated (see comments surrounding
# the call to sqlite3PagerAcquire() from within pager_playback_one_page()
# for details). Test the effects of injecting an OOM at this point.
#
do_test pagerfault-9-pre1 {
  faultsim_delete_and_reopen
  execsql {
    PRAGMA auto_vacuum = incremental;
    CREATE TABLE t1(x);
    CREATE TABLE t2(y);
    CREATE TABLE t3(z);

    INSERT INTO t1 VALUES(randomblob(900));
    INSERT INTO t1 VALUES(randomblob(900));
    DELETE FROM t1;
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-9.1 -prep {
  faultsim_restore_and_reopen
  execsql { 
    BEGIN;
      INSERT INTO t1 VALUES(randomblob(900));
      INSERT INTO t1 VALUES(randomblob(900));
      DROP TABLE t3;
      DROP TABLE t2;
      SAVEPOINT abc;
        PRAGMA incremental_vacuum;
  }
} -body {
  execsql {
    ROLLBACK TO abc;
    COMMIT;
    PRAGMA freelist_count
  }
} -test {
  faultsim_test_result {0 2}
  faultsim_integrity_check

  set sl [db one { SELECT COALESCE(sum(length(x)), 'null') FROM t1 }]
  if {$sl!="null" && $sl!=1800} { 
    error "Content looks no good... ($sl)" 
  }
}

#-------------------------------------------------------------------------
# Test fault injection with a temporary database file.
#
foreach v {a b} {
  do_faultsim_test pagerfault-10$v -prep {
    sqlite3 db ""
    db func a_string a_string;
    execsql {
      PRAGMA cache_size = 10;
      BEGIN;
        CREATE TABLE xx(a, b, UNIQUE(a, b));
        INSERT INTO xx VALUES(a_string(200), a_string(200));
        INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
        INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
        INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
        INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
      COMMIT;
    }
  } -body {
    execsql { UPDATE xx SET a = a_string(300) }
  } -test {
    faultsim_test_result {0 {}}
    if {$::v == "b"} { execsql { PRAGMA journal_mode = TRUNCATE } }
    faultsim_integrity_check
    faultsim_integrity_check
  }
}

#-------------------------------------------------------------------------
# Test fault injection with transaction savepoints (savepoints created
# when a SAVEPOINT command is executed outside of any other savepoint
# or transaction context).
#
do_test pagerfault-9-pre1 {
  faultsim_delete_and_reopen
  db func a_string a_string;
  execsql {
    PRAGMA auto_vacuum = on;
    CREATE TABLE t1(x UNIQUE);
    CREATE TABLE t2(y UNIQUE);
    CREATE TABLE t3(z UNIQUE);
    BEGIN;
      INSERT INTO t1 VALUES(a_string(202));
      INSERT INTO t2 VALUES(a_string(203));
      INSERT INTO t3 VALUES(a_string(204));
      INSERT INTO t1 SELECT a_string(202) FROM t1;
      INSERT INTO t1 SELECT a_string(203) FROM t1;
      INSERT INTO t1 SELECT a_string(204) FROM t1;
      INSERT INTO t1 SELECT a_string(205) FROM t1;
      INSERT INTO t2 SELECT a_string(length(x)) FROM t1;
      INSERT INTO t3 SELECT a_string(length(x)) FROM t1;
    COMMIT;
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-11 -prep {
  faultsim_restore_and_reopen
  execsql { PRAGMA cache_size = 10 }
} -body {
  execsql {
    SAVEPOINT trans;
      UPDATE t2 SET y = y||'2';
      INSERT INTO t3 SELECT * FROM t2;
      DELETE FROM t1;
    ROLLBACK TO trans;
    UPDATE t1 SET x = x||'3';
    INSERT INTO t2 SELECT * FROM t1;
    DELETE FROM t3;
    RELEASE trans;
  }
} -test {
  faultsim_test_result {0 {}}
  faultsim_integrity_check
}


#-------------------------------------------------------------------------
# Test fault injection when writing to a database file that resides on
# a file-system with a sector-size larger than the database page-size.
#
do_test pagerfault-12-pre1 {
  testvfs ss_layer -default 1
  ss_layer sectorsize 4096
  faultsim_delete_and_reopen
  db func a_string a_string;

  execsql {
    PRAGMA page_size = 1024;
    PRAGMA journal_mode = PERSIST;
    PRAGMA cache_size = 10;
    BEGIN;
      CREATE TABLE t1(x, y UNIQUE);
      INSERT INTO t1 VALUES(a_string(333), a_string(444));
      INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
      INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
      INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
      INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
      INSERT INTO t1 SELECT a_string(44), a_string(55) FROM t1 LIMIT 13;
    COMMIT;
  }
  faultsim_save_and_close
} {}

do_faultsim_test pagerfault-12a -prep {
  faultsim_restore_and_reopen
  execsql { PRAGMA cache_size = 10 }
  db func a_string a_string;
} -body {
  execsql {
    UPDATE t1 SET x = a_string(length(x)), y = a_string(length(y));
  }
} -test {
  faultsim_test_result {0 {}}
  faultsim_integrity_check
}

do_test pagerfault-12-pre2 {
  faultsim_restore_and_reopen
  execsql {
    CREATE TABLE t2 AS SELECT * FROM t1 LIMIT 10;
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-12b -prep {
  faultsim_restore_and_reopen
  db func a_string a_string;
  execsql { SELECT * FROM t1 }
} -body {
  set sql(1) { UPDATE t2 SET x = a_string(280) }
  set sql(2) { UPDATE t1 SET x = a_string(280) WHERE rowid = 5 }

  db eval { SELECT rowid FROM t1 LIMIT 2 } { db eval $sql($rowid) }

} -test {
  faultsim_test_result {0 {}}
  faultsim_integrity_check
}

catch { db close }
ss_layer delete


#-------------------------------------------------------------------------
# Test fault injection when SQLite opens a database where the size of the
# database file is zero bytes but the accompanying journal file is larger
# than that. In this scenario SQLite should delete the journal file 
# without rolling it back, even if it is in all other respects a valid
# hot-journal file.
#
do_test pagerfault-13-pre1 {
  faultsim_delete_and_reopen
  db func a_string a_string;
  execsql {
    PRAGMA journal_mode = PERSIST;
    BEGIN;
      CREATE TABLE t1(x, y UNIQUE);
      INSERT INTO t1 VALUES(a_string(333), a_string(444));
    COMMIT;
  }
  db close
  forcedelete test.db
  faultsim_save
} {}
do_faultsim_test pagerfault-13 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { CREATE TABLE xx(a, b) }
} -test {
  faultsim_test_result {0 {}}
}

#---------------------------------------------------------------------------
# Test fault injection into a small backup operation.
#
do_test pagerfault-14-pre1 {
  faultsim_delete_and_reopen
  db func a_string a_string;
  execsql {
    PRAGMA journal_mode = PERSIST;
    ATTACH 'test.db2' AS two;
    BEGIN;
      CREATE TABLE t1(x, y UNIQUE);
      CREATE TABLE two.t2(x, y UNIQUE);
      INSERT INTO t1 VALUES(a_string(333), a_string(444));
      INSERT INTO t2 VALUES(a_string(333), a_string(444));
    COMMIT;
  }
  faultsim_save_and_close
} {}

do_faultsim_test pagerfault-14a -prep {
  faultsim_restore_and_reopen
} -body {
  if {[catch {db backup test.db2} msg]} { error [regsub {.*: } $msg {}] }
} -test {
  faultsim_test_result {0 {}} {1 {}} {1 {SQL logic error or missing database}}
}

# If TEMP_STORE is 2 or greater, then the database [db2] will be created
# as an in-memory database. This test will not work in that case, as it
# is not possible to change the page-size of an in-memory database. Even
# using the backup API.
#
# Update: It is no longer possible to change the page size of any temp
# database after it has been created.
#
do_faultsim_test pagerfault-14b -prep {
  catch { db2 close }
  faultsim_restore_and_reopen
    sqlite3 db2 ""
    db2 eval { PRAGMA page_size = 4096; CREATE TABLE xx(a) }
} -body {
  sqlite3_backup B db2 main db main
  B step 200
  set rc [B finish]
  if {[string match SQLITE_IOERR_* $rc]} {set rc SQLITE_IOERR}
  if {$rc != "SQLITE_OK"} { error [sqlite3_test_errstr $rc] }
  set {} {}
} -test {
  faultsim_test_result {1 {attempt to write a readonly database}} \
                       {1 {sqlite3_backup_init() failed}}
}

do_faultsim_test pagerfault-14c -prep {
  catch { db2 close }
  faultsim_restore_and_reopen
  sqlite3 db2 test.db2
  db2 eval { 
    PRAGMA synchronous = off; 
    PRAGMA page_size = 4096; 
    CREATE TABLE xx(a);
  }
} -body {
  sqlite3_backup B db2 main db main
  B step 200
  set rc [B finish]
  if {[string match SQLITE_IOERR_* $rc]} {set rc SQLITE_IOERR}
  if {$rc != "SQLITE_OK"} { error [sqlite3_test_errstr $rc] }
  set {} {}
} -test {
  faultsim_test_result {0 {}} {1 {sqlite3_backup_init() failed}}
}

do_test pagerfault-15-pre1 {
  faultsim_delete_and_reopen
  db func a_string a_string;
  execsql {
    BEGIN;
      CREATE TABLE t1(x, y UNIQUE);
      INSERT INTO t1 VALUES(a_string(11), a_string(22));
      INSERT INTO t1 VALUES(a_string(11), a_string(22));
    COMMIT;
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-15 -prep {
  faultsim_restore_and_reopen
  db func a_string a_string;
} -body {
  db eval { SELECT * FROM t1 LIMIT 1 } {
    execsql {
      BEGIN; INSERT INTO t1 VALUES(a_string(333), a_string(555)); COMMIT;
      BEGIN; INSERT INTO t1 VALUES(a_string(333), a_string(555)); COMMIT;
    }
  }
} -test {
  faultsim_test_result {0 {}}
  faultsim_integrity_check
}


do_test pagerfault-16-pre1 {
  faultsim_delete_and_reopen
  execsql { CREATE TABLE t1(x, y UNIQUE) }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-16 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql {
    PRAGMA locking_mode = exclusive;
    PRAGMA journal_mode = wal;
    INSERT INTO t1 VALUES(1, 2);
    INSERT INTO t1 VALUES(3, 4);
    PRAGMA journal_mode = delete;
    INSERT INTO t1 VALUES(4, 5);
    PRAGMA journal_mode = wal;
    INSERT INTO t1 VALUES(6, 7);
    PRAGMA journal_mode = persist;
    INSERT INTO t1 VALUES(8, 9);
  }
} -test {
  faultsim_test_result {0 {exclusive wal delete wal persist}}
  faultsim_integrity_check
}


#-------------------------------------------------------------------------
# Test fault injection while changing into and out of WAL mode.
#
do_test pagerfault-17-pre1 {
  faultsim_delete_and_reopen
  execsql {
    CREATE TABLE t1(a PRIMARY KEY, b);
    INSERT INTO t1 VALUES(1862, 'Botha');
    INSERT INTO t1 VALUES(1870, 'Smuts');
    INSERT INTO t1 VALUES(1866, 'Hertzog');
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-17a -prep {
  faultsim_restore_and_reopen
} -body {
  execsql {
    PRAGMA journal_mode = wal;
    PRAGMA journal_mode = delete;
  }
} -test {
  faultsim_test_result {0 {wal delete}}
  faultsim_integrity_check
}
do_faultsim_test pagerfault-17b -prep {
  faultsim_restore_and_reopen
  execsql { PRAGMA synchronous = OFF }
} -body {
  execsql {
    PRAGMA journal_mode = wal;
    INSERT INTO t1 VALUES(22, 'Clarke');
    PRAGMA journal_mode = delete;
  }
} -test {
  faultsim_test_result {0 {wal delete}}
  faultsim_integrity_check
}
do_faultsim_test pagerfault-17c -prep {
  faultsim_restore_and_reopen
  execsql { 
    PRAGMA locking_mode = exclusive;
    PRAGMA journal_mode = wal;
  }
} -body {
  execsql { PRAGMA journal_mode = delete }
} -test {
  faultsim_test_result {0 delete}
  faultsim_integrity_check
}
do_faultsim_test pagerfault-17d -prep {
  catch { db2 close }
  faultsim_restore_and_reopen
  sqlite3 db2 test.db
  execsql { PRAGMA journal_mode = delete }
  execsql { PRAGMA journal_mode = wal }
  execsql { INSERT INTO t1 VALUES(99, 'Bradman') } db2
} -body {
  execsql { PRAGMA journal_mode = delete }
} -test {
  faultsim_test_result {1 {database is locked}}
  faultsim_integrity_check
}
do_faultsim_test pagerfault-17e -prep {
  catch { db2 close }
  faultsim_restore_and_reopen
  sqlite3 db2 test.db
  execsql { PRAGMA journal_mode = delete }
  execsql { PRAGMA journal_mode = wal }
  set ::chan [launch_testfixture]
  testfixture $::chan {
    sqlite3 db test.db
    db eval { INSERT INTO t1 VALUES(101, 'Latham') }
  }
  catch { testfixture $::chan sqlite_abort }
  catch { close $::chan }
} -body {
  execsql { PRAGMA journal_mode = delete }
} -test {
  faultsim_test_result {0 delete}
  faultsim_integrity_check
}

#-------------------------------------------------------------------------
# Test fault-injection when changing from journal_mode=persist to 
# journal_mode=delete (this involves deleting the journal file).
#
do_test pagerfault-18-pre1 {
  faultsim_delete_and_reopen
  execsql {
    CREATE TABLE qq(x);
    INSERT INTO qq VALUES('Herbert');
    INSERT INTO qq VALUES('Macalister');
    INSERT INTO qq VALUES('Mackenzie');
    INSERT INTO qq VALUES('Lilley');
    INSERT INTO qq VALUES('Palmer');
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-18 -prep {
  faultsim_restore_and_reopen
  execsql {
    PRAGMA journal_mode = PERSIST;
    INSERT INTO qq VALUES('Beatty');
  }
} -body {
  execsql { PRAGMA journal_mode = delete }
} -test {
  faultsim_test_result {0 delete}
  faultsim_integrity_check
}

do_faultsim_test pagerfault-19a -prep {
  sqlite3 db :memory:
  db func a_string a_string
  execsql {
    PRAGMA auto_vacuum = FULL;
    BEGIN;
      CREATE TABLE t1(a, b);
      INSERT INTO t1 VALUES(a_string(5000), a_string(6000));
    COMMIT;
  }
} -body {
  execsql { 
    CREATE TABLE t2(a, b);
    INSERT INTO t2 SELECT * FROM t1; 
    DELETE FROM t1;
  }
} -test {
  faultsim_test_result {0 {}}
}

do_test pagerfault-19-pre1 {
  faultsim_delete_and_reopen
  execsql {
    PRAGMA auto_vacuum = FULL;
    CREATE TABLE t1(x); INSERT INTO t1 VALUES(1);
    CREATE TABLE t2(x); INSERT INTO t2 VALUES(2);
    CREATE TABLE t3(x); INSERT INTO t3 VALUES(3);
    CREATE TABLE t4(x); INSERT INTO t4 VALUES(4);
    CREATE TABLE t5(x); INSERT INTO t5 VALUES(5);
    CREATE TABLE t6(x); INSERT INTO t6 VALUES(6);
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-19b -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { 
    BEGIN;
      UPDATE t4 SET x = x+1;
      UPDATE t6 SET x = x+1;
      SAVEPOINT one;
        UPDATE t3 SET x = x+1;
        SAVEPOINT two;
          DROP TABLE t2;
      ROLLBACK TO one;
    COMMIT;
    SELECT * FROM t3;
    SELECT * FROM t4;
    SELECT * FROM t6;
  }
} -test {
  faultsim_test_result {0 {3 5 7}}
}

#-------------------------------------------------------------------------
# This tests fault-injection in a special case in the auto-vacuum code.
#
do_test pagerfault-20-pre1 {
  faultsim_delete_and_reopen
  execsql {
    PRAGMA cache_size = 10;
    PRAGMA auto_vacuum = FULL;
    CREATE TABLE t0(a, b);
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-20 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { 
    BEGIN;
      CREATE TABLE t1(a, b);
      CREATE TABLE t2(a, b);
      DROP TABLE t1;
    COMMIT;
  }
} -test {
  faultsim_test_result {0 {}}
}

do_test pagerfault-21-pre1 {
  faultsim_delete_and_reopen
  execsql {
    PRAGMA cache_size = 10;
    CREATE TABLE t0(a PRIMARY KEY, b);
    INSERT INTO t0 VALUES(1, 2);
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-21 -prep {
  faultsim_restore_and_reopen
} -body {
  db eval { SELECT * FROM t0 LIMIT 1 } {
    db eval { INSERT INTO t0 SELECT a+1, b FROM t0 }
    db eval { INSERT INTO t0 SELECT a+2, b FROM t0 }
  }
} -test {
  faultsim_test_result {0 {}}
}


#-------------------------------------------------------------------------
# Test fault-injection and rollback when the nReserve header value 
# is non-zero.
#
do_test pagerfault-21-pre1 {
  faultsim_delete_and_reopen
  execsql {
    PRAGMA page_size = 1024;
    PRAGMA journal_mode = WAL;
    PRAGMA journal_mode = DELETE;
  }
  db close
  hexio_write test.db 20    10
  hexio_write test.db 105 03F0
  sqlite3 db test.db
  db func a_string a_string
  execsql {
    CREATE TABLE t0(a PRIMARY KEY, b UNIQUE);
    INSERT INTO t0 VALUES(a_string(222), a_string(333));
    INSERT INTO t0 VALUES(a_string(223), a_string(334));
    INSERT INTO t0 VALUES(a_string(224), a_string(335));
    INSERT INTO t0 VALUES(a_string(225), a_string(336));
  }
  faultsim_save_and_close
} {}

do_faultsim_test pagerfault-21 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { INSERT INTO t0 SELECT a||'x', b||'x' FROM t0 }
} -test {
  faultsim_test_result {0 {}}
  faultsim_integrity_check
}
ifcapable crashtest {
  faultsim_delete_and_reopen
  execsql {
    PRAGMA page_size = 1024;
    PRAGMA journal_mode = WAL;
    PRAGMA journal_mode = DELETE;
  }
  db close
  hexio_write test.db 20    10
  hexio_write test.db 105 03F0

  sqlite3 db test.db
  db func a_string a_string
  execsql {
    CREATE TABLE t0(a PRIMARY KEY, b UNIQUE);
    INSERT INTO t0 VALUES(a_string(222), a_string(333));
    INSERT INTO t0 VALUES(a_string(223), a_string(334));
  }
  faultsim_save_and_close

  for {set iTest 1} {$iTest<50} {incr iTest} {
    do_test pagerfault-21.crash.$iTest.1 {
      crashsql -delay 1 -file test.db -seed $iTest {
        BEGIN;
          CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
          INSERT INTO t1 SELECT a, b FROM t0;
        COMMIT;
      }
    } {1 {child process exited abnormally}}
    do_test pagerfault-22.$iTest.2 {
      sqlite3 db test.db
      execsql { PRAGMA integrity_check }
    } {ok}
    db close
  }
}


#-------------------------------------------------------------------------
# When a 3.7.0 client opens a write-transaction on a database file that
# has been appended to or truncated by a pre-370 client, it updates
# the db-size in the file header immediately. This test case provokes
# errors during that operation.
#
do_test pagerfault-22-pre1 {
  faultsim_delete_and_reopen
  db func a_string a_string
  execsql {
    PRAGMA page_size = 1024;
    PRAGMA auto_vacuum = 0;
    CREATE TABLE t1(a);
    CREATE INDEX i1 ON t1(a);
    INSERT INTO t1 VALUES(a_string(3000));
    CREATE TABLE t2(a);
    INSERT INTO t2 VALUES(1);
  }
  db close
  sql36231 { INSERT INTO t1 VALUES(a_string(3000)) }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-22 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { INSERT INTO t2 VALUES(2) }
  execsql { SELECT * FROM t2 }
} -test {
  faultsim_test_result {0 {1 2}}
  faultsim_integrity_check
}

#-------------------------------------------------------------------------
# Provoke an OOM error during a commit of multi-file transaction. One of
# the databases written during the transaction is an in-memory database.
# This test causes rollback of the in-memory database after CommitPhaseOne()
# has successfully returned. i.e. the series of calls for the aborted commit 
# is:
#
#   PagerCommitPhaseOne(<in-memory-db>)   ->   SQLITE_OK
#   PagerCommitPhaseOne(<file-db>)        ->   SQLITE_IOERR
#   PagerRollback(<in-memory-db>)
#   PagerRollback(<file-db>)
#
do_faultsim_test pagerfault-23 -prep {
  sqlite3 db :memory:
  foreach f [glob -nocomplain test.db*] { forcedelete $f }
  db eval { 
    ATTACH 'test.db2' AS aux;
    CREATE TABLE t1(a, b);
    CREATE TABLE aux.t2(a, b);
  }
} -body {
  execsql { 
    BEGIN;
      INSERT INTO t1 VALUES(1,2);
      INSERT INTO t2 VALUES(3,4); 
    COMMIT;
  }
} -test {
  faultsim_test_result {0 {}}
  faultsim_integrity_check
}

do_faultsim_test pagerfault-24 -prep {
  faultsim_delete_and_reopen
  db eval { PRAGMA temp_store = file }
  execsql { CREATE TABLE x(a, b) }
} -body {
  execsql { CREATE TEMP TABLE t1(a, b) }
} -test {
  faultsim_test_result {0 {}} \
    {1 {unable to open a temporary database file for storing temporary tables}}
  set ic [db eval { PRAGMA temp.integrity_check }]
  if {$ic != "ok"} { error "Integrity check: $ic" }
}

proc lockrows {n} {
  if {$n==0} { return "" }
  db eval { SELECT * FROM t1 WHERE oid = $n } { 
    return [lockrows [expr {$n-1}]]
  }
}


do_test pagerfault-25-pre1 {
  faultsim_delete_and_reopen
  db func a_string a_string
  execsql {
    PRAGMA page_size = 1024;
    PRAGMA auto_vacuum = 0;
    CREATE TABLE t1(a);
    INSERT INTO t1 VALUES(a_string(500));
    INSERT INTO t1 SELECT a_string(500) FROM t1;
    INSERT INTO t1 SELECT a_string(500) FROM t1;
    INSERT INTO t1 SELECT a_string(500) FROM t1;
    INSERT INTO t1 SELECT a_string(500) FROM t1;
    INSERT INTO t1 SELECT a_string(500) FROM t1;
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-25 -prep {
  faultsim_restore_and_reopen
  db func a_string a_string
  set ::channel [db incrblob -readonly t1 a 1]
  execsql { 
    PRAGMA cache_size = 10;
    BEGIN;
      INSERT INTO t1 VALUES(a_string(3000));
      INSERT INTO t1 VALUES(a_string(3000));
  }
} -body {
  lockrows 30
} -test {
  catch { lockrows 30 }
  catch { db eval COMMIT }
  close $::channel
  faultsim_test_result {0 {}} 
}

do_faultsim_test pagerfault-26 -prep {
  faultsim_delete_and_reopen
  execsql {
    PRAGMA page_size = 1024;
    PRAGMA journal_mode = truncate;
    PRAGMA auto_vacuum = full;
    PRAGMA locking_mode=exclusive;
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES(1, 2);
    PRAGMA page_size = 4096;
  }
} -body {
  execsql {
    VACUUM;
  }
} -test {
  faultsim_test_result {0 {}}

  set contents [db eval {SELECT * FROM t1}]
  if {$contents != "1 2"} { error "Bad database contents ($contents)" }

  set sz [file size test.db]
  if {$testrc!=0 && $sz!=1024*3 && $sz!=4096*3} { 
    error "Expected file size to be 3072 or 12288 bytes - actual size $sz bytes"
  }
  if {$testrc==0 && $sz!=4096*3} { 
    error "Expected file size to be 12288 bytes - actual size $sz bytes"
  }
} 

do_test pagerfault-27-pre {
  faultsim_delete_and_reopen
  db func a_string a_string
  execsql {
    PRAGMA page_size = 1024;
    CREATE TABLE t1(a, b);
    CREATE TABLE t2(a UNIQUE, b UNIQUE);
    INSERT INTO t2 VALUES( a_string(800), a_string(800) );
    INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
    INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
    INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
    INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
    INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
    INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
    INSERT INTO t1 VALUES (a_string(20000), a_string(20000));
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-27 -faults ioerr-persistent -prep {
  faultsim_restore_and_reopen
  db func a_string a_string
  execsql { 
    PRAGMA cache_size = 10;
    BEGIN EXCLUSIVE;
  }
  set ::channel [db incrblob t1 a 1]
} -body {
  puts $::channel [string repeat abc 6000]
  flush $::channel
} -test {
  catchsql { UPDATE t2 SET a = a_string(800), b = a_string(800) }
  catch { close $::channel }
  catchsql { ROLLBACK }
  faultsim_integrity_check
}


#-------------------------------------------------------------------------
#
do_test pagerfault-28-pre {
  faultsim_delete_and_reopen
  db func a_string a_string
  execsql {
    PRAGMA page_size = 512;

    PRAGMA journal_mode = wal;
    PRAGMA wal_autocheckpoint = 0;
    PRAGMA cache_size = 100000;

    BEGIN;
      CREATE TABLE t2(a UNIQUE, b UNIQUE);
      INSERT INTO t2 VALUES( a_string(800), a_string(800) );
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
    COMMIT;
    CREATE TABLE t1(a PRIMARY KEY, b);
  }
  expr {[file size test.db-shm] >= 96*1024}
} {1}
faultsim_save_and_close

do_faultsim_test pagerfault-28a -faults oom* -prep {
  faultsim_restore_and_reopen
  execsql { PRAGMA mmap_size=0 }

  sqlite3 db2 test.db
  db2 eval { SELECT count(*) FROM t2 }

  db func a_string a_string
  execsql { 
    BEGIN;
      INSERT INTO t1 VALUES(a_string(2000), a_string(2000));
      INSERT INTO t1 VALUES(a_string(2000), a_string(2000));
  }
  set ::STMT [sqlite3_prepare db "SELECT * FROM t1 ORDER BY a" -1 DUMMY]
  sqlite3_step $::STMT
} -body {
  execsql { ROLLBACK }
} -test {
  db2 close
  sqlite3_finalize $::STMT
  catchsql { ROLLBACK }
  faultsim_integrity_check
}

faultsim_restore_and_reopen
sqlite3 db2 test.db
db2 eval {SELECT count(*) FROM t2}
db close

do_faultsim_test pagerfault-28b -faults oom* -prep {
  sqlite3 db test.db
} -body {
  execsql { SELECT count(*) FROM t2 }
} -test {
  faultsim_test_result {0 2048}
  db close
}

db2 close

#-------------------------------------------------------------------------
# Try this:
#
#    1) Put the pager in ERROR state (error during rollback)
#
#    2) Next time the connection is used inject errors into all xWrite() and
#       xUnlock() calls. This causes the hot-journal rollback to fail and
#       the pager to declare its locking state UNKNOWN.
#
#    3) Same again.
#
#    4a) Stop injecting errors. Allow the rollback to succeed. Check that
#        the database is Ok. Or, 
#
#    4b) Close and reopen the db. Check that the db is Ok.
#
proc custom_injectinstall {} {
  testvfs custom -default true
  custom filter {xWrite xUnlock}
}
proc custom_injectuninstall {} {
  catch {db  close}
  catch {db2 close}
  custom delete
}
proc custom_injectstart {iFail} {
  custom ioerr $iFail 1
}
proc custom_injectstop {} {
  custom ioerr
}
set ::FAULTSIM(custom)          [list      \
  -injectinstall   custom_injectinstall    \
  -injectstart     custom_injectstart      \
  -injectstop      custom_injectstop       \
  -injecterrlist   {{1 {disk I/O error}}}  \
  -injectuninstall custom_injectuninstall  \
]

do_test pagerfault-29-pre {
  faultsim_delete_and_reopen
  db func a_string a_string
  execsql {
    PRAGMA page_size = 1024;
    PRAGMA cache_size = 5;

    BEGIN;
      CREATE TABLE t2(a UNIQUE, b UNIQUE);
      INSERT INTO t2 VALUES( a_string(800), a_string(800) );
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
      INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
    COMMIT;
  }
  expr {[file size test.db] >= 50*1024}
} {1}
faultsim_save_and_close
foreach {tn tt} {
  29 { catchsql ROLLBACK }
  30 { db close ; sqlite3 db test.db }
} {
  do_faultsim_test pagerfault-$tn -faults custom -prep {
    faultsim_restore_and_reopen
      db func a_string a_string
      execsql {
        PRAGMA cache_size = 5;
        BEGIN;
        UPDATE t2 SET a = a_string(799);
      }
  } -body {
    catchsql ROLLBACK
    catchsql ROLLBACK
    catchsql ROLLBACK
  } -test {
    eval $::tt
    if {"ok" != [db one {PRAGMA integrity_check}]} {
      error "integrity check failed"
    }
  }
}

do_test pagerfault-31-pre {
  sqlite3_shutdown
  sqlite3_config_uri 1
} {SQLITE_OK}
do_faultsim_test pagerfault-31 -faults oom* -body {
  sqlite3 db {file:one?mode=memory&cache=shared}
  db eval {
    CREATE TABLE t1(x);
    INSERT INTO t1 VALUES(1);
    SELECT * FROM t1;
  }
} -test {
  faultsim_test_result {0 1} {1 {}}
  catch { db close }
}
sqlite3_shutdown
sqlite3_config_uri 0

do_test pagerfault-32-pre {
  reset_db
  execsql {
    CREATE TABLE t1(x);
    INSERT INTO t1 VALUES('one');
  }
} {}
faultsim_save_and_close

do_faultsim_test pagerfault-32 -prep {
  faultsim_restore_and_reopen
  db eval { SELECT * FROM t1; }
} -body {
  execsql { SELECT * FROM t1; }
} -test {
  faultsim_test_result {0 one}
}
sqlite3_shutdown
sqlite3_config_uri 0

do_faultsim_test pagerfault-33a -prep {
  sqlite3 db :memory:
  execsql {
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES(1, 2);
  }
} -body {
  execsql { VACUUM }
} -test {
  faultsim_test_result {0 {}}
} 
do_faultsim_test pagerfault-33b -prep {
  sqlite3 db ""
  execsql {
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES(1, 2);
  }
} -body {
  execsql { VACUUM }
} -test {
  faultsim_test_result {0 {}}
} 

do_test pagerfault-34-pre {
  reset_db
  execsql {
    CREATE TABLE t1(x PRIMARY KEY);
  }
} {}
faultsim_save_and_close
do_faultsim_test pagerfault-34 -prep {
  faultsim_restore_and_reopen
  execsql {
    BEGIN;
      INSERT INTO t1 VALUES( randomblob(4000) );
      DELETE FROM t1;
  }
} -body {
  execsql COMMIT
} -test {
  faultsim_test_result {0 {}}
} 

do_test pagerfault-35-pre {
  faultsim_delete_and_reopen
  execsql {
    CREATE TABLE t1(x PRIMARY KEY, y);
    INSERT INTO t1 VALUES(randomblob(200), randomblob(200));
    INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
    INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
    INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  }
  faultsim_save_and_close
} {}
testvfs tv -default 1
tv sectorsize 8192;
tv devchar [list]
do_faultsim_test pagerfault-35 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { UPDATE t1 SET x=randomblob(200) }
} -test {
  faultsim_test_result {0 {}}
}
catch {db close}
tv delete

sqlite3_shutdown
sqlite3_config_uri 1
do_test pagerfault-36-pre {
  faultsim_delete_and_reopen
  execsql {
    CREATE TABLE t1(x PRIMARY KEY, y);
    INSERT INTO t1 VALUES(randomblob(200), randomblob(200));
    INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
    INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
    INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  }
  faultsim_save_and_close
} {}
do_faultsim_test pagerfault-36 -prep {
  faultsim_restore
  sqlite3 db file:test.db?cache=shared
  sqlite3 db2 file:test.db?cache=shared
  db2 eval {
    BEGIN;
    SELECT count(*) FROM sqlite_master;
  }
  db eval {
    PRAGMA cache_size = 1;
    BEGIN;
      UPDATE t1 SET x = randomblob(200);
  }
} -body {
  execsql ROLLBACK db
} -test {
  catch { db eval {UPDATE t1 SET x = randomblob(200)} }
  faultsim_test_result {0 {}}
  catch { db close }
  catch { db2 close }
}

sqlite3_shutdown
sqlite3_config_uri 0
sqlite3_initialize

finish_test