misuse.test 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.3 2002/05/21 11:38:12 drh Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. # Make sure the test logic works
  20. #
  21. do_test misuse-1.1 {
  22. db close
  23. catch {file delete -force test2.db}
  24. set ::DB [sqlite db test2.db]
  25. execsql {
  26. CREATE TABLE t1(a,b);
  27. INSERT INTO t1 VALUES(1,2);
  28. }
  29. sqlite_exec_printf $::DB {SELECT * FROM t1} {}
  30. } {0 {a b 1 2}}
  31. do_test misuse-1.2 {
  32. sqlite_exec_printf $::DB {SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1} {}
  33. } {1 {no such function: x_coalesce}}
  34. do_test misuse-1.3 {
  35. sqlite_create_function $::DB
  36. sqlite_exec_printf $::DB {SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1} {}
  37. } {0 {xyz 1}}
  38. # Use the x_sqlite_exec() SQL function to simulate the effect of two
  39. # threads trying to use the same database at the same time.
  40. #
  41. do_test misuse-1.4 {
  42. sqlite_exec_printf $::DB {
  43. SELECT x_sqlite_exec('SELECT * FROM t1');
  44. } {}
  45. } {21 {library routine called out of sequence}}
  46. do_test misuse-1.5 {
  47. sqlite_exec_printf $::DB {SELECT * FROM t1} {}
  48. } {21 {library routine called out of sequence}}
  49. do_test misuse-1.6 {
  50. catchsql {
  51. SELECT * FROM t1
  52. }
  53. } {1 {library routine called out of sequence}}
  54. # Attempt to register a new SQL function while an sqlite_exec() is active.
  55. #
  56. do_test misuse-2.1 {
  57. db close
  58. set ::DB [sqlite db test2.db]
  59. execsql {
  60. SELECT * FROM t1
  61. }
  62. } {1 2}
  63. do_test misuse-2.2 {
  64. sqlite_exec_printf $::DB {SELECT * FROM t1} {}
  65. } {0 {a b 1 2}}
  66. do_test misuse-2.3 {
  67. set v [catch {
  68. db eval {SELECT * FROM t1} {} {
  69. sqlite_create_function $::DB
  70. }
  71. } msg]
  72. lappend v $msg
  73. } {1 {library routine called out of sequence}}
  74. do_test misuse-2.4 {
  75. sqlite_exec_printf $::DB {SELECT * FROM t1} {}
  76. } {21 {library routine called out of sequence}}
  77. do_test misuse-2.5 {
  78. catchsql {
  79. SELECT * FROM t1
  80. }
  81. } {1 {library routine called out of sequence}}
  82. # Attempt to register a new SQL aggregate while an sqlite_exec() is active.
  83. #
  84. do_test misuse-3.1 {
  85. db close
  86. set ::DB [sqlite db test2.db]
  87. execsql {
  88. SELECT * FROM t1
  89. }
  90. } {1 2}
  91. do_test misuse-3.2 {
  92. sqlite_exec_printf $::DB {SELECT * FROM t1} {}
  93. } {0 {a b 1 2}}
  94. do_test misuse-3.3 {
  95. set v [catch {
  96. db eval {SELECT * FROM t1} {} {
  97. sqlite_create_aggregate $::DB
  98. }
  99. } msg]
  100. lappend v $msg
  101. } {1 {library routine called out of sequence}}
  102. do_test misuse-3.4 {
  103. sqlite_exec_printf $::DB {SELECT * FROM t1} {}
  104. } {21 {library routine called out of sequence}}
  105. do_test misuse-3.5 {
  106. catchsql {
  107. SELECT * FROM t1
  108. }
  109. } {1 {library routine called out of sequence}}
  110. # Attempt to close the database from an sqlite_exec callback.
  111. #
  112. do_test misuse-4.1 {
  113. db close
  114. set ::DB [sqlite db test2.db]
  115. execsql {
  116. SELECT * FROM t1
  117. }
  118. } {1 2}
  119. do_test misuse-4.2 {
  120. sqlite_exec_printf $::DB {SELECT * FROM t1} {}
  121. } {0 {a b 1 2}}
  122. do_test misuse-4.3 {
  123. set v [catch {
  124. db eval {SELECT * FROM t1} {} {
  125. sqlite_close $::DB
  126. }
  127. } msg]
  128. lappend v $msg
  129. } {1 {library routine called out of sequence}}
  130. do_test misuse-4.4 {
  131. sqlite_exec_printf $::DB {SELECT * FROM t1} {}
  132. } {21 {library routine called out of sequence}}
  133. do_test misuse-4.5 {
  134. catchsql {
  135. SELECT * FROM t1
  136. }
  137. } {1 {library routine called out of sequence}}
  138. # Attempt to use a database after it has been closed.
  139. #
  140. do_test misuse-5.1 {
  141. db close
  142. set ::DB [sqlite db test2.db]
  143. execsql {
  144. SELECT * FROM t1
  145. }
  146. } {1 2}
  147. do_test misuse-5.2 {
  148. sqlite_exec_printf $::DB {SELECT * FROM t1} {}
  149. } {0 {a b 1 2}}
  150. do_test misuse-5.3 {
  151. db close
  152. sqlite_exec_printf $::DB {SELECT * FROM t1} {}
  153. } {21 {library routine called out of sequence}}
  154. finish_test