Debian Codegraph @

test/corruptM.test

raw
1 # 2019-08-12
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 #
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.
15 #
16
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19 set testprefix corruptM
20
21 # These tests deal with corrupt database files
22 #
23 database_may_be_corrupt
24
25 proc open_db2_and_catchsql {sql} {
26 set rc [catch { sqlite3 db2 test.db } msg]
27 if {$rc} {
28 return [list $rc $msg]
29 }
30 set res [catchsql $sql db2]
31 db2 close
32 set res
33 }
34
35 db close
36 forcedelete test.db
37 sqlite3 db test.db
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 {
53 PRAGMA quick_check;
54 }
55 } {1 {malformed database schema (t1)}}
56
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 {
63 PRAGMA quick_check;
64 }
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 {
72 PRAGMA quick_check;
73 }
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 {
81 PRAGMA quick_check;
82 }
83 } {1 {malformed database schema (t9)}}
84
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 {
91 PRAGMA quick_check;
92 SELECT * FROM t1, v2;
93 }
94 } {0 {ok 111 222 333 15 22}}
95
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 {
102 PRAGMA quick_check;
103 SELECT * FROM t1, v2;
104 }
105 } {1 {malformed database schema (t1)}}
106
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 {
114 PRAGMA quick_check;
115 SELECT * FROM t1, v2;
116 }
117 } {1 {malformed database schema (i1)}}
118
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 {
125 PRAGMA quick_check;
126 SELECT * FROM t1, v2;
127 }
128 } {1 {malformed database schema (i1)}}
129
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 {
136 PRAGMA quick_check;
137 SELECT * FROM t1, v2;
138 }
139 } {1 {malformed database schema (i1)}}
140
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 {
148 PRAGMA quick_check;
149 SELECT * FROM t1, v2;
150 }
151 } {1 {malformed database schema (v2)}}
152
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 {
159 PRAGMA quick_check;
160 SELECT * FROM t1, v2;
161 }
162 } {1 {malformed database schema (v3)}}
163
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 {
171 PRAGMA quick_check;
172 SELECT * FROM t1, v2;
173 }
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 {
181 PRAGMA quick_check;
182 SELECT * FROM t1, v2;
183 }
184 } {1 {malformed database schema (r1)}}
185
186 finish_test