xref: /sqlite-3.40.0/test/auth3.test (revision 37f3ac8f)
152bd7912Sdanielk1977# 2008 October 27
252bd7912Sdanielk1977#
352bd7912Sdanielk1977# The author disclaims copyright to this source code.  In place of
452bd7912Sdanielk1977# a legal notice, here is a blessing:
552bd7912Sdanielk1977#
652bd7912Sdanielk1977#    May you do good and not evil.
752bd7912Sdanielk1977#    May you find forgiveness for yourself and forgive others.
852bd7912Sdanielk1977#    May you share freely, never taking more than you give.
952bd7912Sdanielk1977#
1052bd7912Sdanielk1977#***********************************************************************
1152bd7912Sdanielk1977#
1252bd7912Sdanielk1977# Test that the truncate optimization is disabled if the SQLITE_DELETE
1352bd7912Sdanielk1977# authorization callback returns SQLITE_IGNORE.
1452bd7912Sdanielk1977#
15a8914faaSdrh# Test that authorizer is disabled during schema parsing.
1652bd7912Sdanielk1977
1752bd7912Sdanielk1977set testdir [file dirname $argv0]
1852bd7912Sdanielk1977source $testdir/tester.tcl
1952bd7912Sdanielk1977
2052bd7912Sdanielk1977# disable this test if the SQLITE_OMIT_AUTHORIZATION macro is
2152bd7912Sdanielk1977# defined during compilation.
2252bd7912Sdanielk1977if {[catch {db auth {}} msg]} {
2352bd7912Sdanielk1977  finish_test
2452bd7912Sdanielk1977  return
2552bd7912Sdanielk1977}
2652bd7912Sdanielk1977
2752bd7912Sdanielk1977# Disable the statement cache for these tests.
2852bd7912Sdanielk1977#
2952bd7912Sdanielk1977db cache size 0
3052bd7912Sdanielk1977
3152bd7912Sdanielk1977db authorizer ::auth
3232c6a48bSdrhproc auth {code arg1 arg2 arg3 arg4 args} {
3352bd7912Sdanielk1977  if {$code=="SQLITE_DELETE"} {
3452bd7912Sdanielk1977    return $::authcode
3552bd7912Sdanielk1977  }
3652bd7912Sdanielk1977  return SQLITE_OK
3752bd7912Sdanielk1977}
3852bd7912Sdanielk1977
3952bd7912Sdanielk1977#--------------------------------------------------------------------------
4052bd7912Sdanielk1977# The following tests - auth3-1.* - test that return values of SQLITE_DENY,
4152bd7912Sdanielk1977# SQLITE_IGNORE, SQLITE_OK and <invalid> are correctly handled when returned
4252bd7912Sdanielk1977# by an SQLITE_DELETE authorization callback triggered by a
4352bd7912Sdanielk1977# "DELETE FROM <table-name>" statement.
4452bd7912Sdanielk1977#
4552bd7912Sdanielk1977do_test auth3-1.1 {
4652bd7912Sdanielk1977  execsql {
4752bd7912Sdanielk1977    CREATE TABLE t1(a,b,c);
4852bd7912Sdanielk1977    INSERT INTO t1 VALUES(1, 2, 3);
4952bd7912Sdanielk1977    INSERT INTO t1 VALUES(4, 5, 6);
5052bd7912Sdanielk1977  }
5152bd7912Sdanielk1977} {}
5252bd7912Sdanielk1977do_test auth3.1.2 {
5352bd7912Sdanielk1977  set ::authcode SQLITE_DENY
5452bd7912Sdanielk1977  catchsql { DELETE FROM t1 }
5552bd7912Sdanielk1977} {1 {not authorized}}
569418921cSdrh# EVIDENCE-OF: R-64962-58611 If the authorizer callback returns any
579418921cSdrh# value other than SQLITE_IGNORE, SQLITE_OK, or SQLITE_DENY then the
589418921cSdrh# sqlite3_prepare_v2() or equivalent call that triggered the authorizer
599418921cSdrh# will fail with an error message.
6052bd7912Sdanielk1977do_test auth3.1.3 {
6152bd7912Sdanielk1977  set ::authcode SQLITE_INVALID
6252bd7912Sdanielk1977  catchsql { DELETE FROM t1 }
63ce9b0157Sdrh} {1 {authorizer malfunction}}
6452bd7912Sdanielk1977do_test auth3.1.4 {
6552bd7912Sdanielk1977  execsql { SELECT * FROM t1 }
6652bd7912Sdanielk1977} {1 2 3 4 5 6}
6752bd7912Sdanielk1977do_test auth3-1.5 {
6852bd7912Sdanielk1977  set ::authcode SQLITE_IGNORE
6952bd7912Sdanielk1977  execsql {
7052bd7912Sdanielk1977    DELETE FROM t1;
7152bd7912Sdanielk1977    SELECT * FROM t1;
7252bd7912Sdanielk1977  }
7352bd7912Sdanielk1977} {}
7452bd7912Sdanielk1977do_test auth3-1.6 {
7552bd7912Sdanielk1977  set ::authcode SQLITE_OK
7652bd7912Sdanielk1977  execsql {
7752bd7912Sdanielk1977    INSERT INTO t1 VALUES(1, 2, 3);
7852bd7912Sdanielk1977    INSERT INTO t1 VALUES(4, 5, 6);
7952bd7912Sdanielk1977    DELETE FROM t1;
8052bd7912Sdanielk1977    SELECT * FROM t1;
8152bd7912Sdanielk1977  }
8252bd7912Sdanielk1977} {}
8352bd7912Sdanielk1977
8452bd7912Sdanielk1977#--------------------------------------------------------------------------
8552bd7912Sdanielk1977# These tests - auth3-2.* - test that returning SQLITE_IGNORE really does
8652bd7912Sdanielk1977# disable the truncate optimization.
8752bd7912Sdanielk1977#
8852bd7912Sdanielk1977do_test auth3-2.1 {
8952bd7912Sdanielk1977  set ::authcode SQLITE_OK
9052bd7912Sdanielk1977  execsql {
9152bd7912Sdanielk1977    INSERT INTO t1 VALUES(1, 2, 3);
9252bd7912Sdanielk1977    INSERT INTO t1 VALUES(4, 5, 6);
9352bd7912Sdanielk1977  }
9452bd7912Sdanielk1977  set sqlite_search_count 0
9552bd7912Sdanielk1977  execsql {
9652bd7912Sdanielk1977    DELETE FROM t1;
9752bd7912Sdanielk1977  }
9852bd7912Sdanielk1977  set sqlite_search_count
9952bd7912Sdanielk1977} {0}
10052bd7912Sdanielk1977
10152bd7912Sdanielk1977do_test auth3-2.2 {
10252bd7912Sdanielk1977  set ::authcode SQLITE_IGNORE
10352bd7912Sdanielk1977  execsql {
10452bd7912Sdanielk1977    INSERT INTO t1 VALUES(1, 2, 3);
10552bd7912Sdanielk1977    INSERT INTO t1 VALUES(4, 5, 6);
10652bd7912Sdanielk1977  }
10752bd7912Sdanielk1977  set sqlite_search_count 0
10852bd7912Sdanielk1977  execsql {
10952bd7912Sdanielk1977    DELETE FROM t1;
11052bd7912Sdanielk1977  }
11152bd7912Sdanielk1977  set sqlite_search_count
11252bd7912Sdanielk1977} {1}
11352bd7912Sdanielk1977
114a8914faaSdrh# 2016-07-28.  A problem report from a private client complaining about
115a8914faaSdrh# an authorizer failure during an ALTER TABLE.  The solution (I think) is
116a8914faaSdrh# to disable the authorizer during schema parsing.
117a8914faaSdrh#
118*37f3ac8fSdanifcapable altertable {
119a8914faaSdrh  proc auth {code args} {
120a8914faaSdrh    if {$code=="SQLITE_READ" && [regexp {DoNotRead} $args]} {
121a8914faaSdrh      return SQLITE_DENY
122a8914faaSdrh    }
123a8914faaSdrh    return SQLITE_OK
124a8914faaSdrh  }
125a8914faaSdrh  do_execsql_test auth3-3.0 {
126a8914faaSdrh    CREATE TEMPORARY TABLE TempTable (
127a8914faaSdrh        key TEXT NOT NULL ON CONFLICT FAIL UNIQUE ON CONFLICT REPLACE,
128a8914faaSdrh        value TEXT NOT NULL ON CONFLICT FAIL);
129a8914faaSdrh    ALTER TABLE TempTable RENAME TO DoNotRead;
130e0a04a36Sdrh    SELECT name FROM temp.sqlite_master;
131a8914faaSdrh  } {DoNotRead sqlite_autoindex_DoNotRead_1}
132*37f3ac8fSdan}
133a8914faaSdrh
13452bd7912Sdanielk1977finish_test
135