xref: /sqlite-3.40.0/test/loadext.test (revision e7c8a5c7)
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 extension loading.
13#
14# $Id: loadext.test,v 1.15 2008/08/22 13:57:39 pweilbacher Exp $
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19ifcapable !load_ext {
20  finish_test
21  return
22}
23
24# The name of the test extension varies by operating system.
25#
26if {$::tcl_platform(platform) eq "windows" || $::tcl_platform(platform) eq "os2"} {
27  set testextension ./testloadext.dll
28} else {
29  set testextension ./libtestloadext.so
30}
31set gcc_shared -shared
32if {$::tcl_platform(os) eq "Darwin"} {
33  set gcc_shared -dynamiclib
34}
35
36# The error messages tested by this file are operating system dependent
37# (because they are returned by sqlite3OsDlError()). For now, they only
38# work with UNIX (and probably only certain kinds of UNIX).
39#
40# When a shared-object cannot be opened because it does not exist, the
41# 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 4
46# 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 error
51# 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 is
56# 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 not found}
65  set dlerror_notadll    {dlopen(%1$s, 10): no suitable image found.  Did find:
66	%1$s: file to short}
67  set dlerror_nosymbol   {dlsym(XXX, %2$s): symbol not found}
68}
69
70# Make sure the test extension actually exists.  If it does not
71# exist, try to create it.  If unable to create it, then skip this
72# test file.
73#
74if {![file exists $testextension]} {
75  set srcdir [file dir $testdir]/src
76  set testextsrc $srcdir/test_loadext.c
77  if {[catch {
78    exec gcc $gcc_shared -Wall -I$srcdir -I. -g $testextsrc -o $testextension
79  } msg]} {
80    puts "Skipping loadext tests: Test extension not built..."
81    puts $msg
82    finish_test
83    return
84  }
85}
86
87# Test that loading the extension produces the expected results - adding
88# the half() function to the specified database handle.
89#
90do_test loadext-1.1 {
91  catchsql {
92    SELECT half(1.0);
93  }
94} {1 {no such function: half}}
95do_test loadext-1.2 {
96  db enable_load_extension 1
97  sqlite3_load_extension db $testextension testloadext_init
98  catchsql {
99    SELECT half(1.0);
100  }
101} {0 0.5}
102
103# Test that a second database connection (db2) can load the extension also.
104#
105do_test loadext-1.3 {
106  sqlite3 db2 test.db
107  sqlite3_enable_load_extension db2 1
108  catchsql {
109    SELECT half(1.0);
110  } db2
111} {1 {no such function: half}}
112do_test loadext-1.4 {
113  sqlite3_load_extension db2 $testextension testloadext_init
114  catchsql {
115    SELECT half(1.0);
116  } db2
117} {0 0.5}
118
119# Close the first database connection. Then check that the second database
120# can still use the half() function without a problem.
121#
122do_test loadext-1.5 {
123  db close
124  catchsql {
125    SELECT half(1.0);
126  } db2
127} {0 0.5}
128
129db2 close
130sqlite3 db test.db
131sqlite3_enable_load_extension db 1
132
133# Try to load an extension for which the file does not exist.
134#
135do_test loadext-2.1 {
136  file delete -force ${testextension}xx
137  set rc [catch {
138    sqlite3_load_extension db "${testextension}xx"
139  } msg]
140  list $rc $msg
141} [list 1 [format $dlerror_nosuchfile ${testextension}xx]]
142
143# Try to load an extension for which the file is not a shared object
144#
145do_test loadext-2.2 {
146  set fd [open "${testextension}xx" w]
147  puts $fd blah
148  close $fd
149  set rc [catch {
150    sqlite3_load_extension db "${testextension}xx"
151  } msg]
152  list $rc $msg
153} [list 1 [format $dlerror_notadll ${testextension}xx]]
154
155# Try to load an extension for which the file is present but the
156# entry point is not.
157#
158do_test loadext-2.3 {
159  set rc [catch {
160    sqlite3_load_extension db $testextension icecream
161  } msg]
162  if {$::tcl_platform(os) eq "Darwin"} {
163    regsub {0x[1234567890abcdefABCDEF]*} $msg XXX msg
164  }
165  list $rc $msg
166} [list 1 [format $dlerror_nosymbol $testextension icecream]]
167
168# Try to load an extension for which the entry point fails (returns non-zero)
169#
170do_test loadext-2.4 {
171  set rc [catch {
172    sqlite3_load_extension db $testextension testbrokenext_init
173  } msg]
174  list $rc $msg
175} {1 {error during initialization: broken!}}
176
177############################################################################
178# Tests for the load_extension() SQL function
179#
180
181db close
182sqlite3 db test.db
183sqlite3_enable_load_extension db 1
184do_test loadext-3.1 {
185  catchsql {
186    SELECT half(5);
187  }
188} {1 {no such function: half}}
189do_test loadext-3.2 {
190  set res [catchsql {
191    SELECT load_extension($::testextension)
192  }]
193  if {$::tcl_platform(os) eq "Darwin"} {
194    regsub {0x[1234567890abcdefABCDEF]*} $res XXX res
195  }
196  set res
197} [list 1 [format $dlerror_nosymbol $testextension sqlite3_extension_init]]
198do_test loadext-3.3 {
199  catchsql {
200    SELECT load_extension($::testextension,'testloadext_init')
201  }
202} {0 {{}}}
203do_test loadext-3.4 {
204  catchsql {
205    SELECT half(5);
206  }
207} {0 2.5}
208do_test loadext-3.5 {
209  db eval {
210    SELECT sqlite3_status('MEMORY_USED') AS mused
211  } break
212  puts -nonewline " (memory_used=$mused) "
213  expr {$mused>0}
214} {1}
215do_test loadext-3.6 {
216  catchsql {
217    SELECT sqlite3_status('MEMORY_USED_X') AS mused
218  }
219} {1 {unknown status property: MEMORY_USED_X}}
220do_test loadext-3.7 {
221  catchsql {
222    SELECT sqlite3_status(4.53) AS mused
223  }
224} {1 {unknown status type}}
225do_test loadext-3.8 {
226  catchsql {
227    SELECT sqlite3_status(23) AS mused
228  }
229} {1 {sqlite3_status(23,...) returns 21}}
230
231# Ticket #1863
232# Make sure the extension loading mechanism will not work unless it
233# is explicitly enabled.
234#
235db close
236sqlite3 db test.db
237do_test loadext-4.1 {
238  catchsql {
239    SELECT load_extension($::testextension,'testloadext_init')
240  }
241} {1 {not authorized}}
242do_test loadext-4.2 {
243  sqlite3_enable_load_extension db 1
244  catchsql {
245    SELECT load_extension($::testextension,'testloadext_init')
246  }
247} {0 {{}}}
248
249do_test loadext-4.3 {
250  sqlite3_enable_load_extension db 0
251  catchsql {
252    SELECT load_extension($::testextension,'testloadext_init')
253  }
254} {1 {not authorized}}
255
256source $testdir/malloc_common.tcl
257
258
259# Malloc failure in sqlite3_auto_extension and sqlite3_load_extension
260#
261do_malloc_test loadext-5 -tclprep {
262  sqlite3_reset_auto_extension
263} -tclbody {
264  if {[autoinstall_test_functions]==7} {error "out of memory"}
265}
266do_malloc_test loadext-6 -tclbody {
267  db enable_load_extension 1
268  sqlite3_load_extension db $::testextension testloadext_init
269}
270autoinstall_test_functions
271
272finish_test
273