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 | # |
drh | 6be240e | 2009-07-14 02:33:02 +0000 | [diff] [blame] | 16 | # $Id: zeroblob.test,v 1.14 2009/07/14 02:33:02 drh Exp $ |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
dan | a4d5ae8 | 2015-07-24 16:24:37 +0000 | [diff] [blame] | 20 | set testprefix zeroblob |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 21 | |
danielk1977 | f12737d | 2007-05-17 06:44:28 +0000 | [diff] [blame] | 22 | ifcapable !incrblob { |
| 23 | finish_test |
| 24 | return |
| 25 | } |
| 26 | |
dan | 03bc525 | 2015-07-24 14:17:17 +0000 | [diff] [blame] | 27 | test_set_config_pagecache 0 0 |
| 28 | |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 29 | # When zeroblob() is used for the last field of a column, then the |
| 30 | # content of the zeroblob is never instantiated on the VDBE stack. |
| 31 | # But it does get inserted into the database correctly. |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 32 | # |
drh | 655194d | 2008-06-12 18:17:40 +0000 | [diff] [blame] | 33 | db eval {PRAGMA cache_size=10} |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 34 | sqlite3_memory_highwater 1 |
drh | 655194d | 2008-06-12 18:17:40 +0000 | [diff] [blame] | 35 | unset -nocomplain memused |
| 36 | set memused [sqlite3_memory_used] |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 37 | do_test zeroblob-1.1 { |
| 38 | execsql { |
| 39 | CREATE TABLE t1(a,b,c,d); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 40 | } |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 41 | set ::sqlite3_max_blobsize 0 |
| 42 | execsql { |
drh | 655194d | 2008-06-12 18:17:40 +0000 | [diff] [blame] | 43 | INSERT INTO t1 VALUES(2,3,4,zeroblob(1000000)); |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 44 | } |
| 45 | set ::sqlite3_max_blobsize |
| 46 | } {10} |
dan | 03bc525 | 2015-07-24 14:17:17 +0000 | [diff] [blame] | 47 | |
drh | 655194d | 2008-06-12 18:17:40 +0000 | [diff] [blame] | 48 | do_test zeroblob-1.1.1 { |
drh | 53e66c3 | 2015-07-24 15:49:23 +0000 | [diff] [blame] | 49 | expr {[sqlite3_memory_highwater]<$::memused+35000} |
drh | 655194d | 2008-06-12 18:17:40 +0000 | [diff] [blame] | 50 | } {1} |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 51 | do_test zeroblob-1.2 { |
| 52 | execsql { |
| 53 | SELECT length(d) FROM t1 |
| 54 | } |
drh | 655194d | 2008-06-12 18:17:40 +0000 | [diff] [blame] | 55 | } {1000000} |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 56 | |
| 57 | # If a non-NULL column follows the zeroblob, then the content of |
| 58 | # the zeroblob must be instantiated. |
| 59 | # |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 60 | do_test zeroblob-1.3 { |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 61 | set ::sqlite3_max_blobsize 0 |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 62 | execsql { |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 63 | INSERT INTO t1 VALUES(3,4,zeroblob(10000),5); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 64 | } |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 65 | set ::sqlite3_max_blobsize |
| 66 | } {10010} |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 67 | do_test zeroblob-1.4 { |
| 68 | execsql { |
| 69 | SELECT length(c), length(d) FROM t1 |
| 70 | } |
drh | 655194d | 2008-06-12 18:17:40 +0000 | [diff] [blame] | 71 | } {1 1000000 10000 1} |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 72 | |
| 73 | # Multiple zeroblobs can appear at the end of record. No instantiation |
| 74 | # of the blob content occurs on the stack. |
| 75 | # |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 76 | do_test zeroblob-1.5 { |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 77 | set ::sqlite3_max_blobsize 0 |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 78 | execsql { |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 79 | INSERT INTO t1 VALUES(4,5,zeroblob(10000),zeroblob(10000)); |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 80 | } |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 81 | set ::sqlite3_max_blobsize |
| 82 | } {11} |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 83 | do_test zeroblob-1.6 { |
| 84 | execsql { |
| 85 | SELECT length(c), length(d) FROM t1 |
| 86 | } |
drh | 655194d | 2008-06-12 18:17:40 +0000 | [diff] [blame] | 87 | } {1 1000000 10000 1 10000 10000} |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 88 | |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 89 | # NULLs can follow the zeroblob() or be intermixed with zeroblobs and |
| 90 | # no instantiation of the zeroblobs occurs on the stack. |
| 91 | # |
| 92 | do_test zeroblob-1.7 { |
| 93 | set ::sqlite3_max_blobsize 0 |
| 94 | execsql { |
| 95 | INSERT INTO t1 VALUES(5,zeroblob(10000),NULL,zeroblob(10000)); |
| 96 | } |
| 97 | set ::sqlite3_max_blobsize |
| 98 | } {10} |
| 99 | do_test zeroblob-1.8 { |
| 100 | execsql { |
| 101 | SELECT length(b), length(d) FROM t1 WHERE a=5 |
| 102 | } |
| 103 | } {10000 10000} |
| 104 | |
| 105 | # Comparisons against zeroblobs work. |
| 106 | # |
| 107 | do_test zeroblob-2.1 { |
| 108 | execsql { |
| 109 | SELECT a FROM t1 WHERE b=zeroblob(10000) |
| 110 | } |
| 111 | } {5} |
| 112 | |
| 113 | # Comparisons against zeroblobs work even when indexed. |
| 114 | # |
| 115 | do_test zeroblob-2.2 { |
| 116 | execsql { |
| 117 | CREATE INDEX i1_1 ON t1(b); |
| 118 | SELECT a FROM t1 WHERE b=zeroblob(10000); |
| 119 | } |
| 120 | } {5} |
| 121 | |
| 122 | # DISTINCT works for zeroblobs |
| 123 | # |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 124 | ifcapable bloblit&&subquery&&compound { |
| 125 | do_test zeroblob-3.1 { |
| 126 | execsql { |
| 127 | SELECT count(DISTINCT a) FROM ( |
| 128 | SELECT x'00000000000000000000' AS a |
| 129 | UNION ALL |
| 130 | SELECT zeroblob(10) AS a |
| 131 | ) |
| 132 | } |
| 133 | } {1} |
| 134 | } |
drh | 50027d1 | 2007-05-23 06:31:38 +0000 | [diff] [blame] | 135 | |
| 136 | # Concatentation works with zeroblob |
| 137 | # |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 138 | ifcapable bloblit { |
| 139 | do_test zeroblob-4.1 { |
| 140 | execsql { |
| 141 | SELECT hex(zeroblob(2) || x'61') |
| 142 | } |
| 143 | } {000061} |
| 144 | } |
danielk1977 | 6b28f05 | 2007-05-30 06:19:32 +0000 | [diff] [blame] | 145 | |
| 146 | # Check various CAST(...) operations on zeroblob. |
| 147 | # |
| 148 | do_test zeroblob-5.1 { |
| 149 | execsql { |
| 150 | SELECT CAST (zeroblob(100) AS REAL); |
| 151 | } |
| 152 | } {0.0} |
| 153 | do_test zeroblob-5.2 { |
| 154 | execsql { |
| 155 | SELECT CAST (zeroblob(100) AS INTEGER); |
| 156 | } |
| 157 | } {0} |
| 158 | do_test zeroblob-5.3 { |
| 159 | execsql { |
| 160 | SELECT CAST (zeroblob(100) AS TEXT); |
| 161 | } |
| 162 | } {{}} |
| 163 | do_test zeroblob-5.4 { |
| 164 | execsql { |
| 165 | SELECT CAST(zeroblob(100) AS BLOB); |
| 166 | } |
| 167 | } [execsql {SELECT zeroblob(100)}] |
drh | ae7e151 | 2007-05-02 16:51:59 +0000 | [diff] [blame] | 168 | |
| 169 | |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 170 | # Check for malicious use of zeroblob. Make sure nothing crashes. |
| 171 | # |
| 172 | do_test zeroblob-6.1.1 { |
| 173 | execsql {select zeroblob(-1)} |
| 174 | } {{}} |
| 175 | do_test zeroblob-6.1.2 { |
| 176 | execsql {select zeroblob(-10)} |
| 177 | } {{}} |
| 178 | do_test zeroblob-6.1.3 { |
| 179 | execsql {select zeroblob(-100)} |
| 180 | } {{}} |
| 181 | do_test zeroblob-6.2 { |
| 182 | execsql {select length(zeroblob(-1))} |
| 183 | } {0} |
| 184 | do_test zeroblob-6.3 { |
| 185 | execsql {select zeroblob(-1)|1} |
| 186 | } {1} |
| 187 | do_test zeroblob-6.4 { |
| 188 | catchsql {select length(zeroblob(2147483648))} |
| 189 | } {1 {string or blob too big}} |
| 190 | do_test zeroblob-6.5 { |
| 191 | catchsql {select zeroblob(2147483648)} |
drh | c0b3abb | 2007-09-04 12:18:41 +0000 | [diff] [blame] | 192 | } {1 {string or blob too big}} |
| 193 | do_test zeroblob-6.6 { |
| 194 | execsql {select hex(zeroblob(-1))} |
| 195 | } {{}} |
| 196 | do_test zeroblob-6.7 { |
| 197 | execsql {select typeof(zeroblob(-1))} |
| 198 | } {blob} |
drh | 98640a3 | 2007-06-07 19:08:32 +0000 | [diff] [blame] | 199 | |
danielk1977 | 28c6630 | 2007-09-01 11:04:26 +0000 | [diff] [blame] | 200 | # Test bind_zeroblob() |
| 201 | # |
drh | c91d86c | 2008-06-12 18:05:40 +0000 | [diff] [blame] | 202 | sqlite3_memory_highwater 1 |
| 203 | unset -nocomplain memused |
| 204 | set memused [sqlite3_memory_used] |
danielk1977 | 28c6630 | 2007-09-01 11:04:26 +0000 | [diff] [blame] | 205 | do_test zeroblob-7.1 { |
| 206 | set ::STMT [sqlite3_prepare $::DB "SELECT length(?)" -1 DUMMY] |
drh | c91d86c | 2008-06-12 18:05:40 +0000 | [diff] [blame] | 207 | set ::sqlite3_max_blobsize 0 |
| 208 | sqlite3_bind_zeroblob $::STMT 1 450000 |
danielk1977 | 28c6630 | 2007-09-01 11:04:26 +0000 | [diff] [blame] | 209 | sqlite3_step $::STMT |
| 210 | } {SQLITE_ROW} |
| 211 | do_test zeroblob-7.2 { |
| 212 | sqlite3_column_int $::STMT 0 |
drh | c91d86c | 2008-06-12 18:05:40 +0000 | [diff] [blame] | 213 | } {450000} |
danielk1977 | 28c6630 | 2007-09-01 11:04:26 +0000 | [diff] [blame] | 214 | do_test zeroblob-7.3 { |
| 215 | sqlite3_finalize $::STMT |
| 216 | } {SQLITE_OK} |
drh | c91d86c | 2008-06-12 18:05:40 +0000 | [diff] [blame] | 217 | do_test zeroblob-7.4 { |
| 218 | set ::sqlite3_max_blobsize |
| 219 | } {0} |
| 220 | do_test zeroblob-7.5 { |
| 221 | expr {[sqlite3_memory_highwater]<$::memused+10000} |
| 222 | } {1} |
danielk1977 | 28c6630 | 2007-09-01 11:04:26 +0000 | [diff] [blame] | 223 | |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 224 | # Test that MakeRecord can handle a value with some real content |
| 225 | # and a zero-blob tail. |
| 226 | # |
| 227 | do_test zeroblob-8.1 { |
| 228 | llength [execsql { |
| 229 | SELECT 'hello' AS a, zeroblob(10) as b from t1 ORDER BY a, b; |
| 230 | }] |
| 231 | } {8} |
| 232 | |
| 233 | |
drh | 6be240e | 2009-07-14 02:33:02 +0000 | [diff] [blame] | 234 | # Ticket #3965 |
| 235 | # zeroblobs on either size of an IN operator |
| 236 | # |
| 237 | do_test zeroblob-9.1 { |
| 238 | db eval {SELECT x'0000' IN (x'000000')} |
| 239 | } {0} |
| 240 | do_test zeroblob-9.2 { |
| 241 | db eval {SELECT x'0000' IN (x'0000')} |
| 242 | } {1} |
| 243 | do_test zeroblob-9.3 { |
| 244 | db eval {SELECT zeroblob(2) IN (x'000000')} |
| 245 | } {0} |
| 246 | do_test zeroblob-9.4 { |
| 247 | db eval {SELECT zeroblob(2) IN (x'0000')} |
| 248 | } {1} |
| 249 | do_test zeroblob-9.5 { |
| 250 | db eval {SELECT x'0000' IN (zeroblob(3))} |
| 251 | } {0} |
| 252 | do_test zeroblob-9.6 { |
| 253 | db eval {SELECT x'0000' IN (zeroblob(2))} |
| 254 | } {1} |
| 255 | do_test zeroblob-9.7 { |
| 256 | db eval {SELECT zeroblob(2) IN (zeroblob(3))} |
| 257 | } {0} |
| 258 | do_test zeroblob-9.8 { |
| 259 | db eval {SELECT zeroblob(2) IN (zeroblob(2))} |
| 260 | } {1} |
| 261 | |
drh | 4a33507 | 2015-04-11 02:08:48 +0000 | [diff] [blame] | 262 | # Oversized zeroblob records |
| 263 | # |
| 264 | do_test zeroblob-10.1 { |
| 265 | db eval { |
| 266 | CREATE TABLE t10(a,b,c); |
| 267 | } |
| 268 | catchsql {INSERT INTO t10 VALUES(zeroblob(1e9),zeroblob(1e9),zeroblob(1e9))} |
| 269 | } {1 {string or blob too big}} |
| 270 | |
dan | a4d5ae8 | 2015-07-24 16:24:37 +0000 | [diff] [blame] | 271 | #------------------------------------------------------------------------- |
dan | 80c0302 | 2015-07-24 17:36:34 +0000 | [diff] [blame] | 272 | # Test the zeroblob() function on its own with negative or oversized |
| 273 | # arguments. |
| 274 | # |
dan | a4d5ae8 | 2015-07-24 16:24:37 +0000 | [diff] [blame] | 275 | do_execsql_test 11.0 { |
| 276 | SELECT length(zeroblob(-1444444444444444)); |
| 277 | } {0} |
| 278 | do_catchsql_test 11.1 { |
drh | c6edb3a | 2015-07-24 23:28:05 +0000 | [diff] [blame] | 279 | SELECT zeroblob(5000 * 1024 * 1024); |
dan | a4d5ae8 | 2015-07-24 16:24:37 +0000 | [diff] [blame] | 280 | } {1 {string or blob too big}} |
| 281 | do_catchsql_test 11.2 { |
drh | c6edb3a | 2015-07-24 23:28:05 +0000 | [diff] [blame] | 282 | SELECT quote(zeroblob(5000 * 1024 * 1024)); |
dan | a4d5ae8 | 2015-07-24 16:24:37 +0000 | [diff] [blame] | 283 | } {1 {string or blob too big}} |
| 284 | do_catchsql_test 11.3 { |
| 285 | SELECT quote(zeroblob(-1444444444444444)); |
| 286 | } {0 X''} |
| 287 | do_catchsql_test 11.4 { |
| 288 | SELECT quote(test_zeroblob(-1)); |
| 289 | } {0 X''} |
drh | 6be240e | 2009-07-14 02:33:02 +0000 | [diff] [blame] | 290 | |
dan | 80c0302 | 2015-07-24 17:36:34 +0000 | [diff] [blame] | 291 | #------------------------------------------------------------------------- |
| 292 | # Test the sqlite3_bind_zeroblob64() API. |
| 293 | # |
| 294 | proc bind_and_run {stmt nZero} { |
| 295 | sqlite3_bind_zeroblob64 $stmt 1 $nZero |
| 296 | sqlite3_step $stmt |
| 297 | set ret [sqlite3_column_int $stmt 0] |
| 298 | sqlite3_reset $stmt |
| 299 | set ret |
| 300 | } |
| 301 | set stmt [sqlite3_prepare db "SELECT length(?)" -1 dummy] |
| 302 | |
| 303 | do_test 12.1 { bind_and_run $stmt 40 } 40 |
| 304 | do_test 12.2 { bind_and_run $stmt 0 } 0 |
| 305 | do_test 12.3 { bind_and_run $stmt 1000 } 1000 |
| 306 | |
| 307 | do_test 12.4 { |
drh | c6edb3a | 2015-07-24 23:28:05 +0000 | [diff] [blame] | 308 | list [catch { bind_and_run $stmt [expr 5000 * 1024 * 1024] } msg] $msg |
dan | 80c0302 | 2015-07-24 17:36:34 +0000 | [diff] [blame] | 309 | } {1 SQLITE_TOOBIG} |
| 310 | do_test 12.5 { |
| 311 | sqlite3_step $stmt |
| 312 | set ret [sqlite3_column_int $stmt 0] |
| 313 | sqlite3_reset $stmt |
| 314 | set ret |
| 315 | } {1000} |
| 316 | |
| 317 | sqlite3_finalize $stmt |
| 318 | |
dan | 03bc525 | 2015-07-24 14:17:17 +0000 | [diff] [blame] | 319 | test_restore_config_pagecache |
drh | fdf972a | 2007-05-02 13:30:27 +0000 | [diff] [blame] | 320 | finish_test |