AryaWu/sqlite
0
1# 2011 March 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# This file checks to make sure SQLite is able to gracEFully14# handle malformed UTF-8.15#16 17set testdir [file dirname $argv0]18source $testdir/tester.tcl19 20proc utf8_to_ustr2 {s} {21 set r ""22 foreach i [split $s ""] {23 scan $i %c c24 append r [format \\u%04.4X $c]25 }26 set r27}28 29proc utf8_to_hstr {in} {30 regsub -all -- {(..)} $in {%[format "%s" \1]} out31 subst $out32}33 34proc utf8_to_xstr {in} {35 regsub -all -- {(..)} $in {\\\\x[format "%s" \1]} out36 subst $out37}38 39proc utf8_to_ustr {in} {40 regsub -all -- {(..)} $in {\\\\u[format "%04.4X" 0x\1]} out41 subst $out42}43 44do_test badutf2-1.0 {45 db close46 forcedelete test.db47 sqlite3 db test.db48 db eval "PRAGMA encoding = 'UTF-8'"49} {}50 51do_test badutf2-4.0 {52 set S [sqlite3_prepare_v2 db "SELECT ?" -1 dummy]53 sqlite3_expired $S54} {0}55 56foreach { i len uval xstr ustr u2u } {571 1 00 \x00 {} {}582 1 01 \x01 "\\u0001" 01593 1 3F \x3F "\\u003F" 3F604 1 7F \x7F "\\u007F" 7F615 1 80 \x80 "\\u0080" C280626 1 C3BF \xFF "\\u00FF" C3BF637 3 EFBFBD \xEF\xBF\xBD "\\uFFFD" {}64} {65 66 set hstr [ utf8_to_hstr $uval ]67 68 ifcapable bloblit {69 if {$hstr != "%00"} {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] 174 } {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] 179 } {1}80 }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] 185 } $uval86 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] 190 } $uval91 }92 93 if {$hstr != "%00"} {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] 198 } $uval99 }100 101 # Tcl 8.7 and later do automatic bad-utf8 correction for102 # characters 0x80 thru 0x9f so test case 5 does not work here.103 if {$i==5 && $tcl_version>=8.7} {104 # no-op105 } else {106 do_test badutf2-4.1.$i {107 sqlite3_reset $S108 sqlite3_bind_text $S 1 $xstr $len109 sqlite3_step $S110 utf8_to_ustr2 [ sqlite3_column_text $S 0 ]111 } $ustr112 }113 114 ifcapable debug {115 do_test badutf2-5.1.$i {116 utf8_to_utf8 $uval117 } $u2u118 }119 120}121 122do_test badutf2-4.2 {123 sqlite3_finalize $S124} {SQLITE_OK}125 126 127finish_test128 