1
0

incrblob3.test 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # 2010 October 20
  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. #
  13. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. sqlite3 db test.db
  16. sqlite3_db_config_lookaside db 0 0 0
  17. do_execsql_test incrblob3-1.1 {
  18. CREATE TABLE blobs(k INTEGER PRIMARY KEY, v BLOB);
  19. INSERT INTO blobs VALUES(1, zeroblob(100));
  20. INSERT INTO blobs VALUES(2, zeroblob(100));
  21. } {}
  22. # Test the sqlite3_blob_reopen()/read()/write() functions.
  23. #
  24. do_test incrblob3-1.2 {
  25. set ::blob [db incrblob blobs v 1]
  26. puts $::blob "hello world"
  27. } {}
  28. do_test incrblob3-1.3 {
  29. sqlite3_blob_reopen $::blob 2
  30. puts $::blob "world hello"
  31. } {}
  32. do_test incrblob3-1.4 {
  33. sqlite3_blob_reopen $::blob 1
  34. gets $::blob
  35. } {hello world}
  36. do_test incrblob3-1.5 {
  37. sqlite3_blob_reopen $::blob 2
  38. gets $::blob
  39. } {world hello}
  40. do_test incrblob3-1.6 { close $::blob } {}
  41. # Test some error conditions.
  42. #
  43. # incrblob3-2.1: Attempting to reopen a row that does not exist.
  44. # incrblob3-2.2: Attempting to reopen a row that does not contain a blob
  45. # or text value.
  46. #
  47. do_test incrblob3-2.1.1 {
  48. set ::blob [db incrblob blobs v 1]
  49. list [catch {sqlite3_blob_reopen $::blob 3} msg] $msg
  50. } {1 SQLITE_ERROR}
  51. do_test incrblob3-2.1.2 {
  52. list [sqlite3_errcode db] [sqlite3_errmsg db]
  53. } {SQLITE_ERROR {no such rowid: 3}}
  54. do_test incrblob3-2.1.3 {
  55. list [catch {sqlite3_blob_reopen $::blob 1} msg] $msg
  56. } {1 SQLITE_ABORT}
  57. do_test incrblob3-2.1.4 { close $::blob } {}
  58. do_execsql_test incrblob3-2.2.1 {
  59. INSERT INTO blobs VALUES(3, 42);
  60. INSERT INTO blobs VALUES(4, 54.4);
  61. INSERT INTO blobs VALUES(5, NULL);
  62. }
  63. foreach {tn rowid type} {
  64. 1 3 integer
  65. 2 4 real
  66. 3 5 null
  67. } {
  68. do_test incrblob3-2.2.$tn.1 {
  69. set ::blob [db incrblob blobs v 1]
  70. list [catch {sqlite3_blob_reopen $::blob $rowid} msg] $msg
  71. } {1 SQLITE_ERROR}
  72. do_test incrblob3-2.2.$tn.2 {
  73. list [sqlite3_errcode db] [sqlite3_errmsg db]
  74. } "SQLITE_ERROR {cannot open value of type $type}"
  75. do_test incrblob3-2.2.$tn.3 {
  76. list [catch {sqlite3_blob_reopen $::blob 1} msg] $msg
  77. } {1 SQLITE_ABORT}
  78. do_test incrblob3-2.2.$tn.4 {
  79. list [catch {sqlite3_blob_read $::blob 0 10} msg] $msg
  80. } {1 SQLITE_ABORT}
  81. do_test incrblob3-2.2.$tn.5 {
  82. list [catch {sqlite3_blob_write $::blob 0 "abcd"} msg] $msg
  83. } {1 SQLITE_ABORT}
  84. do_test incrblob3-2.2.$tn.6 {
  85. sqlite3_blob_bytes $::blob
  86. } {0}
  87. do_test incrblob3-2.2.$tn.7 { close $::blob } {}
  88. }
  89. # Test that passing NULL to sqlite3_blob_XXX() APIs returns SQLITE_MISUSE.
  90. #
  91. # incrblob3-3.1: sqlite3_blob_reopen()
  92. # incrblob3-3.2: sqlite3_blob_read()
  93. # incrblob3-3.3: sqlite3_blob_write()
  94. # incrblob3-3.4: sqlite3_blob_bytes()
  95. #
  96. do_test incrblob3-3.1 {
  97. list [catch {sqlite3_blob_reopen {} 3} msg] $msg
  98. } {1 SQLITE_MISUSE}
  99. do_test incrblob3-3.2 {
  100. list [catch {sqlite3_blob_read {} 0 10} msg] $msg
  101. } {1 SQLITE_MISUSE}
  102. do_test incrblob3-3.3 {
  103. list [catch {sqlite3_blob_write {} 0 "abcd"} msg] $msg
  104. } {1 SQLITE_MISUSE}
  105. do_test incrblob3-3.4 { sqlite3_blob_bytes {} } {0}
  106. do_test incrblob3-3.5 { sqlite3_blob_close {} } {}
  107. # Test out-of-range reading and writing
  108. #
  109. do_test incrblob3-4.1 {
  110. set ::blob [db incrblob blobs v 1]
  111. sqlite3_blob_bytes $::blob
  112. } {100}
  113. do_test incrblob3-4.2 {
  114. list [catch { sqlite3_blob_read $::blob -1 10 } msg] $msg
  115. } {1 SQLITE_ERROR}
  116. do_test incrblob3-4.3 {
  117. list [catch { sqlite3_blob_read $::blob 0 -10 } msg] $msg
  118. } {1 SQLITE_ERROR}
  119. do_test incrblob3-4.4 {
  120. list [catch { sqlite3_blob_read $::blob 95 10 } msg] $msg
  121. } {1 SQLITE_ERROR}
  122. do_test incrblob3-4.5 {
  123. list [catch { sqlite3_blob_write $::blob -1 "abcdefghij" 10 } msg] $msg
  124. } {1 SQLITE_ERROR}
  125. do_test incrblob3-4.6 {
  126. list [catch { sqlite3_blob_write $::blob 0 "abcdefghij" -10 } msg] $msg
  127. } {1 SQLITE_ERROR}
  128. do_test incrblob3-4.7 {
  129. list [catch { sqlite3_blob_write $::blob 95 "abcdefghij" } msg] $msg
  130. } {1 SQLITE_ERROR}
  131. do_test incrblob3-4.8 { close $::blob } {}
  132. # Test that modifying the row a blob handle points to aborts the blob.
  133. #
  134. do_test incrblob3-5.1 {
  135. set ::blob [db incrblob blobs v 1]
  136. sqlite3_blob_bytes $::blob
  137. } {100}
  138. do_test incrblob3-5.2 {
  139. execsql { UPDATE blobs SET v = '123456789012345678901234567890' WHERE k = 1 }
  140. list [catch { sqlite3_blob_read $::blob 0 10 } msg] $msg
  141. } {1 SQLITE_ABORT}
  142. # Test various errors that can occur in sqlite3_blob_open():
  143. #
  144. # 1. Trying to open a virtual table column.
  145. # 2. Trying to open a view column.
  146. # 3. Trying to open a column that does not exist.
  147. # 4. Trying to open a read/write handle on an indexed column.
  148. # 5. Trying to open a read/write handle on the child key of an FK constraint.
  149. #
  150. ifcapable fts3 {
  151. do_test incrblob3-6.1 {
  152. execsql {
  153. CREATE VIRTUAL TABLE ft USING fts3;
  154. INSERT INTO ft VALUES('rules to open a column to which');
  155. }
  156. list [catch { db incrblob ft content 1 } msg] $msg
  157. } {1 {cannot open virtual table: ft}}
  158. }
  159. ifcapable view {
  160. do_test incrblob3-6.2 {
  161. execsql { CREATE VIEW v1 AS SELECT * FROM blobs }
  162. list [catch { db incrblob v1 content 1 } msg] $msg
  163. } {1 {cannot open view: v1}}
  164. }
  165. do_test incrblob3-6.3 {
  166. list [catch { db incrblob blobs content 1 } msg] $msg
  167. } {1 {no such column: "content"}}
  168. do_test incrblob3-6.4.1 {
  169. execsql {
  170. CREATE TABLE t1(a, b);
  171. CREATE INDEX i1 ON t1(b);
  172. INSERT INTO t1 VALUES(zeroblob(100), zeroblob(100));
  173. }
  174. list [catch { db incrblob t1 b 1 } msg] $msg
  175. } {1 {cannot open indexed column for writing}}
  176. do_test incrblob3-6.4.2 {
  177. set ::blob [db incrblob t1 a 1]
  178. close $::blob
  179. } {}
  180. do_test incrblob3-6.4.3 {
  181. set ::blob [db incrblob -readonly t1 b 1]
  182. close $::blob
  183. } {}
  184. do_test incrblob3-6.5.1 {
  185. execsql {
  186. CREATE TABLE p1(a PRIMARY KEY);
  187. CREATE TABLE c1(a, b REFERENCES p1);
  188. PRAGMA foreign_keys = 1;
  189. INSERT INTO p1 VALUES(zeroblob(100));
  190. INSERT INTO c1 VALUES(zeroblob(100), zeroblob(100));
  191. }
  192. list [catch { db incrblob c1 b 1 } msg] $msg
  193. } {1 {cannot open foreign key column for writing}}
  194. do_test incrblob3-6.5.2 {
  195. set ::blob [db incrblob c1 a 1]
  196. close $::blob
  197. } {}
  198. do_test incrblob3-6.5.3 {
  199. set ::blob [db incrblob -readonly c1 b 1]
  200. close $::blob
  201. } {}
  202. do_test incrblob3-6.5.4 {
  203. execsql { PRAGMA foreign_keys = 0 }
  204. set ::blob [db incrblob c1 b 1]
  205. close $::blob
  206. } {}
  207. # Test that sqlite3_blob_open() handles transient and persistent schema
  208. # errors correctly.
  209. #
  210. do_test incrblob3-7.1 {
  211. sqlite3 db2 test.db
  212. sqlite3_db_config_lookaside db2 0 0 0
  213. execsql { CREATE TABLE t2(x) } db2
  214. set ::blob [db incrblob blobs v 1]
  215. close $::blob
  216. } {}
  217. db2 close
  218. testvfs tvfs -default 1
  219. tvfs filter xAccess
  220. tvfs script access_method
  221. proc access_method {args} {
  222. set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
  223. incr schemacookie
  224. hexio_write test.db 40 [hexio_render_int32 $schemacookie]
  225. set dbversion [hexio_get_int [hexio_read test.db 24 4]]
  226. incr dbversion
  227. hexio_write test.db 24 [hexio_render_int32 $dbversion]
  228. return ""
  229. }
  230. do_test incrblob3-7.2 {
  231. sqlite3 db test.db
  232. sqlite3_db_config_lookaside db 0 0 0
  233. list [catch {db incrblob blobs v 1} msg] $msg
  234. } {1 {database schema has changed}}
  235. db close
  236. tvfs delete
  237. finish_test