vtab3.test 3.1 KB

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