PageRenderTime 48ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/sqlite/test/misuse.test

#
Unknown | 207 lines | 195 code | 12 blank | 0 comment | 0 complexity | 70b8773afd77dd5f9cb1cc4a22c951e2 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # 2002 May 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.
  12. #
  13. # This file implements tests for the SQLITE_MISUSE detection logic.
  14. # This test file leaks memory and file descriptors.
  15. #
  16. # $Id: misuse.test,v 1.11 2006/01/03 00:33:50 drh Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. proc catchsql2 {sql} {
  20. set r [
  21. catch {
  22. set res [list]
  23. db eval $sql data {
  24. if { $res==[list] } {
  25. foreach f $data(*) {lappend res $f}
  26. }
  27. foreach f $data(*) {lappend res $data($f)}
  28. }
  29. set res
  30. } msg
  31. ]
  32. lappend r $msg
  33. }
  34. # Make sure the test logic works
  35. #
  36. do_test misuse-1.1 {
  37. db close
  38. catch {file delete -force test2.db}
  39. catch {file delete -force test2.db-journal}
  40. sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
  41. execsql {
  42. CREATE TABLE t1(a,b);
  43. INSERT INTO t1 VALUES(1,2);
  44. }
  45. catchsql2 {
  46. SELECT * FROM t1
  47. }
  48. } {0 {a b 1 2}}
  49. do_test misuse-1.2 {
  50. catchsql2 {
  51. SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1
  52. }
  53. } {1 {no such function: x_coalesce}}
  54. do_test misuse-1.3 {
  55. sqlite3_create_function $::DB
  56. catchsql2 {
  57. SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1
  58. }
  59. } {0 {xyz 1}}
  60. # Use the x_sqlite_exec() SQL function to simulate the effect of two
  61. # threads trying to use the same database at the same time.
  62. #
  63. # It used to be prohibited to invoke sqlite_exec() from within a function,
  64. # but that has changed. The following tests used to cause errors but now
  65. # they do not.
  66. #
  67. ifcapable {utf16} {
  68. do_test misuse-1.4 {
  69. catchsql2 {
  70. SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz;
  71. }
  72. } {0 {xyz {1 2}}}
  73. }
  74. do_test misuse-1.5 {
  75. catchsql2 {SELECT * FROM t1}
  76. } {0 {a b 1 2}}
  77. do_test misuse-1.6 {
  78. catchsql {
  79. SELECT * FROM t1
  80. }
  81. } {0 {1 2}}
  82. # Attempt to register a new SQL function while an sqlite_exec() is active.
  83. #
  84. do_test misuse-2.1 {
  85. db close
  86. sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
  87. execsql {
  88. SELECT * FROM t1
  89. }
  90. } {1 2}
  91. do_test misuse-2.2 {
  92. catchsql2 {SELECT * FROM t1}
  93. } {0 {a b 1 2}}
  94. # We used to disallow creating new function from within an exec().
  95. # But now this is acceptable.
  96. do_test misuse-2.3 {
  97. set v [catch {
  98. db eval {SELECT * FROM t1} {} {
  99. sqlite3_create_function $::DB
  100. }
  101. } msg]
  102. lappend v $msg
  103. } {0 {}}
  104. do_test misuse-2.4 {
  105. catchsql2 {SELECT * FROM t1}
  106. } {0 {a b 1 2}}
  107. do_test misuse-2.5 {
  108. catchsql {
  109. SELECT * FROM t1
  110. }
  111. } {0 {1 2}}
  112. # Attempt to register a new SQL aggregate while an sqlite_exec() is active.
  113. #
  114. do_test misuse-3.1 {
  115. db close
  116. sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
  117. execsql {
  118. SELECT * FROM t1
  119. }
  120. } {1 2}
  121. do_test misuse-3.2 {
  122. catchsql2 {SELECT * FROM t1}
  123. } {0 {a b 1 2}}
  124. # We used to disallow creating new function from within an exec().
  125. # But now this is acceptable.
  126. do_test misuse-3.3 {
  127. set v [catch {
  128. db eval {SELECT * FROM t1} {} {
  129. sqlite3_create_aggregate $::DB
  130. }
  131. } msg]
  132. lappend v $msg
  133. } {0 {}}
  134. do_test misuse-3.4 {
  135. catchsql2 {SELECT * FROM t1}
  136. } {0 {a b 1 2}}
  137. do_test misuse-3.5 {
  138. catchsql {
  139. SELECT * FROM t1
  140. }
  141. } {0 {1 2}}
  142. # Attempt to close the database from an sqlite_exec callback.
  143. #
  144. # Update for v3: The db cannot be closed because there are active
  145. # VMs. The sqlite3_close call would return SQLITE_BUSY.
  146. do_test misuse-4.1 {
  147. db close
  148. sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
  149. execsql {
  150. SELECT * FROM t1
  151. }
  152. } {1 2}
  153. do_test misuse-4.2 {
  154. catchsql2 {SELECT * FROM t1}
  155. } {0 {a b 1 2}}
  156. do_test misuse-4.3 {
  157. set v [catch {
  158. db eval {SELECT * FROM t1} {} {
  159. set r [sqlite3_close $::DB]
  160. }
  161. } msg]
  162. lappend v $msg $r
  163. } {0 {} SQLITE_BUSY}
  164. do_test misuse-4.4 {
  165. # Flush the TCL statement cache here, otherwise the sqlite3_close() will
  166. # fail because there are still un-finalized() VDBEs.
  167. db cache flush
  168. sqlite3_close $::DB
  169. catchsql2 {SELECT * FROM t1}
  170. } {1 {library routine called out of sequence}}
  171. do_test misuse-4.5 {
  172. catchsql {
  173. SELECT * FROM t1
  174. }
  175. } {1 {library routine called out of sequence}}
  176. # Attempt to use a database after it has been closed.
  177. #
  178. do_test misuse-5.1 {
  179. db close
  180. sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
  181. execsql {
  182. SELECT * FROM t1
  183. }
  184. } {1 2}
  185. do_test misuse-5.2 {
  186. catchsql2 {SELECT * FROM t1}
  187. } {0 {a b 1 2}}
  188. do_test misuse-5.3 {
  189. db close
  190. set r [catch {
  191. sqlite3_prepare $::DB {SELECT * FROM t1} -1 TAIL
  192. } msg]
  193. lappend r $msg
  194. } {1 {(21) library routine called out of sequence}}
  195. finish_test