os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/auth2.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2006 Aug 24
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this script is testing the sqlite3_set_authorizer() API
sl@0
    13
# and related functionality.
sl@0
    14
#
sl@0
    15
# $Id: auth2.test,v 1.3 2008/07/02 13:13:53 danielk1977 Exp $
sl@0
    16
#
sl@0
    17
sl@0
    18
set testdir [file dirname $argv0]
sl@0
    19
source $testdir/tester.tcl
sl@0
    20
sl@0
    21
# disable this test if the SQLITE_OMIT_AUTHORIZATION macro is
sl@0
    22
# defined during compilation.
sl@0
    23
if {[catch {db auth {}} msg]} {
sl@0
    24
  finish_test
sl@0
    25
  return
sl@0
    26
}
sl@0
    27
sl@0
    28
do_test auth2-1.1 {
sl@0
    29
  execsql {
sl@0
    30
    CREATE TABLE t1(a,b,c);
sl@0
    31
    INSERT INTO t1 VALUES(1,2,3);
sl@0
    32
  }
sl@0
    33
  set ::flist {}
sl@0
    34
  proc auth {code arg1 arg2 arg3 arg4} {
sl@0
    35
    if {$code=="SQLITE_FUNCTION"} {
sl@0
    36
      lappend ::flist $arg2
sl@0
    37
      if {$arg2=="max"} {
sl@0
    38
        return SQLITE_DENY
sl@0
    39
      } elseif {$arg2=="min"} {
sl@0
    40
        return SQLITE_IGNORE
sl@0
    41
      } else {
sl@0
    42
        return SQLITE_OK
sl@0
    43
      }
sl@0
    44
    }
sl@0
    45
    return SQLITE_OK
sl@0
    46
  }
sl@0
    47
  db authorizer ::auth
sl@0
    48
  catchsql {SELECT max(a,b,c) FROM t1}
sl@0
    49
} {1 {not authorized to use function: max}}
sl@0
    50
do_test auth2-1.2 {
sl@0
    51
  set ::flist
sl@0
    52
} max
sl@0
    53
do_test auth2-1.3 {
sl@0
    54
  set ::flist {}
sl@0
    55
  catchsql {SELECT min(a,b,c) FROM t1}
sl@0
    56
} {0 {{}}}
sl@0
    57
do_test auth2-1.4 {
sl@0
    58
  set ::flist
sl@0
    59
} min
sl@0
    60
do_test auth2-1.5 {
sl@0
    61
  set ::flist {}
sl@0
    62
  catchsql {SELECT coalesce(min(a,b,c),999) FROM t1}
sl@0
    63
} {0 999}
sl@0
    64
do_test auth2-1.6 {
sl@0
    65
  set ::flist
sl@0
    66
} {coalesce min}
sl@0
    67
do_test auth2-1.7 {
sl@0
    68
  set ::flist {}
sl@0
    69
  catchsql {SELECT coalesce(a,b,c) FROM t1}
sl@0
    70
} {0 1}
sl@0
    71
do_test auth2-1.8 {
sl@0
    72
  set ::flist
sl@0
    73
} coalesce
sl@0
    74
sl@0
    75
# Make sure the authorizer is not called when parsing the schema
sl@0
    76
# and when computing the result set of a view.
sl@0
    77
#
sl@0
    78
db close
sl@0
    79
sqlite3 db test.db
sl@0
    80
sqlite3 db2 test.db
sl@0
    81
proc auth {args} {
sl@0
    82
  global authargs
sl@0
    83
  append authargs $args\n
sl@0
    84
  return SQLITE_OK
sl@0
    85
}
sl@0
    86
db auth auth
sl@0
    87
