sqlcipher/test/loadext.test

300 lines
7.9 KiB
Plaintext
Raw Permalink Normal View History

2008-07-30 13:15:43 +00:00
# 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.
#
2009-04-21 14:39:00 +00:00
# $Id: loadext.test,v 1.17 2009/03/20 09:09:37 danielk1977 Exp $
2008-07-30 13:15:43 +00:00
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.
#
2012-11-15 23:45:58 +00:00
if {$::tcl_platform(platform) eq "windows"} {
2008-07-30 13:15:43 +00:00
set testextension ./testloadext.dll
} else {
set testextension ./libtestloadext.so
}
2009-02-20 20:33:35 +00:00
set gcc_shared "-shared -fPIC"
2008-07-30 13:15:43 +00:00
if {$::tcl_platform(os) eq "Darwin"} {
set gcc_shared -dynamiclib
}
# 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}
if {$::tcl_platform(os) eq "Darwin"} {
2018-12-13 00:43:10 +00:00
set dlerror_nosuchfile {dlopen.%s, 10.: .*image.*found.*}
set dlerror_notadll {dlopen.%1$s, 10.: .*image.*found.*}
set dlerror_nosymbol {dlsym.XXX, %2$s.: symbol not found}
2008-07-30 13:15:43 +00:00
}
2014-04-04 15:16:20 +00:00
if {$::tcl_platform(platform) eq "windows"} {
set dlerror_nosuchfile {The specified module could not be found.*}
set dlerror_notadll {%%1 is not a valid Win32 application.*}
set dlerror_nosymbol {The specified procedure could not be found.*}
}
2008-07-30 13:15:43 +00:00
# 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
2009-04-21 14:39:00 +00:00
set cmdline [concat exec gcc $gcc_shared]
2016-12-05 20:29:15 +00:00
lappend cmdline -Wall -I$srcdir -I. -I.. -g $testextsrc -o $testextension
2009-04-21 14:39:00 +00:00
if {[catch $cmdline msg]} {
2008-07-30 13:15:43 +00:00
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
2016-12-05 20:29:15 +00:00
sqlite3_db_config db2 SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1
2008-07-30 13:15:43 +00:00
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 {
2011-12-26 19:52:17 +00:00
forcedelete ${testextension}xx
2008-07-30 13:15:43 +00:00
set rc [catch {
sqlite3_load_extension db "${testextension}xx"
} msg]
list $rc $msg
2013-05-21 20:38:11 +00:00
} /[list 1 [format $dlerror_nosuchfile ${testextension}xx.*]]/
2008-07-30 13:15:43 +00:00
# Try to load an extension for which the file is not a shared object
#
do_test loadext-2.2 {
2013-05-21 20:38:11 +00:00
set fd [open "./notasharedlib.so" w]
puts $fd blah
close $fd
set fd [open "./notasharedlib.dll" w]
2008-07-30 13:15:43 +00:00
puts $fd blah
close $fd
set rc [catch {
2013-05-21 20:38:11 +00:00
sqlite3_load_extension db "./notasharedlib"
2008-07-30 13:15:43 +00:00
} msg]
2013-05-21 20:38:11 +00:00
list $rc $msg
} /[list 1 [format $dlerror_notadll ./notasharedlib.*]]/
2008-07-30 13:15:43 +00:00
# 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]
if {$::tcl_platform(os) eq "Darwin"} {
regsub {0x[1234567890abcdefABCDEF]*} $msg XXX msg
}
list $rc $msg
2014-04-04 15:16:20 +00:00
} /[list 1 [format $dlerror_nosymbol $testextension icecream]]/
2008-07-30 13:15:43 +00:00
# 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 {
set res [catchsql {
SELECT load_extension($::testextension)
}]
if {$::tcl_platform(os) eq "Darwin"} {
regsub {0x[1234567890abcdefABCDEF]*} $res XXX res
}
set res
2013-05-21 20:38:11 +00:00
} /[list 1 [format $dlerror_nosymbol $testextension sqlite3_.*_init]]/
2008-07-30 13:15:43 +00:00
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}
do_test loadext-3.5 {
db eval {
SELECT sqlite3_status('MEMORY_USED') AS mused
} break
puts -nonewline " (memory_used=$mused) "
expr {$mused>0}
} {1}
do_test loadext-3.6 {
catchsql {
SELECT sqlite3_status('MEMORY_USED_X') AS mused
}
} {1 {unknown status property: MEMORY_USED_X}}
do_test loadext-3.7 {
catchsql {
SELECT sqlite3_status(4.53) AS mused
}
} {1 {unknown status type}}
do_test loadext-3.8 {
catchsql {
SELECT sqlite3_status(23) AS mused
}
} {1 {sqlite3_status(23,...) returns 21}}
# 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 {{}}}
2016-12-05 20:29:15 +00:00
# disable all extension loading
2008-07-30 13:15:43 +00:00
do_test loadext-4.3 {
sqlite3_enable_load_extension db 0
catchsql {
SELECT load_extension($::testextension,'testloadext_init')
}
} {1 {not authorized}}
2016-12-05 20:29:15 +00:00
# enable C-api extension loading only. Show that the SQL function
# still does not work.
do_test loadext-4.4 {
sqlite3_db_config db SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1
catchsql {
SELECT load_extension($::testextension,'testloadext_init')
}
} {1 {not authorized}}
source $testdir/malloc_common.tcl
2008-07-30 13:15:43 +00:00
# Malloc failure in sqlite3_auto_extension and sqlite3_load_extension
#
do_malloc_test loadext-5 -tclprep {
sqlite3_reset_auto_extension
} -tclbody {
if {[autoinstall_test_functions]==7} {error "out of memory"}
}
2014-04-04 15:16:20 +00:00
# On Windows, this malloc test must be skipped because the winDlOpen
# function itself can fail due to "out of memory" conditions.
#
if {$::tcl_platform(platform) ne "windows"} {
do_malloc_test loadext-6 -tclbody {
db enable_load_extension 1
sqlite3_load_extension db $::testextension testloadext_init
}
}
2014-04-04 15:16:20 +00:00
autoinstall_test_functions
2008-07-30 13:15:43 +00:00
finish_test