Home | History | Annotate | Download | only in test
      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.8 2009/04/28 18:00:27 drh Exp $
     14 
     15 set testdir [file dirname $argv0]
     16 source $testdir/tester.tcl
     17 
     18 ifcapable {!bloblit} {
     19   finish_test
     20   return
     21 }
     22 
     23 proc 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
     32 do_test blob-1.0 {
     33   set blob [execsql {SELECT X'01020304';}]
     34   bin_to_hex [lindex $blob 0]
     35 } {01020304}
     36 do_test blob-1.1 {
     37   set blob [execsql {SELECT x'ABCDEF';}]
     38   bin_to_hex [lindex $blob 0]
     39 } {ABCDEF}
     40 do_test blob-1.2 {
     41   set blob [execsql {SELECT x'';}]
     42   bin_to_hex [lindex $blob 0]
     43 } {}
     44 do_test blob-1.3 {
     45   set blob [execsql {SELECT x'abcdEF12';}]
     46   bin_to_hex [lindex $blob 0]
     47 } {ABCDEF12}
     48 do_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.
     54 do_test blob-1.4 {
     55   catchsql {SELECT X'01020k304', 100}
     56 } {1 {unrecognized token: "X'01020k304'"}}
     57 do_test blob-1.5 {
     58   catchsql {SELECT X'01020, 100}
     59 } {1 {unrecognized token: "X'01020, 100"}}
     60 do_test blob-1.6 {
     61   catchsql {SELECT X'01020 100'}
     62 } {1 {unrecognized token: "X'01020 100'"}}
     63 do_test blob-1.7 {
     64   catchsql {SELECT X'01001'}
     65 } {1 {unrecognized token: "X'01001'"}}
     66 do_test blob-1.8 {
     67   catchsql {SELECT x'012/45'}
     68 } {1 {unrecognized token: "x'012/45'"}}
     69 do_test blob-1.9 {
     70   catchsql {SELECT x'012:45'}
     71 } {1 {unrecognized token: "x'012:45'"}}
     72 do_test blob-1.10 {
     73   catchsql {SELECT x'012@45'}
     74 } {1 {unrecognized token: "x'012@45'"}}
     75 do_test blob-1.11 {
     76   catchsql {SELECT x'012G45'}
     77 } {1 {unrecognized token: "x'012G45'"}}
     78 do_test blob-1.12 {
     79   catchsql {SELECT x'012`45'}
     80 } {1 {unrecognized token: "x'012`45'"}}
     81 do_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.
     87 do_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 blobs2
     97 } {123456 7890AB CDEF12 345678}
     98 
     99 # An index on a blob column
    100 do_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 blobs2
    108 } {123456 7890AB CDEF12 345678}
    109 do_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 blobs2
    114 } {123456 7890AB}
    115 do_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 blobs2
    120 } {CDEF12 345678}
    121 do_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 blobs2
    126 } {}
    127 
    128 # Try to bind a blob value to a prepared statement.
    129 do_test blob-3.0 {
    130   sqlite3 db2 test.db
    131   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" 3
    134   sqlite3_step $STMT
    135 } {SQLITE_DONE}
    136 do_test blob-3.1 {
    137   sqlite3_finalize $STMT
    138   db2 close
    139 } {}
    140 do_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 blobs2
    145 } {CDEF12 345678}
    146 
    147 finish_test
    148