1# 2006 July 14 2# 3# The author disclaims copyright to this source code. In place of 4# 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. The 12# focus of this script is in-memory database backend. 13# 14# $Id: loadext.test,v 1.5 2006/06/27 15:16:16 drh Exp $ 15 16set testdir [file dirname $argv0] 17source $testdir/tester.tcl 18 19# The name of the test extension varies by operating system. 20# 21if {$::tcl_platform(platform) eq "windows"} { 22 set testextension ./testloadext.dll 23} else { 24 set testextension ./libtestloadext.so 25} 26 27# Make sure the test extension actually exists. If it does not 28# exist, try to create it. If unable to create it, then skip this 29# test file. 30# 31if {![file exists $testextension]} { 32 set srcdir [file dir $testdir]/src 33 set testextsrc $srcdir/test_loadext.c 34 if {[catch { 35 exec gcc -Wall -g -shared $testextsrc -o $testextension 36 } msg]} { 37 puts "Skipping loadext tests: Test extension not built..." 38 puts $msg 39 finish_test 40 return 41 } 42} 43 44# Test that loading the extension produces the expected results - adding 45# the half() function to the specified database handle. 46# 47do_test loadext-1.1 { 48 catchsql { 49 SELECT half(1.0); 50 } 51} {1 {no such function: half}} 52do_test loadext-1.2 { 53 sqlite3_enable_load_extension db 1 54 sqlite3_load_extension db $testextension testloadext_init 55 catchsql { 56 SELECT half(1.0); 57 } 58} {0 0.5} 59 60# Test that a second database connection (db2) can load the extension also. 61# 62do_test loadext-1.3 { 63 sqlite3 db2 test.db 64 sqlite3_enable_load_extension db2 1 65 catchsql { 66 SELECT half(1.0); 67 } db2 68} {1 {no such function: half}} 69do_test loadext-1.4 { 70 sqlite3_load_extension db2 $testextension testloadext_init 71 catchsql { 72 SELECT half(1.0); 73 } db2 74} {0 0.5} 75 76# Close the first database connection. Then check that the second database 77# can still use the half() function without a problem. 78# 79do_test loadext-1.5 { 80 db close 81 catchsql { 82 SELECT half(1.0); 83 } db2 84} {0 0.5} 85 86db2 close 87sqlite3 db test.db 88sqlite3_enable_load_extension db 1 89 90# Try to load an extension for which the file does not exist. 91# 92do_test loadext-2.1 { 93 set rc [catch { 94 sqlite3_load_extension db "${testextension}xx" 95 } msg] 96 list $rc $msg 97} [list 1 [subst -nocommands \ 98 {unable to open shared library [${testextension}xx]} 99]] 100 101# Try to load an extension for which the file is not a shared object 102# 103do_test loadext-2.2 { 104 set fd [open "${testextension}xx" w] 105 puts $fd blah 106 close $fd 107 set rc [catch { 108 sqlite3_load_extension db "${testextension}xx" 109 } msg] 110 list $rc $msg 111} [list 1 [subst -nocommands \ 112 {unable to open shared library [${testextension}xx]} 113]] 114 115# Try to load an extension for which the file is present but the 116# entry point is not. 117# 118do_test loadext-2.3 { 119 set rc [catch { 120 sqlite3_load_extension db $testextension icecream 121 } msg] 122 list $rc $msg 123} [list 1 [subst -nocommands \ 124 {no entry point [icecream] in shared library [$testextension]} 125]] 126 127# Try to load an extension for which the entry point fails (returns non-zero) 128# 129do_test loadext-2.4 { 130 set rc [catch { 131 sqlite3_load_extension db $testextension testbrokenext_init 132 } msg] 133 list $rc $msg 134} {1 {error during initialization: broken!}} 135 136############################################################################ 137# Tests for the load_extension() SQL function 138# 139 140db close 141sqlite3 db test.db 142sqlite3_enable_load_extension db 1 143do_test loadext-3.1 { 144 catchsql { 145 SELECT half(5); 146 } 147} {1 {no such function: half}} 148do_test loadext-3.2 { 149 catchsql { 150 SELECT load_extension($::testextension) 151 } 152} [list 1 "no entry point \[sqlite3_extension_init\]\ 153 in shared library \[$testextension\]"] 154do_test loadext-3.3 { 155 catchsql { 156 SELECT load_extension($::testextension,'testloadext_init') 157 } 158} {0 {{}}} 159do_test loadext-3.4 { 160 catchsql { 161 SELECT half(5); 162 } 163} {0 2.5} 164 165# Ticket #1863 166# Make sure the extension loading mechanism will not work unless it 167# is explicitly enabled. 168# 169db close 170sqlite3 db test.db 171do_test loadext-4.1 { 172 catchsql { 173 SELECT load_extension($::testextension,'testloadext_init') 174 } 175} {1 {not authorized}} 176do_test loadext-4.2 { 177 sqlite3_enable_load_extension db 1 178 catchsql { 179 SELECT load_extension($::testextension,'testloadext_init') 180 } 181} {0 {{}}} 182 183do_test loadext-4.3 { 184 sqlite3_enable_load_extension db 0 185 catchsql { 186 SELECT load_extension($::testextension,'testloadext_init') 187 } 188} {1 {not authorized}} 189 190 191 192finish_test 193