drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 1 | # 2007 May 02 |
| 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 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this file is testing of the zero-filled blob functionality |
| 13 | # including the sqlite3_bind_zeroblob(), sqlite3_result_zeroblob(), |
| 14 | # and the built-in zeroblob() SQL function. |
| 15 | # |
danielk1977 | 28c6630 | 2007-09-01 11:04:26 +0000 | [diff] [blame^] | 16 | # $Id: zeroblob.test,v 1.7 2007/09/01 11:04:28 danielk1977 Exp $ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
| 20 | |
danielk1977 | f12737d | 2007-05-17 06:44:28 +0000 | [diff] [blame] | 21 | ifcapable !incrblob { |
| 22 | finish_test |
| 23 | return |
| 24 | } |
| 25 | |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 26 | # When zeroblob() is used for the last field of a column, then the |
| 27 | # content of the zeroblob is never instantiated on the VDBE stack. |
| 28 | # But it does get inserted into the database correctly. |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 29 | # |
| 30 | do_test zeroblob-1.1 { |
| 31 | execsql { |
| 32 | CREATE TABLE t1(a,b,c,d); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 33 | } |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 34 | set ::sqlite3_max_blobsize 0 |
| 35 | execsql { |
| 36 | INSERT INTO t1 VALUES(2,3,4,zeroblob(10000)); |
| 37 | } |
| 38 | set ::sqlite3_max_blobsize |
| 39 | } {10} |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 40 | do_test zeroblob-1.2 { |
| 41 | execsql { |
| 42 | SELECT length(d) FROM t1 |
| 43 | } |
| 44 | } {10000} |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 45 | |
| 46 | # If a non-NULL column follows the zeroblob, then the content of |
| 47 | # the zeroblob must be instantiated. |
| 48 | # |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 49 | do_test zeroblob-1.3 { |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 50 | set ::sqlite3_max_blobsize 0 |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 51 | execsql { |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 52 | INSERT INTO t1 VALUES(3,4,zeroblob(10000),5); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 53 | } |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 54 | set ::sqlite3_max_blobsize |
| 55 | } {10010} |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 56 | do_test zeroblob-1.4 { |
| 57 | execsql { |
| 58 | SELECT length(c), length(d) FROM t1 |
| 59 | } |
| 60 | } {1 10000 10000 1} |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 61 | |
| 62 | # Multiple zeroblobs can appear at the end of record. No instantiation |
| 63 | # of the blob content occurs on the stack. |
| 64 | # |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 65 | do_test zeroblob-1.5 { |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 66 | set ::sqlite3_max_blobsize 0 |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 67 | execsql { |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 68 | INSERT INTO t1 VALUES(4,5,zeroblob(10000),zeroblob(10000)); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 69 | } |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 70 | set ::sqlite3_max_blobsize |
| 71 | } {11} |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 72 | do_test zeroblob-1.6 { |
| 73 | execsql { |
| 74 | SELECT length(c), length(d) FROM t1 |
| 75 | } |
| 76 | } {1 10000 10000 1 10000 10000} |
| 77 | |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 78 | # NULLs can follow the zeroblob() or be intermixed with zeroblobs and |
| 79 | # no instantiation of the zeroblobs occurs on the stack. |
| 80 | # |
| 81 | do_test zeroblob-1.7 { |
| 82 | set ::sqlite3_max_blobsize 0 |
| 83 | execsql { |
| 84 | INSERT INTO t1 VALUES(5,zeroblob(10000),NULL,zeroblob(10000)); |
| 85 | } |
| 86 | set ::sqlite3_max_blobsize |
| 87 | } {10} |
| 88 | do_test zeroblob-1.8 { |
| 89 | execsql { |
| 90 | SELECT length(b), length(d) FROM t1 WHERE a=5 |
| 91 | } |
| 92 | } {10000 10000} |
| 93 | |
| 94 | # Comparisons against zeroblobs work. |
| 95 | # |
| 96 | do_test zeroblob-2.1 { |
| 97 | execsql { |
| 98 | SELECT a FROM t1 WHERE b=zeroblob(10000) |
| 99 | } |
| 100 | } {5} |
| 101 | |
| 102 | # Comparisons against zeroblobs work even when indexed. |
| 103 | # |
| 104 | do_test zeroblob-2.2 { |
| 105 | execsql { |
| 106 | CREATE INDEX i1_1 ON t1(b); |
| 107 | SELECT a FROM t1 WHERE b=zeroblob(10000); |
| 108 | } |
| 109 | } {5} |
| 110 | |
| 111 | # DISTINCT works for zeroblobs |
| 112 | # |
| 113 | do_test zeroblob-3.1 { |
| 114 | execsql { |
| 115 | SELECT count(DISTINCT a) FROM ( |
| 116 | SELECT x'00000000000000000000' AS a |
| 117 | UNION ALL |
| 118 | SELECT zeroblob(10) AS a |
| 119 | ) |
| 120 | } |
| 121 | } {1} |
drh | 50027d1 | 2007-05-23 06:31:38 +0000 | [diff] [blame] | 122 | |
| 123 | # Concatentation works with zeroblob |
| 124 | # |
| 125 | do_test zeroblob-4.1 { |
| 126 | execsql { |
| 127 | SELECT hex(zeroblob(2) || x'61') |
| 128 | } |
| 129 | } {000061} |
danielk1977 | 6b28f05 | 2007-05-30 06:19:32 +0000 | [diff] [blame] | 130 | |
| 131 | # Check various CAST(...) operations on zeroblob. |
| 132 | # |
| 133 | do_test zeroblob-5.1 { |
| 134 | execsql { |
| 135 | SELECT CAST (zeroblob(100) AS REAL); |
| 136 | } |
| 137 | } {0.0} |
| 138 | do_test zeroblob-5.2 { |
| 139 | execsql { |
| 140 | SELECT CAST (zeroblob(100) AS INTEGER); |
| 141 | } |
| 142 | } {0} |
| 143 | do_test zeroblob-5.3 { |
| 144 | execsql { |
| 145 | SELECT CAST (zeroblob(100) AS TEXT); |
| 146 | } |
| 147 | } {{}} |
| 148 | do_test zeroblob-5.4 { |
| 149 | execsql { |
| 150 | SELECT CAST(zeroblob(100) AS BLOB); |
| 151 | } |
| 152 | } [execsql {SELECT zeroblob(100)}] |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 153 | |
| 154 | |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 155 | # Check for malicious use of zeroblob. Make sure nothing crashes. |
| 156 | # |
| 157 | do_test zeroblob-6.1.1 { |
| 158 | execsql {select zeroblob(-1)} |
| 159 | } {{}} |
| 160 | do_test zeroblob-6.1.2 { |
| 161 | execsql {select zeroblob(-10)} |
| 162 | } {{}} |
| 163 | do_test zeroblob-6.1.3 { |
| 164 | execsql {select zeroblob(-100)} |
| 165 | } {{}} |
| 166 | do_test zeroblob-6.2 { |
| 167 | execsql {select length(zeroblob(-1))} |
| 168 | } {0} |
| 169 | do_test zeroblob-6.3 { |
| 170 | execsql {select zeroblob(-1)|1} |
| 171 | } {1} |
| 172 | do_test zeroblob-6.4 { |
| 173 | catchsql {select length(zeroblob(2147483648))} |
| 174 | } {1 {string or blob too big}} |
| 175 | do_test zeroblob-6.5 { |
| 176 | catchsql {select zeroblob(2147483648)} |
| 177 | } {1 {string or blob too big}} |
| 178 | |
danielk1977 | 28c6630 | 2007-09-01 11:04:26 +0000 | [diff] [blame^] | 179 | # Test bind_zeroblob() |
| 180 | # |
| 181 | do_test zeroblob-7.1 { |
| 182 | set ::STMT [sqlite3_prepare $::DB "SELECT length(?)" -1 DUMMY] |
| 183 | sqlite3_bind_zeroblob $::STMT 1 450 |
| 184 | sqlite3_step $::STMT |
| 185 | } {SQLITE_ROW} |
| 186 | do_test zeroblob-7.2 { |
| 187 | sqlite3_column_int $::STMT 0 |
| 188 | } {450} |
| 189 | do_test zeroblob-7.3 { |
| 190 | sqlite3_finalize $::STMT |
| 191 | } {SQLITE_OK} |
| 192 | |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 193 | finish_test |