# 2008 June 28
#
# 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.  The
# focus of this script is database proxy locks.
#
# $Id$

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

# This file is only run if using the unix backend compiled with the
# SQLITE_ENABLE_LOCKING_STYLE macro.
db close
if {[catch {sqlite3 db test.db -vfs unix-none} msg]} {
  finish_test
  return
}
db close
file delete -force test.db.lock

#####################################################################
ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
  set ::using_proxy 0
  foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
    set ::using_proxy $value
  }

  # enable the proxy locking for these tests
  set env(SQLITE_FORCE_PROXY_LOCKING) "1"

  # test conch file creation
  #

  catch { file delete lock_proxy_test.db }
  catch { file delete .lock_proxy_test.db-conch }
  # test that proxy locking mode creates conch files
  do_test lock_proxy1.0 {
    sqlite3 db2 lock_proxy_test.db
    catchsql {
      create table x(y);
    } db2
    file exists .lock_proxy_test.db-conch
  } {1}
  catch { db2 close }


  # test proxy locking readonly file system handling
  #

  if {[file exists /usr/bin/hdiutil]} {

    puts "Creating readonly file system for proxy locking tests..."
    if {[file exists /Volumes/readonly]} {
      exec hdiutil detach /Volumes/readonly
    }
    if {[file exists readonly.dmg]} {
      file delete readonly.dmg
    }
    exec hdiutil create -megabytes 1 -fs HFS+ readonly.dmg -volname readonly
    exec hdiutil attach readonly.dmg

    # create test1.db and a .test1.db-conch for host4
    set sqlite_hostid_num 4
    sqlite3 db2 /Volumes/readonly/test1.db 
    execsql {
      create table x(y);
    } db2
    db2 close

    # create test2.db and a .test2.db-conch for host5
    set sqlite_hostid_num 5
    sqlite3 db2 /Volumes/readonly/test2.db 
    execsql {
      create table x(y);
    } db2
    db2 close

    # create test3.db without a conch file
    set env(SQLITE_FORCE_PROXY_LOCKING) "0"
    sqlite3 db2 /Volumes/readonly/test3.db 
    execsql {
      create table x(y);
    } db2
    db2 close
    exec hdiutil detach /Volumes/readonly
    exec hdiutil attach -readonly readonly.dmg

    # test that an unwritable, host-mismatched conch file prevents 
    # read only proxy-locking mode database access
    set env(SQLITE_FORCE_PROXY_LOCKING) "1"
    do_test lock_proxy2.0 {
      sqlite3 db2 /Volumes/readonly/test1.db
      catchsql {
        select * from sqlite_master;
      } db2
    } {1 {database is locked}}
    catch { db2 close }
    
    # test that an unwritable, host-matching conch file allows
    # read only proxy-locking mode database access
    do_test lock_proxy2.1 {
      sqlite3 db2 /Volumes/readonly/test2.db
      catchsql {
        select * from sqlite_master;
      } db2
    } {0 {table x x 2 {CREATE TABLE x(y)}}}
    catch { db2 close }

    # test that an unwritable, nonexistant conch file allows
    # read only proxy-locking mode database access
    do_test lock_proxy2.2 {
      sqlite3 db2 /Volumes/readonly/test3.db
      catchsql {
        select * from sqlite_master;
      } db2
    } {0 {table x x 2 {CREATE TABLE x(y)}}}
    catch { db2 close }

    exec hdiutil detach /Volumes/readonly
    file delete readonly.dmg
  }
  set env(SQLITE_FORCE_PROXY_LOCKING) "0"
  set sqlite_hostid_num 0
}

#####################################################################

file delete -force test.db

ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
  set env(SQLITE_FORCE_PROXY_LOCKING) $::using_proxy
}

finish_test
