3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file checks to make sure SQLite is able to gracEFully
14 # handle malformed UTF-8.
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 proc utf8_to_ustr2 {s} {
22 foreach i [split $s ""] {
24 append r [format \\u%04.4X $c]
29 proc utf8_to_hstr {in} {
30 regsub -all -- {(..)} $in {%[format "%s" \1]} out
34 proc utf8_to_xstr {in} {
35 regsub -all -- {(..)} $in {\\\\x[format "%s" \1]} out
39 proc utf8_to_ustr {in} {
40 regsub -all -- {(..)} $in {\\\\u[format "%04.4X" 0x\1]} out
48 db eval "PRAGMA encoding = 'UTF-8'"
52 set S [sqlite3_prepare_v2 db "SELECT ?" -1 dummy]
56 foreach { i len uval xstr ustr u2u } {
58 2 1 01 \x01 "\\u0001" 01
59 3 1 3F \x3F "\\u003F" 3F
60 4 1 7F \x7F "\\u007F" 7F
61 5 1 80 \x80 "\\u0080" C280
62 6 1 C3BF \xFF "\\u00FF" C3BF
63 7 3 EFBFBD \xEF\xBF\xBD "\\uFFFD" {}
66 set hstr [ utf8_to_hstr $uval ]
70 do_test badutf2-2.1.$i {
71 set sql "SELECT '$hstr'=CAST(x'$uval' AS text) AS x;"
72 set res [ sqlite3_exec db $sql ]
73 lindex [ lindex $res 1] 1
75 do_test badutf2-2.2.$i {
76 set sql "SELECT CAST('$hstr' AS blob)=x'$uval' AS x;"
77 set res [ sqlite3_exec db $sql ]
78 lindex [ lindex $res 1] 1
81 do_test badutf2-2.3.$i {
82 set sql "SELECT hex(CAST(x'$uval' AS text)) AS x;"
83 set res [ sqlite3_exec db $sql ]
84 lindex [ lindex $res 1] 1
86 do_test badutf2-2.4.$i {
87 set sql "SELECT hex(CAST(x'$uval' AS text)) AS x;"
88 set res [ sqlite3_exec db $sql ]
89 lindex [ lindex $res 1] 1
94 do_test badutf2-3.1.$i {
95 set sql "SELECT hex('$hstr') AS x;"
96 set res [ sqlite3_exec db $sql ]
97 lindex [ lindex $res 1] 1
101 # Tcl 8.7 and later do automatic bad-utf8 correction for
102 # characters 0x80 thru 0x9f so test case 5 does not work here.
103 if {$i==5 && $tcl_version>=8.7} {
106 do_test badutf2-4.1.$i {
108 sqlite3_bind_text $S 1 $xstr $len
110 utf8_to_ustr2 [ sqlite3_column_text $S 0 ]
115 do_test badutf2-5.1.$i {
122 do_test badutf2-4.2 {