3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing that SQLite can follow symbolic links.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set testprefix symlink
19 # This only runs on unix.
20 if {$::tcl_platform(platform)!="unix"} {
25 # Ensure that test.db has been created.
28 CREATE TABLE t1(x, y);
31 # Test that SQLite follows symlinks when opening files.
35 file link test.db2 test.db
37 sqlite3_db_filename db2 main
38 } [file join [pwd] test.db]
40 # But not with the -nofollow flag
44 set rc [catch {sqlite3 db2 test.db2 -nofollow 1} msg]
46 } {1 {unable to open database file}}
48 # If the main database is successfully opened with -nofollow, then -nofollow
49 # is also used for ATTACH.
53 sqlite3 db2 test.db -nofollow 1
56 catchsql {ATTACH 'test.db2' AS aux1;} db2
57 } {1 {unable to open database: test.db2}}
59 # Test that if the symlink points to a file that does not exists, it is
60 # created when it is opened.
73 sqlite3_db_filename db2 main
74 } [file join [pwd] test.db]
77 # Test that a loop of symlinks cannot be opened.
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}}
86 # Test that overly large paths cannot be opened.
89 set name "test.db[string repeat x 502]"
90 list [catch { sqlite3 db $name } msg] $msg
91 } {1 {unable to open database file}}
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}}
98 #-------------------------------------------------------------------------
99 # Test that journal and wal files are created next to the real file,
105 forcedelete test.db test.db2 test.db3
107 execsql { CREATE TABLE t1(x) }
108 file link test.db2 test.db
109 file link test.db3 test.db2
113 foreach {tn f} {1 test.db2 2 test.db3} {
116 file exists test.db-journal
121 INSERT INTO t1 VALUES(1);
123 file exists test.db-journal
124 } [expr [atomic_batch_write test.db]==0]
126 list [file exists test2.db-journal] [file exists test3.db-journal]
131 PRAGMA journal_mode = wal;
132 INSERT INTO t1 VALUES(2);
134 file exists test.db-wal
137 list [file exists test2.db-wal] [file exists test3.db-wal]
139 do_execsql_test 2.$tn.6 {
143 do_execsql_test 2.$tn.7 {
145 PRAGMA journal_mode = delete;
149 # Try to open a ridiculously long pathname. Bug found by
150 # Kostya Serebryany using libFuzzer on 2015-11-30.
154 catch {sqlite3 db [string repeat [string repeat x 100]/ 6]} res
156 } {unable to open database file}
158 #-------------------------------------------------------------------------
159 # Test that relative symlinks that are not located in the cwd work.
167 file link y/test.db ../x/test.db
168 file link z/test.db ../y/test.db
170 PRAGMA journal_mode = wal;
171 CREATE TABLE t1(x, y);
172 INSERT INTO t1 VALUES('hello', 'world');
179 db eval { SELECT * FROM t1 }
182 list [file exists x/test.db-wal] [file exists y/test.db-wal]
188 db eval { SELECT * FROM t1 }
191 list [file exists x/test.db-wal] [file exists y/test.db-wal] \
192 [file exists z/test.db-wal]
198 file link w/test.db [file join [pwd] x/test.db]
204 db eval { SELECT * FROM t1 }
207 list [file exists x/test.db-wal] [file exists w/test.db-wal]