JS error message and doc typos reported in the forum. No code changes.
[sqlite.git] / test / quota2.test
blob8682bd86a331441e1b496b0201bd88b4600ba3db
1 # 2011 December 1
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.
17 ifcapable !curdir {
18   finish_test
19   return
22 source $testdir/malloc_common.tcl
24 db close
25 sqlite3_quota_initialize "" 1
27 foreach dir {quota2a/x1 quota2a/x2 quota2a quota2b quota2c} {
28   forcedelete $dir
30 foreach dir {quota2a quota2a/x1 quota2a/x2 quota2b quota2c} {
31   file mkdir $dir
34 # The standard_path procedure converts a pathname into a standard format
35 # that is the same across platforms.
37 unset -nocomplain ::quota_pwd ::quota_mapping
38 set ::quota_pwd [string map {\\ /} [get_pwd]]
39 set ::quota_mapping [list $::quota_pwd PWD]
40 proc standard_path {x} {
41   set x [string map {\\ /} $x]
42   return [string map $::quota_mapping $x]
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.  
53 # The quota is increased to the request if the ::quota_request_ok 
54 # global variable is true.
56 set ::quota {}
57 set ::quota_request_ok 0
59 proc 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}
65 sqlite3_quota_set */quota2a/* 4000 quota_check
66 sqlite3_quota_set */quota2b/* 5000 quota_check
68 unset -nocomplain bigtext
69 for {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   }
77 catch { unset h1 }
78 catch { unset x }
79 do_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}
83 do_test quota2-1.2 {
84   set ::quota
85 } {PWD/quota2a/xyz.txt 4000 7000}
86 do_test quota2-1.2.1 {
87   sqlite3_quota_file_size $::h1
88 } {4000}
89 do_test quota2-1.2.2 {
90   sqlite3_quota_fflush $::h1 1
91   sqlite3_quota_file_truesize $::h1
92 } {4000}
93 do_test quota2-1.3 {
94   sqlite3_quota_rewind $::h1
95   set ::x [sqlite3_quota_fread $::h1 1001 7]
96   string length $::x
97 } {3003}
98 do_test quota2-1.4 {
99   string match $::x [string range $::bigtext 0 3002]
100 } {1}
101 do_test quota2-1.5 {
102   sqlite3_quota_fseek $::h1 0 SEEK_END
103   sqlite3_quota_ftell $::h1
104 } {4000}
105 do_test quota2-1.6 {
106   sqlite3_quota_fseek $::h1 -100 SEEK_END
107   sqlite3_quota_ftell $::h1
108 } {3900}
109 do_test quota2-1.7 {
110   sqlite3_quota_fseek $::h1 -100 SEEK_CUR
111   sqlite3_quota_ftell $::h1
112 } {3800}
113 do_test quota2-1.8 {
114   sqlite3_quota_fseek $::h1 50 SEEK_CUR
115   sqlite3_quota_ftell $::h1
116 } {3850}
117 do_test quota2-1.9 {
118   sqlite3_quota_fseek $::h1 50 SEEK_SET
119   sqlite3_quota_ftell $::h1
120 } {50}
121 do_test quota2-1.10 {
122   sqlite3_quota_rewind $::h1
123   sqlite3_quota_ftell $::h1
124 } {0}
125 do_test quota2-1.11 {
126   standard_path [sqlite3_quota_dump]
127 } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 4000 {PWD/quota2a/xyz.txt 4000 1 0}}}
128 do_test quota2-1.12 {
129   sqlite3_quota_ftruncate $::h1 3500
130   sqlite3_quota_file_size $::h1
131 } {3500}
132 do_test quota2-1.13 {
133   sqlite3_quota_file_truesize $::h1
134 } {3500}
135 do_test quota2-1.14 {
136   standard_path [sqlite3_quota_dump]
137 } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 3500 {PWD/quota2a/xyz.txt 3500 1 0}}}
138 do_test quota2-1.15 {
139   sqlite3_quota_fseek $::h1 0 SEEK_END
140   sqlite3_quota_ftell $::h1
141 } {3500}
142 do_test quota2-1.16 {
143   sqlite3_quota_fwrite $::h1 1 7000 $bigtext
144 } {500}
145 do_test quota2-1.17 {
146   sqlite3_quota_ftell $::h1
147 } {4000}
148 do_test quota2-1.18 {
149   sqlite3_quota_file_size $::h1
150 } {4000}
151 do_test quota2-1.19 {
152   sqlite3_quota_fflush $::h1 1
153   sqlite3_quota_file_truesize $::h1
154 } {4000}
155 do_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}}}
159 do_test quota2-1.21 {
160   sqlite3_quota_remove quota2a/xyz.txt
161   standard_path [sqlite3_quota_dump]
162 } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
166 set quota {}
167 do_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}
171 do_test quota2-2.2 {
172   set ::quota
173 } {}
174 do_test quota2-2.3.1 {
175   sqlite3_quota_rewind $::h1
176   sqlite3_quota_file_available $::h1
177 } {7000}
178 do_test quota2-2.3.2 {
179   set ::x [sqlite3_quota_fread $::h1 1001 7]
180   string length $::x
181 } {6006}
182 do_test quota2-2.3.3 {
183   sqlite3_quota_file_available $::h1
184 } {0}
185 do_test quota2-2.4 {
186   string match $::x [string range $::bigtext 0 6005]
187 } {1}
188 do_test quota2-2.5 {
189   sqlite3_quota_fseek $::h1 0 SEEK_END
190   sqlite3_quota_ftell $::h1
191 } {7000}
192 do_test quota2-2.6 {
193   sqlite3_quota_fseek $::h1 -100 SEEK_END
194   sqlite3_quota_ftell $::h1
195 } {6900}
196 do_test quota2-2.6.1 {
197   sqlite3_quota_file_available $::h1
198 } {100}
199 do_test quota2-2.7 {
200   sqlite3_quota_fseek $::h1 -100 SEEK_CUR
201   sqlite3_quota_ftell $::h1
202 } {6800}
203 do_test quota2-2.7.1 {
204   sqlite3_quota_file_available $::h1
205 } {200}
206 do_test quota2-2.8 {
207   sqlite3_quota_fseek $::h1 50 SEEK_CUR
208   sqlite3_quota_ftell $::h1
209 } {6850}
210 do_test quota2-2.8.1 {
211   sqlite3_quota_file_available $::h1
212 } {150}
213 do_test quota2-2.9 {
214   sqlite3_quota_fseek $::h1 50 SEEK_SET
215   sqlite3_quota_ftell $::h1
216 } {50}
217 do_test quota2-2.9.1 {
218   sqlite3_quota_file_available $::h1
219 } {6950}
220 do_test quota2-2.10 {
221   sqlite3_quota_rewind $::h1
222   sqlite3_quota_ftell $::h1
223 } {0}
224 do_test quota2-2.10.1 {
225   sqlite3_quota_file_available $::h1
226 } {7000}
227 do_test quota2-2.10.2 {
228   sqlite3_quota_ferror $::h1
229 } {0}
230 do_test quota2-2.11 {
231   standard_path [sqlite3_quota_dump]
232 } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
233 do_test quota2-2.12 {
234   sqlite3_quota_fclose $::h1
235   standard_path [sqlite3_quota_dump]
236 } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
238 do_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}
243 do_test quota2-3.2 {
244   standard_path [sqlite3_quota_dump]
245 } {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
246 do_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}}}
250 do_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}}}
254 do_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}}}
258 do_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}}}
262 do_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}}}
267 do_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}}}
272 do_test quota2-3.7 {
273   file exists quota2a/x1/a.txt
274 } {1}
275 do_test quota2-3.8 {
276   file exists quota2a/x2/b.txt
277 } {1}
278 do_test quota2-3.9 {
279   file exists quota2a/x1/c.txt
280 } {1}
281 do_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}}}
285 do_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}}}
290 do_test quota2-3.12 {
291   file exists quota2a/x1/a.txt
292 } {0}
293 do_test quota2-3.13 {
294   file exists quota2a/x2/b.txt
295 } {1}
296 do_test quota2-3.14 {
297   file exists quota2a/x1/c.txt
298 } {0}
300 catch { sqlite3_quota_shutdown }
301 catch { unset quota_request_ok }
302 finish_test