xref: /sqlite-3.40.0/test/mjournal.test (revision 6ab91a7a)
1629ec14aSdan# 2017 September 15
2629ec14aSdan#
3629ec14aSdan# The author disclaims copyright to this source code.  In place of
4629ec14aSdan# a legal notice, here is a blessing:
5629ec14aSdan#
6629ec14aSdan#    May you do good and not evil.
7629ec14aSdan#    May you find forgiveness for yourself and forgive others.
8629ec14aSdan#    May you share freely, never taking more than you give.
9629ec14aSdan#
10629ec14aSdan#***********************************************************************
11629ec14aSdan# This file implements regression tests for SQLite library.
12629ec14aSdan#
13629ec14aSdan
14629ec14aSdanset testdir [file dirname $argv0]
15629ec14aSdansource $testdir/tester.tcl
16629ec14aSdanset testprefix mjournal
17629ec14aSdan
18*92b67025Sdanif {[permutation]=="inmemory_journal"} {
19*92b67025Sdan  finish_test
20*92b67025Sdan  return
21*92b67025Sdan}
22*92b67025Sdan
23629ec14aSdan# Test that nothing bad happens if a journal file contains a pointer to
24629ec14aSdan# a master journal file that does not have a "-" in the name. At one point
25629ec14aSdan# this was causing a segfault on unix.
26629ec14aSdan#
27629ec14aSdando_execsql_test 1.0 {
28629ec14aSdan  CREATE TABLE t1(a, b);
29629ec14aSdan}
30629ec14aSdan
31629ec14aSdando_test 1.1 {
32629ec14aSdan  forcedelete test.db2journal test.db-journal
33629ec14aSdan
34629ec14aSdan  close [open test.db-journal w]
35629ec14aSdan
36629ec14aSdan  hexio_write test.db-journal 0 746573742e6462326a6f75726e616c00
37629ec14aSdan  hexio_write test.db-journal 16 00000010
38629ec14aSdan  hexio_write test.db-journal 20 000005e1
39629ec14aSdan  hexio_write test.db-journal 24 d9d505f920a163d7
40629ec14aSdan
41629ec14aSdan  close [open test.db2journal w]
42629ec14aSdan  hexio_write test.db2journal 0 abcd
43629ec14aSdan} {2}
44629ec14aSdan
45629ec14aSdando_execsql_test 1.2 {
46629ec14aSdan  SELECT * FROM t1;
47629ec14aSdan}
48629ec14aSdan
49629ec14aSdando_test 1.3 {
50629ec14aSdan  forcedelete test0db2journal test.db-journal
51629ec14aSdan  close [open test.db-journal w]
52629ec14aSdan  hexio_write test.db-journal 0 74657374306462326a6f75726e616c00
53629ec14aSdan  hexio_write test.db-journal 16 00000010
54629ec14aSdan  hexio_write test.db-journal 20 000005e3
55629ec14aSdan  hexio_write test.db-journal 24 d9d505f920a163d7
56629ec14aSdan
57629ec14aSdan  close [open test0db2journal w]
58629ec14aSdan  hexio_write test0db2journal 0 abcd
59629ec14aSdan} {2}
60629ec14aSdan
61629ec14aSdando_execsql_test 1.4 {
62629ec14aSdan  SELECT * FROM t1;
63629ec14aSdan}
64629ec14aSdan
653ed0f1c9Sdan# And now test that nothing bad happens if a master journal contains a
663ed0f1c9Sdan# pointer to a journal file that does not have a "-" in the name.
673ed0f1c9Sdan#
683ed0f1c9Sdando_test 1.5 {
693ed0f1c9Sdan  forcedelete test.db2-master test.db-journal test1
703ed0f1c9Sdan  close [open test.db-journal w]
713ed0f1c9Sdan  hexio_write test.db-journal 0 746573742e6462322d6d617374657200
723ed0f1c9Sdan  hexio_write test.db-journal 16 00000010
733ed0f1c9Sdan  hexio_write test.db-journal 20 0000059f
743ed0f1c9Sdan  hexio_write test.db-journal 24 d9d505f920a163d7
753ed0f1c9Sdan
763ed0f1c9Sdan  close [open test.db2-master w]
773ed0f1c9Sdan  hexio_write test.db2-master 0 746573743100
783ed0f1c9Sdan
793ed0f1c9Sdan  close [open test1 w]
803ed0f1c9Sdan  hexio_write test1 0 abcd
813ed0f1c9Sdan} {2}
823ed0f1c9Sdan
833ed0f1c9Sdando_execsql_test 1.6 {
843ed0f1c9Sdan  SELECT * FROM t1;
853ed0f1c9Sdan}
86629ec14aSdan
876cbc5074Sdan#-------------------------------------------------------------------------
886cbc5074Sdan# Check that master journals are not created if the transaction involves
896cbc5074Sdan# multiple temp files.
906cbc5074Sdan#
916cbc5074Sdandb close
926cbc5074Sdantestvfs tvfs
936cbc5074Sdantvfs filter xOpen
946cbc5074Sdantvfs script open_cb
956cbc5074Sdanset ::open ""
966cbc5074Sdanproc open_cb {method file arglist} {
976cbc5074Sdan  lappend ::open $file
986cbc5074Sdan}
996cbc5074Sdan
1006cbc5074Sdanproc contains_mj {} {
1016cbc5074Sdan  foreach f $::open {
1026cbc5074Sdan    set t [file tail $f]
1036cbc5074Sdan    if {[string match *mj* $t]} { return 1 }
1046cbc5074Sdan  }
1056cbc5074Sdan  return 0
1066cbc5074Sdan}
1076cbc5074Sdan
1086cbc5074Sdan# Like [do_execsql_test], except that a boolean indicating whether or
1096cbc5074Sdan# not a master journal file was opened ([file tail] contains "mj") or
1106cbc5074Sdan# not. Example:
1116cbc5074Sdan#
1126cbc5074Sdan#   do_hasmj_test 1.0 { SELECT 'a', 'b' } {0 a b}
1136cbc5074Sdan#
1146cbc5074Sdanproc do_hasmj_test {tn sql expected} {
1156cbc5074Sdan  set ::open [list]
1166cbc5074Sdan  uplevel [list do_test $tn [subst -nocommands {
1176cbc5074Sdan    set res [execsql "$sql"]
1186cbc5074Sdan    concat [contains_mj] [set res]
1196cbc5074Sdan  }] [list {*}$expected]]
1206cbc5074Sdan}
1216cbc5074Sdan
1226cbc5074Sdanforcedelete test.db
1236cbc5074Sdanforcedelete test.db2
1246cbc5074Sdanforcedelete test.db3
1256cbc5074Sdansqlite3 db test.db -vfs tvfs
1266cbc5074Sdan
1276cbc5074Sdando_execsql_test 2.0 {
1286cbc5074Sdan  ATTACH 'test.db2' AS dbfile;
1296cbc5074Sdan  ATTACH ''         AS dbtemp;
1306cbc5074Sdan  ATTACH ':memory:'  AS dbmem;
1316cbc5074Sdan
1326cbc5074Sdan  CREATE TABLE t1(x);
1336cbc5074Sdan  CREATE TABLE dbfile.t2(x);
1346cbc5074Sdan  CREATE TABLE dbtemp.t3(x);
1356cbc5074Sdan  CREATE TABLE dbmem.t4(x);
1366cbc5074Sdan}
1376cbc5074Sdan
1386cbc5074Sdan# Two real files.
1396cbc5074Sdando_hasmj_test 2.1 {
1406cbc5074Sdan  BEGIN;
1416cbc5074Sdan    INSERT INTO t1 VALUES(1);
1426cbc5074Sdan    INSERT INTO t2 VALUES(1);
1436cbc5074Sdan  COMMIT;
1446cbc5074Sdan} {1}
1456cbc5074Sdan
1466cbc5074Sdan# One real, one temp file.
1476cbc5074Sdando_hasmj_test 2.2 {
1486cbc5074Sdan  BEGIN;
1496cbc5074Sdan    INSERT INTO t1 VALUES(1);
1506cbc5074Sdan    INSERT INTO t3 VALUES(1);
1516cbc5074Sdan  COMMIT;
1526cbc5074Sdan} {0}
1536cbc5074Sdan
1546cbc5074Sdan# One file, one :memory: db.
1556cbc5074Sdando_hasmj_test 2.3 {
1566cbc5074Sdan  BEGIN;
1576cbc5074Sdan    INSERT INTO t1 VALUES(1);
1586cbc5074Sdan    INSERT INTO t4 VALUES(1);
1596cbc5074Sdan  COMMIT;
1606cbc5074Sdan} {0}
161629ec14aSdan
162629ec14aSdanfinish_test
163