blob: 54a39778bb3acac9c19e996075c347fd3067ae81 [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#
danielk197749fc1f62008-07-08 17:43:56 +000012# $Id: incrblob_err.test,v 1.12 2008/07/08 17:43:57 danielk1977 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}
118
119do_ioerr_test incrblob_err-8 -cksum 1 -sqlprep {
120 PRAGMA auto_vacuum = 1;
121 CREATE TABLE blobs(k INTEGER PRIMARY KEY, v BLOB);
122 INSERT INTO blobs VALUES(1, zeroblob(500 * 1020));
123} -tclbody {
124 # Read some data from the end of the large blob inserted into table
125 # "blobs". This forces the IO error to occur while reading a pointer
126 # map page for the purposes of seeking to the end of the blob.
127 #
128 sqlite3 db2 test.db
129 set ::blob [db2 incrblob blobs v 1]
130 sqlite3_blob_write $::blob [expr 500*1020-20] 12345678900987654321
131 close $::blob
132}
133
danielk197749fc1f62008-07-08 17:43:56 +0000134db2 close
135
danielk197792d4d7a2007-05-04 12:05:56 +0000136finish_test