sl@0: # 2001 September 15 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. sl@0: # sl@0: # $Id: blob.test,v 1.7 2008/04/04 15:12:22 drh Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable {!bloblit} { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: proc bin_to_hex {blob} { sl@0: set bytes {} sl@0: binary scan $blob \c* bytes sl@0: set bytes2 [list] sl@0: foreach b $bytes {lappend bytes2 [format %02X [expr $b & 0xFF]]} sl@0: join $bytes2 {} sl@0: } sl@0: sl@0: # Simplest possible case. Specify a blob literal sl@0: do_test blob-1.0 { sl@0: set blob [execsql {SELECT X'01020304';}] sl@0: bin_to_hex [lindex $blob 0] sl@0: } {01020304} sl@0: do_test blob-1.1 { sl@0: set blob [execsql {SELECT x'ABCDEF';}] sl@0: bin_to_hex [lindex $blob 0] sl@0: } {ABCDEF} sl@0: do_test blob-1.2 { sl@0: set blob [execsql {SELECT x'';}] sl@0: bin_to_hex [lindex $blob 0] sl@0: } {} sl@0: do_test blob-1.3 { sl@0: set blob [execsql {SELECT x'abcdEF12';}] sl@0: bin_to_hex [lindex $blob 0] sl@0: } {ABCDEF12} sl@0: do_test blob-1.3.2 { sl@0: set blob [execsql {SELECT x'0123456789abcdefABCDEF';}] sl@0: bin_to_hex [lindex $blob 0] sl@0: } {0123456789ABCDEFABCDEF} sl@0: sl@0: # Try some syntax errors in blob literals. sl@0: do_test blob-1.4 { sl@0: catchsql {SELECT X'01020k304', 100} sl@0: } {1 {unrecognized token: "X'01020k304'"}} sl@0: do_test blob-1.5 { sl@0: catchsql {SELECT X'01020, 100} sl@0: } {1 {unrecognized token: "X'01020, 100"}} sl@0: do_test blob-1.6 { sl@0: catchsql {SELECT X'01020 100'} sl@0: } {1 {unrecognized token: "X'01020 100'"}} sl@0: do_test blob-1.7 { sl@0: catchsql {SELECT X'01001'} sl@0: } {1 {unrecognized token: "X'01001'"}} sl@0: do_test blob-1.8 { sl@0: catchsql {SELECT x'012/45'} sl@0: } {1 {unrecognized token: "x'012/45'"}} sl@0: do_test blob-1.9 { sl@0: catchsql {SELECT x'012:45'} sl@0: } {1 {unrecognized token: "x'012:45'"}} sl@0: do_test blob-1.10 { sl@0: catchsql {SELECT x'012@45'} sl@0: } {1 {unrecognized token: "x'012@45'"}} sl@0: do_test blob-1.11 { sl@0: catchsql {SELECT x'012G45'} sl@0: } {1 {unrecognized token: "x'012G45'"}} sl@0: do_test blob-1.12 { sl@0: catchsql {SELECT x'012`45'} sl@0: } {1 {unrecognized token: "x'012`45'"}} sl@0: do_test blob-1.13 { sl@0: catchsql {SELECT x'012g45'} sl@0: } {1 {unrecognized token: "x'012g45'"}} sl@0: sl@0: sl@0: # Insert a blob into a table and retrieve it. sl@0: do_test blob-2.0 { sl@0: execsql { sl@0: CREATE TABLE t1(a BLOB, b BLOB); sl@0: INSERT INTO t1 VALUES(X'123456', x'7890ab'); sl@0: INSERT INTO t1 VALUES(X'CDEF12', x'345678'); sl@0: } sl@0: set blobs [execsql {SELECT * FROM t1}] sl@0: set blobs2 [list] sl@0: foreach b $blobs {lappend blobs2 [bin_to_hex $b]} sl@0: set blobs2 sl@0: } {123456 7890AB CDEF12 345678} sl@0: sl@0: # An index on a blob column sl@0: do_test blob-2.1 { sl@0: execsql { sl@0: CREATE INDEX i1 ON t1(a); sl@0: } sl@0: set blobs [execsql {SELECT * FROM t1}] sl@0: set blobs2 [list] sl@0: foreach b $blobs {lappend blobs2 [bin_to_hex $b]} sl@0: set blobs2 sl@0: } {123456 7890AB CDEF12 345678} sl@0: do_test blob-2.2 { sl@0: set blobs [execsql {SELECT * FROM t1 where a = X'123456'}] sl@0: set blobs2 [list] sl@0: foreach b $blobs {lappend blobs2 [bin_to_hex $b]} sl@0: set blobs2 sl@0: } {123456 7890AB} sl@0: do_test blob-2.3 { sl@0: set blobs [execsql {SELECT * FROM t1 where a = X'CDEF12'}] sl@0: set blobs2 [list] sl@0: foreach b $blobs {lappend blobs2 [bin_to_hex $b]} sl@0: set blobs2 sl@0: } {CDEF12 345678} sl@0: do_test blob-2.4 { sl@0: set blobs [execsql {SELECT * FROM t1 where a = X'CD12'}] sl@0: set blobs2 [list] sl@0: foreach b $blobs {lappend blobs2 [bin_to_hex $b]} sl@0: set blobs2 sl@0: } {} sl@0: sl@0: # Try to bind a blob value to a prepared statement. sl@0: do_test blob-3.0 { sl@0: sqlite3 db2 test.db sl@0: set DB [sqlite3_connection_pointer db2] sl@0: set STMT [sqlite3_prepare $DB "DELETE FROM t1 WHERE a = ?" -1 DUMMY] sl@0: sqlite3_bind_blob $STMT 1 "\x12\x34\x56" 3 sl@0: sqlite3_step $STMT sl@0: } {SQLITE_DONE} sl@0: do_test blob-3.1 { sl@0: sqlite3_finalize $STMT sl@0: db2 close sl@0: } {} sl@0: do_test blob-2.3 { sl@0: set blobs [execsql {SELECT * FROM t1}] sl@0: set blobs2 [list] sl@0: foreach b $blobs {lappend blobs2 [bin_to_hex $b]} sl@0: set blobs2 sl@0: } {CDEF12 345678} sl@0: sl@0: finish_test