summaryrefslogtreecommitdiff
path: root/test/blob.test
blob: b1aae381a9f16b5c1688a812f6725a795f14ed98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 2001 September 15
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# $Id: blob.test,v 1.8 2009/04/28 18:00:27 drh Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

ifcapable {!bloblit} {
  finish_test
  return
}

proc bin_to_hex {blob} {
  set bytes {}
  binary scan $blob \c* bytes
  set bytes2 [list]
  foreach b $bytes {lappend bytes2 [format %02X [expr $b & 0xFF]]}
  join $bytes2 {}
}

# Simplest possible case. Specify a blob literal
do_test blob-1.0 {
  set blob [execsql {SELECT X'01020304';}]
  bin_to_hex [lindex $blob 0]
} {01020304}
do_test blob-1.1 {
  set blob [execsql {SELECT x'ABCDEF';}]
  bin_to_hex [lindex $blob 0]
} {ABCDEF}
do_test blob-1.2 {
  set blob [execsql {SELECT x'';}]
  bin_to_hex [lindex $blob 0]
} {}
do_test blob-1.3 {
  set blob [execsql {SELECT x'abcdEF12';}]
  bin_to_hex [lindex $blob 0]
} {ABCDEF12}
do_test blob-1.3.2 {
  set blob [execsql {SELECT x'0123456789abcdefABCDEF';}]
  bin_to_hex [lindex $blob 0]
} {0123456789ABCDEFABCDEF}

# Try some syntax errors in blob literals.
do_test blob-1.4 {
  catchsql {SELECT X'01020k304', 100}
} {1 {unrecognized token: "X'01020k304'"}}
do_test blob-1.5 {
  catchsql {SELECT X'01020, 100}
} {1 {unrecognized token: "X'01020, 100"}}
do_test blob-1.6 {
  catchsql {SELECT X'01020 100'}
} {1 {unrecognized token: "X'01020 100'"}}
do_test blob-1.7 {
  catchsql {SELECT X'01001'}
} {1 {unrecognized token: "X'01001'"}}
do_test blob-1.8 {
  catchsql {SELECT x'012/45'}
} {1 {unrecognized token: "x'012/45'"}}
do_test blob-1.9 {
  catchsql {SELECT x'012:45'}
} {1 {unrecognized token: "x'012:45'"}}
do_test blob-1.10 {
  catchsql {SELECT x'012@45'}
} {1 {unrecognized token: "x'012@45'"}}
do_test blob-1.11 {
  catchsql {SELECT x'012G45'}
} {1 {unrecognized token: "x'012G45'"}}
do_test blob-1.12 {
  catchsql {SELECT x'012`45'}
} {1 {unrecognized token: "x'012`45'"}}
do_test blob-1.13 {
  catchsql {SELECT x'012g45'}
} {1 {unrecognized token: "x'012g45'"}}


# Insert a blob into a table and retrieve it.
do_test blob-2.0 {
  execsql {
    CREATE TABLE t1(a BLOB, b BLOB);
    INSERT INTO t1 VALUES(X'123456', x'7890ab');
    INSERT INTO t1 VALUES(X'CDEF12', x'345678');
  }
  set blobs [execsql {SELECT * FROM t1}]
  set blobs2 [list]
  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
  set blobs2
} {123456 7890AB CDEF12 345678}

# An index on a blob column
do_test blob-2.1 {
  execsql {
    CREATE INDEX i1 ON t1(a);
  }
  set blobs [execsql {SELECT * FROM t1}]
  set blobs2 [list]
  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
  set blobs2
} {123456 7890AB CDEF12 345678}
do_test blob-2.2 {
  set blobs [execsql {SELECT * FROM t1 where a = X'123456'}]
  set blobs2 [list]
  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
  set blobs2
} {123456 7890AB}
do_test blob-2.3 {
  set blobs [execsql {SELECT * FROM t1 where a = X'CDEF12'}]
  set blobs2 [list]
  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
  set blobs2
} {CDEF12 345678}
do_test blob-2.4 {
  set blobs [execsql {SELECT * FROM t1 where a = X'CD12'}]
  set blobs2 [list]
  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
  set blobs2
} {}

# Try to bind a blob value to a prepared statement.
do_test blob-3.0 {
  sqlite3 db2 test.db
  set DB [sqlite3_connection_pointer db2]
  set STMT [sqlite3_prepare $DB "DELETE FROM t1 WHERE a = ?" -1 DUMMY]
  sqlite3_bind_blob $STMT 1 "\x12\x34\x56" 3
  sqlite3_step $STMT
} {SQLITE_DONE}
do_test blob-3.1 {
  sqlite3_finalize $STMT
  db2 close
} {}
do_test blob-3.2 {
  set blobs [execsql {SELECT * FROM t1}]
  set blobs2 [list]
  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
  set blobs2
} {CDEF12 345678}

finish_test