loadext.test 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. # 2006 July 14
  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 script is extension loading.
  13. #
  14. # $Id: loadext.test,v 1.17 2009/03/20 09:09:37 danielk1977 Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. ifcapable !load_ext {
  18. finish_test
  19. return
  20. }
  21. # The name of the test extension varies by operating system.
  22. #
  23. if {$::tcl_platform(platform) eq "windows"} {
  24. set testextension ./testloadext.dll
  25. } else {
  26. set testextension ./libtestloadext.so
  27. }
  28. set gcc_shared "-shared -fPIC"
  29. if {$::tcl_platform(os) eq "Darwin"} {
  30. set gcc_shared -dynamiclib
  31. }
  32. # The error messages tested by this file are operating system dependent
  33. # (because they are returned by sqlite3OsDlError()). For now, they only
  34. # work with UNIX (and probably only certain kinds of UNIX).
  35. #
  36. # When a shared-object cannot be opened because it does not exist, the
  37. # format of the message returned is:
  38. #
  39. # [format $dlerror_nosuchfile <shared-object-name>]
  40. #
  41. # When a shared-object cannot be opened because it consists of the 4
  42. # characters "blah" only, we expect the error message to be:
  43. #
  44. # [format $dlerror_notadll <shared-object-name>]
  45. #
  46. # When a symbol cannot be found within an open shared-object, the error
  47. # message should be:
  48. #
  49. # [format $dlerror_nosymbol <shared-object-name> <symbol-name>]
  50. #
  51. # The exact error messages are not important. The important bit is
  52. # that SQLite is correctly copying the message from xDlError().
  53. #
  54. set dlerror_nosuchfile \
  55. {%s: cannot open shared object file: No such file or directory}
  56. set dlerror_notadll {%s: file too short}
  57. set dlerror_nosymbol {%s: undefined symbol: %s}
  58. if {$::tcl_platform(os) eq "Darwin"} {
  59. set dlerror_nosuchfile {dlopen(%s, 10): image not found}
  60. set dlerror_notadll {dlopen(%1$s, 10): no suitable image found.*}
  61. set dlerror_nosymbol {dlsym(XXX, %2$s): symbol not found}
  62. }
  63. # Make sure the test extension actually exists. If it does not
  64. # exist, try to create it. If unable to create it, then skip this
  65. # test file.
  66. #
  67. if {![file exists $testextension]} {
  68. set srcdir [file dir $testdir]/src
  69. set testextsrc $srcdir/test_loadext.c
  70. set cmdline [concat exec gcc $gcc_shared]
  71. lappend cmdline -Wall -I$srcdir -I. -g $testextsrc -o $testextension
  72. if {[catch $cmdline msg]} {
  73. puts "Skipping loadext tests: Test extension not built..."
  74. puts $msg
  75. finish_test
  76. return
  77. }
  78. }
  79. # Test that loading the extension produces the expected results - adding
  80. # the half() function to the specified database handle.
  81. #
  82. do_test loadext-1.1 {
  83. catchsql {
  84. SELECT half(1.0);
  85. }
  86. } {1 {no such function: half}}
  87. do_test loadext-1.2 {
  88. db enable_load_extension 1
  89. sqlite3_load_extension db $testextension testloadext_init
  90. catchsql {
  91. SELECT half(1.0);
  92. }
  93. } {0 0.5}
  94. # Test that a second database connection (db2) can load the extension also.
  95. #
  96. do_test loadext-1.3 {
  97. sqlite3 db2 test.db
  98. sqlite3_enable_load_extension db2 1
  99. catchsql {
  100. SELECT half(1.0);
  101. } db2
  102. } {1 {no such function: half}}
  103. do_test loadext-1.4 {
  104. sqlite3_load_extension db2 $testextension testloadext_init
  105. catchsql {
  106. SELECT half(1.0);
  107. } db2
  108. } {0 0.5}
  109. # Close the first database connection. Then check that the second database
  110. # can still use the half() function without a problem.
  111. #
  112. do_test loadext-1.5 {
  113. db close
  114. catchsql {
  115. SELECT half(1.0);
  116. } db2
  117. } {0 0.5}
  118. db2 close
  119. sqlite3 db test.db
  120. sqlite3_enable_load_extension db 1
  121. # Try to load an extension for which the file does not exist.
  122. #
  123. do_test loadext-2.1 {
  124. forcedelete ${testextension}xx
  125. set rc [catch {
  126. sqlite3_load_extension db "${testextension}xx"
  127. } msg]
  128. list $rc $msg
  129. } /[list 1 [format $dlerror_nosuchfile ${testextension}xx.*]]/
  130. # Try to load an extension for which the file is not a shared object
  131. #
  132. do_test loadext-2.2 {
  133. set fd [open "./notasharedlib.so" w]
  134. puts $fd blah
  135. close $fd
  136. set fd [open "./notasharedlib.dll" w]
  137. puts $fd blah
  138. close $fd
  139. set rc [catch {
  140. sqlite3_load_extension db "./notasharedlib"
  141. } msg]
  142. list $rc $msg
  143. } /[list 1 [format $dlerror_notadll ./notasharedlib.*]]/
  144. # Try to load an extension for which the file is present but the
  145. # entry point is not.
  146. #
  147. do_test loadext-2.3 {
  148. set rc [catch {
  149. sqlite3_load_extension db $testextension icecream
  150. } msg]
  151. if {$::tcl_platform(os) eq "Darwin"} {
  152. regsub {0x[1234567890abcdefABCDEF]*} $msg XXX msg
  153. }
  154. list $rc $msg
  155. } [list 1 [format $dlerror_nosymbol $testextension icecream]]
  156. # Try to load an extension for which the entry point fails (returns non-zero)
  157. #
  158. do_test loadext-2.4 {
  159. set rc [catch {
  160. sqlite3_load_extension db $testextension testbrokenext_init
  161. } msg]
  162. list $rc $msg
  163. } {1 {error during initialization: broken!}}
  164. ############################################################################
  165. # Tests for the load_extension() SQL function
  166. #
  167. db close
  168. sqlite3 db test.db
  169. sqlite3_enable_load_extension db 1
  170. do_test loadext-3.1 {
  171. catchsql {
  172. SELECT half(5);
  173. }
  174. } {1 {no such function: half}}
  175. do_test loadext-3.2 {
  176. set res [catchsql {
  177. SELECT load_extension($::testextension)
  178. }]
  179. if {$::tcl_platform(os) eq "Darwin"} {
  180. regsub {0x[1234567890abcdefABCDEF]*} $res XXX res
  181. }
  182. set res
  183. } /[list 1 [format $dlerror_nosymbol $testextension sqlite3_.*_init]]/
  184. do_test loadext-3.3 {
  185. catchsql {
  186. SELECT load_extension($::testextension,'testloadext_init')
  187. }
  188. } {0 {{}}}
  189. do_test loadext-3.4 {
  190. catchsql {
  191. SELECT half(5);
  192. }
  193. } {0 2.5}
  194. do_test loadext-3.5 {
  195. db eval {
  196. SELECT sqlite3_status('MEMORY_USED') AS mused
  197. } break
  198. puts -nonewline " (memory_used=$mused) "
  199. expr {$mused>0}
  200. } {1}
  201. do_test loadext-3.6 {
  202. catchsql {
  203. SELECT sqlite3_status('MEMORY_USED_X') AS mused
  204. }
  205. } {1 {unknown status property: MEMORY_USED_X}}
  206. do_test loadext-3.7 {
  207. catchsql {
  208. SELECT sqlite3_status(4.53) AS mused
  209. }
  210. } {1 {unknown status type}}
  211. do_test loadext-3.8 {
  212. catchsql {
  213. SELECT sqlite3_status(23) AS mused
  214. }
  215. } {1 {sqlite3_status(23,...) returns 21}}
  216. # Ticket #1863
  217. # Make sure the extension loading mechanism will not work unless it
  218. # is explicitly enabled.
  219. #
  220. db close
  221. sqlite3 db test.db
  222. do_test loadext-4.1 {
  223. catchsql {
  224. SELECT load_extension($::testextension,'testloadext_init')
  225. }
  226. } {1 {not authorized}}
  227. do_test loadext-4.2 {
  228. sqlite3_enable_load_extension db 1
  229. catchsql {
  230. SELECT load_extension($::testextension,'testloadext_init')
  231. }
  232. } {0 {{}}}
  233. do_test loadext-4.3 {
  234. sqlite3_enable_load_extension db 0
  235. catchsql {
  236. SELECT load_extension($::testextension,'testloadext_init')
  237. }
  238. } {1 {not authorized}}
  239. source $testdir/malloc_common.tcl
  240. # Malloc failure in sqlite3_auto_extension and sqlite3_load_extension
  241. #
  242. do_malloc_test loadext-5 -tclprep {
  243. sqlite3_reset_auto_extension
  244. } -tclbody {
  245. if {[autoinstall_test_functions]==7} {error "out of memory"}
  246. }
  247. do_malloc_test loadext-6 -tclbody {
  248. db enable_load_extension 1
  249. sqlite3_load_extension db $::testextension testloadext_init
  250. }
  251. autoinstall_test_functions
  252. finish_test