forked from sqlite/sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpager1.test
2956 lines (2777 loc) · 85.4 KB
/
pager1.test
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
# 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
source $testdir/wal_common.tcl
set testprefix pager1
if {[atomic_batch_write test.db]} {
finish_test
return
}
ifcapable !incrblob {
finish_test
return
}
# Do not use a codec for tests in this file, as the database file is
# manipulated directly using tcl scripts (using the [hexio_write] command).
#
do_not_use_codec
#
# pager1-1.*: Test inter-process locking (clients in multiple processes).
#
# pager1-2.*: Test intra-process locking (multiple clients in this process).
#
# pager1-3.*: Savepoint related tests.
#
# pager1-4.*: Hot-journal related tests.
#
# pager1-5.*: Cases related to multi-file commits.
#
# pager1-6.*: Cases related to "PRAGMA max_page_count"
#
# pager1-7.*: Cases specific to "PRAGMA journal_mode=TRUNCATE"
#
# pager1-8.*: Cases using temporary and in-memory databases.
#
# pager1-9.*: Tests related to the backup API.
#
# pager1-10.*: Test that the assumed file-system sector-size is limited to
# 64KB.
#
# pager1-12.*: Tests involving "PRAGMA page_size"
#
# pager1-13.*: Cases specific to "PRAGMA journal_mode=PERSIST"
#
# pager1-14.*: Cases specific to "PRAGMA journal_mode=OFF"
#
# pager1-15.*: Varying sqlite3_vfs.szOsFile
#
# pager1-16.*: Varying sqlite3_vfs.mxPathname
#
# pager1-17.*: Tests related to "PRAGMA omit_readlock"
# (The omit_readlock pragma has been removed and so have
# these tests.)
#
# pager1-18.*: Test that the pager layer responds correctly if the b-tree
# requests an invalid page number (due to db corruption).
#
proc recursive_select {id table {script {}}} {
set cnt 0
db eval "SELECT rowid, * FROM $table WHERE rowid = ($id-1)" {
recursive_select $rowid $table $script
incr cnt
}
if {$cnt==0} { eval $script }
}
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
do_multiclient_test tn {
# Create and populate a database table using connection [db]. Check
# that connections [db2] and [db3] can see the schema and content.
#
do_test pager1-$tn.1 {
sql1 {
CREATE TABLE t1(a PRIMARY KEY, b);
CREATE INDEX i1 ON t1(b);
INSERT INTO t1 VALUES(1, 'one'); INSERT INTO t1 VALUES(2, 'two');
}
} {}
do_test pager1-$tn.2 { sql2 { SELECT * FROM t1 } } {1 one 2 two}
do_test pager1-$tn.3 { sql3 { SELECT * FROM t1 } } {1 one 2 two}
# Open a transaction and add a row using [db]. This puts [db] in
# RESERVED state. Check that connections [db2] and [db3] can still
# read the database content as it was before the transaction was
# opened. [db] should see the inserted row.
#
do_test pager1-$tn.4 {
sql1 {
BEGIN;
INSERT INTO t1 VALUES(3, 'three');
}
} {}
do_test pager1-$tn.5 { sql2 { SELECT * FROM t1 } } {1 one 2 two}
do_test pager1-$tn.7 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three}
# [db] still has an open write transaction. Check that this prevents
# other connections (specifically [db2]) from writing to the database.
#
# Even if [db2] opens a transaction first, it may not write to the
# database. After the attempt to write the db within a transaction,
# [db2] is left with an open transaction, but not a read-lock on
# the main database. So it does not prevent [db] from committing.
#
do_test pager1-$tn.8 {
csql2 { UPDATE t1 SET a = a + 10 }
} {1 {database is locked}}
do_test pager1-$tn.9 {
csql2 {
BEGIN;
UPDATE t1 SET a = a + 10;
}
} {1 {database is locked}}
# Have [db] commit its transactions. Check the other connections can
# now see the new database content.
#
do_test pager1-$tn.10 { sql1 { COMMIT } } {}
do_test pager1-$tn.11 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three}
do_test pager1-$tn.12 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
do_test pager1-$tn.13 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
# Check that, as noted above, [db2] really did keep an open transaction
# after the attempt to write the database failed.
#
do_test pager1-$tn.14 {
csql2 { BEGIN }
} {1 {cannot start a transaction within a transaction}}
do_test pager1-$tn.15 { sql2 { ROLLBACK } } {}
# Have [db2] open a transaction and take a read-lock on the database.
# Check that this prevents [db] from writing to the database (outside
# of any transaction). After this fails, check that [db3] can read
# the db (showing that [db] did not take a PENDING lock etc.)
#
do_test pager1-$tn.15 {
sql2 { BEGIN; SELECT * FROM t1; }
} {1 one 2 two 3 three}
do_test pager1-$tn.16 {
csql1 { UPDATE t1 SET a = a + 10 }
} {1 {database is locked}}
do_test pager1-$tn.17 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
# This time, have [db] open a transaction before writing the database.
# This works - [db] gets a RESERVED lock which does not conflict with
# the SHARED lock [db2] is holding.
#
do_test pager1-$tn.18 {
sql1 {
BEGIN;
UPDATE t1 SET a = a + 10;
}
} {}
do_test pager1-$tn-19 {
sql1 { PRAGMA lock_status }
} {main reserved temp closed}
do_test pager1-$tn-20 {
sql2 { PRAGMA lock_status }
} {main shared temp closed}
# Check that all connections can still read the database. Only [db] sees
# the updated content (as the transaction has not been committed yet).
#
do_test pager1-$tn.21 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three}
do_test pager1-$tn.22 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
do_test pager1-$tn.23 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
# Because [db2] still has the SHARED lock, [db] is unable to commit the
# transaction. If it tries, an error is returned and the connection
# upgrades to a PENDING lock.
#
# Once this happens, [db] can read the database and see the new content,
# [db2] (still holding SHARED) can still read the old content, but [db3]
# (not holding any lock) is prevented by [db]'s PENDING from reading
# the database.
#
do_test pager1-$tn.24 { csql1 { COMMIT } } {1 {database is locked}}
do_test pager1-$tn-25 {
sql1 { PRAGMA lock_status }
} {main pending temp closed}
do_test pager1-$tn.26 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three}
do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
do_test pager1-$tn.28 { csql3 { SELECT * FROM t1 } } {1 {database is locked}}
# Have [db2] commit its read transaction, releasing the SHARED lock it
# is holding. Now, neither [db2] nor [db3] may read the database (as [db]
# is still holding a PENDING).
#
do_test pager1-$tn.29 { sql2 { COMMIT } } {}
do_test pager1-$tn.30 { csql2 { SELECT * FROM t1 } } {1 {database is locked}}
do_test pager1-$tn.31 { csql3 { SELECT * FROM t1 } } {1 {database is locked}}
# [db] is now able to commit the transaction. Once the transaction is
# committed, all three connections can read the new content.
#
do_test pager1-$tn.25 { sql1 { UPDATE t1 SET a = a+10 } } {}
do_test pager1-$tn.26 { sql1 { COMMIT } } {}
do_test pager1-$tn.27 { sql1 { SELECT * FROM t1 } } {21 one 22 two 23 three}
do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {21 one 22 two 23 three}
do_test pager1-$tn.28 { sql3 { SELECT * FROM t1 } } {21 one 22 two 23 three}
# Install a busy-handler for connection [db].
#
set ::nbusy [list]
proc busy {n} {
lappend ::nbusy $n
if {$n>5} { sql2 COMMIT }
return 0
}
db busy busy
do_test pager1-$tn.29 {
sql1 { BEGIN ; INSERT INTO t1 VALUES('x', 'y') }
} {}
do_test pager1-$tn.30 {
sql2 { BEGIN ; SELECT * FROM t1 }
} {21 one 22 two 23 three}
do_test pager1-$tn.31 { sql1 COMMIT } {}
do_test pager1-$tn.32 { set ::nbusy } {0 1 2 3 4 5 6}
}
#-------------------------------------------------------------------------
# Savepoint related test cases.
#
# pager1-3.1.2.*: Force a savepoint rollback to cause the database file
# to grow.
#
# pager1-3.1.3.*: Use a journal created in synchronous=off mode as part
# of a savepoint rollback.
#
do_test pager1-3.1.1 {
faultsim_delete_and_reopen
execsql {
CREATE TABLE t1(a PRIMARY KEY, b);
CREATE TABLE counter(
i CHECK (i<5),
u CHECK (u<10)
);
INSERT INTO counter VALUES(0, 0);
CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
UPDATE counter SET i = i+1;
END;
CREATE TRIGGER tr2 AFTER UPDATE ON t1 BEGIN
UPDATE counter SET u = u+1;
END;
}
execsql { SELECT * FROM counter }
} {0 0}
do_execsql_test pager1-3.1.2 {
PRAGMA cache_size = 10;
BEGIN;
INSERT INTO t1 VALUES(1, randomblob(1500));
INSERT INTO t1 VALUES(2, randomblob(1500));
INSERT INTO t1 VALUES(3, randomblob(1500));
SELECT * FROM counter;
} {3 0}
do_catchsql_test pager1-3.1.3 {
INSERT INTO t1 SELECT a+3, randomblob(1500) FROM t1
} {1 {CHECK constraint failed: i<5}}
do_execsql_test pager1-3.4 { SELECT * FROM counter } {3 0}
do_execsql_test pager1-3.5 { SELECT a FROM t1 } {1 2 3}
do_execsql_test pager1-3.6 { COMMIT } {}
foreach {tn sql tcl} {
7 { PRAGMA synchronous = NORMAL ; PRAGMA temp_store = 0 } {
testvfs tv -default 1
tv devchar safe_append
}
8 { PRAGMA synchronous = NORMAL ; PRAGMA temp_store = 2 } {
testvfs tv -default 1
tv devchar sequential
}
9 { PRAGMA synchronous = FULL } { }
10 { PRAGMA synchronous = NORMAL } { }
11 { PRAGMA synchronous = OFF } { }
12 { PRAGMA synchronous = FULL ; PRAGMA fullfsync = 1 } { }
13 { PRAGMA synchronous = FULL } {
testvfs tv -default 1
tv devchar sequential
}
14 { PRAGMA locking_mode = EXCLUSIVE } {
}
} {
do_test pager1-3.$tn.1 {
eval $tcl
faultsim_delete_and_reopen
db func a_string a_string
execsql $sql
execsql {
PRAGMA auto_vacuum = 2;
PRAGMA cache_size = 10;
CREATE TABLE z(x INTEGER PRIMARY KEY, y);
BEGIN;
INSERT INTO z VALUES(NULL, a_string(800));
INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 2
INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 4
INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 8
INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 16
INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 32
INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 64
INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 128
INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 256
COMMIT;
}
execsql { PRAGMA auto_vacuum }
} {2}
do_execsql_test pager1-3.$tn.2 {
BEGIN;
INSERT INTO z VALUES(NULL, a_string(800));
INSERT INTO z VALUES(NULL, a_string(800));
SAVEPOINT one;
UPDATE z SET y = NULL WHERE x>256;
PRAGMA incremental_vacuum;
SELECT count(*) FROM z WHERE x < 100;
ROLLBACK TO one;
COMMIT;
} {99}
do_execsql_test pager1-3.$tn.3 {
BEGIN;
SAVEPOINT one;
UPDATE z SET y = y||x;
ROLLBACK TO one;
COMMIT;
SELECT count(*) FROM z;
} {258}
do_execsql_test pager1-3.$tn.4 {
SAVEPOINT one;
UPDATE z SET y = y||x;
ROLLBACK TO one;
} {}
do_execsql_test pager1-3.$tn.5 {
SELECT count(*) FROM z;
RELEASE one;
PRAGMA integrity_check;
} {258 ok}
do_execsql_test pager1-3.$tn.6 {
SAVEPOINT one;
RELEASE one;
} {}
db close
catch { tv delete }
}
#-------------------------------------------------------------------------
# Hot journal rollback related test cases.
#
# pager1.4.1.*: Test that the pager module deletes very small invalid
# journal files.
#
# pager1.4.2.*: Test that if the master journal pointer at the end of a
# hot-journal file appears to be corrupt (checksum does not
# compute) the associated journal is rolled back (and no
# xAccess() call to check for the presence of any master
# journal file is made).
#
# pager1.4.3.*: Test that the contents of a hot-journal are ignored if the
# page-size or sector-size in the journal header appear to
# be invalid (too large, too small or not a power of 2).
#
# pager1.4.4.*: Test hot-journal rollback of journal file with a master
# journal pointer generated in various "PRAGMA synchronous"
# modes.
#
# pager1.4.5.*: Test that hot-journal rollback stops if it encounters a
# journal-record for which the checksum fails.
#
# pager1.4.6.*: Test that when rolling back a hot-journal that contains a
# master journal pointer, the master journal file is deleted
# after all the hot-journals that refer to it are deleted.
#
# pager1.4.7.*: Test that if a hot-journal file exists but a client can
# open it for reading only, the database cannot be accessed and
# SQLITE_CANTOPEN is returned.
#
do_test pager1.4.1.1 {
faultsim_delete_and_reopen
execsql {
CREATE TABLE x(y, z);
INSERT INTO x VALUES(1, 2);
}
set fd [open test.db-journal w]
puts -nonewline $fd "helloworld"
close $fd
file exists test.db-journal
} {1}
do_test pager1.4.1.2 { execsql { SELECT * FROM x } } {1 2}
do_test pager1.4.1.3 { file exists test.db-journal } {0}
# Set up a [testvfs] to snapshot the file-system just before SQLite
# deletes the master-journal to commit a multi-file transaction.
#
# In subsequent test cases, invoking [faultsim_restore_and_reopen] sets
# up the file system to contain two databases, two hot-journal files and
# a master-journal.
#
do_test pager1.4.2.1 {
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
} {}
if {$::tcl_platform(platform)!="windows"} {
do_test pager1.4.2.2 {
faultsim_restore_and_reopen
execsql {
SELECT count(*) FROM t1;
PRAGMA integrity_check;
}
} {4 ok}
do_test pager1.4.2.3 {
faultsim_restore_and_reopen
foreach f [glob test.db-mj*] { forcedelete $f }
execsql {
SELECT count(*) FROM t1;
PRAGMA integrity_check;
}
} {64 ok}
do_test pager1.4.2.4 {
faultsim_restore_and_reopen
hexio_write test.db-journal [expr [file size test.db-journal]-30] 123456
execsql {
SELECT count(*) FROM t1;
PRAGMA integrity_check;
}
} {4 ok}
do_test pager1.4.2.5 {
faultsim_restore_and_reopen
hexio_write test.db-journal [expr [file size test.db-journal]-30] 123456
foreach f [glob test.db-mj*] { forcedelete $f }
execsql {
SELECT count(*) FROM t1;
PRAGMA integrity_check;
}
} {4 ok}
}
do_test pager1.4.3.1 {
testvfs tstvfs -default 1
tstvfs filter xSync
tstvfs script xSyncCallback
proc xSyncCallback {method file args} {
set file [file tail $file]
if { 0==[string match *journal $file] } { faultsim_save }
}
faultsim_delete_and_reopen
execsql {
PRAGMA journal_mode = DELETE;
CREATE TABLE t1(a, b);
INSERT INTO t1 VALUES(1, 2);
INSERT INTO t1 VALUES(3, 4);
}
db close
tstvfs delete
} {}
foreach {tn ofst value result} {
2 20 31 {1 2 3 4}
3 20 32 {1 2 3 4}
4 20 33 {1 2 3 4}
5 20 65536 {1 2 3 4}
6 20 131072 {1 2 3 4}
7 24 511 {1 2 3 4}
8 24 513 {1 2 3 4}
9 24 131072 {1 2 3 4}
10 32 65536 {1 2}
} {
do_test pager1.4.3.$tn {
faultsim_restore_and_reopen
hexio_write test.db-journal $ofst [format %.8x $value]
execsql { SELECT * FROM t1 }
} $result
}
db close
# Set up a VFS that snapshots the file-system just before a master journal
# file is deleted to commit a multi-file transaction. Specifically, the
# file-system is saved just before the xDelete() call to remove the
# master journal file from the file-system.
#
set pwd [get_pwd]
testvfs tv -default 1
tv script copy_on_mj_delete
set ::mj_filename_length 0
set ::mj_delete_cnt 0
proc copy_on_mj_delete {method filename args} {
if {[string match *mj* [file tail $filename]]} {
#
# NOTE: Is the file name relative? If so, add the length of the current
# directory.
#
if {[is_relative_file $filename]} {
set ::mj_filename_length \
[expr {[string length $filename] + [string length $::pwd]}]
} else {
set ::mj_filename_length [string length $filename]
}
faultsim_save
incr ::mj_delete_cnt
}
return SQLITE_OK
}
foreach {tn1 tcl} {
1 { set prefix "test.db" }
2 {
# This test depends on the underlying VFS being able to open paths
# 512 bytes in length. The idea is to create a hot-journal file that
# contains a master-journal pointer so large that it could contain
# a valid page record (if the file page-size is 512 bytes). So as to
# make sure SQLite doesn't get confused by this.
#
set nPadding [expr 511 - $::mj_filename_length]
if {$tcl_platform(platform)=="windows"} {
# TBD need to figure out how to do this correctly for Windows!!!
set nPadding [expr 255 - $::mj_filename_length]
}
# We cannot just create a really long database file name to open, as
# Linux limits a single component of a path to 255 bytes by default
# (and presumably other systems have limits too). So create a directory
# hierarchy to work in.
#
set dirname "d123456789012345678901234567890/"
set nDir [expr $nPadding / 32]
if { $nDir } {
set p [string repeat $dirname $nDir]
file mkdir $p
cd $p
}
set padding [string repeat x [expr $nPadding %32]]
set prefix "test.db${padding}"
}
} {
eval $tcl
foreach {tn2 sql usesMJ} {
o {
PRAGMA main.synchronous=OFF;
PRAGMA aux.synchronous=OFF;
PRAGMA journal_mode = DELETE;
} 0
o512 {
PRAGMA main.synchronous=OFF;
PRAGMA aux.synchronous=OFF;
PRAGMA main.page_size = 512;
PRAGMA aux.page_size = 512;
PRAGMA journal_mode = DELETE;
} 0
n {
PRAGMA main.synchronous=NORMAL;
PRAGMA aux.synchronous=NORMAL;
PRAGMA journal_mode = DELETE;
} 1
f {
PRAGMA main.synchronous=FULL;
PRAGMA aux.synchronous=FULL;
PRAGMA journal_mode = DELETE;
} 1
w1 {
PRAGMA main.synchronous=NORMAL;
PRAGMA aux.synchronous=NORMAL;
PRAGMA journal_mode = WAL;
} 0
w2 {
PRAGMA main.synchronous=NORMAL;
PRAGMA aux.synchronous=NORMAL;
PRAGMA main.journal_mode=DELETE;
PRAGMA aux.journal_mode=WAL;
} 0
o1a {
PRAGMA main.synchronous=FULL;
PRAGMA aux.synchronous=OFF;
PRAGMA journal_mode=DELETE;
} 0
o1b {
PRAGMA main.synchronous=OFF;
PRAGMA aux.synchronous=NORMAL;
PRAGMA journal_mode=DELETE;
} 0
m1 {
PRAGMA main.synchronous=NORMAL;
PRAGMA aux.synchronous=NORMAL;
PRAGMA main.journal_mode=DELETE;
PRAGMA aux.journal_mode = MEMORY;
} 0
t1 {
PRAGMA main.synchronous=NORMAL;
PRAGMA aux.synchronous=NORMAL;
PRAGMA main.journal_mode=DELETE;
PRAGMA aux.journal_mode = TRUNCATE;
} 1
p1 {
PRAGMA main.synchronous=NORMAL;
PRAGMA aux.synchronous=NORMAL;
PRAGMA main.journal_mode=DELETE;
PRAGMA aux.journal_mode = PERSIST;
} 1
} {
set tn "${tn1}.${tn2}"
# Set up a connection to have two databases, test.db (main) and
# test.db2 (aux). Then run a multi-file transaction on them. The
# VFS will snapshot the file-system just before the master-journal
# file is deleted to commit the transaction.
#
tv filter xDelete
do_test pager1-4.4.$tn.1 {
set ::mj_delete_cnt 0
faultsim_delete_and_reopen $prefix
execsql "
ATTACH '${prefix}2' AS aux;
$sql
CREATE TABLE a(x);
CREATE TABLE aux.b(x);
INSERT INTO a VALUES('double-you');
INSERT INTO a VALUES('why');
INSERT INTO a VALUES('zed');
INSERT INTO b VALUES('won');
INSERT INTO b VALUES('too');
INSERT INTO b VALUES('free');
"
execsql {
BEGIN;
INSERT INTO a SELECT * FROM b WHERE rowid<=3;
INSERT INTO b SELECT * FROM a WHERE rowid<=3;
COMMIT;
}
} {}
tv filter {}
# Verify that a master journal was deleted only for those cases where
# master journals really ought to be used
#
do_test pager1-4.4.$tn.1b {
set ::mj_delete_cnt
} $usesMJ
# Check that the transaction was committed successfully.
#
do_execsql_test pager1-4.4.$tn.2 {
SELECT * FROM a
} {double-you why zed won too free}
do_execsql_test pager1-4.4.$tn.3 {
SELECT * FROM b
} {won too free double-you why zed}
if {$usesMJ} {
# Restore the file-system and reopen the databases. Check that it now
# appears that the transaction was not committed (because the file-system
# was restored to the state where it had not been).
#
do_test pager1-4.4.$tn.4 {
faultsim_restore_and_reopen $prefix
execsql "ATTACH '${prefix}2' AS aux"
} {}
do_execsql_test pager1-4.4.$tn.5 {SELECT * FROM a} {double-you why zed}
do_execsql_test pager1-4.4.$tn.6 {SELECT * FROM b} {won too free}
}
# Restore the file-system again. This time, before reopening the databases,
# delete the master-journal file from the file-system. It now appears that
# the transaction was committed (no master-journal file == no rollback).
#
do_test pager1-4.4.$tn.7 {
if {$::mj_delete_cnt>0} {
faultsim_restore_and_reopen $prefix
foreach f [glob ${prefix}-mj*] { forcedelete $f }
} else {
db close
sqlite3 db $prefix
}
execsql "ATTACH '${prefix}2' AS aux"
glob -nocomplain ${prefix}-mj*
} {}
do_execsql_test pager1-4.4.$tn.8 {
SELECT * FROM a
} {double-you why zed won too free}
do_execsql_test pager1-4.4.$tn.9 {
SELECT * FROM b
} {won too free double-you why zed}
}
cd $pwd
}
db close
tv delete
forcedelete $dirname
# Set up a VFS to make a copy of the file-system just before deleting a
# journal file to commit a transaction. The transaction modifies exactly
# two database pages (and page 1 - the change counter).
#
testvfs tv -default 1
tv sectorsize 512
tv script copy_on_journal_delete
tv filter xDelete
proc copy_on_journal_delete {method filename args} {
if {[string match *journal $filename]} faultsim_save
return SQLITE_OK
}
faultsim_delete_and_reopen
do_execsql_test pager1.4.5.1 {
PRAGMA journal_mode = DELETE;
PRAGMA page_size = 1024;
CREATE TABLE t1(a, b);
CREATE TABLE t2(a, b);
INSERT INTO t1 VALUES('I', 'II');
INSERT INTO t2 VALUES('III', 'IV');
BEGIN;
INSERT INTO t1 VALUES(1, 2);
INSERT INTO t2 VALUES(3, 4);
COMMIT;
} {delete}
tv filter {}
# Check the transaction was committed:
#
do_execsql_test pager1.4.5.2 {
SELECT * FROM t1;
SELECT * FROM t2;
} {I II 1 2 III IV 3 4}
# Now try four tests:
#
# pager1-4.5.3: Restore the file-system. Check that the whole transaction
# is rolled back.
#
# pager1-4.5.4: Restore the file-system. Corrupt the first record in the
# journal. Check the transaction is not rolled back.
#
# pager1-4.5.5: Restore the file-system. Corrupt the second record in the
# journal. Check that the first record in the transaction is
# played back, but not the second.
#
# pager1-4.5.6: Restore the file-system. Try to open the database with a
# readonly connection. This should fail, as a read-only
# connection cannot roll back the database file.
#
faultsim_restore_and_reopen
do_execsql_test pager1.4.5.3 {
SELECT * FROM t1;
SELECT * FROM t2;
} {I II III IV}
faultsim_restore_and_reopen
hexio_write test.db-journal [expr 512+4+1024 - 202] 0123456789ABCDEF
do_execsql_test pager1.4.5.4 {
SELECT * FROM t1;
SELECT * FROM t2;
} {I II 1 2 III IV 3 4}
faultsim_restore_and_reopen
hexio_write test.db-journal [expr 512+4+1024+4+4+1024 - 202] 0123456789ABCDEF
do_execsql_test pager1.4.5.5 {
SELECT * FROM t1;
SELECT * FROM t2;
} {I II III IV 3 4}
faultsim_restore_and_reopen
db close
sqlite3 db test.db -readonly 1
do_catchsql_test pager1.4.5.6 {
SELECT * FROM t1;
SELECT * FROM t2;
} {1 {attempt to write a readonly database}}
db close
# Snapshot the file-system just before multi-file commit. Save the name
# of the master journal file in $::mj_filename.
#
tv script copy_on_mj_delete
tv filter xDelete
proc copy_on_mj_delete {method filename args} {
if {[string match *mj* [file tail $filename]]} {
set ::mj_filename $filename
faultsim_save
}
return SQLITE_OK
}
do_test pager1.4.6.1 {
faultsim_delete_and_reopen
execsql {
PRAGMA journal_mode = DELETE;
ATTACH 'test.db2' AS two;
CREATE TABLE t1(a, b);
CREATE TABLE two.t2(a, b);
INSERT INTO t1 VALUES(1, 't1.1');
INSERT INTO t2 VALUES(1, 't2.1');
BEGIN;
UPDATE t1 SET b = 't1.2';
UPDATE t2 SET b = 't2.2';
COMMIT;
}
tv filter {}
db close
} {}
faultsim_restore_and_reopen
do_execsql_test pager1.4.6.2 { SELECT * FROM t1 } {1 t1.1}
do_test pager1.4.6.3 { file exists $::mj_filename } {1}
do_execsql_test pager1.4.6.4 {
ATTACH 'test.db2' AS two;
SELECT * FROM t2;
} {1 t2.1}
do_test pager1.4.6.5 { file exists $::mj_filename } {0}
faultsim_restore_and_reopen
db close
do_test pager1.4.6.8 {
set ::mj_filename1 $::mj_filename
tv filter xDelete
sqlite3 db test.db2
execsql {
PRAGMA journal_mode = DELETE;
ATTACH 'test.db3' AS three;
CREATE TABLE three.t3(a, b);
INSERT INTO t3 VALUES(1, 't3.1');
BEGIN;
UPDATE t2 SET b = 't2.3';
UPDATE t3 SET b = 't3.3';
COMMIT;
}
expr {$::mj_filename1 != $::mj_filename}
} {1}
faultsim_restore_and_reopen
tv filter {}
# The file-system now contains:
#
# * three databases
# * three hot-journal files
# * two master-journal files.
#
# The hot-journals associated with test.db2 and test.db3 point to
# master journal $::mj_filename. The hot-journal file associated with
# test.db points to master journal $::mj_filename1. So reading from
# test.db should delete $::mj_filename1.
#
do_test pager1.4.6.9 {
lsort [glob test.db*]
} [lsort [list \
test.db test.db2 test.db3 \
test.db-journal test.db2-journal test.db3-journal \
[file tail $::mj_filename] [file tail $::mj_filename1]
]]
# The master-journal $::mj_filename1 contains pointers to test.db and
# test.db2. However the hot-journal associated with test.db2 points to
# a different master-journal. Therefore, reading from test.db only should
# be enough to cause SQLite to delete $::mj_filename1.
#
do_test pager1.4.6.10 { file exists $::mj_filename } {1}
do_test pager1.4.6.11 { file exists $::mj_filename1 } {1}
do_execsql_test pager1.4.6.12 { SELECT * FROM t1 } {1 t1.1}
do_test pager1.4.6.13 { file exists $::mj_filename } {1}
do_test pager1.4.6.14 { file exists $::mj_filename1 } {0}
do_execsql_test pager1.4.6.12 {
ATTACH 'test.db2' AS two;
SELECT * FROM t2;
} {1 t2.1}
do_test pager1.4.6.13 { file exists $::mj_filename } {1}
do_execsql_test pager1.4.6.14 {
ATTACH 'test.db3' AS three;
SELECT * FROM t3;
} {1 t3.1}
do_test pager1.4.6.15 { file exists $::mj_filename } {0}
db close
tv delete
testvfs tv -default 1
tv sectorsize 512
tv script copy_on_journal_delete
tv filter xDelete
proc copy_on_journal_delete {method filename args} {
if {[string match *journal $filename]} faultsim_save
return SQLITE_OK
}
faultsim_delete_and_reopen
do_execsql_test pager1.4.7.1 {
PRAGMA journal_mode = DELETE;
CREATE TABLE t1(x PRIMARY KEY, y);
CREATE INDEX i1 ON t1(y);
INSERT INTO t1 VALUES('I', 'one');
INSERT INTO t1 VALUES('II', 'four');
INSERT INTO t1 VALUES('III', 'nine');
BEGIN;
INSERT INTO t1 VALUES('IV', 'sixteen');
INSERT INTO t1 VALUES('V' , 'twentyfive');
COMMIT;
} {delete}
tv filter {}
db close
tv delete
catch {
test_syscall install fchmod
test_syscall fault 1 1
}
do_test pager1.4.7.2 {
faultsim_restore_and_reopen
catch {file attributes test.db-journal -permissions r--------}
catch {file attributes test.db-journal -readonly 1}
catchsql { SELECT * FROM t1 }
} {1 {unable to open database file}}
catch {
test_syscall reset
test_syscall fault 0 0
}
do_test pager1.4.7.3 {
db close
catch {file attributes test.db-journal -permissions rw-rw-rw-}
catch {file attributes test.db-journal -readonly 0}
delete_file test.db-journal
file exists test.db-journal
} {0}
do_test pager1.4.8.1 {
catch {file attributes test.db -permissions r--------}
catch {file attributes test.db -readonly 1}
sqlite3 db test.db
db eval { SELECT * FROM t1 }
sqlite3_db_readonly db main
} {1}
do_test pager1.4.8.2 {
sqlite3_db_readonly db xyz
} {-1}
do_test pager1.4.8.3 {
db close
catch {file attributes test.db -readonly 0}
catch {file attributes test.db -permissions rw-rw-rw-} msg
sqlite3 db test.db
db eval { SELECT * FROM t1 }
sqlite3_db_readonly db main
} {0}
#-------------------------------------------------------------------------
# The following tests deal with multi-file commits.
#
# pager1-5.1.*: The case where a multi-file cannot be committed because
# another connection is holding a SHARED lock on one of the
# files. After the SHARED lock is removed, the COMMIT succeeds.
#
# pager1-5.2.*: Multi-file commits with journal_mode=memory.
#