OLD | NEW |
1 # 2014-01-20 | 1 # 2014-01-20 |
2 # | 2 # |
3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
5 # | 5 # |
6 # May you do good and not evil. | 6 # May you do good and not evil. |
7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
9 # | 9 # |
10 #*********************************************************************** | 10 #*********************************************************************** |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 execsql { INSERT INTO t1 VALUES(5, 'klmnopqrst') } | 98 execsql { INSERT INTO t1 VALUES(5, 'klmnopqrst') } |
99 execsql { INSERT INTO t1 VALUES(7, 'klmnopqrst') } | 99 execsql { INSERT INTO t1 VALUES(7, 'klmnopqrst') } |
100 } {} | 100 } {} |
101 | 101 |
102 db close | 102 db close |
103 sqlite3 db test.db | 103 sqlite3 db test.db |
104 do_catchsql_test 3.3 { | 104 do_catchsql_test 3.3 { |
105 INSERT INTO t1 VALUES(9, 'klmnopqrst'); | 105 INSERT INTO t1 VALUES(9, 'klmnopqrst'); |
106 } {1 {database disk image is malformed}} | 106 } {1 {database disk image is malformed}} |
107 } ;# end-if !defined(ENABLE_OVERSIZE_CELL_CHECK) | 107 } ;# end-if !defined(ENABLE_OVERSIZE_CELL_CHECK) |
| 108 |
| 109 |
| 110 #------------------------------------------------------------------------- |
| 111 # Test that an assert() failure discovered by AFL corrupt database file |
| 112 # testing has been fixed. |
| 113 # |
| 114 reset_db |
| 115 do_execsql_test 4.0 { |
| 116 PRAGMA page_size = 65536; |
| 117 PRAGMA autovacuum = 0; |
| 118 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); |
| 119 INSERT INTO t1 VALUES(-1, 'abcdefghij'); |
| 120 INSERT INTO t1 VALUES(0, 'abcdefghij'); |
| 121 } |
| 122 |
| 123 set root [db one {SELECT rootpage FROM sqlite_master}] |
| 124 set offset [expr ($root-1) * 65536] |
| 125 |
| 126 ifcapable oversize_cell_check { |
| 127 set res {1 {database disk image is malformed}} |
| 128 } else { |
| 129 set res {0 {}} |
| 130 } |
| 131 do_test 4.1 { |
| 132 db close |
| 133 hexio_write test.db [expr $offset + 8 + 2] 0000 |
| 134 hexio_write test.db [expr $offset + 5] 0000 |
| 135 sqlite3 db test.db |
| 136 catchsql { DELETE FROM t1 WHERE a=0 } |
| 137 } $res |
| 138 |
| 139 |
| 140 #------------------------------------------------------------------------- |
| 141 # Database properties: |
| 142 # |
| 143 # * Incremental vacuum mode. |
| 144 # * Database root table has a single leaf page. |
| 145 # * Free list consists of a single trunk page. |
| 146 # |
| 147 # The db is then corrupted by adding the root table leaf page as a free-list |
| 148 # leaf page (so that it is referenced twice). |
| 149 # |
| 150 # Then, a new table is created. The new root page is the current free-list |
| 151 # trunk. This means that the root table leaf page is made into the new |
| 152 # free list trunk, which corrupts its header. Then, when the new entry is |
| 153 # inserted into the root table, things would get chaotic. |
| 154 # |
| 155 reset_db |
| 156 do_test 5.0 { |
| 157 execsql { |
| 158 PRAGMA page_size = 512; |
| 159 PRAGMA auto_vacuum = 2; |
| 160 } |
| 161 for {set i 3} {1} {incr i} { |
| 162 execsql "CREATE TABLE t${i}(x)" |
| 163 if {[db one {PRAGMA page_count}]>$i} break |
| 164 } |
| 165 set nPage [db one {PRAGMA page_count}] |
| 166 execsql { |
| 167 CREATE TABLE t100(x); |
| 168 DROP TABLE t100; |
| 169 } |
| 170 } {} |
| 171 |
| 172 do_execsql_test 5.1 { |
| 173 PRAGMA page_count |
| 174 } [expr $nPage+1] |
| 175 |
| 176 do_test 5.2 { |
| 177 # The last page of the db is now the only leaf of the sqlite_master table. |
| 178 # Corrupt the db by adding it to the free-list as well (the second last |
| 179 # page of the db is the free-list trunk). |
| 180 db close |
| 181 hexio_write test.db [expr 512*($nPage-1)] [ |
| 182 format "%.8X%.8X%.8X" 0 1 [expr $nPage+1] |
| 183 ] |
| 184 } {12} |
| 185 |
| 186 do_test 5.3 { |
| 187 sqlite3 db test.db |
| 188 catchsql { CREATE TABLE tx(x); } |
| 189 } {1 {database disk image is malformed}} |
| 190 |
| 191 |
| 192 #------------------------------------------------------------------------- |
| 193 # Set the payload size of a cell to just less than 2^32 bytes (not |
| 194 # possible in an uncorrupted db). Then try to delete the cell. At one |
| 195 # point this led to an integer overflow that caused an assert() to fail. |
| 196 # |
| 197 reset_db |
| 198 do_execsql_test 6.0 { |
| 199 PRAGMA page_size = 512; |
| 200 PRAGMA auto_vacuum=0; |
| 201 CREATE TABLE t1(x); |
| 202 INSERT INTO t1 VALUES(zeroblob(300)); |
| 203 INSERT INTO t1 VALUES(zeroblob(600)); |
| 204 } {} |
| 205 do_test 6.1 { |
| 206 db close |
| 207 hexio_write test.db 616 8FFFFFFF7F02 |
| 208 sqlite3 db test.db |
| 209 breakpoint |
| 210 execsql { DELETE FROM t1 WHERE rowid=2 } |
| 211 } {} |
| 212 |
| 213 #------------------------------------------------------------------------- |
| 214 # See what happens if the sqlite_master entry associated with a PRIMARY |
| 215 # KEY or UNIQUE index is removed. |
| 216 # |
| 217 reset_db |
| 218 do_execsql_test 7.0 { |
| 219 PRAGMA auto_vacuum=0; |
| 220 CREATE TABLE t1(x PRIMARY KEY, y); |
| 221 INSERT INTO t1 VALUES('a', 'A'); |
| 222 INSERT INTO t1 VALUES('b', 'A'); |
| 223 INSERT INTO t1 VALUES('c', 'A'); |
| 224 SELECT name FROM sqlite_master; |
| 225 } {t1 sqlite_autoindex_t1_1} |
| 226 do_execsql_test 7.1 { |
| 227 PRAGMA writable_schema = 1; |
| 228 DELETE FROM sqlite_master WHERE name = 'sqlite_autoindex_t1_1'; |
| 229 } |
| 230 do_test 7.2 { |
| 231 db close |
| 232 sqlite3 db test.db |
| 233 catchsql { UPDATE t1 SET x='d' AND y='D' WHERE rowid = 2 } |
| 234 } {1 {database disk image is malformed}} |
| 235 |
| 236 #------------------------------------------------------------------------- |
| 237 # At one point an assert() would fail if attempt was made to free page 1. |
| 238 # |
| 239 reset_db |
| 240 do_execsql_test 8.0 { |
| 241 PRAGMA auto_vacuum=0; |
| 242 CREATE TABLE t1(x); |
| 243 INSERT INTO t1 VALUES(zeroblob(300)); |
| 244 INSERT INTO t1 VALUES(zeroblob(300)); |
| 245 INSERT INTO t1 VALUES(zeroblob(300)); |
| 246 INSERT INTO t1 VALUES(zeroblob(300)); |
| 247 } {} |
| 248 |
| 249 do_test 8.1 { |
| 250 db close |
| 251 hexio_write test.db [expr 1024 + 8] 00000001 |
| 252 sqlite3 db test.db |
| 253 catchsql { DELETE FROM t1 } |
| 254 } {1 {database disk image is malformed}} |
| 255 |
| 256 do_test 8.2 { |
| 257 db close |
| 258 sqlite3 db test.db |
| 259 execsql { PRAGMA integrity_check } |
| 260 } {/.*in database main.*/} |
| 261 |
| 262 |
108 finish_test | 263 finish_test |
OLD | NEW |