AryaWu/sqlite
0
1# 2015 October 312#3# The author disclaims copyright to this source code. In place of4# 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. The12# focus of this file is testing that SQLite can follow symbolic links.13#14 15set testdir [file dirname $argv0]16source $testdir/tester.tcl17set testprefix symlink18 19# This only runs on unix.20if {$::tcl_platform(os) eq "Windows NT"} {21 finish_test22 return23}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.db234do_test 1.1 {35 file link test.db2 test.db36 sqlite3 db2 test.db237 sqlite3_db_filename db2 main38} [file join [pwd] test.db]39 40# But not with the -nofollow flag41#42do_test 1.1.2 {43 db2 close44 set rc [catch {sqlite3 db2 test.db2 -nofollow 1} msg]45 lappend rc $msg46} {1 {unable to open database file}}47 48# If the main database is successfully opened with -nofollow, then -nofollow49# is also used for ATTACH.50#51do_test 1.1.3 {52 catch {db2 close}53 sqlite3 db2 test.db -nofollow 154} {}55do_test 1.1.4 {56 catchsql {ATTACH 'test.db2' AS aux1;} db257} {1 {unable to open database: test.db2}}58 59# Test that if the symlink points to a file that does not exists, it is60# created when it is opened.61#62do_test 1.2.1 {63 catch {db2 close}64 db close65 forcedelete test.db66 file exists test.db67} 068do_test 1.2.2 {69 sqlite3 db2 test.db270 file exists test.db71} 172do_test 1.2.3 {73 sqlite3_db_filename db2 main74} [file join [pwd] test.db]75db2 close76 77# Test that a loop of symlinks cannot be opened.78#79do_test 1.3 {80 forcedelete test.db81 # Note: Tcl [file link] command is too smart to create loops of symlinks.82 exec ln -s test.db2 test.db83 list [catch { sqlite3 db test.db } msg] $msg84} {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] $msg91} {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] $msg96} {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 }105 forcedelete test.db test.db2 test.db3106 sqlite3 db test.db107 execsql { CREATE TABLE t1(x) }108 file link test.db2 test.db109 file link test.db3 test.db2110 set {} {}111} {}112 113foreach {tn f} {1 test.db2 2 test.db3} {114 do_test 2.$tn.1 {115 sqlite3 db2 $f116 file exists test.db-journal117 } 0118 do_test 2.$tn.2 {119 execsql {120 BEGIN;121 INSERT INTO t1 VALUES(1);122 } db2123 file exists test.db-journal124 } [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 } db2134 file exists test.db-wal135 } 1136 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 close143 do_execsql_test 2.$tn.7 {144 DELETE FROM t1;145 PRAGMA journal_mode = delete;146 } delete147}148 149# Try to open a ridiculously long pathname. Bug found by150# Kostya Serebryany using libFuzzer on 2015-11-30.151#152do_test 3.1 {153 db close154 catch {sqlite3 db [string repeat [string repeat x 100]/ 6]} res155 set res156} {unable to open database file}157 158#-------------------------------------------------------------------------159# Test that relative symlinks that are not located in the cwd work.160#161do_test 4.1 {162 forcedelete x y z163 file mkdir x164 file mkdir y165 file mkdir z166 sqlite3 db x/test.db167 file link y/test.db ../x/test.db168 file link z/test.db ../y/test.db169 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 close178 sqlite3 db y/test.db179 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 close187 sqlite3 db z/test.db188 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 w197 file mkdir w198 file link w/test.db [file join [pwd] x/test.db] 199 set {} {}200} {}201do_test 4.4.1 {202 db close203 sqlite3 db w/test.db204 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}209 210#-------------------------------------------------------------------------211# Check that extra ".." in a path are ignored.212reset_db213do_execsql_test 5.0 {214 CREATE TABLE xyz(x, y, z);215 INSERT INTO xyz VALUES(1, 2, 3);216}217 218set path [pwd]219set nLink [llength [split $path /]]220set path "[string repeat ../ [expr $nLink*2]]..${path}/test.db"221 222sqlite3 db2 $path223do_execsql_test -db db2 5.1 {224 SELECT * FROM xyz;225} {1 2 3}226db close227 228forcedelete test.db2229file link test.db2 $path230sqlite3 db2 test.db2231do_execsql_test -db db2 5.2 {232 SELECT * FROM xyz;233} {1 2 3}234forcedelete test.db2235 236 237 238finish_test239 