do_test auth2-2.1 {
sl@0
    88
  set ::authargs {}
sl@0
    89
  db eval {
sl@0
    90
    CREATE TABLE t2(x,y,z);
sl@0
    91
  }
sl@0
    92
  set ::authargs
sl@0
    93
} {SQLITE_INSERT sqlite_master {} main {}
sl@0
    94
SQLITE_CREATE_TABLE t2 {} main {}
sl@0
    95
SQLITE_UPDATE sqlite_master type main {}
sl@0
    96
SQLITE_UPDATE sqlite_master name main {}
sl@0
    97
SQLITE_UPDATE sqlite_master tbl_name main {}
sl@0
    98
SQLITE_UPDATE sqlite_master rootpage main {}
sl@0
    99
SQLITE_UPDATE sqlite_master sql main {}
sl@0
   100
SQLITE_READ sqlite_master ROWID main {}
sl@0
   101
SQLITE_READ sqlite_master name main {}
sl@0
   102
SQLITE_READ sqlite_master rootpage main {}
sl@0
   103
SQLITE_READ sqlite_master sql main {}
sl@0
   104
SQLITE_READ sqlite_master tbl_name main {}
sl@0
   105
}
sl@0
   106
do_test auth2-2.2 {
sl@0
   107
  set ::authargs {}
sl@0
   108
  db eval {
sl@0
   109
    CREATE VIEW v2 AS SELECT x+y AS a, y+z AS b from t2;
sl@0
   110
  }
sl@0
   111
  set ::authargs
sl@0
   112
} {SQLITE_INSERT sqlite_master {} main {}
sl@0
   113
SQLITE_CREATE_VIEW v2 {} main {}
sl@0
   114
SQLITE_UPDATE sqlite_master type main {}
sl@0
   115
SQLITE_UPDATE sqlite_master name main {}
sl@0
   116
SQLITE_UPDATE sqlite_master tbl_name main {}
sl@0
   117
SQLITE_UPDATE sqlite_master rootpage main {}
sl@0
   118
SQLITE_UPDATE sqlite_master sql main {}
sl@0
   119
SQLITE_READ sqlite_master ROWID main {}
sl@0
   120
SQLITE_READ sqlite_master name main {}
sl@0
   121
SQLITE_READ sqlite_master rootpage main {}
sl@0
   122
SQLITE_READ sqlite_master sql main {}
sl@0
   123
SQLITE_READ sqlite_master tbl_name main {}
sl@0
   124
}
sl@0
   125
do_test auth2-2.3 {
sl@0
   126
  set ::authargs {}
sl@0
   127
  db eval {
sl@0
   128
    SELECT a, b FROM v2;
sl@0
   129
  }
sl@0
   130
  set ::authargs
sl@0
   131
} {SQLITE_SELECT {} {} {} {}
sl@0
   132
SQLITE_READ v2 a main {}
sl@0
   133
SQLITE_READ v2 b main {}
sl@0
   134
SQLITE_READ t2 x main v2
sl@0
   135
SQLITE_READ t2 y main v2
sl@0
   136
SQLITE_READ t2 y main v2
sl@0
   137
SQLITE_READ t2 z main v2
sl@0
   138
SQLITE_SELECT {} {} {} v2
sl@0
   139
}
sl@0
   140
do_test auth2-2.4 {
sl@0
   141
  db2 eval {
sl@0
   142
    CREATE TABLE t3(p,q,r);
sl@0
   143
  }
sl@0
   144
  set ::authargs {}
sl@0
   145
  db eval {
sl@0
   146
    SELECT b, a FROM v2;
sl@0
   147
  }
sl@0
   148
  set ::authargs
sl@0
   149
} {SQLITE_SELECT {} {} {} {}
sl@0
   150
SQLITE_READ v2 b main {}
sl@0
   151
SQLITE_READ v2 a main {}
sl@0
   152
SQLITE_READ t2 x main v2
sl@0
   153
SQLITE_READ t2 y main v2
sl@0
   154
SQLITE_READ t2 y main v2
sl@0
   155
SQLITE_READ t2 z main v2
sl@0
   156
SQLITE_SELECT {} {} {} v2
sl@0
   157
SQLITE_SELECT {} {} {} {}
sl@0
   158
SQLITE_READ v2 b main {}
sl@0
   159
SQLITE_READ v2 a main {}
sl@0
   160
SQLITE_READ t2 x main v2
sl@0
   161
SQLITE_READ t2 y main v2
sl@0
   162
SQLITE_READ t2 y main v2
sl@0
   163
SQLITE_READ t2 z main v2
sl@0
   164
SQLITE_SELECT {} {} {} v2
sl@0
   165
}
sl@0
   166
db2 close
sl@0
   167
sl@0
   168
finish_test