summaryrefslogtreecommitdiff
path: root/test/quota.test
blob: 816dec8b8f4a3f4c09d8e2cb5dff2e02043d40f9 (plain)
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# 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

# If SQLITE_CURDIR is not defined, omit this file.
ifcapable !curdir {
  finish_test
  return
}

source $testdir/malloc_common.tcl

unset -nocomplain defaultVfs
set defaultVfs [file_control_vfsname db]
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

unset -nocomplain quota_request_ok
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.2.1 {
  file_control_vfsname db
} quota/$defaultVfs
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 {
  forcedelete 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 {
  delete_file 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
  if {$::tcl_platform(platform)=="windows"} {
    set file [ lindex [string map {\\ \/} $file] 0 ]
  }
  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 [get_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 their 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]
}
proc quota_size {name} {
  set allq {}
  foreach q [sqlite3_quota_dump] {
    if {[lindex $q 0]==$name} {return [lindex $q 2]}
  }
  return 0
}

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 {
  forcedelete 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
} {}

unset -nocomplain quotagroup
if {$tcl_platform(platform)=="windows"} {
  set quotagroup *\\quota-test-A?.db
} else {
  set quotagroup */quota-test-A?.db
} 
foreach file [glob -nocomplain quota-test-A*] {
  forcedelete $file
}
do_test quota-4.4.1 {
  set ::quota {}
  sqlite3_quota_set $::quotagroup 10000 quota_callback
  file delete -force ./quota-test-A1.db ./quota-test-A2.db
  sqlite3 db ./quota-test-A1.db
  db eval {
     CREATE TABLE t1(x);
     INSERT INTO t1 VALUES(randomblob(5000));
  }
  quota_list
} [list $quotagroup]
do_test quota-4.4.2 {
  expr {$::quota==""}
} {1}
do_test quota-4.4.3 {
  db close
  sqlite3 db ./quota-test-A2.db
  db eval {
     CREATE TABLE t1(x);
     INSERT INTO t1 VALUES(randomblob(5000));
  }
  quota_list
} [list $quotagroup]
do_test quota-4.4.4 {
  expr {$::quota!=""}
} {1}
do_test quota-4.4.5 {
  db close
  sqlite3_quota_set $::quotagroup 0 {}
  sqlite3_quota_dump
} {}
do_test quota-4.4.6 {
  sqlite3_quota_set $quotagroup 10000 quota_callback
  sqlite3 db quota-test-A1.db
  db eval {SELECT count(*) FROM sqlite_master}
  quota_size $quotagroup
} [file size quota-test-A1.db]
do_test quota-4.4.7 {
  sqlite3_quota_file quota-test-A2.db
  quota_size $::quotagroup
} [expr {[file size quota-test-A1.db]+[file size quota-test-A2.db]}]

unset -nocomplain quotagroup
if {$tcl_platform(platform)=="windows"} {
  set quotagroup *\\quota-test-B*
} else {
  set quotagroup */quota-test-B*
} 
foreach file [glob -nocomplain quota-test-B*] {
  forcedelete $file
}
do_test quota-4.5.1 {
  sqlite3_quota_set $::quotagroup 100000 quota_callback
  quota_size $::quotagroup
} {0}
do_test quota-4.5.2 {
  sqlite3_quota_file quota-test-B1.txt
  quota_size $::quotagroup
} {0}
proc add_to_file {name n} {
  set out [open $name a]
  fconfigure $out -translation binary
  puts -nonewline $out [string repeat x $n]
  close $out
}
do_test quota-4.5.3 {
  add_to_file quota-test-B1.txt 123
  sqlite3_quota_file quota-test-B1.txt
  quota_size $::quotagroup
} {123}
do_test quota-4.5.4 {
  add_to_file quota-test-B2.txt 234
  sqlite3_quota_file quota-test-B2.txt
  quota_size $::quotagroup
} {357}
do_test quota-4.5.5 {
  add_to_file quota-test-B1.txt 2000
  sqlite3_quota_file quota-test-B1.txt
  quota_size $::quotagroup
} {2357}
do_test quota-4.5.6 {
  forcedelete quota-test-B1.txt
  sqlite3_quota_file quota-test-B1.txt
  quota_size $::quotagroup
} {234}
do_test quota-4.5.7 {
  forcedelete quota-test-B2.txt
  sqlite3_quota_file quota-test-B2.txt
  quota_size $::quotagroup
} {0}
do_test quota-4.5.8 {
  add_to_file quota-test-B3.txt 1234
  sqlite3_quota_file quota-test-B3.txt
  quota_size $::quotagroup
} {1234}
do_test quota-4.5.9 {
  sqlite3_quota_set $quotagroup 0 {}
  quota_size $::quotagroup
} {0}

do_test quota-4.9.1 {
  db close
  sqlite3_quota_set A 1000 quota_callback
  sqlite3_quota_shutdown
} {SQLITE_OK}
do_test quota-4.9.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 }
forcedelete 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 }
  forcedelete 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