github.com/jdgcs/sqlite3@v1.12.1-0.20210908114423-bc5f96e4dd51/testdata/tcl/vtab3.test (about)

     1  # 2006 June 10
     2  #
     3  # The author disclaims copyright to this source code.  In place of
     4  # 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.  The
    12  # focus of this file is the authorisation callback and virtual tables.
    13  #
    14  # $Id: vtab3.test,v 1.3 2008/07/12 14:52:21 drh Exp $
    15  
    16  set testdir [file dirname $argv0]
    17  source $testdir/tester.tcl
    18  
    19  ifcapable !vtab||!auth {
    20    finish_test
    21    return
    22  }
    23  
    24  set ::auth_fail 0
    25  set ::auth_log [list]
    26  set ::auth_filter [list SQLITE_READ SQLITE_UPDATE SQLITE_SELECT SQLITE_PRAGMA]
    27  
    28  proc auth {code arg1 arg2 arg3 arg4 args} {
    29    if {[lsearch $::auth_filter $code]>-1} {
    30      return SQLITE_OK
    31    }
    32    lappend ::auth_log $code $arg1 $arg2 $arg3 $arg4
    33    incr ::auth_fail -1
    34    if {$::auth_fail == 0} {
    35      return SQLITE_DENY
    36    }
    37    return SQLITE_OK
    38  }
    39  
    40  do_test vtab3-1.1 {
    41    execsql {
    42      CREATE TABLE elephant(
    43        name VARCHAR(32), 
    44        color VARCHAR(16), 
    45        age INTEGER, 
    46        UNIQUE(name, color)
    47      );
    48    }
    49  } {}
    50  
    51  
    52  do_test vtab3-1.2 {
    53    register_echo_module [sqlite3_connection_pointer db]
    54    db authorizer ::auth
    55    execsql {
    56      CREATE VIRTUAL TABLE pachyderm USING echo(elephant);
    57    }
    58    set ::auth_log
    59  } [list \
    60    SQLITE_INSERT        sqlite_master {}   main {} \
    61    SQLITE_CREATE_VTABLE pachyderm     echo main {} \
    62  ]
    63  
    64  do_test vtab3-1.3 {
    65    set ::auth_log [list]
    66    execsql {
    67      DROP TABLE pachyderm;
    68    }
    69    set ::auth_log
    70  } [list \
    71    SQLITE_DELETE        sqlite_master {}   main {} \
    72    SQLITE_DROP_VTABLE   pachyderm     echo main {} \
    73    SQLITE_DELETE        pachyderm     {}   main {} \
    74    SQLITE_DELETE        sqlite_master {}   main {} \
    75  ]
    76  
    77  do_test vtab3-1.4 {
    78    set ::auth_fail 1
    79    catchsql {
    80      CREATE VIRTUAL TABLE pachyderm USING echo(elephant);
    81    }
    82  } {1 {not authorized}}
    83  do_test vtab3-1.5 {
    84    execsql {
    85      SELECT name FROM sqlite_master WHERE type = 'table';
    86    }
    87  } {elephant}
    88  
    89  do_test vtab3-1.5 {
    90    set ::auth_fail 2
    91    catchsql {
    92      CREATE VIRTUAL TABLE pachyderm USING echo(elephant);
    93    }
    94  } {1 {not authorized}}
    95  do_test vtab3-1.6 {
    96    execsql {
    97      SELECT name FROM sqlite_master WHERE type = 'table';
    98    }
    99  } {elephant}
   100  
   101  do_test vtab3-1.5 {
   102    set ::auth_fail 3
   103    catchsql {
   104      CREATE VIRTUAL TABLE pachyderm USING echo(elephant);
   105    }
   106  } {0 {}}
   107  do_test vtab3-1.6 {
   108    execsql {
   109      SELECT name FROM sqlite_master WHERE type = 'table';
   110    }
   111  } {elephant pachyderm}
   112  
   113  foreach i [list 1 2 3 4] {
   114    set ::auth_fail $i
   115    do_test vtab3-1.7.$i.1 {
   116      set rc [catch {
   117        execsql {DROP TABLE pachyderm;}
   118      } msg]
   119      if {$msg eq "authorization denied"} {set msg "not authorized"}
   120      list $rc $msg
   121    } {1 {not authorized}}
   122    do_test vtab3-1.7.$i.2 {
   123      execsql {
   124        SELECT name FROM sqlite_master WHERE type = 'table';
   125      }
   126    } {elephant pachyderm}
   127  }
   128  do_test vtab3-1.8.1 {
   129    set ::auth_fail 0
   130    catchsql {
   131      DROP TABLE pachyderm;
   132    }
   133  } {0 {}}
   134  do_test vtab3-1.8.2 {
   135    execsql {
   136      SELECT name FROM sqlite_master WHERE type = 'table';
   137    }
   138  } {elephant}
   139  
   140  finish_test