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