badutf2.test 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. proc utf8_to_ustr2 {s} {
  19. set r ""
  20. foreach i [split $s ""] {
  21. scan $i %c c
  22. append r [format \\u%04.4X $c]
  23. }
  24. set r
  25. }
  26. proc utf8_to_hstr {in} {
  27. regsub -all -- {(..)} $in {%[format "%s" \1]} out
  28. subst $out
  29. }
  30. proc utf8_to_xstr {in} {
  31. regsub -all -- {(..)} $in {\\\\x[format "%s" \1]} out
  32. subst $out
  33. }
  34. proc utf8_to_ustr {in} {
  35. regsub -all -- {(..)} $in {\\\\u[format "%04.4X" 0x\1]} out
  36. subst $out
  37. }
  38. do_test badutf2-1.0 {
  39. db close
  40. forcedelete test.db
  41. sqlite3 db test.db
  42. db eval "PRAGMA encoding = 'UTF-8'"
  43. } {}
  44. do_test badutf2-4.0 {
  45. set S [sqlite3_prepare_v2 db "SELECT ?" -1 dummy]
  46. sqlite3_expired $S
  47. } {0}
  48. foreach { i len uval xstr ustr u2u } {
  49. 1 1 00 \x00 {} {}
  50. 2 1 01 \x01 "\\u0001" 01
  51. 3 1 3F \x3F "\\u003F" 3F
  52. 4 1 7F \x7F "\\u007F" 7F
  53. 5 1 80 \x80 "\\u0080" C280
  54. 6 1 C3BF \xFF "\\u00FF" C3BF
  55. 7 3 EFBFBD \xEF\xBF\xBD "\\uFFFD" {}
  56. } {
  57. set hstr [ utf8_to_hstr $uval ]
  58. ifcapable bloblit {
  59. if {$hstr != "%00"} {
  60. do_test badutf2-2.1.$i {
  61. set sql "SELECT '$hstr'=CAST(x'$uval' AS text) AS x;"
  62. set res [ sqlite3_exec db $sql ]
  63. lindex [ lindex $res 1] 1
  64. } {1}
  65. do_test badutf2-2.2.$i {
  66. set sql "SELECT CAST('$hstr' AS blob)=x'$uval' AS x;"
  67. set res [ sqlite3_exec db $sql ]
  68. lindex [ lindex $res 1] 1
  69. } {1}
  70. }
  71. do_test badutf2-2.3.$i {
  72. set sql "SELECT hex(CAST(x'$uval' AS text)) AS x;"
  73. set res [ sqlite3_exec db $sql ]
  74. lindex [ lindex $res 1] 1
  75. } $uval
  76. do_test badutf2-2.4.$i {
  77. set sql "SELECT hex(CAST(x'$uval' AS text)) AS x;"
  78. set res [ sqlite3_exec db $sql ]
  79. lindex [ lindex $res 1] 1
  80. } $uval
  81. }
  82. if {$hstr != "%00"} {
  83. do_test badutf2-3.1.$i {
  84. set sql "SELECT hex('$hstr') AS x;"
  85. set res [ sqlite3_exec db $sql ]
  86. lindex [ lindex $res 1] 1
  87. } $uval
  88. }
  89. do_test badutf2-4.1.$i {
  90. sqlite3_reset $S
  91. sqlite3_bind_text $S 1 $xstr $len
  92. sqlite3_step $S
  93. utf8_to_ustr2 [ sqlite3_column_text $S 0 ]
  94. } $ustr
  95. ifcapable debug {
  96. do_test badutf2-5.1.$i {
  97. utf8_to_utf8 $uval
  98. } $u2u
  99. }
  100. }
  101. do_test badutf2-4.2 {
  102. sqlite3_finalize $S
  103. } {SQLITE_OK}
  104. finish_test