xref: /sqlite-3.40.0/test/loadext.test (revision fdb83b2f)
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.3 2006/06/17 14:12:48 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 -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_load_extension db $testextension testloadext_init
54  catchsql {
55    SELECT half(1.0);
56  }
57} {0 0.5}
58
59# Test that a second database connection (db2) can load the extension also.
60#
61do_test loadext-1.3 {
62  sqlite3 db2 test.db
63  catchsql {
64    SELECT half(1.0);
65  } db2
66} {1 {no such function: half}}
67do_test loadext-1.4 {
68  sqlite3_load_extension db2 $testextension testloadext_init
69  catchsql {
70    SELECT half(1.0);
71  } db2
72} {0 0.5}
73
74# Close the first database connection. Then check that the second database
75# can still use the half() function without a problem.
76#
77do_test loadext-1.5 {
78  db close
79  catchsql {
80    SELECT half(1.0);
81  } db2
82} {0 0.5}
83
84db2 close
85sqlite3 db test.db
86
87# Try to load an extension for which the file does not exist.
88#
89do_test loadext-2.1 {
90  set rc [catch {
91    sqlite3_load_extension db "${testextension}xx"
92  } msg]
93  list $rc $msg
94} [list 1 [subst -nocommands \
95  {unable to open shared library [${testextension}xx]}
96]]
97
98# Try to load an extension for which the file is not a shared object
99#
100do_test loadext-2.2 {
101  set fd [open "${testextension}xx" w]
102  puts $fd blah
103  close $fd
104  set rc [catch {
105    sqlite3_load_extension db "${testextension}xx"
106  } msg]
107  list $rc $msg
108} [list 1 [subst -nocommands \
109  {unable to open shared library [${testextension}xx]}
110]]
111
112# Try to load an extension for which the file is present but the
113# entry point is not.
114#
115do_test loadext-2.3 {
116  set rc [catch {
117    sqlite3_load_extension db $testextension icecream
118  } msg]
119  list $rc $msg
120} [list 1 [subst -nocommands \
121  {no entry point [icecream] in shared library [$testextension]}
122]]
123
124# Try to load an extension for which the entry point fails (returns non-zero)
125#
126do_test loadext-2.4 {
127  set rc [catch {
128    sqlite3_load_extension db $testextension testbrokenext_init
129  } msg]
130  list $rc $msg
131} {1 {error during initialization: broken!}}
132
133############################################################################
134# Tests for the load_extension() SQL function
135#
136
137db close
138sqlite3 db test.db
139do_test loadext-3.1 {
140  catchsql {
141    SELECT half(5);
142  }
143} {1 {no such function: half}}
144do_test loadext-3.2 {
145  catchsql {
146    SELECT load_extension($::testextension)
147  }
148} [list 1 "no entry point \[sqlite3_extension_init\]\
149           in shared library \[$testextension\]"]
150do_test loadext-3.3 {
151  catchsql {
152    SELECT load_extension($::testextension,'testloadext_init')
153  }
154} {0 {{}}}
155do_test loadext-3.4 {
156  catchsql {
157    SELECT half(5);
158  }
159} {0 2.5}
160
161finish_test
162