quota2.test 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. set testdir [file dirname $argv0]
  13. source $testdir/tester.tcl
  14. # If SQLITE_CURDIR is not defined, omit this file.
  15. ifcapable !curdir {
  16. finish_test
  17. return
  18. }
  19. source $testdir/malloc_common.tcl
  20. db close
  21. sqlite3_quota_initialize "" 1
  22. foreach dir {quota2a/x1 quota2a/x2 quota2a quota2b quota2c} {
  23. forcedelete $dir
  24. }
  25. foreach dir {quota2a quota2a/x1 quota2a/x2 quota2b quota2c} {
  26. file mkdir $dir
  27. }
  28. # The standard_path procedure converts a pathname into a standard format
  29. # that is the same across platforms.
  30. #
  31. unset -nocomplain ::quota_pwd ::quota_mapping
  32. set ::quota_pwd [string map {\\ /} [get_pwd]]
  33. set ::quota_mapping [list $::quota_pwd PWD]
  34. proc standard_path {x} {
  35. set x [string map {\\ /} $x]
  36. return [string map $::quota_mapping $x]
  37. }
  38. # The quota_check procedure is a callback from the quota handler.
  39. # It has three arguments which are (1) the full pathname of the file
  40. # that has gone over quota, (2) the quota limit, (3) the requested
  41. # new quota size to cover the last write. These three values are
  42. # appended to the global variable $::quota. The filename is processed
  43. # to convert every \ character into / and to change the name of the
  44. # working directory to PWD.
  45. #
  46. # The quota is increased to the request if the ::quota_request_ok
  47. # global variable is true.
  48. #
  49. set ::quota {}
  50. set ::quota_request_ok 0
  51. proc quota_check {filename limitvar size} {
  52. upvar $limitvar limit
  53. lappend ::quota [standard_path $filename] [set limit] $size
  54. if {$::quota_request_ok} {set limit $size}
  55. }
  56. sqlite3_quota_set */quota2a/* 4000 quota_check
  57. sqlite3_quota_set */quota2b/* 5000 quota_check
  58. unset -nocomplain bigtext
  59. for {set i 1} {$i<=1000} {incr i} {
  60. if {$i%10==0} {
  61. append bigtext [format "%06d\n" $i]
  62. } else {
  63. append bigtext [format "%06d " $i]
  64. }
  65. }
  66. catch { unset h1 }
  67. catch { unset x }
  68. do_test quota2-1.1 {
  69. set ::h1 [sqlite3_quota_fopen quota2a/xyz.txt w+b]
  70. sqlite3_quota_fwrite $::h1 1 7000 $bigtext
  71. } {4000}
  72. do_test quota2-1.2 {
  73. set ::quota
  74. } {PWD/quota2a/xyz.txt 4000 7000}
  75. do_test quota2-1.2.1 {
  76. sqlite3_quota_file_size $::h1
  77. } {4000}
  78. do_test quota2-1.2.2 {
  79. sqlite3_quota_fflush $::h1 1
  80. sqlite3_quota_file_truesize $::h1
  81. } {4000}
  82. do_test quota2-1.3 {
  83. sqlite3_quota_rewind $::h1
  84. set ::x [sqlite3_quota_fread $::h1 1001 7]
  85. string length $::x
  86. } {3003}
  87. do_test quota2-1.4 {
  88. string match $::x [string range $::bigtext 0 3002]
  89. } {1}
  90. do_test quota2-1.5 {
  91. sqlite3_quota_fseek $::h1 0 SEEK_END
  92. sqlite3_quota_ftell $::h1
  93. } {4000}
  94. do_test quota2-1.6 {
  95. sqlite3_quota_fseek $::h1 -100 SEEK_END
  96. sqlite3_quota_ftell $::h1
  97. } {3900}
  98. do_test quota2-1.7 {
  99. sqlite3_quota_fseek $::h1 -100 SEEK_CUR
  100. sqlite3_quota_ftell $::h1
  101. } {3800}
  102. do_test quota2-1.8 {
  103. sqlite3_quota_fseek $::h1 50 SEEK_CUR
  104. sqlite3_quota_ftell $::h1
  105. } {3850}
  106. do_test quota2-1.9 {
  107. sqlite3_quota_fseek $::h1 50 SEEK_SET
  108. sqlite3_quota_ftell $::h1
  109. } {50}
  110. do_test quota2-1.10 {
  111. sqlite3_quota_rewind $::h1
  112. sqlite3_quota_ftell $::h1
  113. } {0}
  114. do_test quota2-1.11 {
  115. standard_path [sqlite3_quota_dump]
  116. } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 4000 {PWD/quota2a/xyz.txt 4000 1 0}}}
  117. do_test quota2-1.12 {
  118. sqlite3_quota_ftruncate $::h1 3500
  119. sqlite3_quota_file_size $::h1
  120. } {3500}
  121. do_test quota2-1.13 {
  122. sqlite3_quota_file_truesize $::h1
  123. } {3500}
  124. do_test quota2-1.14 {
  125. standard_path [sqlite3_quota_dump]
  126. } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 3500 {PWD/quota2a/xyz.txt 3500 1 0}}}
  127. do_test quota2-1.15 {
  128. sqlite3_quota_fseek $::h1 0 SEEK_END
  129. sqlite3_quota_ftell $::h1
  130. } {3500}
  131. do_test quota2-1.16 {
  132. sqlite3_quota_fwrite $::h1 1 7000 $bigtext
  133. } {500}
  134. do_test quota2-1.17 {
  135. sqlite3_quota_ftell $::h1
  136. } {4000}
  137. do_test quota2-1.18 {
  138. sqlite3_quota_file_size $::h1
  139. } {4000}
  140. do_test quota2-1.19 {
  141. sqlite3_quota_fflush $::h1 1
  142. sqlite3_quota_file_truesize $::h1
  143. } {4000}
  144. do_test quota2-1.20 {
  145. sqlite3_quota_fclose $::h1
  146. standard_path [sqlite3_quota_dump]
  147. } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 4000 {PWD/quota2a/xyz.txt 4000 0 0}}}
  148. do_test quota2-1.21 {
  149. sqlite3_quota_remove quota2a/xyz.txt
  150. standard_path [sqlite3_quota_dump]
  151. } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
  152. set quota {}
  153. do_test quota2-2.1 {
  154. set ::h1 [sqlite3_quota_fopen quota2c/xyz.txt w+b]
  155. sqlite3_quota_fwrite $::h1 1 7000 $bigtext
  156. } {7000}
  157. do_test quota2-2.2 {
  158. set ::quota
  159. } {}
  160. do_test quota2-2.3.1 {
  161. sqlite3_quota_rewind $::h1
  162. sqlite3_quota_file_available $::h1
  163. } {7000}
  164. do_test quota2-2.3.2 {
  165. set ::x [sqlite3_quota_fread $::h1 1001 7]
  166. string length $::x
  167. } {6006}
  168. do_test quota2-2.3.3 {
  169. sqlite3_quota_file_available $::h1
  170. } {0}
  171. do_test quota2-2.4 {
  172. string match $::x [string range $::bigtext 0 6005]
  173. } {1}
  174. do_test quota2-2.5 {
  175. sqlite3_quota_fseek $::h1 0 SEEK_END
  176. sqlite3_quota_ftell $::h1
  177. } {7000}
  178. do_test quota2-2.6 {
  179. sqlite3_quota_fseek $::h1 -100 SEEK_END
  180. sqlite3_quota_ftell $::h1
  181. } {6900}
  182. do_test quota2-2.6.1 {
  183. sqlite3_quota_file_available $::h1
  184. } {100}
  185. do_test quota2-2.7 {
  186. sqlite3_quota_fseek $::h1 -100 SEEK_CUR
  187. sqlite3_quota_ftell $::h1
  188. } {6800}
  189. do_test quota2-2.7.1 {
  190. sqlite3_quota_file_available $::h1
  191. } {200}
  192. do_test quota2-2.8 {
  193. sqlite3_quota_fseek $::h1 50 SEEK_CUR
  194. sqlite3_quota_ftell $::h1
  195. } {6850}
  196. do_test quota2-2.8.1 {
  197. sqlite3_quota_file_available $::h1
  198. } {150}
  199. do_test quota2-2.9 {
  200. sqlite3_quota_fseek $::h1 50 SEEK_SET
  201. sqlite3_quota_ftell $::h1
  202. } {50}
  203. do_test quota2-2.9.1 {
  204. sqlite3_quota_file_available $::h1
  205. } {6950}
  206. do_test quota2-2.10 {
  207. sqlite3_quota_rewind $::h1
  208. sqlite3_quota_ftell $::h1
  209. } {0}
  210. do_test quota2-2.10.1 {
  211. sqlite3_quota_file_available $::h1
  212. } {7000}
  213. do_test quota2-2.10.2 {
  214. sqlite3_quota_ferror $::h1
  215. } {0}
  216. do_test quota2-2.11 {
  217. standard_path [sqlite3_quota_dump]
  218. } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
  219. do_test quota2-2.12 {
  220. sqlite3_quota_fclose $::h1
  221. standard_path [sqlite3_quota_dump]
  222. } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
  223. do_test quota2-3.1 {
  224. sqlite3_quota_set */quota2b/* 0 quota_check
  225. set ::h1 [sqlite3_quota_fopen quota2a/x1/a.txt a]
  226. sqlite3_quota_fwrite $::h1 10 10 $bigtext
  227. } {10}
  228. do_test quota2-3.2 {
  229. standard_path [sqlite3_quota_dump]
  230. } {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
  231. do_test quota2-3.3a {
  232. sqlite3_quota_fflush $::h1 0
  233. standard_path [sqlite3_quota_dump]
  234. } {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
  235. do_test quota2-3.3b {
  236. sqlite3_quota_fflush $::h1 1
  237. standard_path [sqlite3_quota_dump]
  238. } {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
  239. do_test quota2-3.3c {
  240. sqlite3_quota_fflush $::h1
  241. standard_path [sqlite3_quota_dump]
  242. } {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
  243. do_test quota2-3.4 {
  244. sqlite3_quota_fclose $::h1
  245. standard_path [sqlite3_quota_dump]
  246. } {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 0 0}}}
  247. do_test quota2-3.5 {
  248. set ::h2 [sqlite3_quota_fopen quota2a/x2/b.txt a]
  249. sqlite3_quota_fwrite $::h2 10 20 $bigtext
  250. standard_path [sqlite3_quota_dump]
  251. } {{*/quota2a/* 4000 300 {PWD/quota2a/x2/b.txt 200 1 0} {PWD/quota2a/x1/a.txt 100 0 0}}}
  252. do_test quota2-3.6 {
  253. set ::h3 [sqlite3_quota_fopen quota2a/x1/c.txt a]
  254. sqlite3_quota_fwrite $::h3 10 50 $bigtext
  255. standard_path [sqlite3_quota_dump]
  256. } {{*/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}}}
  257. do_test quota2-3.7 {
  258. file exists quota2a/x1/a.txt
  259. } {1}
  260. do_test quota2-3.8 {
  261. file exists quota2a/x2/b.txt
  262. } {1}
  263. do_test quota2-3.9 {
  264. file exists quota2a/x1/c.txt
  265. } {1}
  266. do_test quota2-3.10 {
  267. sqlite3_quota_remove quota2a/x1
  268. standard_path [sqlite3_quota_dump]
  269. } {{*/quota2a/* 4000 700 {PWD/quota2a/x1/c.txt 500 1 1} {PWD/quota2a/x2/b.txt 200 1 0}}}
  270. do_test quota2-3.11 {
  271. sqlite3_quota_fclose $::h2
  272. sqlite3_quota_fclose $::h3
  273. standard_path [sqlite3_quota_dump]
  274. } {{*/quota2a/* 4000 200 {PWD/quota2a/x2/b.txt 200 0 0}}}
  275. do_test quota2-3.12 {
  276. file exists quota2a/x1/a.txt
  277. } {0}
  278. do_test quota2-3.13 {
  279. file exists quota2a/x2/b.txt
  280. } {1}
  281. do_test quota2-3.14 {
  282. file exists quota2a/x1/c.txt
  283. } {0}
  284. catch { sqlite3_quota_shutdown }
  285. catch { unset quota_request_ok }
  286. finish_test