xref: /sqlite-3.40.0/test/symlink.test (revision 38d69855)
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
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
40# Test that if the symlink points to a file that does not exists, it is
41# created when it is opened.
42#
43do_test 1.2.1 {
44  db2 close
45  db close
46  forcedelete test.db
47  file exists test.db
48} 0
49do_test 1.2.2 {
50  sqlite3 db2 test.db2
51  file exists test.db
52} 1
53do_test 1.2.3 {
54  sqlite3_db_filename db2 main
55} [file join [pwd] test.db]
56db2 close
57
58# Test that a loop of symlinks cannot be opened.
59#
60do_test 1.3 {
61  forcedelete test.db
62  # Note: Tcl [file link] command is too smart to create loops of symlinks.
63  exec ln -s test.db2 test.db
64  list [catch { sqlite3 db test.db } msg] $msg
65} {1 {unable to open database file}}
66
67# Test that overly large paths cannot be opened.
68#
69do_test 1.4 {
70  set name "test.db[string repeat x 502]"
71  list [catch { sqlite3 db $name } msg] $msg
72} {1 {unable to open database file}}
73do_test 1.5 {
74  set r [expr 510 - [string length test.db] - [string length [pwd]]]
75  set name "test.db[string repeat x $r]"
76  list [catch { sqlite3 db $name } msg] $msg
77} {1 {unable to open database file}}
78
79#-------------------------------------------------------------------------
80# Test that journal and wal files are created next to the real file,
81# not the symlink.
82#
83do_test 2.0 {
84  catch { db close }
85  catch { db2 close }
86  forcedelete test.db test.db2 test.db3
87  sqlite3 db test.db
88  execsql { CREATE TABLE t1(x) }
89  file link test.db2 test.db
90  file link test.db3 test.db2
91  set {} {}
92} {}
93
94foreach {tn f} {1 test.db2 2 test.db3} {
95  do_test 2.$tn.1 {
96    sqlite3 db2 $f
97    file exists test.db-journal
98  } 0
99  do_test 2.$tn.2 {
100    execsql {
101      BEGIN;
102        INSERT INTO t1 VALUES(1);
103    } db2
104    file exists test.db-journal
105  } 1
106  do_test 2.$tn.3 {
107    list [file exists test2.db-journal] [file exists test3.db-journal]
108  } {0 0}
109  do_test 2.$tn.4 {
110    execsql {
111      COMMIT;
112      PRAGMA journal_mode = wal;
113      INSERT INTO t1 VALUES(2);
114    } db2
115    file exists test.db-wal
116  } 1
117  do_test 2.$tn.5 {
118    list [file exists test2.db-wal] [file exists test3.db-wal]
119  } {0 0}
120  do_execsql_test 2.$tn.6 {
121    SELECT * FROM t1;
122  } {1 2}
123  db2 close
124  do_execsql_test 2.$tn.7 {
125    DELETE FROM t1;
126    PRAGMA journal_mode = delete;
127  } delete
128}
129
130# Try to open a ridiculously long pathname.  Bug found by
131# Kostya Serebryany using libFuzzer on 2015-11-30.
132#
133do_test 3.1 {
134  db close
135  catch {sqlite3 db [string repeat [string repeat x 100]/ 6]} res
136  set res
137} {unable to open database file}
138
139#-------------------------------------------------------------------------
140# Test that relative symlinks that are not located in the cwd work.
141#
142do_test 4.1 {
143  forcedelete x y z
144  file mkdir x
145  file mkdir y
146  file mkdir z
147  sqlite3 db x/test.db
148  file link y/test.db ../x/test.db
149  file link z/test.db ../y/test.db
150  execsql {
151    PRAGMA journal_mode = wal;
152    CREATE TABLE t1(x, y);
153    INSERT INTO t1 VALUES('hello', 'world');
154  }
155} {wal}
156
157do_test 4.2.1 {
158  db close
159  sqlite3 db y/test.db
160  db eval { SELECT * FROM t1 }
161} {hello world}
162do_test 4.2.2 {
163  list [file exists x/test.db-wal] [file exists y/test.db-wal]
164} {1 0}
165
166do_test 4.3.1 {
167  db close
168  sqlite3 db z/test.db
169  db eval { SELECT * FROM t1 }
170} {hello world}
171do_test 4.3.2 {
172  list [file exists x/test.db-wal] [file exists y/test.db-wal] \
173       [file exists z/test.db-wal]
174} {1 0 0}
175
176do_test 4.4.0 {
177  forcedelete w
178  file mkdir w
179  file link w/test.db [file join [pwd] x/test.db]
180  set {} {}
181} {}
182do_test 4.4.1 {
183  db close
184  sqlite3 db w/test.db
185  db eval { SELECT * FROM t1 }
186} {hello world}
187do_test 4.4.2 {
188  list [file exists x/test.db-wal] [file exists w/test.db-wal]
189} {1 0}
190
191finish_test
192