blob: b4c258772aabc03dcc9f783568e96cfac9be123f [file] [log] [blame]
danielk197792d4d7a2007-05-04 12:05:56 +00001# 2007 May 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#
drh4a0611d2008-07-18 17:16:26 +000012# $Id: incrblob_err.test,v 1.14 2008/07/18 17:16:27 drh Exp $
danielk197792d4d7a2007-05-04 12:05:56 +000013#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17
danielk19774152e672007-09-12 17:01:45 +000018ifcapable {!incrblob || !memdebug || !tclvar} {
danielk197732a0d8b2007-05-04 19:03:02 +000019 finish_test
20 return
21}
22
danielk1977cdc3a6b2007-08-25 13:09:26 +000023source $testdir/malloc_common.tcl
danielk197792d4d7a2007-05-04 12:05:56 +000024
drh3f994d02008-06-25 14:31:53 +000025unset -nocomplain ::fd ::data
danielk197792d4d7a2007-05-04 12:05:56 +000026set ::fd [open [info script]]
27set ::data [read $::fd]
28close $::fd
29
30do_malloc_test 1 -tclprep {
31 set bytes [file size [info script]]
32 execsql {
33 CREATE TABLE blobs(k, v BLOB);
34 INSERT INTO blobs VALUES(1, zeroblob($::bytes));
35 }
36} -tclbody {
37 set ::blob [db incrblob blobs v 1]
38 set rc [catch {puts -nonewline $::blob $::data}]
39 if {$rc} { error "out of memory" }
40}
41
42do_malloc_test 2 -tclprep {
43 execsql {
44 CREATE TABLE blobs(k, v BLOB);
45 INSERT INTO blobs VALUES(1, $::data);
46 }
47} -tclbody {
48 set ::blob [db incrblob blobs v 1]
49 set rc [catch {set ::r [read $::blob]}]
50 if {$rc} {
51 error "out of memory"
52 } elseif {$::r ne $::data} {
53 error "Bad data read..."
54 }
55}
56
57do_malloc_test 3 -tclprep {
58 execsql {
59 CREATE TABLE blobs(k, v BLOB);
60 INSERT INTO blobs VALUES(1, $::data);
61 }
62} -tclbody {
63 set ::blob [db incrblob blobs v 1]
64 set rc [catch {set ::r [read $::blob]}]
65 if {$rc} {
66 error "out of memory"
67 } elseif {$::r ne $::data} {
68 error "Bad data read..."
69 }
70 set rc [catch {close $::blob}]
71 if {$rc} {
72 error "out of memory"
73 }
74}
danielk197792d4d7a2007-05-04 12:05:56 +000075
danielk197727467042008-05-12 07:42:20 +000076
danielk197792d4d7a2007-05-04 12:05:56 +000077do_ioerr_test incrblob_err-4 -cksum 1 -sqlprep {
78 CREATE TABLE blobs(k, v BLOB);
79 INSERT INTO blobs VALUES(1, $::data);
80} -tclbody {
81 set ::blob [db incrblob blobs v 1]
82 read $::blob
83}
84
85do_ioerr_test incrblob_err-5 -cksum 1 -sqlprep {
86 CREATE TABLE blobs(k, v BLOB);
87 INSERT INTO blobs VALUES(1, zeroblob(length(CAST($::data AS BLOB))));
88} -tclbody {
89 set ::blob [db incrblob blobs v 1]
90 puts -nonewline $::blob $::data
91 close $::blob
92}
93
94do_ioerr_test incrblob_err-6 -cksum 1 -sqlprep {
95 CREATE TABLE blobs(k, v BLOB);
96 INSERT INTO blobs VALUES(1, $::data || $::data || $::data);
97} -tclbody {
98 set ::blob [db incrblob blobs v 1]
99 seek $::blob -20 end
100 puts -nonewline $::blob "12345678900987654321"
101 close $::blob
102}
103
danielk1977a9613392008-07-08 12:07:32 +0000104do_ioerr_test incrblob_err-7 -cksum 1 -sqlprep {
105 PRAGMA auto_vacuum = 1;
106 CREATE TABLE blobs(k INTEGER PRIMARY KEY, v BLOB);
107 INSERT INTO blobs VALUES(1, zeroblob(500 * 1020));
108} -tclbody {
109 # Read some data from the end of the large blob inserted into table
110 # "blobs". This forces the IO error to occur while reading a pointer
111 # map page for the purposes of seeking to the end of the blob.
112 #
113 sqlite3 db2 test.db
114 set ::blob [db2 incrblob blobs v 1]
115 sqlite3_blob_read $::blob [expr 500*1020-20] 20
116 close $::blob
117}
drh137802c2008-07-16 18:35:45 +0000118catch {db2 close}
danielk1977a9613392008-07-08 12:07:32 +0000119
120do_ioerr_test incrblob_err-8 -cksum 1 -sqlprep {
121 PRAGMA auto_vacuum = 1;
122 CREATE TABLE blobs(k INTEGER PRIMARY KEY, v BLOB);
123 INSERT INTO blobs VALUES(1, zeroblob(500 * 1020));
124} -tclbody {
125 # Read some data from the end of the large blob inserted into table
126 # "blobs". This forces the IO error to occur while reading a pointer
127 # map page for the purposes of seeking to the end of the blob.
128 #
129 sqlite3 db2 test.db
130 set ::blob [db2 incrblob blobs v 1]
131 sqlite3_blob_write $::blob [expr 500*1020-20] 12345678900987654321
132 close $::blob
133}
134
drh4a0611d2008-07-18 17:16:26 +0000135catch {db2 close}
danielk197749fc1f62008-07-08 17:43:56 +0000136
danielk197792d4d7a2007-05-04 12:05:56 +0000137finish_test