AryaWu/sqlite
0
1# 2001 September 152#3# The author disclaims copyright to this source code. In place of4# 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.8 2009/04/28 18:00:27 drh Exp $14 15set testdir [file dirname $argv0]16source $testdir/tester.tcl17 18ifcapable {!bloblit} {19 finish_test20 return21}22 23proc bin_to_hex {blob} {24 set bytes {}25 binary scan $blob \c* bytes26 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 literal32do_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}48do_test blob-1.3.2 {49 set blob [execsql {SELECT x'0123456789abcdefABCDEF';}]50 bin_to_hex [lindex $blob 0]51} {0123456789ABCDEFABCDEF}52 53# Try some syntax errors in blob literals.54do_test blob-1.4 {55 catchsql {SELECT X'01020k304', 100}56} {1 {unrecognized token: "X'01020k304'"}}57do_test blob-1.5 {58 catchsql {SELECT X'01020, 100}59} {1 {unrecognized token: "X'01020, 100"}}60do_test blob-1.6 {61 catchsql {SELECT X'01020 100'}62} {1 {unrecognized token: "X'01020 100'"}}63do_test blob-1.7 {64 catchsql {SELECT X'01001'}65} {1 {unrecognized token: "X'01001'"}}66do_test blob-1.8 {67 catchsql {SELECT x'012/45'}68} {1 {unrecognized token: "x'012/45'"}}69do_test blob-1.9 {70 catchsql {SELECT x'012:45'}71} {1 {unrecognized token: "x'012:45'"}}72do_test blob-1.10 {73 catchsql {SELECT x'012@45'}74} {1 {unrecognized token: "x'012@45'"}}75do_test blob-1.11 {76 catchsql {SELECT x'012G45'}77} {1 {unrecognized token: "x'012G45'"}}78do_test blob-1.12 {79 catchsql {SELECT x'012`45'}80} {1 {unrecognized token: "x'012`45'"}}81do_test blob-1.13 {82 catchsql {SELECT x'012g45'}83} {1 {unrecognized token: "x'012g45'"}}84 85 86# Insert a blob into a table and retrieve it.87do_test blob-2.0 {88 execsql {89 CREATE TABLE t1(a BLOB, b BLOB);90 INSERT INTO t1 VALUES(X'123456', x'7890ab');91 INSERT INTO t1 VALUES(X'CDEF12', x'345678');92 }93 set blobs [execsql {SELECT * FROM t1}]94 set blobs2 [list]95 foreach b $blobs {lappend blobs2 [bin_to_hex $b]}96 set blobs297} {123456 7890AB CDEF12 345678}98 99# An index on a blob column100do_test blob-2.1 {101 execsql {102 CREATE INDEX i1 ON t1(a);103 }104 set blobs [execsql {SELECT * FROM t1}]105 set blobs2 [list]106 foreach b $blobs {lappend blobs2 [bin_to_hex $b]}107 set blobs2108} {123456 7890AB CDEF12 345678}109do_test blob-2.2 {110 set blobs [execsql {SELECT * FROM t1 where a = X'123456'}]111 set blobs2 [list]112 foreach b $blobs {lappend blobs2 [bin_to_hex $b]}113 set blobs2114} {123456 7890AB}115do_test blob-2.3 {116 set blobs [execsql {SELECT * FROM t1 where a = X'CDEF12'}]117 set blobs2 [list]118 foreach b $blobs {lappend blobs2 [bin_to_hex $b]}119 set blobs2120} {CDEF12 345678}121do_test blob-2.4 {122 set blobs [execsql {SELECT * FROM t1 where a = X'CD12'}]123 set blobs2 [list]124 foreach b $blobs {lappend blobs2 [bin_to_hex $b]}125 set blobs2126} {}127 128# Try to bind a blob value to a prepared statement.129do_test blob-3.0 {130 sqlite3 db2 test.db131 set DB [sqlite3_connection_pointer db2]132 set STMT [sqlite3_prepare $DB "DELETE FROM t1 WHERE a = ?" -1 DUMMY]133 sqlite3_bind_blob $STMT 1 "\x12\x34\x56" 3134 sqlite3_step $STMT135} {SQLITE_DONE}136do_test blob-3.1 {137 sqlite3_finalize $STMT138 db2 close139} {}140do_test blob-3.2 {141 set blobs [execsql {SELECT * FROM t1}]142 set blobs2 [list]143 foreach b $blobs {lappend blobs2 [bin_to_hex $b]}144 set blobs2145} {CDEF12 345678}146 147finish_test148 