SQLite4
Artifact Content
Not logged in

Artifact 550d80f91a1ba9dcdee0f9b729cc850795df9cef:


# 2012 November 02
#
# 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
set testprefix bt1

# Set tcl variable $str to the hex representation of the 24 byte string 
# located at the start of every bt database file.
set str ""
binary scan "SQLite4 bt database 0001" c24 I
foreach i $I {
  append str [format %02X $i ]
}

#-------------------------------------------------------------------------
# Test that, assuming there is no *-wal file, the bt module will refuse
# to open an existing database that does not contain a valid header.
#
do_test 1.0 {
  execsql {
    CREATE TABLE t1(x);
    INSERT INTO t1 VALUES('abcd');
  }

  db close
  list [file exists test.db] [file exists test.db-wal]
} {1 0}

do_test 1.1 { 
  hexio_read test.db 0 24 
} $str

do_test 1.2 { 
  sqlite4 db test.db
  execsql { SELECT * FROM t1 }
} {abcd}

do_test 1.3 { 
  db close
  hexio_write test.db  8 55555555

  sqlite4 db test.db
  catchsql { SELECT * FROM t1 }
} {1 {file is encrypted or is not a database}}

#-------------------------------------------------------------------------
# Test that, if there is a *-wal file that contains a valid copy of page
# 1 (with the db header), it is possible to open the database even if
# the header at byte offset 0 is damaged.
#
reset_db
do_test 2.0 {
  execsql {
    CREATE TABLE t1(x);
    INSERT INTO t1 VALUES(randomblob(20000));
  }
  db close
  list [file exists test.db] [file exists test.db-wal]
} {1 0}

do_test 2.1 {
  sqlite4 db test.db
  # This moves pages to the free-list. Meaning page 1 must be 
  # updated to set the pointer to the first free-list page.
  execsql { UPDATE t1 SET x = 'abcd' }
  db_save
  db close
  db_restore
  list [file exists test.db] [file exists test.db-wal]
} {1 1}

do_test 2.2 { 
  hexio_write test.db  8 55555555
  sqlite4 db test.db
  execsql { SELECT * FROM t1 }
} {abcd}

#-------------------------------------------------------------------------
#
do_test 3.1 {
  reset_db
  execsql {
    PRAGMA page_size = 4096;
    CREATE TABLE t1(x);
  }
  db close
  file size test.db
} {8192}

do_test 3.2 {
  sqlite4 db test.db
  execsql { PRAGMA page_size }
} {1024}

do_test 3.3 {
  execsql { SELECT * FROM t1 }
  execsql { PRAGMA page_size }
} {4096}

do_test 3.4 {
  execsql { PRAGMA page_size = 1024 }
} {4096}

do_test 3.5 {
  db close
  sqlite4 db test.db
  execsql { PRAGMA page_size = 2048 }
} {2048}

do_test 3.6 {
  execsql { SELECT * FROM t1 }
  execsql { PRAGMA page_size }
} {4096}

finish_test