summaryrefslogtreecommitdiffstats
path: root/test/incrblob3.test
blob: a9097e01a5cd490ac2eef3ebd6d2603193be97db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# 2010 October 20
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix incrblob3

ifcapable !incrblob {
  finish_test
  return
}

sqlite3 db test.db
sqlite3_db_config_lookaside db 0 0 0

do_execsql_test incrblob3-1.1 {
  CREATE TABLE blobs(k INTEGER PRIMARY KEY, v BLOB);
  INSERT INTO blobs VALUES(1, zeroblob(100));
  INSERT INTO blobs VALUES(2, zeroblob(100));
} {}

# Test the sqlite3_blob_reopen()/read()/write() functions.
#
do_test incrblob3-1.2 {
  set ::blob [db incrblob blobs v 1]
  puts $::blob "hello world"
} {}

do_test incrblob3-1.3 {
  sqlite3_blob_reopen $::blob 2
  puts $::blob "world hello"
} {}

do_test incrblob3-1.4 {
  sqlite3_blob_reopen $::blob 1
  gets $::blob
} {hello world}

do_test incrblob3-1.5 {
  sqlite3_blob_reopen $::blob 2
  gets $::blob
} {world hello}

do_test incrblob3-1.6 { close $::blob } {}

# Test some error conditions.
#
#   incrblob3-2.1: Attempting to reopen a row that does not exist.
#   incrblob3-2.2: Attempting to reopen a row that does not contain a blob
#                  or text value.
#
do_test incrblob3-2.1.1 {
  set ::blob [db incrblob blobs v 1]
  list [catch {sqlite3_blob_reopen $::blob 3} msg] $msg
} {1 SQLITE_ERROR}
do_test incrblob3-2.1.2 {
  list [sqlite3_errcode db] [sqlite3_errmsg db]
} {SQLITE_ERROR {no such rowid: 3}}
do_test incrblob3-2.1.3 {
  list [catch {sqlite3_blob_reopen $::blob 1} msg] $msg
} {1 SQLITE_ABORT}
do_test incrblob3-2.1.4 { close $::blob } {}

do_execsql_test incrblob3-2.2.1 {
  INSERT INTO blobs VALUES(3, 42);
  INSERT INTO blobs VALUES(4, 54.4);
  INSERT INTO blobs VALUES(5, NULL);
}
foreach {tn rowid type} {
  1 3 integer
  2 4 real
  3 5 null
} {
  do_test incrblob3-2.2.$tn.1 {
    set ::blob [db incrblob blobs v 1]
    list [catch {sqlite3_blob_reopen $::blob $rowid} msg] $msg
  } {1 SQLITE_ERROR}
  do_test incrblob3-2.2.$tn.2 {
    list [sqlite3_errcode db] [sqlite3_errmsg db]
  } "SQLITE_ERROR {cannot open value of type $type}"

  do_test incrblob3-2.2.$tn.3 {
    list [catch {sqlite3_blob_reopen $::blob 1} msg] $msg
  } {1 SQLITE_ABORT}
  do_test incrblob3-2.2.$tn.4 {
    list [catch {sqlite3_blob_read $::blob 0 10} msg] $msg
  } {1 SQLITE_ABORT}
  do_test incrblob3-2.2.$tn.5 {
    list [catch {sqlite3_blob_write $::blob 0 "abcd"} msg] $msg
  } {1 SQLITE_ABORT}
  do_test incrblob3-2.2.$tn.6 {
    sqlite3_blob_bytes $::blob
  } {0}

  do_test incrblob3-2.2.$tn.7 { close $::blob } {}
}

# Test that passing NULL to sqlite3_blob_XXX() APIs returns SQLITE_MISUSE.
#
#   incrblob3-3.1: sqlite3_blob_reopen()
#   incrblob3-3.2: sqlite3_blob_read()
#   incrblob3-3.3: sqlite3_blob_write()
#   incrblob3-3.4: sqlite3_blob_bytes()
#
do_test incrblob3-3.1 {
  list [catch {sqlite3_blob_reopen {} 3} msg] $msg
} {1 SQLITE_MISUSE}

do_test incrblob3-3.2 {
  list [catch {sqlite3_blob_read {} 0 10} msg] $msg
} {1 SQLITE_MISUSE}

do_test incrblob3-3.3 {
  list [catch {sqlite3_blob_write {} 0 "abcd"} msg] $msg
} {1 SQLITE_MISUSE}

do_test incrblob3-3.4 { sqlite3_blob_bytes {} } {0}

do_test incrblob3-3.5 { sqlite3_blob_close {} } {}

# Test out-of-range reading and writing
#
do_test incrblob3-4.1 {
  set ::blob [db incrblob blobs v 1]
  sqlite3_blob_bytes $::blob
} {100}
do_test incrblob3-4.2 {
  list [catch { sqlite3_blob_read $::blob -1 10 } msg] $msg
} {1 SQLITE_ERROR}
do_test incrblob3-4.3 {
  list [catch { sqlite3_blob_read $::blob 0 -10 } msg] $msg
} {1 SQLITE_ERROR}
do_test incrblob3-4.4 {
  list [catch { sqlite3_blob_read $::blob 95 10 } msg] $msg
} {1 SQLITE_ERROR}
do_test incrblob3-4.5 {
  list [catch { sqlite3_blob_write $::blob -1 "abcdefghij" 10 } msg] $msg
} {1 SQLITE_ERROR}
do_test incrblob3-4.6 {
  list [catch { sqlite3_blob_write $::blob 0 "abcdefghij" -10 } msg] $msg
} {1 SQLITE_ERROR}
do_test incrblob3-4.7 {
  list [catch { sqlite3_blob_write $::blob 95 "abcdefghij" } msg] $msg
} {1 SQLITE_ERROR}

