1# 2011 March 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# This file checks to make sure SQLite is able to gracEFully 14# handle malformed UTF-8. 15# 16 17set testdir [file dirname $argv0] 18source $testdir/tester.tcl 19 20proc utf8_to_ustr2 {s} { 21 set r "" 22 foreach i [split $s ""] { 23 scan $i %c c 24 append r [format \\u%04.4X $c] 25 } 26 set r 27} 28 29proc utf8_to_hstr {in} { 30 regsub -all -- {(..)} $in {%[format "%s" \1]} out 31 subst $out 32} 33 34proc utf8_to_xstr {in} { 35 regsub -all -- {(..)} $in {\\\\x[format "%s" \1]} out 36 subst $out 37} 38 39proc utf8_to_ustr {in} { 40 regsub -all -- {(..)} $in {\\\\u[format "%04.4X" 0x\1]} out 41 subst $out 42} 43 44do_test badutf2-1.0 { 45 db close 46 forcedelete test.db 47 sqlite3 db test.db 48 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 $S 54} {0} 55 56foreach { i len uval xstr ustr u2u } { 571 1 00 \x00 {} {} 582 1 01 \x01 "\\u0001" 01 593 1 3F \x3F "\\u003F" 3F 604 1 7F \x7F "\\u007F" 7F 615 1 80 \x80 "\\u0080" C280 626 1 C3BF \xFF "\\u00FF" C3BF 637 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] 1 74 } {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 79 } {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] 1 85 } $uval 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 90 } $uval 91 } 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] 1 98 } $uval 99 } 100 101 do_test badutf2-4.1.$i { 102 sqlite3_reset $S 103 sqlite3_bind_text $S 1 $xstr $len 104 sqlite3_step $S 105 utf8_to_ustr2 [ sqlite3_column_text $S 0 ] 106 } $ustr 107 108 ifcapable debug { 109 do_test badutf2-5.1.$i { 110 utf8_to_utf8 $uval 111 } $u2u 112 } 113 114} 115 116do_test badutf2-4.2 { 117 sqlite3_finalize $S 118} {SQLITE_OK} 119 120 121finish_test 122