AryaWu/sqlite
0
1# 2006 July 142#3# The author disclaims copyright to this source code. In place of4# 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. The12# focus of this script is extension loading.13#14# $Id: loadext.test,v 1.17 2009/03/20 09:09:37 danielk1977 Exp $15 16set testdir [file dirname $argv0]17source $testdir/tester.tcl18 19ifcapable !load_ext {20 finish_test21 return22}23 24# The name of the test extension varies by operating system.25#26if {$::tcl_platform(platform) eq "windows"} {27 set testextension ./testloadext.dll28} else {29 set testextension ./libtestloadext.so30}31set gcc_shared "-shared -fPIC"32if {$::tcl_platform(os) eq "Darwin"} {33 set gcc_shared -dynamiclib34}35 36# The error messages tested by this file are operating system dependent37# (because they are returned by sqlite3OsDlError()). For now, they only38# work with UNIX (and probably only certain kinds of UNIX).39#40# When a shared-object cannot be opened because it does not exist, the41# 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 446# 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 error51# 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 is56# that SQLite is correctly copying the message from xDlError().57#58set dlerror_nosuchfile \59 {%s: cannot open shared object file: No such file or directory}60set dlerror_notadll {%s: file too short}61set dlerror_nosymbol {%s: undefined symbol: %s}62 63if {$::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 69if {$::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_nosuchfile73 } 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 not80# exist, try to create it. If unable to create it, then skip this81# test file.82#83if {![file exists $testextension]} {84 set srcdir [file dir $testdir]/src85 set testextsrc $srcdir/test_loadext.c86 87 set cmdline [concat exec gcc $gcc_shared]88 lappend cmdline -Wall -I$srcdir -I. -I.. -g $testextsrc -o $testextension89 90 if {[catch $cmdline msg]} {91 puts "Skipping loadext tests: Test extension not built..."92 puts $msg93 finish_test94 return95 }96}97 98# Test that loading the extension produces the expected results - adding99# the half() function to the specified database handle.100#101do_test loadext-1.1 {102 catchsql {103 SELECT half(1.0);104 }105} {1 {no such function: half}}106do_test loadext-1.2 {107 db enable_load_extension 1108 sqlite3_load_extension db $testextension testloadext_init109 catchsql {110 SELECT half(1.0);111 }112} {0 0.5}113 114# Test that a second database connection (db2) can load the extension also.115#116do_test loadext-1.3 {117 sqlite3 db2 test.db118 sqlite3_db_config db2 SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1119 catchsql {120 SELECT half(1.0);121 } db2122} {1 {no such function: half}}123do_test loadext-1.4 {124 sqlite3_load_extension db2 $testextension testloadext_init125 catchsql {126 SELECT half(1.0);127 } db2128} {0 0.5}129 130# Close the first database connection. Then check that the second database131# can still use the half() function without a problem.132#133do_test loadext-1.5 {134 db close135 catchsql {136 SELECT half(1.0);137 } db2138} {0 0.5}139 140db2 close141sqlite3 db test.db142sqlite3_enable_load_extension db 1143 144# Try to load an extension for which the file does not exist.145#146do_test loadext-2.1 {147 forcedelete ${testextension}xx148 set rc [catch {149 sqlite3_load_extension db "${testextension}xx"150 } msg]151 list $rc $msg152} /[list 1 [format $dlerror_nosuchfile ${testextension}xx.*]]/153 154# Try to load an extension for which the file is not a shared object155#156do_test loadext-2.2 {157 set fd [open "./notasharedlib.so" w]158 puts $fd blah159 close $fd160 set fd [open "./notasharedlib.dll" w]161 puts $fd blah162 close $fd163 set rc [catch {164 sqlite3_load_extension db "./notasharedlib"165 } msg]166 list $rc $msg167} /[list 1 [format $dlerror_notadll ./notasharedlib.*]]/168 169# Try to load an extension for which the file is present but the170# entry point is not.171#172do_test loadext-2.3 {173 set rc [catch {174 sqlite3_load_extension db $testextension icecream175 } msg]176 if {$::tcl_platform(os) eq "Darwin"} {177 regsub {0x[1234567890abcdefABCDEF]*} $msg XXX msg178 }179 list $rc $msg180} /[list 1 [format $dlerror_nosymbol $testextension icecream]]/181 182# Try to load an extension for which the entry point fails (returns non-zero) 183#184do_test loadext-2.4 {185 set rc [catch {186 sqlite3_load_extension db $testextension testbrokenext_init187 } msg]188 list $rc $msg189} {1 {error during initialization: broken!}}190 191############################################################################192# Tests for the load_extension() SQL function193#194 195db close196sqlite3 db test.db197sqlite3_enable_load_extension db 1198do_test loadext-3.1 {199 catchsql {200 SELECT half(5);201 }202} {1 {no such function: half}}203do_test loadext-3.2 {204 set res [catchsql {205 SELECT load_extension($::testextension)206 }]207 if {$::tcl_platform(os) eq "Darwin"} {208 regsub {0x[1234567890abcdefABCDEF]*} $res XXX res209 }210 set res211} /[list 1 [format $dlerror_nosymbol $testextension sqlite3_.*_init]]/212do_test loadext-3.3 {213 catchsql {214 SELECT load_extension($::testextension,'testloadext_init')215 }216} {0 {{}}}217do_test loadext-3.4 {218 catchsql {219 SELECT half(5);220 }221} {0 2.5}222do_test loadext-3.5 {223 db eval {224 SELECT sqlite3_status('MEMORY_USED') AS mused225 } break226 puts -nonewline " (memory_used=$mused) "227 expr {$mused>0}228} {1}229do_test loadext-3.6 {230 catchsql {231 SELECT sqlite3_status('MEMORY_USED_X') AS mused232 }233} {1 {unknown status property: MEMORY_USED_X}}234do_test loadext-3.7 {235 catchsql {236 SELECT sqlite3_status(4.53) AS mused237 }238} {1 {unknown status type}}239do_test loadext-3.8 {240 catchsql {241 SELECT sqlite3_status(23) AS mused242 }243} {1 {sqlite3_status(23,...) returns 21}}244 245# Ticket #1863246# Make sure the extension loading mechanism will not work unless it247# is explicitly enabled.248#249db close250sqlite3 db test.db251do_test loadext-4.1 {252 catchsql {253 SELECT load_extension($::testextension,'testloadext_init')254 }255} {1 {not authorized}}256do_test loadext-4.2 {257 sqlite3_enable_load_extension db 1258 catchsql {259 SELECT load_extension($::testextension,'testloadext_init')260 }261} {0 {{}}}262 263# disable all extension loading264do_test loadext-4.3 {265 sqlite3_enable_load_extension db 0266 catchsql {267 SELECT load_extension($::testextension,'testloadext_init')268 }269} {1 {not authorized}}270 271# enable C-api extension loading only. Show that the SQL function272# still does not work.273do_test loadext-4.4 {274 sqlite3_db_config db SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1275 catchsql {276 SELECT load_extension($::testextension,'testloadext_init')277 }278} {1 {not authorized}}279 280source $testdir/malloc_common.tcl281 282 283# Malloc failure in sqlite3_auto_extension and sqlite3_load_extension284#285do_malloc_test loadext-5 -tclprep {286 sqlite3_reset_auto_extension287} -tclbody {288 if {[autoinstall_test_functions]==7} {error "out of memory"}289}290 291# On Windows, this malloc test must be skipped because the winDlOpen292# function itself can fail due to "out of memory" conditions.293#294if {$::tcl_platform(platform) ne "windows"} {295 do_malloc_test loadext-6 -tclbody {296 db enable_load_extension 1297 sqlite3_load_extension db $::testextension testloadext_init298 }299}300 301autoinstall_test_functions302 303finish_test304 