# 2008 October 29
#
# 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.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# $Id: tkt3457.test,v 1.3 2009/06/26 07:12:07 danielk1977 Exp $

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

if {$tcl_platform(platform) != "unix"} {
  finish_test
  return
}
if {[atomic_batch_write test.db]} {
  finish_test
  return
}

#-----------------------------------------------------------------------
# To roll back a hot-journal file, the application needs read and write 
# permission on the journal file in question. The following tests test
# the outcome of trying to rollback a hot-journal file when this is not
# the case.
# 
#   tkt3457-1.2: Application has neither read, nor write permission on
#                the hot-journal file. Result: SQLITE_CANTOPEN.
#                
#   tkt3457-1.3: Application has write but not read permission on
#                the hot-journal file. Result: SQLITE_CANTOPEN.
#
#   tkt3457-1.4: Application has read but not write permission on
#                the hot-journal file. Result: SQLITE_CANTOPEN.
#
#   tkt3457-1.5: Application has read/write permission on the hot-journal 
#                file. Result: SQLITE_OK.
# 
do_test tkt3457-1.1 {
  execsql {
    CREATE TABLE t1(a, b, c);
    INSERT INTO t1 VALUES(1, 2, 3);
    BEGIN;
    INSERT INTO t1 VALUES(4, 5, 6);
  }

  forcecopy test.db bak.db
  forcecopy test.db-journal bak.db-journal

  # Fix the first journal-header in the journal-file. Because the
  # journal file has not yet been synced, the 8-byte magic string at the
  # start of the first journal-header has not been written by SQLite.
  # So write it now.
  set fd [open bak.db-journal a+]
  fconfigure $fd -translation binary
  seek $fd 0
  puts -nonewline $fd "\xd9\xd5\x05\xf9\x20\xa1\x63\xd7"
  close $fd

  execsql COMMIT
} {}

# Disable fchmod to make sure SQLite itself does not try to change the
# permission bits on us
#
catch {
  test_syscall install fchmod
  test_syscall fault 1 1
}

do_test tkt3457-1.2 {
  forcecopy bak.db-journal test.db-journal
  file attributes test.db-journal -permissions ---------
  catchsql { SELECT * FROM t1 }
} {1 {unable to open database file}}
do_test tkt3457-1.3 {
  forcecopy bak.db-journal test.db-journal
  file attributes test.db-journal -permissions -w--w--w-
  catchsql { SELECT * FROM t1 }
} {1 {unable to open database file}}
do_test tkt3457-1.4 {
  forcecopy bak.db-journal test.db-journal
  file attributes test.db-journal -permissions r--r--r--
  catchsql { SELECT * FROM t1 }
} {1 {unable to open database file}}

do_test tkt3457-1.5 {
  forcecopy bak.db-journal test.db-journal
  file attributes test.db-journal -permissions rw-rw-rw-
  catchsql { SELECT * FROM t1 }
} {0 {1 2 3 4 5 6}}

# Reenable fchmod
catch {
  test_syscall uninstall
  test_syscall fault 0 0
}

finish_test
