# 2007 May 15
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. 
#
# This file checks to make sure SQLite is able to gracefully
# handle malformed UTF-8.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl

do_test badutf-1.1 {
  db eval {PRAGMA encoding=UTF8}
  sqlite4_exec db {SELECT hex('%80') AS x}
} {0 {x 80}}
do_test badutf-1.2 {
  sqlite4_exec db {SELECT hex('%81') AS x}
} {0 {x 81}}
do_test badutf-1.3 {
  sqlite4_exec db {SELECT hex('%bf') AS x}
} {0 {x bf}}
do_test badutf-1.4 {
  sqlite4_exec db {SELECT hex('%c0') AS x}
} {0 {x c0}}
do_test badutf-1.5 {
  sqlite4_exec db {SELECT hex('%e0') AS x}
} {0 {x e0}}
do_test badutf-1.6 {
  sqlite4_exec db {SELECT hex('%f0') AS x}
} {0 {x f0}}
do_test badutf-1.7 {
  sqlite4_exec db {SELECT hex('%ff') AS x}
} {0 {x ff}}

sqlite4 db2 {}
ifcapable utf16 {
  do_test badutf-1.10 {
    db2 eval {PRAGMA encoding=UTF16be}
    sqlite4_exec db2 {SELECT hex('%80') AS x}
  } {0 {x 0080}}
  do_test badutf-1.11 {
    sqlite4_exec db2 {SELECT hex('%81') AS x}
  } {0 {x 0081}}
  do_test badutf-1.12 {
    sqlite4_exec db2 {SELECT hex('%bf') AS x}
  } {0 {x 00bf}}
  do_test badutf-1.13 {
    sqlite4_exec db2 {SELECT hex('%c0') AS x}
  } {0 {x fffd}}
  do_test badutf-1.14 {
    sqlite4_exec db2 {SELECT hex('%c1') AS x}
  } {0 {x fffd}}
  do_test badutf-1.15 {
    sqlite4_exec db2 {SELECT hex('%c0%bf') AS x}
  } {0 {x fffd}}
  do_test badutf-1.16 {
    sqlite4_exec db2 {SELECT hex('%c1%bf') AS x}
  } {0 {x fffd}}
  do_test badutf-1.17 {
    sqlite4_exec db2 {SELECT hex('%c3%bf') AS x}
  } {0 {x 00ff}}
  do_test badutf-1.18 {
    sqlite4_exec db2 {SELECT hex('%e0') AS x}
  } {0 {x fffd}}
  do_test badutf-1.19 {
    sqlite4_exec db2 {SELECT hex('%f0') AS x}
  } {0 {x fffd}}
  do_test badutf-1.20 {
    sqlite4_exec db2 {SELECT hex('%ff') AS x}
  } {0 {x fffd}}
}


ifcapable bloblit {
  do_test badutf-2.1 {
    sqlite4_exec db {SELECT '%80'=CAST(x'80' AS text) AS x}
  } {0 {x 1}}
  do_test badutf-2.2 {
    sqlite4_exec db {SELECT CAST('%80' AS blob)=x'80' AS x}
  } {0 {x 1}}
}

do_test badutf-3.1 {
  sqlite4_exec db {SELECT length('%80') AS x}
} {0 {x 1}}
do_test badutf-3.2 {
  sqlite4_exec db {SELECT length('%61%62%63') AS x}
} {0 {x 3}}
do_test badutf-3.3 {
  sqlite4_exec db {SELECT length('%7f%80%81') AS x}
} {0 {x 3}}
do_test badutf-3.4 {
  sqlite4_exec db {SELECT length('%61%c0') AS x}
} {0 {x 2}}
do_test badutf-3.5 {
  sqlite4_exec db {SELECT length('%61%c0%80%80%80%80%80%80%80%80%80%80') AS x}
} {0 {x 2}}
do_test badutf-3.6 {
  sqlite4_exec db {SELECT length('%c0%80%80%80%80%80%80%80%80%80%80') AS x}
} {0 {x 1}}
do_test badutf-3.7 {
  sqlite4_exec db {SELECT length('%80%80%80%80%80%80%80%80%80%80') AS x}
} {0 {x 10}}
do_test badutf-3.8 {
  sqlite4_exec db {SELECT length('%80%80%80%80%80%f0%80%80%80%80') AS x}
} {0 {x 6}}
do_test badutf-3.9 {
  sqlite4_exec db {SELECT length('%80%80%80%80%80%f0%80%80%80%ff') AS x}
} {0 {x 7}}

do_test badutf-4.1 {
  sqlite4_exec db {SELECT hex(trim('%80%80%80%f0%80%80%80%ff','%80%ff')) AS x}
} {0 {x f0}}
do_test badutf-4.2 {
  sqlite4_exec db {SELECT hex(ltrim('%80%80%80%f0%80%80%80%ff','%80%ff')) AS x}
} {0 {x f0808080ff}}
do_test badutf-4.3 {
  sqlite4_exec db {SELECT hex(rtrim('%80%80%80%f0%80%80%80%ff','%80%ff')) AS x}
} {0 {x 808080f0}}
do_test badutf-4.4 {
  sqlite4_exec db {SELECT hex(trim('%80%80%80%f0%80%80%80%ff','%ff%80')) AS x}
} {0 {x 808080f0808080ff}}
do_test badutf-4.5 {
  sqlite4_exec db {SELECT hex(trim('%ff%80%80%f0%80%80%80%ff','%ff%80')) AS x}
} {0 {x 80f0808080ff}}
do_test badutf-4.6 {
  sqlite4_exec db {SELECT hex(trim('%ff%80%f0%80%80%80%ff','%ff%80')) AS x}
} {0 {x f0808080ff}}
do_test badutf-4.7 {
  sqlite4_exec db {SELECT hex(trim('%ff%80%f0%80%80%80%ff','%ff%80%80')) AS x}
} {0 {x ff80f0808080ff}}

db2 close
finish_test
