# 2019-04-11 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is testing the sqlite_dbpage virtual table. # set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix dbdata ifcapable !vtab||!compound { finish_test return } # Register the sqlite_dbdata and sqlite_dbptr virtual tables with database # handle $db. Use the [sqlite3_dbdata_init] command if this testfixture # provides it, or else try to load them from a loadable extension. # proc dbdata_load {db} { if {[info commands sqlite3_dbdata_init]!=""} { sqlite3_dbdata_init $db } else { $db enable_load_extension 1 $db eval { SELECT load_extension('../dbdata') } } } if {[catch { dbdata_load db }]} { finish_test return } do_execsql_test 1.0 { PRAGMA auto_vacuum = 0; CREATE TABLE T1(a, b); INSERT INTO t1(rowid, a ,b) VALUES(5, 'v', 'five'); INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten'); } do_execsql_test 1.1 { SELECT pgno, cell, field, quote(value) FROM sqlite_dbdata WHERE pgno=2; } { 2 0 -1 5 2 0 0 'v' 2 0 1 'five' 2 1 -1 10 2 1 0 'x' 2 1 1 'ten' } breakpoint do_execsql_test 1.2 { SELECT pgno, cell, field, quote(value) FROM sqlite_dbdata; } { 1 0 -1 1 1 0 0 'table' 1 0 1 'T1' 1 0 2 'T1' 1 0 3 2 1 0 4 {'CREATE TABLE T1(a, b)'} 2 0 -1 5 2 0 0 'v' 2 0 1 'five' 2 1 -1 10 2 1 0 'x' 2 1 1 'ten' } set big [string repeat big 2000] do_execsql_test 1.3 { INSERT INTO t1 VALUES(NULL, $big); SELECT value FROM sqlite_dbdata WHERE pgno=2 AND cell=2 AND field=1; } $big do_execsql_test 1.4 { DELETE FROM t1; INSERT INTO t1 VALUES(NULL, randomblob(5050)); } do_test 1.5 { execsql { SELECT quote(value) FROM sqlite_dbdata WHERE pgno=2 AND cell=0 AND field=1; } } [db one {SELECT quote(b) FROM t1}] #------------------------------------------------------------------------- reset_db dbdata_load db do_execsql_test 2.0 { PRAGMA auto_vacuum = 0; CREATE TABLE t1(a); CREATE INDEX i1 ON t1(a); WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10 ) INSERT INTO t1 SELECT randomblob(900) FROM s; } do_execsql_test 2.1 { SELECT * FROM sqlite_dbptr WHERE pgno=2; } { 2 25 2 6 2 7 2 9 2 11 2 13 2 15 2 17 2 19 2 21 } do_execsql_test 2.2 { SELECT * FROM sqlite_dbptr WHERE pgno=3; } { 3 24 3 23 } do_execsql_test 2.3 { SELECT * FROM sqlite_dbptr } { 2 25 2 6 2 7 2 9 2 11 2 13 2 15 2 17 2 19 2 21 3 24 3 23 } #------------------------------------------------------------------------- # The sqlite_dbdata and sqlite_dbptr tables may only be used from top-level # SQL. They may not be used within a view or trigger, as this would allow # the schema of one database to read the raw content of another that has # been attached to the same connection. # reset_db dbdata_load db forcedelete test.db2 do_execsql_test 3.0 { ATTACH 'test.db2' AS aux; PRAGMA aux.auto_vacuum = 0; CREATE TABLE aux.t1(x); INSERT INTO aux.t1 VALUES('private data'); CREATE TABLE t2(y); CREATE VIEW v1 AS SELECT value FROM sqlite_dbdata('aux'); CREATE VIEW v2 AS SELECT child FROM sqlite_dbptr('aux'); CREATE TRIGGER tr1 AFTER INSERT ON t2 BEGIN INSERT INTO t2 SELECT value FROM sqlite_dbdata('aux') WHERE pgno=2; END; } do_catchsql_test 3.1 { SELECT * FROM v1; } {1 {unsafe use of virtual table "sqlite_dbdata"}} do_catchsql_test 3.2 { SELECT * FROM v2; } {1 {unsafe use of virtual table "sqlite_dbptr"}} do_catchsql_test 3.3 { INSERT INTO t2 VALUES(1); } {1 {unsafe use of virtual table "sqlite_dbdata"}} do_execsql_test 3.4 { SELECT count(*) FROM t2; } {0} # Direct use is still allowed. do_execsql_test 3.5 { SELECT value FROM sqlite_dbdata('aux') WHERE pgno=2 AND field=0; } {{private data}} do_execsql_test 3.6 { SELECT count(*) FROM sqlite_dbptr('aux'); } {0} finish_test