1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/loadext.test Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,272 @@
1.4 +# 2006 July 14
1.5 +#
1.6 +# The author disclaims copyright to this source code. In place of
1.7 +# a legal notice, here is a blessing:
1.8 +#
1.9 +# May you do good and not evil.
1.10 +# May you find forgiveness for yourself and forgive others.
1.11 +# May you share freely, never taking more than you give.
1.12 +#
1.13 +#***********************************************************************
1.14 +# This file implements regression tests for SQLite library. The
1.15 +# focus of this script is extension loading.
1.16 +#
1.17 +# $Id: loadext.test,v 1.15 2008/08/22 13:57:39 pweilbacher Exp $
1.18 +
1.19 +set testdir [file dirname $argv0]
1.20 +source $testdir/tester.tcl
1.21 +
1.22 +ifcapable !load_ext {
1.23 + finish_test
1.24 + return
1.25 +}
1.26 +
1.27 +# The name of the test extension varies by operating system.
1.28 +#
1.29 +if {$::tcl_platform(platform) eq "windows" || $::tcl_platform(platform) eq "os2"} {
1.30 + set testextension ./testloadext.dll
1.31 +} else {
1.32 + set testextension ./libtestloadext.so
1.33 +}
1.34 +set gcc_shared -shared
1.35 +if {$::tcl_platform(os) eq "Darwin"} {
1.36 + set gcc_shared -dynamiclib
1.37 +}
1.38 +
1.39 +# The error messages tested by this file are operating system dependent
1.40 +# (because they are returned by sqlite3OsDlError()). For now, they only
1.41 +# work with UNIX (and probably only certain kinds of UNIX).
1.42 +#
1.43 +# When a shared-object cannot be opened because it does not exist, the
1.44 +# format of the message returned is:
1.45 +#
1.46 +# [format $dlerror_nosuchfile <shared-object-name>]
1.47 +#
1.48 +# When a shared-object cannot be opened because it consists of the 4
1.49 +# characters "blah" only, we expect the error message to be:
1.50 +#
1.51 +# [format $dlerror_notadll <shared-object-name>]
1.52 +#
1.53 +# When a symbol cannot be found within an open shared-object, the error
1.54 +# message should be:
1.55 +#
1.56 +# [format $dlerror_nosymbol <shared-object-name> <symbol-name>]
1.57 +#
1.58 +# The exact error messages are not important. The important bit is
1.59 +# that SQLite is correctly copying the message from xDlError().
1.60 +#
1.61 +set dlerror_nosuchfile \
1.62 + {%s: cannot open shared object file: No such file or directory}
1.63 +set dlerror_notadll {%s: file too short}
1.64 +set dlerror_nosymbol {%s: undefined symbol: %s}
1.65 +
1.66 +if {$::tcl_platform(os) eq "Darwin"} {
1.67 + set dlerror_nosuchfile {dlopen(%s, 10): image not found}
1.68 + set dlerror_notadll {dlopen(%1$s, 10): no suitable image found. Did find:
1.69 + %1$s: file to short}
1.70 + set dlerror_nosymbol {dlsym(XXX, %2$s): symbol not found}
1.71 +}
1.72 +
1.73 +# Make sure the test extension actually exists. If it does not
1.74 +# exist, try to create it. If unable to create it, then skip this
1.75 +# test file.
1.76 +#
1.77 +if {![file exists $testextension]} {
1.78 + set srcdir [file dir $testdir]/src
1.79 + set testextsrc $srcdir/test_loadext.c
1.80 + if {[catch {
1.81 + exec gcc $gcc_shared -Wall -I$srcdir -I. -g $testextsrc -o $testextension
1.82 + } msg]} {
1.83 + puts "Skipping loadext tests: Test extension not built..."
1.84 + puts $msg
1.85 + finish_test
1.86 + return
1.87 + }
1.88 +}
1.89 +
1.90 +# Test that loading the extension produces the expected results - adding
1.91 +# the half() function to the specified database handle.
1.92 +#
1.93 +do_test loadext-1.1 {
1.94 + catchsql {
1.95 + SELECT half(1.0);
1.96 + }
1.97 +} {1 {no such function: half}}
1.98 +do_test loadext-1.2 {
1.99 + db enable_load_extension 1
1.100 + sqlite3_load_extension db $testextension testloadext_init
1.101 + catchsql {
1.102 + SELECT half(1.0);
1.103 + }
1.104 +} {0 0.5}
1.105 +
1.106 +# Test that a second database connection (db2) can load the extension also.
1.107 +#
1.108 +do_test loadext-1.3 {
1.109 + sqlite3 db2 test.db
1.110 + sqlite3_enable_load_extension db2 1
1.111 + catchsql {
1.112 + SELECT half(1.0);
1.113 + } db2
1.114 +} {1 {no such function: half}}
1.115 +do_test loadext-1.4 {
1.116 + sqlite3_load_extension db2 $testextension testloadext_init
1.117 + catchsql {
1.118 + SELECT half(1.0);
1.119 + } db2
1.120 +} {0 0.5}
1.121 +
1.122 +# Close the first database connection. Then check that the second database
1.123 +# can still use the half() function without a problem.
1.124 +#
1.125 +do_test loadext-1.5 {
1.126 + db close
1.127 + catchsql {
1.128 + SELECT half(1.0);
1.129 + } db2
1.130 +} {0 0.5}
1.131 +
1.132 +db2 close
1.133 +sqlite3 db test.db
1.134 +sqlite3_enable_load_extension db 1
1.135 +
1.136 +# Try to load an extension for which the file does not exist.
1.137 +#
1.138 +do_test loadext-2.1 {
1.139 + file delete -force ${testextension}xx
1.140 + set rc [catch {
1.141 + sqlite3_load_extension db "${testextension}xx"
1.142 + } msg]
1.143 + list $rc $msg
1.144 +} [list 1 [format $dlerror_nosuchfile ${testextension}xx]]
1.145 +
1.146 +# Try to load an extension for which the file is not a shared object
1.147 +#
1.148 +do_test loadext-2.2 {
1.149 + set fd [open "${testextension}xx" w]
1.150 + puts $fd blah
1.151 + close $fd
1.152 + set rc [catch {
1.153 + sqlite3_load_extension db "${testextension}xx"
1.154 + } msg]
1.155 + list $rc $msg
1.156 +} [list 1 [format $dlerror_notadll ${testextension}xx]]
1.157 +
1.158 +# Try to load an extension for which the file is present but the
1.159 +# entry point is not.
1.160 +#
1.161 +do_test loadext-2.3 {
1.162 + set rc [catch {
1.163 + sqlite3_load_extension db $testextension icecream
1.164 + } msg]
1.165 + if {$::tcl_platform(os) eq "Darwin"} {
1.166 + regsub {0x[1234567890abcdefABCDEF]*} $msg XXX msg
1.167 + }
1.168 + list $rc $msg
1.169 +} [list 1 [format $dlerror_nosymbol $testextension icecream]]
1.170 +
1.171 +# Try to load an extension for which the entry point fails (returns non-zero)
1.172 +#
1.173 +do_test loadext-2.4 {
1.174 + set rc [catch {
1.175 + sqlite3_load_extension db $testextension testbrokenext_init
1.176 + } msg]
1.177 + list $rc $msg
1.178 +} {1 {error during initialization: broken!}}
1.179 +
1.180 +############################################################################
1.181 +# Tests for the load_extension() SQL function
1.182 +#
1.183 +
1.184 +db close
1.185 +sqlite3 db test.db
1.186 +sqlite3_enable_load_extension db 1
1.187 +do_test loadext-3.1 {
1.188 + catchsql {
1.189 + SELECT half(5);
1.190 + }
1.191 +} {1 {no such function: half}}
1.192 +do_test loadext-3.2 {
1.193 + set res [catchsql {
1.194 + SELECT load_extension($::testextension)
1.195 + }]
1.196 + if {$::tcl_platform(os) eq "Darwin"} {
1.197 + regsub {0x[1234567890abcdefABCDEF]*} $res XXX res
1.198 + }
1.199 + set res
1.200 +} [list 1 [format $dlerror_nosymbol $testextension sqlite3_extension_init]]
1.201 +do_test loadext-3.3 {
1.202 + catchsql {
1.203 + SELECT load_extension($::testextension,'testloadext_init')
1.204 + }
1.205 +} {0 {{}}}
1.206 +do_test loadext-3.4 {
1.207 + catchsql {
1.208 + SELECT half(5);
1.209 + }
1.210 +} {0 2.5}
1.211 +do_test loadext-3.5 {
1.212 + db eval {
1.213 + SELECT sqlite3_status('MEMORY_USED') AS mused
1.214 + } break
1.215 + puts -nonewline " (memory_used=$mused) "
1.216 + expr {$mused>0}
1.217 +} {1}
1.218 +do_test loadext-3.6 {
1.219 + catchsql {
1.220 + SELECT sqlite3_status('MEMORY_USED_X') AS mused
1.221 + }
1.222 +} {1 {unknown status property: MEMORY_USED_X}}
1.223 +do_test loadext-3.7 {
1.224 + catchsql {
1.225 + SELECT sqlite3_status(4.53) AS mused
1.226 + }
1.227 +} {1 {unknown status type}}
1.228 +do_test loadext-3.8 {
1.229 + catchsql {
1.230 + SELECT sqlite3_status(23) AS mused
1.231 + }
1.232 +} {1 {sqlite3_status(23,...) returns 21}}
1.233 +
1.234 +# Ticket #1863
1.235 +# Make sure the extension loading mechanism will not work unless it
1.236 +# is explicitly enabled.
1.237 +#
1.238 +db close
1.239 +sqlite3 db test.db
1.240 +do_test loadext-4.1 {
1.241 + catchsql {
1.242 + SELECT load_extension($::testextension,'testloadext_init')
1.243 + }
1.244 +} {1 {not authorized}}
1.245 +do_test loadext-4.2 {
1.246 + sqlite3_enable_load_extension db 1
1.247 + catchsql {
1.248 + SELECT load_extension($::testextension,'testloadext_init')
1.249 + }
1.250 +} {0 {{}}}
1.251 +
1.252 +do_test loadext-4.3 {
1.253 + sqlite3_enable_load_extension db 0
1.254 + catchsql {
1.255 + SELECT load_extension($::testextension,'testloadext_init')
1.256 + }
1.257 +} {1 {not authorized}}
1.258 +
1.259 +source $testdir/malloc_common.tcl
1.260 +
1.261 +
1.262 +# Malloc failure in sqlite3_auto_extension and sqlite3_load_extension
1.263 +#
1.264 +do_malloc_test loadext-5 -tclprep {
1.265 + sqlite3_reset_auto_extension
1.266 +} -tclbody {
1.267 + if {[autoinstall_test_functions]==7} {error "out of memory"}
1.268 +}
1.269 +do_malloc_test loadext-6 -tclbody {
1.270 + db enable_load_extension 1
1.271 + sqlite3_load_extension db $::testextension testloadext_init
1.272 +}
1.273 +autoinstall_test_functions
1.274 +
1.275 +finish_test