1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
# 2010 September 1
#
# 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.
#
#***********************************************************************
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/malloc_common.tcl
db close
do_test quota-1.1 { sqlite3_quota_initialize nosuchvfs 1 } {SQLITE_ERROR}
do_test quota-1.2 { sqlite3_quota_initialize "" 1 } {SQLITE_OK}
do_test quota-1.3 { sqlite3_quota_initialize "" 1 } {SQLITE_MISUSE}
do_test quota-1.4 { sqlite3_quota_shutdown } {SQLITE_OK}
do_test quota-1.5 { sqlite3_quota_initialize "" 0 } {SQLITE_OK}
do_test quota-1.6 { sqlite3_quota_shutdown } {SQLITE_OK}
do_test quota-1.7 { sqlite3_quota_initialize "" 1 } {SQLITE_OK}
do_test quota-1.8 { sqlite3_quota_shutdown } {SQLITE_OK}
#-------------------------------------------------------------------------
# Some simple warm-body tests with a single database file in rollback
# mode:
#
# quota-2.1.*: Test that SQLITE_FULL is returned if the database would
# exceed the configured quota.
#
# quota-2.2.*: Test that SQLITE_FULL is not returned and the database
# grows if the callback extends the quota when the database
# attempts to grow beyond the configured quota.
#
# quota-2.3.*: Open and close a db that is not part of any quota group. At
# one point this was causing mutex refs to be leaked.
#
# quota-2.4.*: Try to shutdown the quota system before closing the db
# file. Check that this fails and the quota system still works
# afterwards. Then close the database and successfully shut
# down the quota system.
#
sqlite3_quota_initialize "" 1
proc quota_check {filename limitvar size} {
upvar $limitvar limit
lappend ::quota [set limit] $size
if {[info exists ::quota_request_ok]} { set limit $size }
}
do_test quota-2.1.1 {
sqlite3_quota_set *test.db 4096 quota_check
} {SQLITE_OK}
do_test quota-2.1.2 {
sqlite3 db test.db
execsql {
PRAGMA page_size=1024;
PRAGMA auto_vacuum=OFF;
PRAGMA journal_mode=DELETE;
}
set ::quota [list]
execsql {
CREATE TABLE t1(a, b);
INSERT INTO t1 VALUES(1, randomblob(1100));
INSERT INTO t1 VALUES(2, randomblob(1100));
}
set ::quota
} {}
do_test quota-2.1.3 { file size test.db } {4096}
do_test quota-2.1.4 {
catchsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
} {1 {database or disk is full}}
do_test quota-2.1.5 { set ::quota } {4096 5120}
set ::quota_request_ok 1
set ::quota [list]
do_test quota-2.2.1 {
execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
} {}
do_test quota-2.2.2 { set ::quota } {4096 5120}
do_test quota-2.2.3 { file size test.db } {5120}
unset ::quota_request_ok
do_test quota-2.3.1 {
sqlite3 db2 bak.db
db2 close
} {}
do_test quota-2.4.1 {
sqlite3_quota_shutdown
} {SQLITE_MISUSE}
set ::quota [list]
do_test quota-2.4.2 {
catchsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
} {1 {database or disk is full}}
do_test quota-2.4.3 { set ::quota } {5120 6144}
do_test quota-2.4.4 { file size test.db } {5120}
do_test quota-2.4.99 {
db close
sqlite3_quota_shutdown
} {SQLITE_OK}
#-------------------------------------------------------------------------
# Try some tests with more than one connection to a database file. Still
# in rollback mode.
#
# quota-3.1.*: Two connections to a single database file.
#
# quota-3.2.*: Two connections to each of several database files (that
# are in the same quota group).
#
proc quota_check {filename limitvar size} {
upvar $limitvar limit
lappend ::quota [set limit] $size
if {[info exists ::quota_request_ok]} { set limit $size }
}
do_test quota-3.1.1 {
file delete -force test.db
sqlite3_quota_initialize "" 1
sqlite3_quota_set *test.db 4096 quota_check
} {SQLITE_OK}
do_test quota-3.1.2 {
sqlite3 db test.db
execsql {
PRAGMA page_size = 1024;
PRAGMA journal_mode = delete;
PRAGMA auto_vacuum = off;
CREATE TABLE t1(a PRIMARY KEY, b);
INSERT INTO t1 VALUES(1, 'one');
}
file size test.db
} {3072}
do_test quota-3.1.3 {
sqlite3 db2 test.db
set ::quota [list]
execsql { CREATE TABLE t2(a, b) } db2
set ::quota
} {}
do_test quota-3.1.4 {
catchsql { CREATE TABLE t3(a, b) }
} {1 {database or disk is full}}
do_test quota-3.1.5 {
set ::quota_request_ok 1
execsql { CREATE TABLE t3(a, b) }
} {}
do_test quota-3.1.6 {
db close
db2 close
sqlite3_quota_set *test.db 0 {}
} {SQLITE_OK}
do_test quota-3.2.1 {
file delete force test.db test2.db
sqlite3_quota_set * 4096 {}
sqlite3 db1a test.db
sqlite3 db2a test2.db
foreach db {db1a db2a} {
execsql {
PRAGMA page_size = 1024;
PRAGMA journal_mode = delete;
PRAGMA auto_vacuum = off;
CREATE TABLE t1(a, b);
} $db
}
sqlite3 db1b test.db
sqlite3 db2b test2.db
list [file size test.db] [file size test2.db]
} {2048 2048}
catch { unset ::quota_request_ok }
do_test quota-3.2.2 { execsql { INSERT INTO t1 VALUES('x', 'y') } db1a } {}
do_test quota-3.2.3 { execsql { INSERT INTO t1 VALUES('v', 'w') } db1b } {}
do_test quota-3.2.4 { execsql { INSERT INTO t1 VALUES('t', 'u') } db2a } {}
do_test quota-3.2.5 { execsql { INSERT INTO t1 VALUES('r', 's') } db2b } {}
do_test quota-3.2.6 {
catchsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
} {1 {database or disk is full}}
do_test quota-3.2.7 {
catchsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
} {1 {database or disk is full}}
do_test quota-3.2.8 {
catchsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
} {1 {database or disk is full}}
do_test quota-3.2.9 {
catchsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
} {1 {database or disk is full}}
set ::quota [list]
proc quota_callback {file limitvar size} {
upvar $limitvar limit
lappend ::quota $file $size
set limit 0
}
sqlite3_quota_set * 4096 quota_callback
do_test quota-3.3.1 {
execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
set ::quota
} [list [file join [pwd] test.db] 5120]
do_test quota-3.2.X {
foreach db {db1a db2a db2b db1b} { catch { $db close } }
sqlite3_quota_set * 0 {}
} {SQLITE_OK}
#-------------------------------------------------------------------------
# Quotas are deleted when unused and when there limit is set to zero
#
# Return a list of all currently defined quotas. Each quota is identified
# by its pattern.
proc quota_list {} {
set allq {}
foreach q [sqlite3_quota_dump] {
lappend allq [lindex $q 0]
}
return [lsort $allq]
}
do_test quota-4.1.1 {
sqlite3_quota_set *test.db 0 {}
quota_list
} {}
do_test quota-4.1.2 {
sqlite3_quota_set *test.db 4096 {}
quota_list
} {*test.db}
do_test quota-4.1.3 {
sqlite3_quota_set *test2.db 0 {}
quota_list
} {*test.db}
do_test quota-4.1.4 {
sqlite3_quota_set *test2.db 100000 {}
quota_list
} {*test.db *test2.db}
do_test quota-4.1.5 {
sqlite3_quota_set *test.db 0 {}
quota_list
} {*test2.db}
do_test quota-4.1.6 {
file delete -force test2.db test2.db-journal test2.db-wal
sqlite3 db test2.db
db eval {CREATE TABLE t2(x); INSERT INTO t2 VALUES('tab-t2');}
quota_list
} {*test2.db}
do_test quota-4.1.7 {
catchsql {INSERT INTO t2 VALUES(zeroblob(200000))}
} {1 {database or disk is full}}
do_test quota-4.1.8 {
sqlite3 db2 test2.db
db2 eval {SELECT * FROM t2}
} {tab-t2}
do_test quota-4.1.9 {
sqlite3_quota_set *test2.db 0 {}
catchsql {INSERT INTO t2 VALUES(zeroblob(200000))}
} {0 {}}
do_test quota-4.1.10 {
quota_list
} {*test2.db}
do_test quota-4.1.11 {
db2 close
quota_list
} {*test2.db}
do_test quota-4.1.12 {
db close
quota_list
} {}
do_test quota-4.2.1 {
sqlite3_quota_set A 1000 {}
sqlite3_quota_set B 1000 {}
sqlite3_quota_set C 1000 {}
sqlite3_quota_set D 1000 {}
quota_list
} {A B C D}
do_test quota-4.2.2 {
sqlite3_quota_set C 0 {}
sqlite3_quota_set B 0 {}
quota_list
} {A D}
do_test quota-4.2.3 {
sqlite3_quota_set A 0 {}
sqlite3_quota_set D 0 {}
quota_list
} {}
do_test quota-4.2.4 {
sqlite3_quota_set A 1000 {}
sqlite3_quota_set B 1000 {}
sqlite3_quota_set C 1000 {}
sqlite3_quota_set A 0 {}
sqlite3_quota_set B 0 {}
sqlite3_quota_set C 0 {}
quota_list
} {}
do_test quota-4.2.5 {
sqlite3_quota_set A 1000 {}
sqlite3_quota_set B 1000 {}
sqlite3_quota_set C 1000 {}
sqlite3_quota_set C 0 {}
sqlite3_quota_set B 0 {}
sqlite3_quota_set A 0 {}
quota_list
} {}
do_test quota-4.3.1 {
sqlite3_quota_set A 1000 quota_callback
sqlite3 db A
sqlite3_quota_set A 0 quota_callback
db close
quota_list
} {}
do_test quota-4.4.1 {
sqlite3_quota_set A 1000 quota_callback
sqlite3_quota_shutdown
} {SQLITE_OK}
do_test quota-4.4.2 {
quota_list
} {}
#-------------------------------------------------------------------------
# The following tests test that the quota VFS handles malloc and IO
# errors.
#
sqlite3_quota_initialize "" 1
sqlite3_quota_set *test.db 4096 {}
do_faultsim_test quota-5.1 -prep {
catch {db close}
} -body {
sqlite3 db test2.db
}
do_faultsim_test quota-5.2 -prep {
catch {db close}
} -body {
sqlite3 db test.db
}
catch { db close }
file delete -force test.db
do_test quota-5.3.prep {
sqlite3 db test.db
execsql {
PRAGMA auto_vacuum = 1;
PRAGMA page_size = 1024;
CREATE TABLE t1(a, b);
INSERT INTO t1 VALUES(10, zeroblob(1200));
}
faultsim_save_and_close
} {}
do_faultsim_test quota-5.3 -prep {
faultsim_restore_and_reopen
} -body {
execsql { DELETE FROM t1 }
}
do_test quota-5.4.1 {
catch { db close }
file delete -force test.db
file mkdir test.db
list [catch { sqlite3 db test.db } msg] $msg
} {1 {unable to open database file}}
do_faultsim_test quota-5.5 -prep {
catch { sqlite3_quota_shutdown }
} -body {
sqlite3_quota_initialize "" 1
}
do_faultsim_test quota-5.6 -prep {
catch { sqlite3_quota_shutdown }
sqlite3_quota_initialize "" 1
} -body {
sqlite3_quota_set * 4096 {}
}
catch { sqlite3_quota_shutdown }
finish_test
|