Home | History | Annotate | Download | only in test
      1 # 2007 May 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 # $Id: badutf.test,v 1.2 2007/09/12 17:01:45 danielk1977 Exp $
     17 
     18 set testdir [file dirname $argv0]
     19 source $testdir/tester.tcl
     20 
     21 do_test badutf-1.1 {
     22   db eval {PRAGMA encoding=UTF8}
     23   sqlite3_exec db {SELECT hex('%80') AS x}
     24 } {0 {x 80}}
     25 do_test badutf-1.2 {
     26   sqlite3_exec db {SELECT hex('%81') AS x}
     27 } {0 {x 81}}
     28 do_test badutf-1.3 {
     29   sqlite3_exec db {SELECT hex('%bf') AS x}
     30 } {0 {x BF}}
     31 do_test badutf-1.4 {
     32   sqlite3_exec db {SELECT hex('%c0') AS x}
     33 } {0 {x C0}}
     34 do_test badutf-1.5 {
     35   sqlite3_exec db {SELECT hex('%e0') AS x}
     36 } {0 {x E0}}
     37 do_test badutf-1.6 {
     38   sqlite3_exec db {SELECT hex('%f0') AS x}
     39 } {0 {x F0}}
     40 do_test badutf-1.7 {
     41   sqlite3_exec db {SELECT hex('%ff') AS x}
     42 } {0 {x FF}}
     43 
     44 sqlite3 db2 {}
     45 ifcapable utf16 {
     46   do_test badutf-1.10 {
     47     db2 eval {PRAGMA encoding=UTF16be}
     48     sqlite3_exec db2 {SELECT hex('%80') AS x}
     49   } {0 {x 0080}}
     50   do_test badutf-1.11 {
     51     sqlite3_exec db2 {SELECT hex('%81') AS x}
     52   } {0 {x 0081}}
     53   do_test badutf-1.12 {
     54     sqlite3_exec db2 {SELECT hex('%bf') AS x}
     55   } {0 {x 00BF}}
     56   do_test badutf-1.13 {
     57     sqlite3_exec db2 {SELECT hex('%c0') AS x}
     58   } {0 {x FFFD}}
     59   do_test badutf-1.14 {
     60     sqlite3_exec db2 {SELECT hex('%c1') AS x}
     61   } {0 {x FFFD}}
     62   do_test badutf-1.15 {
     63     sqlite3_exec db2 {SELECT hex('%c0%bf') AS x}
     64   } {0 {x FFFD}}
     65   do_test badutf-1.16 {
     66     sqlite3_exec db2 {SELECT hex('%c1%bf') AS x}
     67   } {0 {x FFFD}}
     68   do_test badutf-1.17 {
     69     sqlite3_exec db2 {SELECT hex('%c3%bf') AS x}
     70   } {0 {x 00FF}}
     71   do_test badutf-1.18 {
     72     sqlite3_exec db2 {SELECT hex('%e0') AS x}
     73   } {0 {x FFFD}}
     74   do_test badutf-1.19 {
     75     sqlite3_exec db2 {SELECT hex('%f0') AS x}
     76   } {0 {x FFFD}}
     77   do_test badutf-1.20 {
     78     sqlite3_exec db2 {SELECT hex('%ff') AS x}
     79   } {0 {x FFFD}}
     80 }
     81 
     82 
     83 ifcapable bloblit {
     84   do_test badutf-2.1 {
     85     sqlite3_exec db {SELECT '%80'=CAST(x'80' AS text) AS x}
     86   } {0 {x 1}}
     87   do_test badutf-2.2 {
     88     sqlite3_exec db {SELECT CAST('%80' AS blob)=x'80' AS x}
     89   } {0 {x 1}}
     90 }
     91 
     92 do_test badutf-3.1 {
     93   sqlite3_exec db {SELECT length('%80') AS x}
     94 } {0 {x 1}}
     95 do_test badutf-3.2 {
     96   sqlite3_exec db {SELECT length('%61%62%63') AS x}
     97 } {0 {x 3}}
     98 do_test badutf-3.3 {
     99   sqlite3_exec db {SELECT length('%7f%80%81') AS x}
    100 } {0 {x 3}}
    101 do_test badutf-3.4 {
    102   sqlite3_exec db {SELECT length('%61%c0') AS x}
    103 } {0 {x 2}}
    104 do_test badutf-3.5 {
    105   sqlite3_exec db {SELECT length('%61%c0%80%80%80%80%80%80%80%80%80%80') AS x}
    106 } {0 {x 2}}
    107 do_test badutf-3.6 {
    108   sqlite3_exec db {SELECT length('%c0%80%80%80%80%80%80%80%80%80%80') AS x}
    109 } {0 {x 1}}
    110 do_test badutf-3.7 {
    111   sqlite3_exec db {SELECT length('%80%80%80%80%80%80%80%80%80%80') AS x}
    112 } {0 {x 10}}
    113 do_test badutf-3.8 {
    114   sqlite3_exec db {SELECT length('%80%80%80%80%80%f0%80%80%80%80') AS x}
    115 } {0 {x 6}}
    116 do_test badutf-3.9 {
    117   sqlite3_exec db {SELECT length('%80%80%80%80%80%f0%80%80%80%ff') AS x}
    118 } {0 {x 7}}
    119 
    120 do_test badutf-4.1 {
    121   sqlite3_exec db {SELECT hex(trim('%80%80%80%f0%80%80%80%ff','%80%ff')) AS x}
    122 } {0 {x F0}}
    123 do_test badutf-4.2 {
    124   sqlite3_exec db {SELECT hex(ltrim('%80%80%80%f0%80%80%80%ff','%80%ff')) AS x}
    125 } {0 {x F0808080FF}}
    126 do_test badutf-4.3 {
    127   sqlite3_exec db {SELECT hex(rtrim('%80%80%80%f0%80%80%80%ff','%80%ff')) AS x}
    128 } {0 {x 808080F0}}
    129 do_test badutf-4.4 {
    130   sqlite3_exec db {SELECT hex(trim('%80%80%80%f0%80%80%80%ff','%ff%80')) AS x}
    131 } {0 {x 808080F0808080FF}}
    132 do_test badutf-4.5 {
    133   sqlite3_exec db {SELECT hex(trim('%ff%80%80%f0%80%80%80%ff','%ff%80')) AS x}
    134 } {0 {x 80F0808080FF}}
    135 do_test badutf-4.6 {
    136   sqlite3_exec db {SELECT hex(trim('%ff%80%f0%80%80%80%ff','%ff%80')) AS x}
    137 } {0 {x F0808080FF}}
    138 do_test badutf-4.7 {
    139   sqlite3_exec db {SELECT hex(trim('%ff%80%f0%80%80%80%ff','%ff%80%80')) AS x}
    140 } {0 {x FF80F0808080FF}}
    141 
    142 db2 close
    143 finish_test
    144