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 #***********************************************************************
12 # Check to ensure that the type, name, and tbl_name fields of the
13 # sqlite_master table are validated and errors are reported if they
14 # are inconsistent with the sql.
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19 set testprefix corruptM
21 # These tests deal with corrupt database files
23 database_may_be_corrupt
25 proc open_db2_and_catchsql {sql} {
26 set rc [catch { sqlite3 db2 test.db } msg]
28 return [list $rc $msg]
30 set res [catchsql $sql db2]
38 do_execsql_test corruptM-100 {
39 CREATE TABLE t1(a,b,c);
40 INSERT INTO t1 VALUES(111,222,333);
41 CREATE INDEX i1 ON t1(b);
42 CREATE VIEW v2 AS SELECT 15,22;
43 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT 5; END;
44 SELECT type, name, tbl_name, '|' FROM sqlite_master;
45 } {table t1 t1 | index i1 t1 | view v2 v2 | trigger r1 t1 |}
46 do_execsql_test corruptM-101 {
47 PRAGMA writable_schema=on;
48 UPDATE sqlite_master SET tbl_name=NULL WHERE name='t1';
49 SELECT type, name, tbl_name, '|' FROM sqlite_master;
50 } {table t1 {} | index i1 t1 | view v2 v2 | trigger r1 t1 |}
51 do_test corruptM-102 {
52 open_db2_and_catchsql {
55 } {1 {malformed database schema (t1)}}
57 do_execsql_test corruptM-110 {
58 UPDATE sqlite_master SET tbl_name='tx' WHERE name='t1';
59 SELECT type, name, tbl_name, '|' FROM sqlite_master;
60 } {table t1 tx | index i1 t1 | view v2 v2 | trigger r1 t1 |}
61 do_test corruptM-111 {
62 open_db2_and_catchsql {
65 } {1 {malformed database schema (t1)}}
66 do_execsql_test corruptM-112 {
67 UPDATE sqlite_master SET tbl_name='t1', type='tabl' WHERE name='t1';
68 SELECT type, name, tbl_name, '|' FROM sqlite_master;
69 } {tabl t1 t1 | index i1 t1 | view v2 v2 | trigger r1 t1 |}
70 do_test corruptM-113 {
71 open_db2_and_catchsql {
74 } {1 {malformed database schema (t1)}}
75 do_execsql_test corruptM-114 {
76 UPDATE sqlite_master SET tbl_name='t9',type='table',name='t9'WHERE name='t1';
77 SELECT type, name, tbl_name, '|' FROM sqlite_master;
78 } {table t9 t9 | index i1 t1 | view v2 v2 | trigger r1 t1 |}
79 do_test corruptM-114 {
80 open_db2_and_catchsql {
83 } {1 {malformed database schema (t9)}}
85 do_execsql_test corruptM-120 {
86 UPDATE sqlite_master SET name='t1',tbl_name='T1' WHERE name='t9';
87 SELECT type, name, tbl_name, '|' FROM sqlite_master;
88 } {table t1 T1 | index i1 t1 | view v2 v2 | trigger r1 t1 |}
89 do_test corruptM-121 {
90 open_db2_and_catchsql {
94 } {0 {ok 111 222 333 15 22}}
96 do_execsql_test corruptM-130 {
97 UPDATE sqlite_master SET type='view' WHERE name='t1';
98 SELECT type, name, tbl_name, '|' FROM sqlite_master;
99 } {view t1 T1 | index i1 t1 | view v2 v2 | trigger r1 t1 |}
100 do_test corruptM-131 {
101 open_db2_and_catchsql {
103 SELECT * FROM t1, v2;
105 } {1 {malformed database schema (t1)}}
107 do_execsql_test corruptM-140 {
108 UPDATE sqlite_master SET type='table', tbl_name='t1' WHERE name='t1';
109 UPDATE sqlite_master SET tbl_name='tx' WHERE name='i1';
110 SELECT type, name, tbl_name, '|' FROM sqlite_master;
111 } {table t1 t1 | index i1 tx | view v2 v2 | trigger r1 t1 |}
112 do_test corruptM-141 {
113 open_db2_and_catchsql {
115 SELECT * FROM t1, v2;
117 } {1 {malformed database schema (i1)}}
119 do_execsql_test corruptM-150 {
120 UPDATE sqlite_master SET type='table', tbl_name='t1' WHERE name='i1';
121 SELECT type, name, tbl_name, '|' FROM sqlite_master;
122 } {table t1 t1 | table i1 t1 | view v2 v2 | trigger r1 t1 |}
123 do_test corruptM-151 {
124 open_db2_and_catchsql {
126 SELECT * FROM t1, v2;
128 } {1 {malformed database schema (i1)}}
130 do_execsql_test corruptM-160 {
131 UPDATE sqlite_master SET type='view', tbl_name='t1' WHERE name='i1';
132 SELECT type, name, tbl_name, '|' FROM sqlite_master;
133 } {table t1 t1 | view i1 t1 | view v2 v2 | trigger r1 t1 |}
134 do_test corruptM-161 {
135 open_db2_and_catchsql {
137 SELECT * FROM t1, v2;
139 } {1 {malformed database schema (i1)}}
141 do_execsql_test corruptM-170 {
142 UPDATE sqlite_master SET type='index', tbl_name='t1' WHERE name='i1';
143 UPDATE sqlite_master SET type='table', tbl_name='v2' WHERE name='v2';
144 SELECT type, name, tbl_name, '|' FROM sqlite_master;
145 } {table t1 t1 | index i1 t1 | table v2 v2 | trigger r1 t1 |}
146 do_test corruptM-171 {
147 open_db2_and_catchsql {
149 SELECT * FROM t1, v2;
151 } {1 {malformed database schema (v2)}}
153 do_execsql_test corruptM-180 {
154 UPDATE sqlite_master SET type='view',name='v3',tbl_name='v3' WHERE name='v2';
155 SELECT type, name, tbl_name, '|' FROM sqlite_master;
156 } {table t1 t1 | index i1 t1 | view v3 v3 | trigger r1 t1 |}
157 do_test corruptM-181 {
158 open_db2_and_catchsql {
160 SELECT * FROM t1, v2;
162 } {1 {malformed database schema (v3)}}
164 do_execsql_test corruptM-190 {
165 UPDATE sqlite_master SET type='view',name='v2',tbl_name='v2' WHERE name='v3';
166 UPDATE sqlite_master SET type='view' WHERE name='r1';
167 SELECT type, name, tbl_name, '|' FROM sqlite_master;
168 } {table t1 t1 | index i1 t1 | view v2 v2 | view r1 t1 |}
169 do_test corruptM-191 {
170 open_db2_and_catchsql {
172 SELECT * FROM t1, v2;
174 } {1 {malformed database schema (r1)}}
175 do_execsql_test corruptM-192 {
176 UPDATE sqlite_master SET type='trigger',tbl_name='v2' WHERE name='r1';
177 SELECT type, name, tbl_name, '|' FROM sqlite_master;
178 } {table t1 t1 | index i1 t1 | view v2 v2 | trigger r1 v2 |}
179 do_test corruptM-193 {
180 open_db2_and_catchsql {
182 SELECT * FROM t1, v2;
184 } {1 {malformed database schema (r1)}}