AryaWu/sqlite
0
1# 2006 Aug 242#3# The author disclaims copyright to this source code. In place of4# a legal notice, here is a blessing:5#6# May you do good and not evil.7# May you find forgiveness for yourself and forgive others.8# May you share freely, never taking more than you give.9#10#***********************************************************************11# This file implements regression tests for SQLite library. The12# focus of this script is testing the sqlite3_set_authorizer() API13# and related functionality.14#15# $Id: auth2.test,v 1.3 2008/07/02 13:13:53 danielk1977 Exp $16#17 18set testdir [file dirname $argv0]19source $testdir/tester.tcl20 21# disable this test if the SQLITE_OMIT_AUTHORIZATION macro is22# defined during compilation.23if {[catch {db auth {}} msg]} {24 finish_test25 return26}27 28do_test auth2-1.1 {29 execsql {30 CREATE TABLE t1(a,b,c);31 INSERT INTO t1 VALUES(1,2,3);32 }33 set ::flist {}34 proc auth {code arg1 arg2 arg3 arg4 args} {35 if {$code=="SQLITE_FUNCTION"} {36 lappend ::flist $arg237 if {$arg2=="max"} {38 return SQLITE_DENY39 } elseif {$arg2=="min"} {40 return SQLITE_IGNORE41 } else {42 return SQLITE_OK43 }44 }45 return SQLITE_OK46 }47 db authorizer ::auth48 catchsql {SELECT max(a,b,c) FROM t1}49} {1 {not authorized to use function: max}}50do_test auth2-1.2 {51 set ::flist52} max53do_test auth2-1.3 {54 set ::flist {}55 catchsql {SELECT min(a,b,c) FROM t1}56} {0 {{}}}57do_test auth2-1.4 {58 set ::flist59} min60do_test auth2-1.5 {61 set ::flist {}62 catchsql {SELECT coalesce(min(a,b,c),999) FROM t1}63} {0 999}64do_test auth2-1.6 {65 set ::flist66} {coalesce min}67do_test auth2-1.7 {68 set ::flist {}69 catchsql {SELECT coalesce(a,b,c) FROM t1}70} {0 1}71do_test auth2-1.8 {72 set ::flist73} coalesce74 75# Make sure the authorizer is not called when parsing the schema76# and when computing the result set of a view.77#78db close79sqlite3 db test.db80sqlite3 db2 test.db81proc auth {args} {82 global authargs83 append authargs [lrange $args 0 4]\n84 return SQLITE_OK85}86db auth auth87do_test auth2-2.1 {88 set ::authargs {}89 db eval {90 CREATE TABLE t2(x,y,z);91 }92 set ::authargs93} {SQLITE_INSERT sqlite_master {} main {}94SQLITE_CREATE_TABLE t2 {} main {}95SQLITE_UPDATE sqlite_master type main {}96SQLITE_UPDATE sqlite_master name main {}97SQLITE_UPDATE sqlite_master tbl_name main {}98SQLITE_UPDATE sqlite_master rootpage main {}99SQLITE_UPDATE sqlite_master sql main {}100SQLITE_READ sqlite_master ROWID main {}101}102do_test auth2-2.2 {103 set ::authargs {}104 db eval {105 CREATE VIEW v2 AS SELECT x+y AS a, y+z AS b from t2;106 }107 set ::authargs108} {SQLITE_INSERT sqlite_master {} main {}109SQLITE_CREATE_VIEW v2 {} main {}110SQLITE_UPDATE sqlite_master type main {}111SQLITE_UPDATE sqlite_master name main {}112SQLITE_UPDATE sqlite_master tbl_name main {}113SQLITE_UPDATE sqlite_master rootpage main {}114SQLITE_UPDATE sqlite_master sql main {}115SQLITE_READ sqlite_master ROWID main {}116}117do_test auth2-2.3 {118 set ::authargs {}119 db eval {120 SELECT a, b FROM v2;121 }122 set ::authargs123} {SQLITE_SELECT {} {} {} {}124SQLITE_READ t2 x main v2125SQLITE_READ t2 y main v2126SQLITE_READ t2 y main v2127SQLITE_READ t2 z main v2128SQLITE_READ v2 a main {}129SQLITE_READ v2 b main {}130SQLITE_SELECT {} {} {} v2131}132do_test auth2-2.4 {133 db2 eval {134 CREATE TABLE t3(p,q,r);135 }136 set ::authargs {}137 db eval {138 SELECT b, a FROM v2;139 }140 set ::authargs141} {SQLITE_SELECT {} {} {} {}142SQLITE_READ t2 x main v2143SQLITE_READ t2 y main v2144SQLITE_READ t2 y main v2145SQLITE_READ t2 z main v2146SQLITE_READ v2 b main {}147SQLITE_READ v2 a main {}148SQLITE_SELECT {} {} {} v2149SQLITE_SELECT {} {} {} {}150SQLITE_READ t2 x main v2151SQLITE_READ t2 y main v2152SQLITE_READ t2 y main v2153SQLITE_READ t2 z main v2154SQLITE_READ v2 b main {}155SQLITE_READ v2 a main {}156SQLITE_SELECT {} {} {} v2157}158db2 close159 160finish_test161 