sl@0: # 2006 July 14 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this script is extension loading. sl@0: # sl@0: # $Id: loadext.test,v 1.15 2008/08/22 13:57:39 pweilbacher Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable !load_ext { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: # The name of the test extension varies by operating system. sl@0: # sl@0: if {$::tcl_platform(platform) eq "windows" || $::tcl_platform(platform) eq "os2"} { sl@0: set testextension ./testloadext.dll sl@0: } else { sl@0: set testextension ./libtestloadext.so sl@0: } sl@0: set gcc_shared -shared sl@0: if {$::tcl_platform(os) eq "Darwin"} { sl@0: set gcc_shared -dynamiclib sl@0: } sl@0: sl@0: # The error messages tested by this file are operating system dependent sl@0: # (because they are returned by sqlite3OsDlError()). For now, they only sl@0: # work with UNIX (and probably only certain kinds of UNIX). sl@0: # sl@0: # When a shared-object cannot be opened because it does not exist, the sl@0: # format of the message returned is: sl@0: # sl@0: # [format $dlerror_nosuchfile ] sl@0: # sl@0: # When a shared-object cannot be opened because it consists of the 4 sl@0: # characters "blah" only, we expect the error message to be: sl@0: # sl@0: # [format $dlerror_notadll ] sl@0: # sl@0: # When a symbol cannot be found within an open shared-object, the error sl@0: # message should be: sl@0: # sl@0: # [format $dlerror_nosymbol ] sl@0: # sl@0: # The exact error messages are not important. The important bit is sl@0: # that SQLite is correctly copying the message from xDlError(). sl@0: # sl@0: set dlerror_nosuchfile \ sl@0: {%s: cannot open shared object file: No such file or directory} sl@0: set dlerror_notadll {%s: file too short} sl@0: set dlerror_nosymbol {%s: undefined symbol: %s} sl@0: sl@0: if {$::tcl_platform(os) eq "Darwin"} { sl@0: set dlerror_nosuchfile {dlopen(%s, 10): image not found} sl@0: set dlerror_notadll {dlopen(%1$s, 10): no suitable image found. Did find: sl@0: %1$s: file to short} sl@0: set dlerror_nosymbol {dlsym(XXX, %2$s): symbol not found} sl@0: } sl@0: sl@0: # Make sure the test extension actually exists. If it does not sl@0: # exist, try to create it. If unable to create it, then skip this sl@0: # test file. sl@0: # sl@0: if {![file exists $testextension]} { sl@0: set srcdir [file dir $testdir]/src sl@0: set testextsrc $srcdir/test_loadext.c sl@0: if {[catch { sl@0: exec gcc $gcc_shared -Wall -I$srcdir -I. -g $testextsrc -o $testextension sl@0: } msg]} { sl@0: puts "Skipping loadext tests: Test extension not built..." sl@0: puts $msg sl@0: finish_test sl@0: return sl@0: } sl@0: } sl@0: sl@0: # Test that loading the extension produces the expected results - adding sl@0: # the half() function to the specified database handle. sl@0: # sl@0: do_test loadext-1.1 { sl@0: catchsql { sl@0: SELECT half(1.0); sl@0: } sl@0: } {1 {no such function: half}} sl@0: do_test loadext-1.2 { sl@0: db enable_load_extension 1 sl@0: sqlite3_load_extension db $testextension testloadext_init sl@0: catchsql { sl@0: SELECT half(1.0); sl@0: } sl@0: } {0 0.5} sl@0: sl@0: # Test that a second database connection (db2) can load the extension also. sl@0: # sl@0: do_test loadext-1.3 { sl@0: sqlite3 db2 test.db sl@0: sqlite3_enable_load_extension db2 1 sl@0: catchsql { sl@0: SELECT half(1.0); sl@0: } db2 sl@0: } {1 {no such function: half}} sl@0: do_test loadext-1.4 { sl@0: sqlite3_load_extension db2 $testextension testloadext_init sl@0: catchsql { sl@0: SELECT half(1.0); sl@0: } db2 sl@0: } {0 0.5} sl@0: sl@0: # Close the first database connection. Then check that the second database sl@0: # can still use the half() function without a problem. sl@0: # sl@0: do_test loadext-1.5 { sl@0: db close sl@0: catchsql { sl@0: SELECT half(1.0); sl@0: } db2 sl@0: } {0 0.5} sl@0: sl@0: db2 close sl@0: sqlite3 db test.db sl@0: sqlite3_enable_load_extension db 1 sl@0: sl@0: # Try to load an extension for which the file does not exist. sl@0: # sl@0: do_test loadext-2.1 { sl@0: file delete -force ${testextension}xx sl@0: set rc [catch { sl@0: sqlite3_load_extension db "${testextension}xx" sl@0: } msg] sl@0: list $rc $msg sl@0: } [list 1 [format $dlerror_nosuchfile ${testextension}xx]] sl@0: sl@0: # Try to load an extension for which the file is not a shared object sl@0: # sl@0: do_test loadext-2.2 { sl@0: set fd [open "${testextension}xx" w] sl@0: puts $fd blah sl@0: close $fd sl@0: set rc [catch { sl@0: sqlite3_load_extension db "${testextension}xx" sl@0: } msg] sl@0: list $rc $msg sl@0: } [list 1 [format $dlerror_notadll ${testextension}xx]] sl@0: sl@0: # Try to load an extension for which the file is present but the sl@0: # entry point is not. sl@0: # sl@0: do_test loadext-2.3 { sl@0: set rc [catch { sl@0: sqlite3_load_extension db $testextension icecream sl@0: } msg] sl@0: if {$::tcl_platform(os) eq "Darwin"} { sl@0: regsub {0x[1234567890abcdefABCDEF]*} $msg XXX msg sl@0: } sl@0: list $rc $msg sl@0: } [list 1 [format $dlerror_nosymbol $testextension icecream]] sl@0: sl@0: # Try to load an extension for which the entry point fails (returns non-zero) sl@0: # sl@0: do_test loadext-2.4 { sl@0: set rc [catch { sl@0: sqlite3_load_extension db $testextension testbrokenext_init sl@0: } msg] sl@0: list $rc $msg sl@0: } {1 {error during initialization: broken!}} sl@0: sl@0: ############################################################################ sl@0: # Tests for the load_extension() SQL function sl@0: # sl@0: sl@0: db close sl@0: sqlite3 db test.db sl@0: sqlite3_enable_load_extension db 1 sl@0: do_test loadext-3.1 { sl@0: catchsql { sl@0: SELECT half(5); sl@0: } sl@0: } {1 {no such function: half}} sl@0: do_test loadext-3.2 { sl@0: set res [catchsql { sl@0: SELECT load_extension($::testextension) sl@0: }] sl@0: if {$::tcl_platform(os) eq "Darwin"} { sl@0: regsub {0x[1234567890abcdefABCDEF]*} $res XXX res sl@0: } sl@0: set res sl@0: } [list 1 [format $dlerror_nosymbol $testextension sqlite3_extension_init]] sl@0: do_test loadext-3.3 { sl@0: catchsql { sl@0: SELECT load_extension($::testextension,'testloadext_init') sl@0: } sl@0: } {0 {{}}} sl@0: do_test loadext-3.4 { sl@0: catchsql { sl@0: SELECT half(5); sl@0: } sl@0: } {0 2.5} sl@0: do_test loadext-3.5 { sl@0: db eval { sl@0: SELECT sqlite3_status('MEMORY_USED') AS mused sl@0: } break sl@0: puts -nonewline " (memory_used=$mused) " sl@0: expr {$mused>0} sl@0: } {1} sl@0: do_test loadext-3.6 { sl@0: catchsql { sl@0: SELECT sqlite3_status('MEMORY_USED_X') AS mused sl@0: } sl@0: } {1 {unknown status property: MEMORY_USED_X}} sl@0: do_test loadext-3.7 { sl@0: catchsql { sl@0: SELECT sqlite3_status(4.53) AS mused sl@0: } sl@0: } {1 {unknown status type}} sl@0: do_test loadext-3.8 { sl@0: catchsql { sl@0: SELECT sqlite3_status(23) AS mused sl@0: } sl@0: } {1 {sqlite3_status(23,...) returns 21}} sl@0: sl@0: # Ticket #1863 sl@0: # Make sure the extension loading mechanism will not work unless it sl@0: # is explicitly enabled. sl@0: # sl@0: db close sl@0: sqlite3 db test.db sl@0: do_test loadext-4.1 { sl@0: catchsql { sl@0: SELECT load_extension($::testextension,'testloadext_init') sl@0: } sl@0: } {1 {not authorized}} sl@0: do_test loadext-4.2 { sl@0: sqlite3_enable_load_extension db 1 sl@0: catchsql { sl@0: SELECT load_extension($::testextension,'testloadext_init') sl@0: } sl@0: } {0 {{}}} sl@0: sl@0: do_test loadext-4.3 { sl@0: sqlite3_enable_load_extension db 0 sl@0: catchsql { sl@0: SELECT load_extension($::testextension,'testloadext_init') sl@0: } sl@0: } {1 {not authorized}} sl@0: sl@0: source $testdir/malloc_common.tcl sl@0: sl@0: sl@0: # Malloc failure in sqlite3_auto_extension and sqlite3_load_extension sl@0: # sl@0: do_malloc_test loadext-5 -tclprep { sl@0: sqlite3_reset_auto_extension sl@0: } -tclbody { sl@0: if {[autoinstall_test_functions]==7} {error "out of memory"} sl@0: } sl@0: do_malloc_test loadext-6 -tclbody { sl@0: db enable_load_extension 1 sl@0: sqlite3_load_extension db $::testextension testloadext_init sl@0: } sl@0: autoinstall_test_functions sl@0: sl@0: finish_test