# 2003 January 12
#
# 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 testing the sqlite_set_authorizer() API.
#
# $Id: auth.test,v 1.2 2003/01/13 23:27:34 drh Exp $
#

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

if {[info command sqlite_set_authorizer]!=""} {

do_test auth-1.1 {
  db close
  set ::DB [sqlite db test.db]
  proc auth {code arg1 arg2} {
    if {$code=="SQLITE_INSERT" 
          && [string compare -nocase $arg1 sqlite_master]==0} {
      return SQLITE_DENY
    }
    return SQLITE_OK
  }
  sqlite_set_authorizer $::DB ::auth
  catchsql {CREATE TABLE t1(a,b,c)}
} {1 {not authorized}}
do_test auth-1.2 {
  execsql {SELECT name FROM sqlite_master}
} {}
do_test auth-1.3 {
  proc auth {code arg1 arg2} {
    if {$code=="SQLITE_CREATE_TABLE"} {
      return SQLITE_DENY
    }
    return SQLITE_OK
  }
  catchsql {CREATE TABLE t1(a,b,c)}
} {1 {not authorized}}
do_test auth-1.4 {
  execsql {SELECT name FROM sqlite_master}
} {}

do_test auth-1.5 {
  proc auth {code arg1 arg2} {
    if {$code=="SQLITE_INSERT" 
          && [string compare -nocase $arg1 sqlite_temp_master]==0} {
      return SQLITE_DENY
    }
    return SQLITE_OK
  }
  catchsql {CREATE TEMP TABLE t1(a,b,c)}
} {1 {not authorized}}
do_test auth-1.6 {
  execsql {SELECT name FROM sqlite_temp_master}
} {}
do_test auth-1.7 {
  proc auth {code arg1 arg2} {
    if {$code=="SQLITE_CREATE_TEMP_TABLE"} {
      return SQLITE_DENY
    }
    return SQLITE_OK
  }
  catchsql {CREATE TEMP TABLE t1(a,b,c)}
} {1 {not authorized}}
do_test auth-1.8 {
  execsql {SELECT name FROM sqlite_temp_master}
} {}

do_test auth-1.9 {
  proc auth {code arg1 arg2} {
    if {$code=="SQLITE_INSERT" 
          && [string compare -nocase $arg1 sqlite_master]==0} {
      return SQLITE_IGNORE
    }
    return SQLITE_OK
  }
  catchsql {CREATE TABLE t1(a,b,c)}
} {0 {}}
do_test auth-1.10 {
  execsql {SELECT name FROM sqlite_master}
} {}
do_test auth-1.11 {
  proc auth {code arg1 arg2} {
    if {$code=="SQLITE_CREATE_TABLE"} {
      return SQLITE_IGNORE
    }
    return SQLITE_OK
  }
  catchsql {CREATE TABLE t1(a,b,c)}
} {0 {}}
do_test auth-1.12 {
  execsql {SELECT name FROM sqlite_master}
} {}
do_test auth-1.13 {
  proc auth {code arg1 arg2} {
    if {$code=="SQLITE_INSERT" 
          && [string compare -nocase $arg1 sqlite_temp_master]==0} {
      return SQLITE_IGNORE
    }
    return SQLITE_OK
  }
  catchsql {CREATE TEMP TABLE t1(a,b,c)}
} {0 {}}
do_test auth-1.14 {
  execsql {SELECT name FROM sqlite_temp_master}
} {}
do_test auth-1.15 {
  proc auth {code arg1 arg2} {
    if {$code=="SQLITE_CREATE_TEMP_TABLE"} {
      return SQLITE_IGNORE
    }
    return SQLITE_OK
  }
  catchsql {CREATE TEMP TABLE t1(a,b,c)}
} {0 {}}
do_test auth-1.16 {
  execsql {SELECT name FROM sqlite_temp_master}
} {}

do_test auth-1.17 {
  proc auth {code arg1 arg2} {
    if {$code=="SQLITE_CREATE_TABLE"} {
      return SQLITE_DEBY
    }
    return SQLITE_OK
  }
  catchsql {CREATE TEMP TABLE t1(a,b,c)}
} {0 {}}
do_test auth-1.18 {
  execsql {SELECT name FROM sqlite_temp_master}
} {t1}
do_test auth-1.19 {
  proc auth {code arg1 arg2} {
    if {$code=="SQLITE_CREATE_TEMP_TABLE"} {
      return SQLITE_DEBY
    }
    return SQLITE_OK
  }
  catchsql {CREATE TABLE t2(a,b,c)}
} {0 {}}
do_test auth-1.20 {
  execsql {SELECT name FROM sqlite_master}
} {t2}




} ;# End of the "if( db command exists )"

finish_test
