xref: /sqlite-3.40.0/test/quota2.test (revision 9ac99313)
1# 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
15
16# If SQLITE_CURDIR is not defined, omit this file.
17ifcapable !curdir {
18  finish_test
19  return
20}
21
22source $testdir/malloc_common.tcl
23
24db close
25sqlite3_quota_initialize "" 1
26
27foreach dir {quota2a/x1 quota2a/x2 quota2a quota2b quota2c} {
28  forcedelete $dir
29}
30foreach dir {quota2a quota2a/x1 quota2a/x2 quota2b quota2c} {
31  file mkdir $dir
32}
33
34# 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
38set ::quota_pwd [string map {\\ /} [get_pwd]]
39set ::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
45# 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
58
59proc quota_check {filename limitvar size} {
60  upvar $limitvar limit
61  lappend ::quota [standard_path $filename] [set limit] $size
62  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 {
80  set ::h1 [sqlite3_quota_fopen quota2a/xyz.txt w+b]
81  sqlite3_quota_fwrite $::h1 1 7000 $bigtext
82} {4000}
83do_test quota2-1.2 {
84  set ::quota
85} {PWD/quota2a/xyz.txt 4000 7000}
86do_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}
93do_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 {
126  standard_path [sqlite3_quota_dump]
127} {{*/quota2b/* 5000 0} {*/quota2a/* 4000 4000 {PWD/quota2a/xyz.txt 4000 1 0}}}
128do_test quota2-1.12 {
129  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 {
156  sqlite3_quota_fclose $::h1
157  standard_path [sqlite3_quota_dump]
158} {{*/quota2b/* 5000 0} {*/quota2a/* 4000 4000 {PWD/quota2a/xyz.txt 4000 0 0}}}
159do_test quota2-1.21 {
160  sqlite3_quota_remove quota2a/xyz.txt
161  standard_path [sqlite3_quota_dump]
162} {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
163
164
165
166set 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} {}
174do_test quota2-2.3.1 {
175  sqlite3_quota_rewind $::h1
176  sqlite3_quota_file_available $::h1
177} {7000}
178do_test quota2-2.3.2 {
179  set ::x [sqlite3_quota_fread $::h1 1001 7]
180  string length $::x
181} {6006}
182do_test quota2-2.3.3 {
183  sqlite3_quota_file_available $::h1
184} {0}
185do_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}
196do_test quota2-2.6.1 {
197  sqlite3_quota_file_available $::h1
198} {100}
199do_test quota2-2.7 {
200  sqlite3_quota_fseek $::h1 -100 SEEK_CUR
201  sqlite3_quota_ftell $::h1
202} {6800}
203do_test quota2-2.7.1 {
204  sqlite3_quota_file_available $::h1
205} {200}
206do_test quota2-2.8 {
207  sqlite3_quota_fseek $::h1 50 SEEK_CUR
208  sqlite3_quota_ftell $::h1
209} {6850}
210do_test quota2-2.8.1 {
211  sqlite3_quota_file_available $::h1
212} {150}
213do_test quota2-2.9 {
214  sqlite3_quota_fseek $::h1 50 SEEK_SET
215  sqlite3_quota_ftell $::h1
216} {50}
217do_test quota2-2.9.1 {
218  sqlite3_quota_file_available $::h1
219} {6950}
220do_test quota2-2.10 {
221  sqlite3_quota_rewind $::h1
222  sqlite3_quota_ftell $::h1
223} {0}
224do_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}
230do_test quota2-2.11 {
231  standard_path [sqlite3_quota_dump]
232} {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
233do_test quota2-2.12 {
234  sqlite3_quota_fclose $::h1
235  standard_path [sqlite3_quota_dump]
236} {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
237
238do_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}}}
246do_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 {
255  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
300catch { sqlite3_quota_shutdown }
301catch { unset quota_request_ok }
302finish_test
303