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
24 unset -nocomplain defaultVfs
25 set defaultVfs [file_control_vfsname db]
28 do_test quota-1.1 { sqlite3_quota_initialize nosuchvfs 1 } {SQLITE_ERROR}
29 do_test quota-1.2 { sqlite3_quota_initialize "" 1 } {SQLITE_OK}
30 do_test quota-1.3 { sqlite3_quota_initialize "" 1 } {SQLITE_MISUSE}
31 do_test quota-1.4 { sqlite3_quota_shutdown } {SQLITE_OK}
33 do_test quota-1.5 { sqlite3_quota_initialize "" 0 } {SQLITE_OK}
34 do_test quota-1.6 { sqlite3_quota_shutdown } {SQLITE_OK}
35 do_test quota-1.7 { sqlite3_quota_initialize "" 1 } {SQLITE_OK}
36 do_test quota-1.8 { sqlite3_quota_shutdown } {SQLITE_OK}
39 #-------------------------------------------------------------------------
40 # Some simple warm-body tests with a single database file in rollback
43 # quota-2.1.*: Test that SQLITE_FULL is returned if the database would
44 # exceed the configured quota.
46 # quota-2.2.*: Test that SQLITE_FULL is not returned and the database
47 # grows if the callback extends the quota when the database
48 # attempts to grow beyond the configured quota.
50 # quota-2.3.*: Open and close a db that is not part of any quota group. At
51 # one point this was causing mutex refs to be leaked.
53 # quota-2.4.*: Try to shutdown the quota system before closing the db
54 # file. Check that this fails and the quota system still works
55 # afterwards. Then close the database and successfully shut
56 # down the quota system.
58 sqlite3_quota_initialize "" 1
60 unset -nocomplain quota_request_ok
61 proc quota_check {filename limitvar size} {
64 lappend ::quota [set limit] $size
65 if {[info exists ::quota_request_ok]} { set limit $size }
69 sqlite3_quota_set *test.db 4096 quota_check
74 PRAGMA page_size=1024;
75 PRAGMA auto_vacuum=OFF;
76 PRAGMA journal_mode=DELETE;
80 CREATE TABLE t1(a, b);
81 INSERT INTO t1 VALUES(1, randomblob(1100));
82 INSERT INTO t1 VALUES(2, randomblob(1100));
86 do_test quota-2.1.2.1 {
87 file_control_vfsname db
89 do_test quota-2.1.3 { file size test.db } {4096}
91 catchsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
92 } {1 {database or disk is full}}
93 do_test quota-2.1.5 { set ::quota } {4096 5120}
95 set ::quota_request_ok 1
98 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
100 do_test quota-2.2.2 { set ::quota } {4096 5120}
101 do_test quota-2.2.3 { file size test.db } {5120}
102 unset ::quota_request_ok
104 do_test quota-2.3.1 {
109 do_test quota-2.4.1 {
110 sqlite3_quota_shutdown
113 do_test quota-2.4.2 {
114 catchsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
115 } {1 {database or disk is full}}
116 do_test quota-2.4.3 { set ::quota } {5120 6144}
117 do_test quota-2.4.4 { file size test.db } {5120}
118 do_test quota-2.4.99 {
120 sqlite3_quota_shutdown
123 #-------------------------------------------------------------------------
124 # Try some tests with more than one connection to a database file. Still
127 # quota-3.1.*: Two connections to a single database file.
129 # quota-3.2.*: Two connections to each of several database files (that
130 # are in the same quota group).
132 proc quota_check {filename limitvar size} {
133 upvar $limitvar limit
134 lappend ::quota [set limit] $size
135 if {[info exists ::quota_request_ok]} { set limit $size }
138 do_test quota-3.1.1 {
140 sqlite3_quota_initialize "" 1
141 sqlite3_quota_set *test.db 4096 quota_check
143 do_test quota-3.1.2 {
146 PRAGMA page_size = 1024;
147 PRAGMA journal_mode = delete;
148 PRAGMA auto_vacuum = off;
149 CREATE TABLE t1(a PRIMARY KEY, b);
150 INSERT INTO t1 VALUES(1, 'one');
154 do_test quota-3.1.3 {
157 execsql { CREATE TABLE t2(a, b) } db2
160 do_test quota-3.1.4 {
161 catchsql { CREATE TABLE t3(a, b) }
162 } {1 {database or disk is full}}
163 do_test quota-3.1.5 {
164 set ::quota_request_ok 1
165 execsql { CREATE TABLE t3(a, b) }
167 do_test quota-3.1.6 {
170 sqlite3_quota_set *test.db 0 {}
173 do_test quota-3.2.1 {
174 delete_file force test.db test2.db
176 sqlite3_quota_set * 4096 {}
178 sqlite3 db2a test2.db
180 foreach db {db1a db2a} {
182 PRAGMA page_size = 1024;
183 PRAGMA journal_mode = delete;
184 PRAGMA auto_vacuum = off;
185 CREATE TABLE t1(a, b);
190 sqlite3 db2b test2.db
192 list [file size test.db] [file size test2.db]
195 catch { unset ::quota_request_ok }
197 do_test quota-3.2.2 { execsql { INSERT INTO t1 VALUES('x', 'y') } db1a } {}
198 do_test quota-3.2.3 { execsql { INSERT INTO t1 VALUES('v', 'w') } db1b } {}
199 do_test quota-3.2.4 { execsql { INSERT INTO t1 VALUES('t', 'u') } db2a } {}
200 do_test quota-3.2.5 { execsql { INSERT INTO t1 VALUES('r', 's') } db2b } {}
202 do_test quota-3.2.6 {
203 catchsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
204 } {1 {database or disk is full}}
205 do_test quota-3.2.7 {
206 catchsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
207 } {1 {database or disk is full}}
208 do_test quota-3.2.8 {
209 catchsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
210 } {1 {database or disk is full}}
211 do_test quota-3.2.9 {
212 catchsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
213 } {1 {database or disk is full}}
216 proc quota_callback {file limitvar size} {
217 upvar $limitvar limit
218 if {$::tcl_platform(platform)=="windows"} {
219 set file [ lindex [string map {\\ \/} $file] 0 ]
221 lappend ::quota $file $size
224 sqlite3_quota_set * 4096 quota_callback
225 do_test quota-3.3.1 {
226 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
227 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
228 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
229 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
231 } [list [file join [get_pwd] test.db] 5120]
233 do_test quota-3.2.X {
234 foreach db {db1a db2a db2b db1b} { catch { $db close } }
235 sqlite3_quota_set * 0 {}
238 #-------------------------------------------------------------------------
239 # Quotas are deleted when unused and when their limit is set to zero
242 # Return a list of all currently defined quotas. Each quota is identified
246 foreach q [sqlite3_quota_dump] {
247 lappend allq [lindex $q 0]
251 proc quota_size {name} {
253 foreach q [sqlite3_quota_dump] {
254 if {[lindex $q 0]==$name} {return [lindex $q 2]}
259 do_test quota-4.1.1 {
260 sqlite3_quota_set *test.db 0 {}
263 do_test quota-4.1.2 {
264 sqlite3_quota_set *test.db 4096 {}
267 do_test quota-4.1.3 {
268 sqlite3_quota_set *test2.db 0 {}
271 do_test quota-4.1.4 {
272 sqlite3_quota_set *test2.db 100000 {}
274 } {*test.db *test2.db}
275 do_test quota-4.1.5 {
276 sqlite3_quota_set *test.db 0 {}
279 do_test quota-4.1.6 {
280 forcedelete test2.db test2.db-journal test2.db-wal
282 db eval {CREATE TABLE t2(x); INSERT INTO t2 VALUES('tab-t2');}
285 do_test quota-4.1.7 {
286 catchsql {INSERT INTO t2 VALUES(zeroblob(200000))}
287 } {1 {database or disk is full}}
288 do_test quota-4.1.8 {
290 db2 eval {SELECT * FROM t2}
292 do_test quota-4.1.9 {
293 sqlite3_quota_set *test2.db 0 {}
294 catchsql {INSERT INTO t2 VALUES(zeroblob(200000))}
296 do_test quota-4.1.10 {
299 do_test quota-4.1.11 {
303 do_test quota-4.1.12 {
308 do_test quota-4.2.1 {
309 sqlite3_quota_set A 1000 {}
310 sqlite3_quota_set B 1000 {}
311 sqlite3_quota_set C 1000 {}
312 sqlite3_quota_set D 1000 {}
315 do_test quota-4.2.2 {
316 sqlite3_quota_set C 0 {}
317 sqlite3_quota_set B 0 {}
320 do_test quota-4.2.3 {
321 sqlite3_quota_set A 0 {}
322 sqlite3_quota_set D 0 {}
325 do_test quota-4.2.4 {
326 sqlite3_quota_set A 1000 {}
327 sqlite3_quota_set B 1000 {}
328 sqlite3_quota_set C 1000 {}
329 sqlite3_quota_set A 0 {}
330 sqlite3_quota_set B 0 {}
331 sqlite3_quota_set C 0 {}
334 do_test quota-4.2.5 {
335 sqlite3_quota_set A 1000 {}
336 sqlite3_quota_set B 1000 {}
337 sqlite3_quota_set C 1000 {}
338 sqlite3_quota_set C 0 {}
339 sqlite3_quota_set B 0 {}
340 sqlite3_quota_set A 0 {}
344 do_test quota-4.3.1 {
345 sqlite3_quota_set A 1000 quota_callback
347 sqlite3_quota_set A 0 quota_callback
352 unset -nocomplain quotagroup
353 if {$tcl_platform(platform)=="windows"} {
354 set quotagroup *\\quota-test-A?.db
356 set quotagroup */quota-test-A?.db
358 foreach file [glob -nocomplain quota-test-A*] {
361 do_test quota-4.4.1 {
363 sqlite3_quota_set $::quotagroup 10000 quota_callback
364 forcedelete ./quota-test-A1.db ./quota-test-A2.db
365 sqlite3 db ./quota-test-A1.db
368 INSERT INTO t1 VALUES(randomblob(5000));
372 do_test quota-4.4.2 {
375 do_test quota-4.4.3 {
377 sqlite3 db ./quota-test-A2.db
380 INSERT INTO t1 VALUES(randomblob(5000));
384 do_test quota-4.4.4 {
387 do_test quota-4.4.5 {
389 sqlite3_quota_set $::quotagroup 0 {}
392 do_test quota-4.4.6 {
393 sqlite3_quota_set $quotagroup 10000 quota_callback
394 sqlite3 db quota-test-A1.db
395 db eval {SELECT count(*) FROM sqlite_master}
396 quota_size $quotagroup
397 } [file size quota-test-A1.db]
398 do_test quota-4.4.7 {
399 sqlite3_quota_file quota-test-A2.db
400 quota_size $::quotagroup
401 } [expr {[file size quota-test-A1.db]+[file size quota-test-A2.db]}]
403 unset -nocomplain quotagroup
404 if {$tcl_platform(platform)=="windows"} {
405 set quotagroup *\\quota-test-B*
407 set quotagroup */quota-test-B*
409 foreach file [glob -nocomplain quota-test-B*] {
412 do_test quota-4.5.1 {
413 sqlite3_quota_set $::quotagroup 100000 quota_callback
414 quota_size $::quotagroup
416 do_test quota-4.5.2 {
417 sqlite3_quota_file quota-test-B1.txt
418 quota_size $::quotagroup
420 proc add_to_file {name n} {
421 set out [open $name a]
422 fconfigure $out -translation binary
423 puts -nonewline $out [string repeat x $n]
426 do_test quota-4.5.3 {
427 add_to_file quota-test-B1.txt 123
428 sqlite3_quota_file quota-test-B1.txt
429 quota_size $::quotagroup
431 do_test quota-4.5.4 {
432 add_to_file quota-test-B2.txt 234
433 sqlite3_quota_file quota-test-B2.txt
434 quota_size $::quotagroup
436 do_test quota-4.5.5 {
437 add_to_file quota-test-B1.txt 2000
438 sqlite3_quota_file quota-test-B1.txt
439 quota_size $::quotagroup
441 do_test quota-4.5.6 {
442 forcedelete quota-test-B1.txt
443 sqlite3_quota_file quota-test-B1.txt
444 quota_size $::quotagroup
446 do_test quota-4.5.7 {
447 forcedelete quota-test-B2.txt
448 sqlite3_quota_file quota-test-B2.txt
449 quota_size $::quotagroup
451 do_test quota-4.5.8 {
452 add_to_file quota-test-B3.txt 1234
453 sqlite3_quota_file quota-test-B3.txt
454 quota_size $::quotagroup
456 do_test quota-4.5.9 {
457 sqlite3_quota_set $quotagroup 0 {}
458 quota_size $::quotagroup
461 do_test quota-4.9.1 {
463 sqlite3_quota_set A 1000 quota_callback
464 sqlite3_quota_shutdown
466 do_test quota-4.9.2 {
470 #-------------------------------------------------------------------------
471 # The following tests test that the quota VFS handles malloc and IO
475 sqlite3_quota_initialize "" 1
476 sqlite3_quota_set *test.db 4096 {}
478 do_faultsim_test quota-5.1 -prep {
483 do_faultsim_test quota-5.2 -prep {
492 do_test quota-5.3.prep {
495 PRAGMA auto_vacuum = 1;
496 PRAGMA page_size = 1024;
497 CREATE TABLE t1(a, b);
498 INSERT INTO t1 VALUES(10, zeroblob(1200));
500 faultsim_save_and_close
502 do_faultsim_test quota-5.3 -prep {
503 faultsim_restore_and_reopen
505 execsql { DELETE FROM t1 }
508 do_test quota-5.4.1 {
512 list [catch { sqlite3 db test.db } msg] $msg
513 } {1 {unable to open database file}}
515 do_faultsim_test quota-5.5 -prep {
516 catch { sqlite3_quota_shutdown }
518 sqlite3_quota_initialize "" 1
521 do_faultsim_test quota-5.6 -prep {
522 catch { sqlite3_quota_shutdown }
523 sqlite3_quota_initialize "" 1
525 sqlite3_quota_set * 4096 {}
528 catch { sqlite3_quota_shutdown }