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 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
16 # If SQLITE_CURDIR is not defined, omit this file.
22 source $testdir/malloc_common.tcl
25 unset -nocomplain defaultVfs
26 set defaultVfs [file_control_vfsname db]
29 do_test quota-1.1 { sqlite3_quota_initialize nosuchvfs 1 } {SQLITE_ERROR}
30 do_test quota-1.2 { sqlite3_quota_initialize "" 1 } {SQLITE_OK}
31 do_test quota-1.3 { sqlite3_quota_initialize "" 1 } {SQLITE_MISUSE}
32 do_test quota-1.4 { sqlite3_quota_shutdown } {SQLITE_OK}
34 do_test quota-1.5 { sqlite3_quota_initialize "" 0 } {SQLITE_OK}
35 do_test quota-1.6 { sqlite3_quota_shutdown } {SQLITE_OK}
36 do_test quota-1.7 { sqlite3_quota_initialize "" 1 } {SQLITE_OK}
37 do_test quota-1.8 { sqlite3_quota_shutdown } {SQLITE_OK}
40 #-------------------------------------------------------------------------
41 # Some simple warm-body tests with a single database file in rollback
44 # quota-2.1.*: Test that SQLITE_FULL is returned if the database would
45 # exceed the configured quota.
47 # quota-2.2.*: Test that SQLITE_FULL is not returned and the database
48 # grows if the callback extends the quota when the database
49 # attempts to grow beyond the configured quota.
51 # quota-2.3.*: Open and close a db that is not part of any quota group. At
52 # one point this was causing mutex refs to be leaked.
54 # quota-2.4.*: Try to shutdown the quota system before closing the db
55 # file. Check that this fails and the quota system still works
56 # afterwards. Then close the database and successfully shut
57 # down the quota system.
59 sqlite3_quota_initialize "" 1
61 unset -nocomplain quota_request_ok
62 proc quota_check {filename limitvar size} {
65 lappend ::quota [set limit] $size
66 if {[info exists ::quota_request_ok]} { set limit $size }
70 sqlite3_quota_set *test.db 4096 quota_check
75 PRAGMA page_size=1024;
76 PRAGMA auto_vacuum=OFF;
77 PRAGMA journal_mode=DELETE;
81 CREATE TABLE t1(a, b);
82 INSERT INTO t1 VALUES(1, randomblob(1100));
83 INSERT INTO t1 VALUES(2, randomblob(1100));
87 do_test quota-2.1.2.1 {
88 file_control_vfsname db
90 do_test quota-2.1.3 { file size test.db } {4096}
92 catchsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
93 } {1 {database or disk is full}}
94 do_test quota-2.1.5 { set ::quota } {4096 5120}
96 set ::quota_request_ok 1
99 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
101 do_test quota-2.2.2 { set ::quota } {4096 5120}
102 do_test quota-2.2.3 { file size test.db } {5120}
103 unset ::quota_request_ok
105 do_test quota-2.3.1 {
110 do_test quota-2.4.1 {
111 sqlite3_quota_shutdown
114 do_test quota-2.4.2 {
115 catchsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
116 } {1 {database or disk is full}}
117 do_test quota-2.4.3 { set ::quota } {5120 6144}
118 do_test quota-2.4.4 { file size test.db } {5120}
119 do_test quota-2.4.99 {
121 sqlite3_quota_shutdown
124 #-------------------------------------------------------------------------
125 # Try some tests with more than one connection to a database file. Still
128 # quota-3.1.*: Two connections to a single database file.
130 # quota-3.2.*: Two connections to each of several database files (that
131 # are in the same quota group).
133 proc quota_check {filename limitvar size} {
134 upvar $limitvar limit
135 lappend ::quota [set limit] $size
136 if {[info exists ::quota_request_ok]} { set limit $size }
139 do_test quota-3.1.1 {
141 sqlite3_quota_initialize "" 1
142 sqlite3_quota_set *test.db 4096 quota_check
144 do_test quota-3.1.2 {
147 PRAGMA page_size = 1024;
148 PRAGMA journal_mode = delete;
149 PRAGMA auto_vacuum = off;
150 CREATE TABLE t1(a PRIMARY KEY, b);
151 INSERT INTO t1 VALUES(1, 'one');
155 do_test quota-3.1.3 {
158 execsql { CREATE TABLE t2(a, b) } db2
161 do_test quota-3.1.4 {
162 catchsql { CREATE TABLE t3(a, b) }
163 } {1 {database or disk is full}}
164 do_test quota-3.1.5 {
165 set ::quota_request_ok 1
166 execsql { CREATE TABLE t3(a, b) }
168 do_test quota-3.1.6 {
171 sqlite3_quota_set *test.db 0 {}
174 do_test quota-3.2.1 {
175 delete_file force test.db test2.db
177 sqlite3_quota_set * 4096 {}
179 sqlite3 db2a test2.db
181 foreach db {db1a db2a} {
183 PRAGMA page_size = 1024;
184 PRAGMA journal_mode = delete;
185 PRAGMA auto_vacuum = off;
186 CREATE TABLE t1(a, b);
191 sqlite3 db2b test2.db
193 list [file size test.db] [file size test2.db]
196 catch { unset ::quota_request_ok }
198 do_test quota-3.2.2 { execsql { INSERT INTO t1 VALUES('x', 'y') } db1a } {}
199 do_test quota-3.2.3 { execsql { INSERT INTO t1 VALUES('v', 'w') } db1b } {}
200 do_test quota-3.2.4 { execsql { INSERT INTO t1 VALUES('t', 'u') } db2a } {}
201 do_test quota-3.2.5 { execsql { INSERT INTO t1 VALUES('r', 's') } db2b } {}
203 do_test quota-3.2.6 {
204 catchsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
205 } {1 {database or disk is full}}
206 do_test quota-3.2.7 {
207 catchsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
208 } {1 {database or disk is full}}
209 do_test quota-3.2.8 {
210 catchsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
211 } {1 {database or disk is full}}
212 do_test quota-3.2.9 {
213 catchsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
214 } {1 {database or disk is full}}
217 proc quota_callback {file limitvar size} {
218 upvar $limitvar limit
219 if {$::tcl_platform(platform)=="windows"} {
220 set file [ lindex [string map {\\ \/} $file] 0 ]
222 lappend ::quota $file $size
225 sqlite3_quota_set * 4096 quota_callback
226 do_test quota-3.3.1 {
227 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
228 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
229 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
230 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
232 } [list [file join [get_pwd] test.db] 5120]
234 do_test quota-3.2.X {
235 foreach db {db1a db2a db2b db1b} { catch { $db close } }
236 sqlite3_quota_set * 0 {}
239 #-------------------------------------------------------------------------
240 # Quotas are deleted when unused and when their limit is set to zero
243 # Return a list of all currently defined quotas. Each quota is identified
247 foreach q [sqlite3_quota_dump] {
248 lappend allq [lindex $q 0]
252 proc quota_size {name} {
254 foreach q [sqlite3_quota_dump] {
255 if {[lindex $q 0]==$name} {return [lindex $q 2]}
260 do_test quota-4.1.1 {
261 sqlite3_quota_set *test.db 0 {}
264 do_test quota-4.1.2 {
265 sqlite3_quota_set *test.db 4096 {}
268 do_test quota-4.1.3 {
269 sqlite3_quota_set *test2.db 0 {}
272 do_test quota-4.1.4 {
273 sqlite3_quota_set *test2.db 100000 {}
275 } {*test.db *test2.db}
276 do_test quota-4.1.5 {
277 sqlite3_quota_set *test.db 0 {}
280 do_test quota-4.1.6 {
281 forcedelete test2.db test2.db-journal test2.db-wal
283 db eval {CREATE TABLE t2(x); INSERT INTO t2 VALUES('tab-t2');}
286 do_test quota-4.1.7 {
287 catchsql {INSERT INTO t2 VALUES(zeroblob(200000))}
288 } {1 {database or disk is full}}
289 do_test quota-4.1.8 {
291 db2 eval {SELECT * FROM t2}
293 do_test quota-4.1.9 {
294 sqlite3_quota_set *test2.db 0 {}
295 catchsql {INSERT INTO t2 VALUES(zeroblob(200000))}
297 do_test quota-4.1.10 {
300 do_test quota-4.1.11 {
304 do_test quota-4.1.12 {
309 do_test quota-4.2.1 {
310 sqlite3_quota_set A 1000 {}
311 sqlite3_quota_set B 1000 {}
312 sqlite3_quota_set C 1000 {}
313 sqlite3_quota_set D 1000 {}
316 do_test quota-4.2.2 {
317 sqlite3_quota_set C 0 {}
318 sqlite3_quota_set B 0 {}
321 do_test quota-4.2.3 {
322 sqlite3_quota_set A 0 {}
323 sqlite3_quota_set D 0 {}
326 do_test quota-4.2.4 {
327 sqlite3_quota_set A 1000 {}
328 sqlite3_quota_set B 1000 {}
329 sqlite3_quota_set C 1000 {}
330 sqlite3_quota_set A 0 {}
331 sqlite3_quota_set B 0 {}
332 sqlite3_quota_set C 0 {}
335 do_test quota-4.2.5 {
336 sqlite3_quota_set A 1000 {}
337 sqlite3_quota_set B 1000 {}
338 sqlite3_quota_set C 1000 {}
339 sqlite3_quota_set C 0 {}
340 sqlite3_quota_set B 0 {}
341 sqlite3_quota_set A 0 {}
345 do_test quota-4.3.1 {
346 sqlite3_quota_set A 1000 quota_callback
348 sqlite3_quota_set A 0 quota_callback
353 unset -nocomplain quotagroup
354 if {$tcl_platform(platform)=="windows"} {
355 set quotagroup *\\quota-test-A?.db
357 set quotagroup */quota-test-A?.db
359 foreach file [glob -nocomplain quota-test-A*] {
362 do_test quota-4.4.1 {
364 sqlite3_quota_set $::quotagroup 10000 quota_callback
365 forcedelete ./quota-test-A1.db ./quota-test-A2.db
366 sqlite3 db ./quota-test-A1.db
369 INSERT INTO t1 VALUES(randomblob(5000));
373 do_test quota-4.4.2 {
376 do_test quota-4.4.3 {
378 sqlite3 db ./quota-test-A2.db
381 INSERT INTO t1 VALUES(randomblob(5000));
385 do_test quota-4.4.4 {
388 do_test quota-4.4.5 {
390 sqlite3_quota_set $::quotagroup 0 {}
393 do_test quota-4.4.6 {
394 sqlite3_quota_set $quotagroup 10000 quota_callback
395 sqlite3 db quota-test-A1.db
396 db eval {SELECT count(*) FROM sqlite_master}
397 quota_size $quotagroup
398 } [file size quota-test-A1.db]
399 do_test quota-4.4.7 {
400 sqlite3_quota_file quota-test-A2.db
401 quota_size $::quotagroup
402 } [expr {[file size quota-test-A1.db]+[file size quota-test-A2.db]}]
404 unset -nocomplain quotagroup
405 if {$tcl_platform(platform)=="windows"} {
406 set quotagroup *\\quota-test-B*
408 set quotagroup */quota-test-B*
410 foreach file [glob -nocomplain quota-test-B*] {
413 do_test quota-4.5.1 {
414 sqlite3_quota_set $::quotagroup 100000 quota_callback
415 quota_size $::quotagroup
417 do_test quota-4.5.2 {
418 sqlite3_quota_file quota-test-B1.txt
419 quota_size $::quotagroup
421 proc add_to_file {name n} {
422 set out [open $name a]
423 fconfigure $out -translation binary
424 puts -nonewline $out [string repeat x $n]
427 do_test quota-4.5.3 {
428 add_to_file quota-test-B1.txt 123
429 sqlite3_quota_file quota-test-B1.txt
430 quota_size $::quotagroup
432 do_test quota-4.5.4 {
433 add_to_file quota-test-B2.txt 234
434 sqlite3_quota_file quota-test-B2.txt
435 quota_size $::quotagroup
437 do_test quota-4.5.5 {
438 add_to_file quota-test-B1.txt 2000
439 sqlite3_quota_file quota-test-B1.txt
440 quota_size $::quotagroup
442 do_test quota-4.5.6 {
443 forcedelete quota-test-B1.txt
444 sqlite3_quota_file quota-test-B1.txt
445 quota_size $::quotagroup
447 do_test quota-4.5.7 {
448 forcedelete quota-test-B2.txt
449 sqlite3_quota_file quota-test-B2.txt
450 quota_size $::quotagroup
452 do_test quota-4.5.8 {
453 add_to_file quota-test-B3.txt 1234
454 sqlite3_quota_file quota-test-B3.txt
455 quota_size $::quotagroup
457 do_test quota-4.5.9 {
458 sqlite3_quota_set $quotagroup 0 {}
459 quota_size $::quotagroup
462 do_test quota-4.9.1 {
464 sqlite3_quota_set A 1000 quota_callback
465 sqlite3_quota_shutdown
467 do_test quota-4.9.2 {
471 #-------------------------------------------------------------------------
472 # The following tests test that the quota VFS handles malloc and IO
476 sqlite3_quota_initialize "" 1
477 sqlite3_quota_set *test.db 4096 {}
479 do_faultsim_test quota-5.1 -prep {
484 do_faultsim_test quota-5.2 -prep {
493 do_test quota-5.3.prep {
496 PRAGMA auto_vacuum = 1;
497 PRAGMA page_size = 1024;
498 CREATE TABLE t1(a, b);
499 INSERT INTO t1 VALUES(10, zeroblob(1200));
501 faultsim_save_and_close
503 do_faultsim_test quota-5.3 -prep {
504 faultsim_restore_and_reopen
506 execsql { DELETE FROM t1 }
509 do_test quota-5.4.1 {
513 list [catch { sqlite3 db test.db } msg] $msg
514 } {1 {unable to open database file}}
516 do_faultsim_test quota-5.5 -prep {
517 catch { sqlite3_quota_shutdown }
519 sqlite3_quota_initialize "" 1
522 do_faultsim_test quota-5.6 -prep {
523 catch { sqlite3_quota_shutdown }
524 sqlite3_quota_initialize "" 1
526 sqlite3_quota_set * 4096 {}
529 catch { sqlite3_quota_shutdown }