blob: 8682bd86a331441e1b496b0201bd88b4600ba3db [file] [log] [blame]
drha76e8582011-12-01 20:48:15 +00001# 2011 December 1
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
12
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
mistachkin4a41f342012-03-06 03:00:49 +000015
16# If SQLITE_CURDIR is not defined, omit this file.
17ifcapable !curdir {
18 finish_test
19 return
20}
21
drha76e8582011-12-01 20:48:15 +000022source $testdir/malloc_common.tcl
23
24db close
25sqlite3_quota_initialize "" 1
26
drh69b22322011-12-03 00:13:06 +000027foreach dir {quota2a/x1 quota2a/x2 quota2a quota2b quota2c} {
mistachkin9ac99312013-09-13 23:26:47 +000028 forcedelete $dir
drh69b22322011-12-03 00:13:06 +000029}
30foreach dir {quota2a quota2a/x1 quota2a/x2 quota2b quota2c} {
drheff14332011-12-02 15:27:41 +000031 file mkdir $dir
32}
drha76e8582011-12-01 20:48:15 +000033
drhc8ccda62011-12-01 22:07:22 +000034# The standard_path procedure converts a pathname into a standard format
35# that is the same across platforms.
36#
37unset -nocomplain ::quota_pwd ::quota_mapping
mistachkinf8a78462012-03-08 20:00:36 +000038set ::quota_pwd [string map {\\ /} [get_pwd]]
drhc8ccda62011-12-01 22:07:22 +000039set ::quota_mapping [list $::quota_pwd PWD]
40proc standard_path {x} {
41 set x [string map {\\ /} $x]
42 return [string map $::quota_mapping $x]
43}
44
drha76e8582011-12-01 20:48:15 +000045# The quota_check procedure is a callback from the quota handler.
46# It has three arguments which are (1) the full pathname of the file
47# that has gone over quota, (2) the quota limit, (3) the requested
48# new quota size to cover the last write. These three values are
49# appended to the global variable $::quota. The filename is processed
50# to convert every \ character into / and to change the name of the
51# working directory to PWD.
52#
53# The quota is increased to the request if the ::quota_request_ok
54# global variable is true.
55#
56set ::quota {}
57set ::quota_request_ok 0
drhc8ccda62011-12-01 22:07:22 +000058
drha76e8582011-12-01 20:48:15 +000059proc quota_check {filename limitvar size} {
60 upvar $limitvar limit
drhc8ccda62011-12-01 22:07:22 +000061 lappend ::quota [standard_path $filename] [set limit] $size
drha76e8582011-12-01 20:48:15 +000062 if {$::quota_request_ok} {set limit $size}
63}
64
65sqlite3_quota_set */quota2a/* 4000 quota_check
66sqlite3_quota_set */quota2b/* 5000 quota_check
67
68unset -nocomplain bigtext
69for {set i 1} {$i<=1000} {incr i} {
70 if {$i%10==0} {
71 append bigtext [format "%06d\n" $i]
72 } else {
73 append bigtext [format "%06d " $i]
74 }
75}
76
77catch { unset h1 }
78catch { unset x }
79do_test quota2-1.1 {
drh0661ca62011-12-01 22:12:58 +000080 set ::h1 [sqlite3_quota_fopen quota2a/xyz.txt w+b]
drha76e8582011-12-01 20:48:15 +000081 sqlite3_quota_fwrite $::h1 1 7000 $bigtext
82} {4000}
83do_test quota2-1.2 {
84 set ::quota
85} {PWD/quota2a/xyz.txt 4000 7000}
drhc00ce492012-04-10 17:53:47 +000086do_test quota2-1.2.1 {
87 sqlite3_quota_file_size $::h1
88} {4000}
89do_test quota2-1.2.2 {
90 sqlite3_quota_fflush $::h1 1
91 sqlite3_quota_file_truesize $::h1
92} {4000}
drha76e8582011-12-01 20:48:15 +000093do_test quota2-1.3 {
94 sqlite3_quota_rewind $::h1
95 set ::x [sqlite3_quota_fread $::h1 1001 7]
96 string length $::x
97} {3003}
98do_test quota2-1.4 {
99 string match $::x [string range $::bigtext 0 3002]
100} {1}
101do_test quota2-1.5 {
102 sqlite3_quota_fseek $::h1 0 SEEK_END
103 sqlite3_quota_ftell $::h1
104} {4000}
105do_test quota2-1.6 {
106 sqlite3_quota_fseek $::h1 -100 SEEK_END
107 sqlite3_quota_ftell $::h1
108} {3900}
109do_test quota2-1.7 {
110 sqlite3_quota_fseek $::h1 -100 SEEK_CUR
111 sqlite3_quota_ftell $::h1
112} {3800}
113do_test quota2-1.8 {
114 sqlite3_quota_fseek $::h1 50 SEEK_CUR
115 sqlite3_quota_ftell $::h1
116} {3850}
117do_test quota2-1.9 {
118 sqlite3_quota_fseek $::h1 50 SEEK_SET
119 sqlite3_quota_ftell $::h1
120} {50}
121do_test quota2-1.10 {
122 sqlite3_quota_rewind $::h1
123 sqlite3_quota_ftell $::h1
124} {0}
125do_test quota2-1.11 {
drhc8ccda62011-12-01 22:07:22 +0000126 standard_path [sqlite3_quota_dump]
drha76e8582011-12-01 20:48:15 +0000127} {{*/quota2b/* 5000 0} {*/quota2a/* 4000 4000 {PWD/quota2a/xyz.txt 4000 1 0}}}
drh69b22322011-12-03 00:13:06 +0000128do_test quota2-1.12 {
drhc00ce492012-04-10 17:53:47 +0000129 sqlite3_quota_ftruncate $::h1 3500
130 sqlite3_quota_file_size $::h1
131} {3500}
132do_test quota2-1.13 {
133 sqlite3_quota_file_truesize $::h1
134} {3500}
135do_test quota2-1.14 {
136 standard_path [sqlite3_quota_dump]
137} {{*/quota2b/* 5000 0} {*/quota2a/* 4000 3500 {PWD/quota2a/xyz.txt 3500 1 0}}}
138do_test quota2-1.15 {
139 sqlite3_quota_fseek $::h1 0 SEEK_END
140 sqlite3_quota_ftell $::h1
141} {3500}
142do_test quota2-1.16 {
143 sqlite3_quota_fwrite $::h1 1 7000 $bigtext
144} {500}
145do_test quota2-1.17 {
146 sqlite3_quota_ftell $::h1
147} {4000}
148do_test quota2-1.18 {
149 sqlite3_quota_file_size $::h1
150} {4000}
151do_test quota2-1.19 {
152 sqlite3_quota_fflush $::h1 1
153 sqlite3_quota_file_truesize $::h1
154} {4000}
155do_test quota2-1.20 {
drha76e8582011-12-01 20:48:15 +0000156 sqlite3_quota_fclose $::h1
drhc8ccda62011-12-01 22:07:22 +0000157 standard_path [sqlite3_quota_dump]
drha76e8582011-12-01 20:48:15 +0000158} {{*/quota2b/* 5000 0} {*/quota2a/* 4000 4000 {PWD/quota2a/xyz.txt 4000 0 0}}}
drhc00ce492012-04-10 17:53:47 +0000159do_test quota2-1.21 {
drha76e8582011-12-01 20:48:15 +0000160 sqlite3_quota_remove quota2a/xyz.txt
drhc8ccda62011-12-01 22:07:22 +0000161 standard_path [sqlite3_quota_dump]
drha76e8582011-12-01 20:48:15 +0000162} {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
163
164
drhc00ce492012-04-10 17:53:47 +0000165
drheff14332011-12-02 15:27:41 +0000166set quota {}
167do_test quota2-2.1 {
168 set ::h1 [sqlite3_quota_fopen quota2c/xyz.txt w+b]
169 sqlite3_quota_fwrite $::h1 1 7000 $bigtext
170} {7000}
171do_test quota2-2.2 {
172 set ::quota
173} {}
drh98c78ea2012-06-05 13:56:15 +0000174do_test quota2-2.3.1 {
drheff14332011-12-02 15:27:41 +0000175 sqlite3_quota_rewind $::h1
drh98c78ea2012-06-05 13:56:15 +0000176 sqlite3_quota_file_available $::h1
177} {7000}
178do_test quota2-2.3.2 {
drheff14332011-12-02 15:27:41 +0000179 set ::x [sqlite3_quota_fread $::h1 1001 7]
180 string length $::x
181} {6006}
drh98c78ea2012-06-05 13:56:15 +0000182do_test quota2-2.3.3 {
183 sqlite3_quota_file_available $::h1
184} {0}
drheff14332011-12-02 15:27:41 +0000185do_test quota2-2.4 {
186 string match $::x [string range $::bigtext 0 6005]
187} {1}
188do_test quota2-2.5 {
189 sqlite3_quota_fseek $::h1 0 SEEK_END
190 sqlite3_quota_ftell $::h1
191} {7000}
192do_test quota2-2.6 {
193 sqlite3_quota_fseek $::h1 -100 SEEK_END
194 sqlite3_quota_ftell $::h1
195} {6900}
drh98c78ea2012-06-05 13:56:15 +0000196do_test quota2-2.6.1 {
197 sqlite3_quota_file_available $::h1
198} {100}
drheff14332011-12-02 15:27:41 +0000199do_test quota2-2.7 {
200 sqlite3_quota_fseek $::h1 -100 SEEK_CUR
201 sqlite3_quota_ftell $::h1
202} {6800}
drh98c78ea2012-06-05 13:56:15 +0000203do_test quota2-2.7.1 {
204 sqlite3_quota_file_available $::h1
205} {200}
drheff14332011-12-02 15:27:41 +0000206do_test quota2-2.8 {
207 sqlite3_quota_fseek $::h1 50 SEEK_CUR
208 sqlite3_quota_ftell $::h1
209} {6850}
drh98c78ea2012-06-05 13:56:15 +0000210do_test quota2-2.8.1 {
211 sqlite3_quota_file_available $::h1
212} {150}
drheff14332011-12-02 15:27:41 +0000213do_test quota2-2.9 {
214 sqlite3_quota_fseek $::h1 50 SEEK_SET
215 sqlite3_quota_ftell $::h1
216} {50}
drh98c78ea2012-06-05 13:56:15 +0000217do_test quota2-2.9.1 {
218 sqlite3_quota_file_available $::h1
219} {6950}
drheff14332011-12-02 15:27:41 +0000220do_test quota2-2.10 {
221 sqlite3_quota_rewind $::h1
222 sqlite3_quota_ftell $::h1
223} {0}
drh98c78ea2012-06-05 13:56:15 +0000224do_test quota2-2.10.1 {
225 sqlite3_quota_file_available $::h1
226} {7000}
227do_test quota2-2.10.2 {
228 sqlite3_quota_ferror $::h1
229} {0}
drheff14332011-12-02 15:27:41 +0000230do_test quota2-2.11 {
231 standard_path [sqlite3_quota_dump]
232} {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
drh69b22322011-12-03 00:13:06 +0000233do_test quota2-2.12 {
drheff14332011-12-02 15:27:41 +0000234 sqlite3_quota_fclose $::h1
235 standard_path [sqlite3_quota_dump]
236} {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
drha76e8582011-12-01 20:48:15 +0000237
drh69b22322011-12-03 00:13:06 +0000238do_test quota2-3.1 {
239 sqlite3_quota_set */quota2b/* 0 quota_check
240 set ::h1 [sqlite3_quota_fopen quota2a/x1/a.txt a]
241 sqlite3_quota_fwrite $::h1 10 10 $bigtext
242} {10}
243do_test quota2-3.2 {
244 standard_path [sqlite3_quota_dump]
245} {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
drh27cec372011-12-13 23:26:10 +0000246do_test quota2-3.3a {
247 sqlite3_quota_fflush $::h1 0
248 standard_path [sqlite3_quota_dump]
249} {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
250do_test quota2-3.3b {
251 sqlite3_quota_fflush $::h1 1
252 standard_path [sqlite3_quota_dump]
253} {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
254do_test quota2-3.3c {
drh69b22322011-12-03 00:13:06 +0000255 sqlite3_quota_fflush $::h1
256 standard_path [sqlite3_quota_dump]
257} {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
258do_test quota2-3.4 {
259 sqlite3_quota_fclose $::h1
260 standard_path [sqlite3_quota_dump]
261} {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 0 0}}}
262do_test quota2-3.5 {
263 set ::h2 [sqlite3_quota_fopen quota2a/x2/b.txt a]
264 sqlite3_quota_fwrite $::h2 10 20 $bigtext
265 standard_path [sqlite3_quota_dump]
266} {{*/quota2a/* 4000 300 {PWD/quota2a/x2/b.txt 200 1 0} {PWD/quota2a/x1/a.txt 100 0 0}}}
267do_test quota2-3.6 {
268 set ::h3 [sqlite3_quota_fopen quota2a/x1/c.txt a]
269 sqlite3_quota_fwrite $::h3 10 50 $bigtext
270 standard_path [sqlite3_quota_dump]
271} {{*/quota2a/* 4000 800 {PWD/quota2a/x1/c.txt 500 1 0} {PWD/quota2a/x2/b.txt 200 1 0} {PWD/quota2a/x1/a.txt 100 0 0}}}
272do_test quota2-3.7 {
273 file exists quota2a/x1/a.txt
274} {1}
275do_test quota2-3.8 {
276 file exists quota2a/x2/b.txt
277} {1}
278do_test quota2-3.9 {
279 file exists quota2a/x1/c.txt
280} {1}
281do_test quota2-3.10 {
282 sqlite3_quota_remove quota2a/x1
283 standard_path [sqlite3_quota_dump]
284} {{*/quota2a/* 4000 700 {PWD/quota2a/x1/c.txt 500 1 1} {PWD/quota2a/x2/b.txt 200 1 0}}}
285do_test quota2-3.11 {
286 sqlite3_quota_fclose $::h2
287 sqlite3_quota_fclose $::h3
288 standard_path [sqlite3_quota_dump]
289} {{*/quota2a/* 4000 200 {PWD/quota2a/x2/b.txt 200 0 0}}}
290do_test quota2-3.12 {
291 file exists quota2a/x1/a.txt
292} {0}
293do_test quota2-3.13 {
294 file exists quota2a/x2/b.txt
295} {1}
296do_test quota2-3.14 {
297 file exists quota2a/x1/c.txt
298} {0}
299
drha76e8582011-12-01 20:48:15 +0000300catch { sqlite3_quota_shutdown }
301catch { unset quota_request_ok }
302finish_test