1# 2001 September 15 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. 12# 13# $Id: blob.test,v 1.5 2006/01/03 00:33:50 drh Exp $ 14 15set testdir [file dirname $argv0] 16source $testdir/tester.tcl 17 18ifcapable {!bloblit} { 19 finish_test 20 return 21} 22 23proc bin_to_hex {blob} { 24 set bytes {} 25 binary scan $blob \c* bytes 26 set bytes2 [list] 27 foreach b $bytes {lappend bytes2 [format %02X [expr $b & 0xFF]]} 28 join $bytes2 {} 29} 30 31# Simplest possible case. Specify a blob literal 32do_test blob-1.0 { 33 set blob [execsql {SELECT X'01020304';}] 34 bin_to_hex [lindex $blob 0] 35} {01020304} 36do_test blob-1.1 { 37 set blob [execsql {SELECT x'ABCDEF';}] 38 bin_to_hex [lindex $blob 0] 39} {ABCDEF} 40do_test blob-1.2 { 41 set blob [execsql {SELECT x'';}] 42 bin_to_hex [lindex $blob 0] 43} {} 44do_test blob-1.3 { 45 set blob [execsql {SELECT x'abcdEF12';}] 46 bin_to_hex [lindex $blob 0] 47} {ABCDEF12} 48 49# Try some syntax errors in blob literals. 50do_test blob-1.4 { 51 catchsql {SELECT X'01020k304', 100} 52} {1 {unrecognized token: "X'01020"}} 53do_test blob-1.5 { 54 catchsql {SELECT X'01020, 100} 55} {1 {unrecognized token: "X'01020"}} 56do_test blob-1.6 { 57 catchsql {SELECT X'01020 100'} 58} {1 {unrecognized token: "X'01020"}} 59do_test blob-1.7 { 60 catchsql {SELECT X'01001'} 61} {1 {unrecognized token: "X'01001'"}} 62 63# Insert a blob into a table and retrieve it. 64do_test blob-2.0 { 65 execsql { 66 CREATE TABLE t1(a BLOB, b BLOB); 67 INSERT INTO t1 VALUES(X'123456', x'7890ab'); 68 INSERT INTO t1 VALUES(X'CDEF12', x'345678'); 69 } 70 set blobs [execsql {SELECT * FROM t1}] 71 set blobs2 [list] 72 foreach b $blobs {lappend blobs2 [bin_to_hex $b]} 73 set blobs2 74} {123456 7890AB CDEF12 345678} 75 76# An index on a blob column 77do_test blob-2.1 { 78 execsql { 79 CREATE INDEX i1 ON t1(a); 80 } 81 set blobs [execsql {SELECT * FROM t1}] 82 set blobs2 [list] 83 foreach b $blobs {lappend blobs2 [bin_to_hex $b]} 84 set blobs2 85} {123456 7890AB CDEF12 345678} 86do_test blob-2.2 { 87 set blobs [execsql {SELECT * FROM t1 where a = X'123456'}] 88 set blobs2 [list] 89 foreach b $blobs {lappend blobs2 [bin_to_hex $b]} 90 set blobs2 91} {123456 7890AB} 92do_test blob-2.3 { 93 set blobs [execsql {SELECT * FROM t1 where a = X'CDEF12'}] 94 set blobs2 [list] 95 foreach b $blobs {lappend blobs2 [bin_to_hex $b]} 96 set blobs2 97} {CDEF12 345678} 98do_test blob-2.4 { 99 set blobs [execsql {SELECT * FROM t1 where a = X'CD12'}] 100 set blobs2 [list] 101 foreach b $blobs {lappend blobs2 [bin_to_hex $b]} 102 set blobs2 103} {} 104 105# Try to bind a blob value to a prepared statement. 106do_test blob-3.0 { 107 sqlite3 db2 test.db 108 set DB [sqlite3_connection_pointer db2] 109 set STMT [sqlite3_prepare $DB "DELETE FROM t1 WHERE a = ?" -1 DUMMY] 110 sqlite3_bind_blob $STMT 1 "\x12\x34\x56" 3 111 sqlite3_step $STMT 112} {SQLITE_DONE} 113do_test blob-3.1 { 114 sqlite3_finalize $STMT 115 db2 close 116} {} 117do_test blob-2.3 { 118 set blobs [execsql {SELECT * FROM t1}] 119 set blobs2 [list] 120 foreach b $blobs {lappend blobs2 [bin_to_hex $b]} 121 set blobs2 122} {CDEF12 345678} 123 124finish_test 125