diff options
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/test/loadext.test')
-rw-r--r-- | libraries/sqlite/unix/sqlite-3.5.1/test/loadext.test | 218 |
1 files changed, 218 insertions, 0 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/test/loadext.test b/libraries/sqlite/unix/sqlite-3.5.1/test/loadext.test new file mode 100644 index 0000000..81e152f --- /dev/null +++ b/libraries/sqlite/unix/sqlite-3.5.1/test/loadext.test | |||
@@ -0,0 +1,218 @@ | |||
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.11 2007/09/01 06:19:06 danielk1977 Exp $ | ||
15 | |||
16 | set testdir [file dirname $argv0] | ||
17 | source $testdir/tester.tcl | ||
18 | |||
19 | ifcapable !load_ext { | ||
20 | finish_test | ||
21 | return | ||
22 | } | ||
23 | |||
24 | # The name of the test extension varies by operating system. | ||
25 | # | ||
26 | if {$::tcl_platform(platform) eq "windows"} { | ||
27 | set testextension ./testloadext.dll | ||
28 | } else { | ||
29 | set testextension ./libtestloadext.so | ||
30 | } | ||
31 | |||
32 | # The error messages tested by this file are operating system dependent | ||
33 | # (because they are returned by sqlite3OsDlError()). For now, they only | ||
34 | # work with UNIX (and probably only certain kinds of UNIX). | ||
35 | # | ||
36 | # When a shared-object cannot be opened because it does not exist, the | ||
37 | # format of the message returned is: | ||
38 | # | ||
39 | # [format $dlerror_nosuchfile <shared-object-name>] | ||
40 | # | ||
41 | # When a shared-object cannot be opened because it consists of the 4 | ||
42 | # characters "blah" only, we expect the error message to be: | ||
43 | # | ||
44 | # [format $dlerror_notadll <shared-object-name>] | ||
45 | # | ||
46 | # When a symbol cannot be found within an open shared-object, the error | ||
47 | # message should be: | ||
48 | # | ||
49 | # [format $dlerror_nosymbol <shared-object-name> <symbol-name>] | ||
50 | # | ||
51 | # The exact error messages are not important. The important bit is | ||
52 | # that SQLite is correctly copying the message from xDlError(). | ||
53 | # | ||
54 | set dlerror_nosuchfile \ | ||
55 | {%s: cannot open shared object file: No such file or directory} | ||
56 | set dlerror_notadll {%s: file too short} | ||
57 | set dlerror_nosymbol {%s: undefined symbol: %s} | ||
58 | |||
59 | # Make sure the test extension actually exists. If it does not | ||
60 | # exist, try to create it. If unable to create it, then skip this | ||
61 | # test file. | ||
62 | # | ||
63 | if {![file exists $testextension]} { | ||
64 | set srcdir [file dir $testdir]/src | ||
65 | set testextsrc $srcdir/test_loadext.c | ||
66 | if {[catch { | ||
67 | exec gcc -Wall -I$srcdir -I. -g -shared $testextsrc -o $testextension | ||
68 | } msg]} { | ||
69 | puts "Skipping loadext tests: Test extension not built..." | ||
70 | puts $msg | ||
71 | finish_test | ||
72 | return | ||
73 | } | ||
74 | } | ||
75 | |||
76 | # Test that loading the extension produces the expected results - adding | ||
77 | # the half() function to the specified database handle. | ||
78 | # | ||
79 | do_test loadext-1.1 { | ||
80 | catchsql { | ||
81 | SELECT half(1.0); | ||
82 | } | ||
83 | } {1 {no such function: half}} | ||
84 | do_test loadext-1.2 { | ||
85 | db enable_load_extension 1 | ||
86 | sqlite3_load_extension db $testextension testloadext_init | ||
87 | catchsql { | ||
88 | SELECT half(1.0); | ||
89 | } | ||
90 | } {0 0.5} | ||
91 | |||
92 | # Test that a second database connection (db2) can load the extension also. | ||
93 | # | ||
94 | do_test loadext-1.3 { | ||
95 | sqlite3 db2 test.db | ||
96 | sqlite3_enable_load_extension db2 1 | ||
97 | catchsql { | ||
98 | SELECT half(1.0); | ||
99 | } db2 | ||
100 | } {1 {no such function: half}} | ||
101 | do_test loadext-1.4 { | ||
102 | sqlite3_load_extension db2 $testextension testloadext_init | ||
103 | catchsql { | ||
104 | SELECT half(1.0); | ||
105 | } db2 | ||
106 | } {0 0.5} | ||
107 | |||
108 | # Close the first database connection. Then check that the second database | ||
109 | # can still use the half() function without a problem. | ||
110 | # | ||
111 | do_test loadext-1.5 { | ||
112 | db close | ||
113 | catchsql { | ||
114 | SELECT half(1.0); | ||
115 | } db2 | ||
116 | } {0 0.5} | ||
117 | |||
118 | db2 close | ||
119 | sqlite3 db test.db | ||
120 | sqlite3_enable_load_extension db 1 | ||
121 | |||
122 | # Try to load an extension for which the file does not exist. | ||
123 | # | ||
124 | do_test loadext-2.1 { | ||
125 | file delete -force ${testextension}xx | ||
126 | set rc [catch { | ||
127 | sqlite3_load_extension db "${testextension}xx" | ||
128 | } msg] | ||
129 | list $rc $msg | ||
130 | } [list 1 [format $dlerror_nosuchfile ${testextension}xx]] | ||
131 | |||
132 | # Try to load an extension for which the file is not a shared object | ||
133 | # | ||
134 | do_test loadext-2.2 { | ||
135 | set fd [open "${testextension}xx" w] | ||
136 | puts $fd blah | ||
137 | close $fd | ||
138 | set rc [catch { | ||
139 | sqlite3_load_extension db "${testextension}xx" | ||
140 | } msg] | ||
141 | list $rc $msg | ||
142 | } [list 1 [format $dlerror_notadll ${testextension}xx]] | ||
143 | |||
144 | # Try to load an extension for which the file is present but the | ||
145 | # entry point is not. | ||
146 | # | ||
147 | do_test loadext-2.3 { | ||
148 | set rc [catch { | ||
149 | sqlite3_load_extension db $testextension icecream | ||
150 | } msg] | ||
151 | list $rc $msg | ||
152 | } [list 1 [format $dlerror_nosymbol $testextension icecream]] | ||
153 | |||
154 | # Try to load an extension for which the entry point fails (returns non-zero) | ||
155 | # | ||
156 | do_test loadext-2.4 { | ||
157 | set rc [catch { | ||
158 | sqlite3_load_extension db $testextension testbrokenext_init | ||
159 | } msg] | ||
160 | list $rc $msg | ||
161 | } {1 {error during initialization: broken!}} | ||
162 | |||
163 | ############################################################################ | ||
164 | # Tests for the load_extension() SQL function | ||
165 | # | ||
166 | |||
167 | db close | ||
168 | sqlite3 db test.db | ||
169 | sqlite3_enable_load_extension db 1 | ||
170 | do_test loadext-3.1 { | ||
171 | catchsql { | ||
172 | SELECT half(5); | ||
173 | } | ||
174 | } {1 {no such function: half}} | ||
175 | do_test loadext-3.2 { | ||
176 | catchsql { | ||
177 | SELECT load_extension($::testextension) | ||
178 | } | ||
179 | } [list 1 [format $dlerror_nosymbol $testextension sqlite3_extension_init]] | ||
180 | do_test loadext-3.3 { | ||
181 | catchsql { | ||
182 | SELECT load_extension($::testextension,'testloadext_init') | ||
183 | } | ||
184 | } {0 {{}}} | ||
185 | do_test loadext-3.4 { | ||
186 | catchsql { | ||
187 | SELECT half(5); | ||
188 | } | ||
189 | } {0 2.5} | ||
190 | |||
191 | # Ticket #1863 | ||
192 | # Make sure the extension loading mechanism will not work unless it | ||
193 | # is explicitly enabled. | ||
194 | # | ||
195 | db close | ||
196 | sqlite3 db test.db | ||
197 | do_test loadext-4.1 { | ||
198 | catchsql { | ||
199 | SELECT load_extension($::testextension,'testloadext_init') | ||
200 | } | ||
201 | } {1 {not authorized}} | ||
202 | do_test loadext-4.2 { | ||
203 | sqlite3_enable_load_extension db 1 | ||
204 | catchsql { | ||
205 | SELECT load_extension($::testextension,'testloadext_init') | ||
206 | } | ||
207 | } {0 {{}}} | ||
208 | |||
209 | do_test loadext-4.3 { | ||
210 | sqlite3_enable_load_extension db 0 | ||
211 | catchsql { | ||
212 | SELECT load_extension($::testextension,'testloadext_init') | ||
213 | } | ||
214 | } {1 {not authorized}} | ||
215 | |||
216 | |||
217 | |||
218 | finish_test | ||