Debian Codegraph @

test/symlink.test

raw
1 # 2015 October 31
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 file is testing that SQLite can follow symbolic links.
13 #
14
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set testprefix symlink
18
19 # This only runs on unix.
20 if {$::tcl_platform(os) eq "Windows NT"} {
21 finish_test
22 return
23 }
24
25 # Ensure that test.db has been created.
26 #
27 do_execsql_test 1.0 {
28 CREATE TABLE t1(x, y);
29 }
30
31 # Test that SQLite follows symlinks when opening files.
32 #
33 forcedelete test.db2
34 do_test 1.1 {
35 file link test.db2 test.db
36 sqlite3 db2 test.db2
37 sqlite3_db_filename db2 main
38 } [file join [pwd] test.db]
39
40 # But not with the -nofollow flag
41 #
42 do_test 1.1.2 {
43 db2 close
44 set rc [catch {sqlite3 db2 test.db2 -nofollow 1} msg]
45 lappend rc $msg
46 } {1 {unable to open database file}}
47
48 # If the main database is successfully opened with -nofollow, then -nofollow
49 # is also used for ATTACH.
50 #
51 do_test 1.1.3 {
52 catch {db2 close}
53 sqlite3 db2 test.db -nofollow 1
54 } {}
55 do_test 1.1.4 {
56 catchsql {ATTACH 'test.db2' AS aux1;} db2
57 } {1 {unable to open database: test.db2}}
58
59 # Test that if the symlink points to a file that does not exists, it is
60 # created when it is opened.
61 #
62 do_test 1.2.1 {
63 catch {db2 close}
64 db close
65 forcedelete test.db
66 file exists test.db
67 } 0
68 do_test 1.2.2 {
69 sqlite3 db2 test.db2
70 file exists test.db
71 } 1
72 do_test 1.2.3 {
73 sqlite3_db_filename db2 main
74 } [file join [pwd] test.db]
75 db2 close
76
77 # Test that a loop of symlinks cannot be opened.
78 #
79 do_test 1.3 {
80 forcedelete test.db
81 # Note: Tcl [file link] command is too smart to create loops of symlinks.
82 exec ln -s test.db2 test.db
83 list [catch { sqlite3 db test.db } msg] $msg
84 } {1 {unable to open database file}}
85
86 # Test that overly large paths cannot be opened.
87 #
88 do_test 1.4 {
89 set name "test.db[string repeat x 502]"
90 list [catch { sqlite3 db $name } msg] $msg
91 } {1 {unable to open database file}}
92 do_test 1.5 {
93 set r [expr 510 - [string length test.db] - [string length [pwd]]]
94 set name "test.db[string repeat x $r]"
95 list [catch { sqlite3 db $name } msg] $msg
96 } {1 {unable to open database file}}
97
98 #-------------------------------------------------------------------------
99 # Test that journal and wal files are created next to the real file,
100 # not the symlink.
101 #
102 do_test 2.0 {
103 catch { db close }
104 catch { db2 close }
105 forcedelete test.db test.db2 test.db3
106 sqlite3 db test.db
107 execsql { CREATE TABLE t1(x) }
108 file link test.db2 test.db
109 file link test.db3 test.db2
110 set {} {}
111 } {}
112
113 foreach {tn f} {1 test.db2 2 test.db3} {
114 do_test 2.$tn.1 {
115 sqlite3 db2 $f
116 file exists test.db-journal
117 } 0
118 do_test 2.$tn.2 {
119 execsql {
120 BEGIN;
121 INSERT INTO t1 VALUES(1);
122 } db2
123 file exists test.db-journal
124 } [expr [atomic_batch_write test.db]==0]
125 do_test 2.$tn.3 {
126 list [file exists test2.db-journal] [file exists test3.db-journal]
127 } {0 0}
128 do_test 2.$tn.4 {
129 execsql {
130 COMMIT;
131 PRAGMA journal_mode = wal;
132 INSERT INTO t1 VALUES(2);
133 } db2
134 file exists test.db-wal
135 } 1
136 do_test 2.$tn.5 {
137 list [file exists test2.db-wal] [file exists test3.db-wal]
138 } {0 0}
139 do_execsql_test 2.$tn.6 {
140 SELECT * FROM t1;
141 } {1 2}
142 db2 close
143 do_execsql_test 2.$tn.7 {
144 DELETE FROM t1;
145 PRAGMA journal_mode = delete;
146 } delete
147 }
148
149 # Try to open a ridiculously long pathname. Bug found by
150 # Kostya Serebryany using libFuzzer on 2015-11-30.
151 #
152 do_test 3.1 {
153 db close
154 catch {sqlite3 db [string repeat [string repeat x 100]/ 6]} res
155 set res
156 } {unable to open database file}
157
158 #-------------------------------------------------------------------------
159 # Test that relative symlinks that are not located in the cwd work.
160 #
161 do_test 4.1 {
162 forcedelete x y z
163 file mkdir x
164 file mkdir y
165 file mkdir z
166 sqlite3 db x/test.db
167 file link y/test.db ../x/test.db
168 file link z/test.db ../y/test.db
169 execsql {
170 PRAGMA journal_mode = wal;
171 CREATE TABLE t1(x, y);
172 INSERT INTO t1 VALUES('hello', 'world');
173 }
174 } {wal}
175
176 do_test 4.2.1 {
177 db close
178 sqlite3 db y/test.db
179 db eval { SELECT * FROM t1 }
180 } {hello world}
181 do_test 4.2.2 {
182 list [file exists x/test.db-wal] [file exists y/test.db-wal]
183 } {1 0}
184
185 do_test 4.3.1 {
186 db close
187 sqlite3 db z/test.db
188 db eval { SELECT * FROM t1 }
189 } {hello world}
190 do_test 4.3.2 {
191 list [file exists x/test.db-wal] [file exists y/test.db-wal] \
192 [file exists z/test.db-wal]
193 } {1 0 0}
194
195 do_test 4.4.0 {
196 forcedelete w
197 file mkdir w
198 file link w/test.db [file join [pwd] x/test.db]
199 set {} {}
200 } {}
201 do_test 4.4.1 {
202 db close
203 sqlite3 db w/test.db
204 db eval { SELECT * FROM t1 }
205 } {hello world}
206 do_test 4.4.2 {
207 list [file exists x/test.db-wal] [file exists w/test.db-wal]
208 } {1 0}
209
210 #-------------------------------------------------------------------------
211 # Check that extra ".." in a path are ignored.
212 reset_db
213 do_execsql_test 5.0 {
214 CREATE TABLE xyz(x, y, z);
215 INSERT INTO xyz VALUES(1, 2, 3);
216 }
217
218 set path [pwd]
219 set nLink [llength [split $path /]]
220 set path "[string repeat ../ [expr $nLink*2]]..${path}/test.db"
221
222 sqlite3 db2 $path
223 do_execsql_test -db db2 5.1 {
224 SELECT * FROM xyz;
225 } {1 2 3}
226 db close
227
228 forcedelete test.db2
229 file link test.db2 $path
230 sqlite3 db2 test.db2
231 do_execsql_test -db db2 5.2 {
232 SELECT * FROM xyz;
233 } {1 2 3}
234 forcedelete test.db2
235
236
237
238 finish_test