do_test incrblob3-4.8 { close $::blob } {}

# Test that modifying the row a blob handle points to aborts the blob.
#
do_test incrblob3-5.1 {
  set ::blob [db incrblob blobs v 1]
  sqlite3_blob_bytes $::blob
} {100}
do_test incrblob3-5.2 {
  execsql { UPDATE blobs SET v = '123456789012345678901234567890' WHERE k = 1 }
  list [catch { sqlite3_blob_read $::blob 0 10 } msg] $msg
} {1 SQLITE_ABORT}

# Test various errors that can occur in sqlite3_blob_open():
#
#   1. Trying to open a virtual table column.
#   2. Trying to open a view column.
#   3. Trying to open a column that does not exist.
#   4. Trying to open a read/write handle on an indexed column.
#   5. Trying to open a read/write handle on the child key of an FK constraint.
#
ifcapable fts3 {
  do_test incrblob3-6.1 {
    execsql {
      CREATE VIRTUAL TABLE ft USING fts3;
      INSERT INTO ft VALUES('rules to open a column to which');
    }

    list [catch { db incrblob ft content 1 } msg] $msg
  } {1 {cannot open virtual table: ft}}
}
ifcapable view {
  do_test incrblob3-6.2 {
    execsql { CREATE VIEW v1 AS SELECT * FROM blobs }
    list [catch { db incrblob v1 content 1 } msg] $msg
  } {1 {cannot open view: v1}}
}

do_test incrblob3-6.3 {
  list [catch { db incrblob blobs content 1 } msg] $msg
} {1 {no such column: "content"}}

do_test incrblob3-6.4.1 {
  execsql { 
    CREATE TABLE t1(a, b);
    CREATE INDEX i1 ON t1(b);
    INSERT INTO t1 VALUES(zeroblob(100), zeroblob(100));
  }
  list [catch { db incrblob t1 b 1 } msg] $msg
} {1 {cannot open indexed column for writing}}
do_test incrblob3-6.4.2 {
  set ::blob [db incrblob t1 a 1]
  close $::blob
} {}
do_test incrblob3-6.4.3 {
  set ::blob [db incrblob -readonly t1 b 1]
  close $::blob
} {}

do_test incrblob3-6.5.1 {
  execsql { 
    CREATE TABLE p1(a PRIMARY KEY);
    CREATE TABLE c1(a, b REFERENCES p1);
    PRAGMA foreign_keys = 1;
    INSERT INTO p1 VALUES(zeroblob(100));
    INSERT INTO c1 VALUES(zeroblob(100), zeroblob(100));
  }
  list [catch { db incrblob c1 b 1 } msg] $msg
} {1 {cannot open foreign key column for writing}}

do_test incrblob3-6.5.2 {
  set ::blob [db incrblob c1 a 1]
  close $::blob
} {}
do_test incrblob3-6.5.3 {
  set ::blob [db incrblob -readonly c1 b 1]
  close $::blob
} {}
do_test incrblob3-6.5.4 {
  execsql { PRAGMA foreign_keys = 0 }
  set ::blob [db incrblob c1 b 1]
  close $::blob
} {}


# Test that sqlite3_blob_open() handles transient and persistent schema 
# errors correctly.
#
do_test incrblob3-7.1 {
  sqlite3 db2 test.db
  sqlite3_db_config_lookaside db2 0 0 0
  execsql { CREATE TABLE t2(x) } db2
  set ::blob [db incrblob blobs v 1]
  close $::blob
} {}
db2 close

testvfs tvfs -default 1
tvfs filter xAccess
tvfs script access_method

proc access_method {args} {
  set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
  incr schemacookie
  hexio_write test.db 40 [hexio_render_int32 $schemacookie]

  set dbversion [hexio_get_int [hexio_read test.db 24 4]]
  incr dbversion
  hexio_write test.db 24 [hexio_render_int32 $dbversion]

  return ""
}

do_test incrblob3-7.2 {
  sqlite3 db test.db 
  sqlite3_db_config_lookaside db 0 0 0
  list [catch {db incrblob blobs v 1} msg] $msg
} {1 {database schema has changed}}
db close
tvfs delete

#-------------------------------------------------------------------------
#
reset_db
forcedelete test.db2
do_execsql_test 8.1 {
  CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
  ATTACH 'test.db2' AS aux;
  CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b);

  INSERT INTO t1 VALUES(4, 'hello');
  INSERT INTO aux.t1 VALUES(4, 'world');
}

do_test 8.2 {
  set ::blob [db incrblob -readonly main t1 b 4]
  read $::blob
} {hello}
close $::blob

do_test 8.3 {
  set ::blob [db incrblob -readonly aux t1 b 4]
  read $::blob
} {world}
close $::blob

do_test 8.4 {
  set ::blob [db incrblob -readonly t1 b 4]
  read $::blob
} {hello}
close $::blob

do_test 8.5 {
  list [catch { db incrblob -readonly nosuchdb t1 b 4 } msg] $msg
} {1 {no such table: nosuchdb.t1}}


db close
finish_test