sqlite/test/loadext.test
danielk1977 c3f759bd33 Fix a bug in loadext.test. (CVS 4359)
FossilOrigin-Name: 2be8af9d402a5cd8da8c348883188eb38900f6dc
2007-09-01 06:19:05 +00:00

219 lines
5.6 KiB
Plaintext

# 2006 July 14
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this script is extension loading.
#
# $Id: loadext.test,v 1.11 2007/09/01 06:19:06 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
ifcapable !load_ext {
finish_test
return
}
# The name of the test extension varies by operating system.
#
if {$::tcl_platform(platform) eq "windows"} {
set testextension ./testloadext.dll
} else {
set testextension ./libtestloadext.so
}
# The error messages tested by this file are operating system dependent
# (because they are returned by sqlite3OsDlError()). For now, they only
# work with UNIX (and probably only certain kinds of UNIX).
#
# When a shared-object cannot be opened because it does not exist, the
# format of the message returned is:
#
# [format $dlerror_nosuchfile <shared-object-name>]
#
# When a shared-object cannot be opened because it consists of the 4
# characters "blah" only, we expect the error message to be:
#
# [format $dlerror_notadll <shared-object-name>]
#
# When a symbol cannot be found within an open shared-object, the error
# message should be:
#
# [format $dlerror_nosymbol <shared-object-name> <symbol-name>]
#
# The exact error messages are not important. The important bit is
# that SQLite is correctly copying the message from xDlError().
#
set dlerror_nosuchfile \
{%s: cannot open shared object file: No such file or directory}
set dlerror_notadll {%s: file too short}
set dlerror_nosymbol {%s: undefined symbol: %s}
# Make sure the test extension actually exists. If it does not
# exist, try to create it. If unable to create it, then skip this
# test file.
#
if {![file exists $testextension]} {
set srcdir [file dir $testdir]/src
set testextsrc $srcdir/test_loadext.c
if {[catch {
exec gcc -Wall -I$srcdir -I. -g -shared $testextsrc -o $testextension
} msg]} {
puts "Skipping loadext tests: Test extension not built..."
puts $msg
finish_test
return
}
}
# Test that loading the extension produces the expected results - adding
# the half() function to the specified database handle.
#
do_test loadext-1.1 {
catchsql {
SELECT half(1.0);
}
} {1 {no such function: half}}
do_test loadext-1.2 {
db enable_load_extension 1
sqlite3_load_extension db $testextension testloadext_init
catchsql {
SELECT half(1.0);
}
} {0 0.5}
# Test that a second database connection (db2) can load the extension also.
#
do_test loadext-1.3 {
sqlite3 db2 test.db
sqlite3_enable_load_extension db2 1
catchsql {
SELECT half(1.0);
} db2
} {1 {no such function: half}}
do_test loadext-1.4 {
sqlite3_load_extension db2 $testextension testloadext_init
catchsql {
SELECT half(1.0);
} db2
} {0 0.5}
# Close the first database connection. Then check that the second database
# can still use the half() function without a problem.
#
do_test loadext-1.5 {
db close
catchsql {
SELECT half(1.0);
} db2
} {0 0.5}
db2 close
sqlite3 db test.db
sqlite3_enable_load_extension db 1
# Try to load an extension for which the file does not exist.
#
do_test loadext-2.1 {
file delete -force ${testextension}xx
set rc [catch {
sqlite3_load_extension db "${testextension}xx"
} msg]
list $rc $msg
} [list 1 [format $dlerror_nosuchfile ${testextension}xx]]
# Try to load an extension for which the file is not a shared object
#
do_test loadext-2.2 {
set fd [open "${testextension}xx" w]
puts $fd blah
close $fd
set rc [catch {
sqlite3_load_extension db "${testextension}xx"
} msg]
list $rc $msg
} [list 1 [format $dlerror_notadll ${testextension}xx]]
# Try to load an extension for which the file is present but the
# entry point is not.
#
do_test loadext-2.3 {
set rc [catch {
sqlite3_load_extension db $testextension icecream
} msg]
list $rc $msg
} [list 1 [format $dlerror_nosymbol $testextension icecream]]
# Try to load an extension for which the entry point fails (returns non-zero)
#
do_test loadext-2.4 {
set rc [catch {
sqlite3_load_extension db $testextension testbrokenext_init
} msg]
list $rc $msg
} {1 {error during initialization: broken!}}
############################################################################
# Tests for the load_extension() SQL function
#
db close
sqlite3 db test.db
sqlite3_enable_load_extension db 1
do_test loadext-3.1 {
catchsql {
SELECT half(5);
}
} {1 {no such function: half}}
do_test loadext-3.2 {
catchsql {
SELECT load_extension($::testextension)
}
} [list 1 [format $dlerror_nosymbol $testextension sqlite3_extension_init]]
do_test loadext-3.3 {
catchsql {
SELECT load_extension($::testextension,'testloadext_init')
}
} {0 {{}}}
do_test loadext-3.4 {
catchsql {
SELECT half(5);
}
} {0 2.5}
# Ticket #1863
# Make sure the extension loading mechanism will not work unless it
# is explicitly enabled.
#
db close
sqlite3 db test.db
do_test loadext-4.1 {
catchsql {
SELECT load_extension($::testextension,'testloadext_init')
}
} {1 {not authorized}}
do_test loadext-4.2 {
sqlite3_enable_load_extension db 1
catchsql {
SELECT load_extension($::testextension,'testloadext_init')
}
} {0 {{}}}
do_test loadext-4.3 {
sqlite3_enable_load_extension db 0
catchsql {
SELECT load_extension($::testextension,'testloadext_init')
}
} {1 {not authorized}}
finish_test