blob: 98b2a32c0e8d4e4493c0865adb87f635aebe7716 [file] [log] [blame]
dan245fdc62015-10-31 17:58:33 +00001# 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
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17set testprefix symlink
18
19# This only runs on unix.
20if {$::tcl_platform(platform)!="unix"} {
21 finish_test
22 return
23}
24
25# Ensure that test.db has been created.
26#
27do_execsql_test 1.0 {
28 CREATE TABLE t1(x, y);
29}
30
31# Test that SQLite follows symlinks when opening files.
32#
33forcedelete test.db2
34do_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
drh0933aad2019-11-18 17:46:38 +000040# But not with the -nofollow flag
41#
42do_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#
51do_test 1.1.3 {
52 catch {db2 close}
53 sqlite3 db2 test.db -nofollow 1
54} {}
55do_test 1.1.4 {
56 catchsql {ATTACH 'test.db2' AS aux1;} db2
57} {1 {unable to open database: test.db2}}
58
dan245fdc62015-10-31 17:58:33 +000059# Test that if the symlink points to a file that does not exists, it is
60# created when it is opened.
61#
62do_test 1.2.1 {
drh0933aad2019-11-18 17:46:38 +000063 catch {db2 close}
dan245fdc62015-10-31 17:58:33 +000064 db close
65 forcedelete test.db
66 file exists test.db
67} 0
68do_test 1.2.2 {
69 sqlite3 db2 test.db2
70 file exists test.db
71} 1
72do_test 1.2.3 {
73 sqlite3_db_filename db2 main
74} [file join [pwd] test.db]
75db2 close
76
77# Test that a loop of symlinks cannot be opened.
78#
79do_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#
88do_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}}
92do_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#
102do_test 2.0 {
103 catch { db close }
104 catch { db2 close }
dane88ec182016-01-25 17:04:48 +0000105 forcedelete test.db test.db2 test.db3
dan245fdc62015-10-31 17:58:33 +0000106 sqlite3 db test.db
107 execsql { CREATE TABLE t1(x) }
108 file link test.db2 test.db
dane88ec182016-01-25 17:04:48 +0000109 file link test.db3 test.db2
110 set {} {}
111} {}
dan245fdc62015-10-31 17:58:33 +0000112
dane88ec182016-01-25 17:04:48 +0000113foreach {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
dan69aedc82018-01-13 13:07:49 +0000124 } [expr [atomic_batch_write test.db]==0]
dane88ec182016-01-25 17:04:48 +0000125 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}
dan245fdc62015-10-31 17:58:33 +0000148
drh025d2f72015-11-30 22:22:23 +0000149# Try to open a ridiculously long pathname. Bug found by
150# Kostya Serebryany using libFuzzer on 2015-11-30.
151#
152do_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
dane88ec182016-01-25 17:04:48 +0000158#-------------------------------------------------------------------------
159# Test that relative symlinks that are not located in the cwd work.
160#
161do_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
176do_test 4.2.1 {
177 db close
178 sqlite3 db y/test.db
179 db eval { SELECT * FROM t1 }
180} {hello world}
181do_test 4.2.2 {
182 list [file exists x/test.db-wal] [file exists y/test.db-wal]
183} {1 0}
184
185do_test 4.3.1 {
186 db close
187 sqlite3 db z/test.db
188 db eval { SELECT * FROM t1 }
189} {hello world}
190do_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
195do_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} {}
201do_test 4.4.1 {
202 db close
dane88ec182016-01-25 17:04:48 +0000203 sqlite3 db w/test.db
204 db eval { SELECT * FROM t1 }
205} {hello world}
206do_test 4.4.2 {
207 list [file exists x/test.db-wal] [file exists w/test.db-wal]
208} {1 0}
drh025d2f72015-11-30 22:22:23 +0000209
dan245fdc62015-10-31 17:58:33 +0000210finish_test